<commit_before>'use strict'
const { describe, it, before, after } = require('mocha')
const { expect } = require('chai')
const { URL } = require('url')
const AccessControl = require('../../../../add-on/src/lib/ipfs-proxy/access-control')
const Storage = require('mem-storage-area/Storage')
const { objToAcl } = require('../../../helpers/acl')

describe('lib/ipfs-proxy/access-control', () => {
  before(() => {
    global.URL = URL
  })

  it('should maintain ACL', async () => {
    const accessControl = new AccessControl(new Storage())
    let acl = await accessControl.getAcl()

    // Ensure ACL starts out empty
    expect(acl).to.deep.equal(new Map())

    const sets = [
      ['http://ipfs.io', 'ipfs.files.add', true],
      ['https://ipld.io', 'ipfs.object.new', false],
      ['https://filecoin.io', 'ipfs.pubsub.subscribe', true],
      ['https://filecoin.io', 'ipfs.pubsub.subscribe', false],
      ['https://filecoin.io', 'ipfs.pubsub.publish', true]
    ]

    await Promise.all(sets.map(s => accessControl.setAccess(...s)))

    const expectedAcl = objToAcl({
      'http://ipfs.io': {
        'ipfs.files.add': true
      },
      'https://ipld.io': {
        'ipfs.object.new': false
      },
      'https://filecoin.io': {
        'ipfs.pubsub.subscribe': false,
        'ipfs.pubsub.publish': true
      }
    })

    acl = await accessControl.getAcl()

    expect(acl).to.deep.equal(expectedAcl)
  })

  it('should get granted access for origin and permission', async () => {
    const accessControl = new AccessControl(new Storage())

    let access = await accessControl.setAccess('http://ipfs.io', 'ipfs.files.add', true)
    const expectedAccess = { origin: 'http://ipfs.io', permission: 'ipfs.files.add', allow: true }<commit_msg>adds access control wildcard tests<commit_after>'use strict'
const { describe, it, before, after } = require('mocha')
const { expect } = require('chai')
const { URL } = require('url')
const AccessControl = require('../../../../add-on/src/lib/ipfs-proxy/access-control')
const Storage = require('mem-storage-area/Storage')
const { objToAcl } = require('../../../helpers/acl')

describe('lib/ipfs-proxy/access-control', () => {
  before(() => {
    global.URL = URL
  })

  it('should maintain ACL', async () => {
    const accessControl = new AccessControl(new Storage())
    let acl = await accessControl.getAcl()

    // Ensure ACL starts out empty
    expect(acl).to.deep.equal(new Map())

    const sets = [
      ['http://ipfs.io', 'ipfs.files.add', true],
      ['https://ipld.io', 'ipfs.object.new', false],
      ['https://filecoin.io', 'ipfs.pubsub.subscribe', true],
      ['https://filecoin.io', 'ipfs.pubsub.subscribe', false],
      ['https://filecoin.io', 'ipfs.pubsub.publish', true]
    ]

    await Promise.all(sets.map(s => accessControl.setAccess(...s)))

    const expectedAcl = objToAcl({
      'http://ipfs.io': {
        'ipfs.files.add': true
      },
      'https://ipld.io': {
        'ipfs.object.new': false
      },
      'https://filecoin.io': {
        'ipfs.pubsub.subscribe': false,
        'ipfs.pubsub.publish': true
      }
    })

    acl = await accessControl.getAcl()

    expect(acl).to.deep.equal(expectedAcl)
  })

  it('should allow access for wildcard allow', async () => {
    const accessControl = new AccessControl(new Storage())
    let access = await accessControl.getAccess('https://ipfs.io', 'files.add')

    expect(access).to.equal(null)

    // Add wildcard
    await accessControl.setAccess('https://ipfs.io', '*', true)

    access = await accessControl.getAccess('https://ipfs.io', 'files.add')

    const expectedAccess = { origin: 'https://ipfs.io', permission: 'files.add', allow: true }

    expect(access).to.deep.equal(expectedAccess)
  })

  it('should deny access for wildcard deny', async () => {
    const accessControl = new AccessControl(new Storage())
    let access = await accessControl.getAccess('https://ipfs.io', 'files.add')

    expect(access).to.equal(null)

    // Add wildcard
    await accessControl.setAccess('https://ipfs.io', '*', false)

    access = await accessControl.getAccess('https://ipfs.io', 'files.add')

    const expectedAccess = { origin: 'https://ipfs.io', permission: 'files.add', allow: false }

    expect(access).to.deep.equal(expectedAccess)
  })

  it('should clear existing grants when setting wildcard access', async () => {
    const accessControl = new AccessControl(new Storage())

    await accessControl.setAccess('https://ipfs.io', 'files.add', false)
    await accessControl.setAccess('https://ipfs.io', 'object.new', true)
    await accessControl.setAccess('https://ipfs.io', 'config.set', false)

    let acl = await accessControl.getAcl()

    let expectedAcl = objToAcl({
      'https://ipfs.io': {
        'files.add': false,
        'object.new': true,
        'config.set': false
      }
    })

    expect(acl).to.deep.equal(expectedAcl)

    // Add wildcard
    await accessControl.setAccess('https://ipfs.io', '*', false)

    acl = await accessControl.getAcl()

    expectedAcl = objToAcl({
      'https://ipfs.io': {
        '*': false
      }
    })

    expect(acl).to.deep.equal(expectedAcl)
  })

  it('should not be able to set different access to specific permission if wildcard access grant exists', async () => {
    const accessControl = new AccessControl(new Storage())

    // Add wildcard
    await accessControl.setAccess('https://ipfs.io', '*', false)

    let error

    try {
      await accessControl.setAccess('https://ipfs.io', 'files.add', true)
    } catch (err) {
      error = err
    }

    expect(() => { if (error) throw error }).to.throw('Illegal set access for files.add when wildcard exists')
  })

  it('should be able set same access to specific permission if wildcard access grant exists', async () => {
    const accessControl = new AccessControl(new Storage())

    // Add wildcard
    await accessControl.setAccess('https://ipfs.io', '*', false)

    let error

    try {
      await accessControl.setAccess('https://ipfs.io', 'files.add', false)
    } catch (err) {
      error = err
    }

    expect(() => { if (error) throw error }).to.not.throw()
  })

  it('should get granted access for origin and permission', async () => {
    const accessControl = new AccessControl(new Storage())

    let access = await accessControl.setAccess('http://ipfs.io', 'ipfs.files.add', true)
    const expectedAccess = { origin: 'http://ipfs.io', permission: 'ipfs.files.add', allow: true }
{'fraction_non_alphanumeric': 0.17462272867262088, 'fraction_numerical': 0.0, 'mean_word_length': 3.021671826625387, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, 'https://': 30, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2491304', 'n_tokens_mistral': 2067, 'n_tokens_neox': 2017, 'n_words': 449}