<commit_before>var expect = require('expect.js');
var simple = require('simple-mock');
var fs = require('fs');
var testHelper = require('../etc/test-helper.js');
var errorFormatter = testHelper.validationErrorFormatter;

// Load the contents of the sync function file into a global variable called syncFunction
/*jslint evil: true */
eval('var syncFunction = ' + fs.readFileSync('build/sync-functions/test-access-assignment-sync-function.js').toString());
/*jslint evil: false */

// Placeholders for stubbing built-in Sync Gateway support functions.
// More info: http://developer.couchbase.com/mobile/develop/guides/sync-gateway/sync-function-api-guide/index.html
var requireAccess;
var channel;
var access;

describe('User and role access assignment:', function() {
  beforeEach(function() {
    testHelper.init('build/sync-functions/test-access-assignment-sync-function.js');

    requireAccess = simple.stub();
    channel = simple.stub();
    access = simple.stub();
  });

  describe('Static assignment of channels to users and roles', function() {
    var expectedStaticAssignments = [
      {
        expectedUsers: [ 'user1', 'user2' ],
        expectedRoles: [ 'role1', 'role2' ],
        expectedChannels: [ 'channel1', 'channel2' ]
      },
      {
        expectedUsers: 'user3',
        expectedChannels: 'channel3'
      },
      {
        expectedRoles: 'role3',
        expectedChannels: 'channel4'
      }
    ];

    it('is applied when creating a valid document', function() {
      var doc = { _id: 'staticAccessDoc' };

      testHelper.verifyDocumentCreated(doc, 'write', expectedStaticAssignments);
    });

    it('is applied when replacing an existing valid document', function() {
      var doc = { _id: 'staticAccessDoc' };
      var oldDoc = { _id: 'staticAccessDoc' };

      testHelper.verifyDocumentReplaced(doc, oldDoc, 'write', expectedStaticAssignments);
    });

    it('is applied when deleting an existing document', function() {
      var oldDoc = { _id: 'staticAccessDoc' };

      testHelper.verifyDocumentDeleted(oldDoc, 'write', expectedStaticAssignments);
    });

    it('is NOT applied when creating an invalid document', function() {
      var doc = {
        _id: 'staticAccessDoc',
        invalidProperty: 'foobar'
      };

      expect(syncFunction).withArgs(doc).to.throwException(function(ex) {
        expect(access.callCount).to.be(0);
      });
    });

    it('is NOT applied when replacing an invalid document', function() {
      var doc = {
        _id: 'staticAccessDoc',
        invalidProperty: 'foobar'
      };
      var oldDoc = { _id: 'staticAccessDoc' };

      expect(syncFunction).withArgs(doc, oldDoc).to.throwException(function(ex) {
        expect(access.callCount).to.be(0);
      });
    });
  });

  describe('Dynamic assignment of channels to users and roles', function() {
    var doc = {
      _id: 'dynamicAccessDoc',
      users: [ 'user1', 'user2' ],
      roles: [ 'role1', 'role2' ]
    };
    var expectedDynamicAssignments = [
      {
        expectedUsers: doc.users,
        expectedRoles: doc.roles,
        expectedChannels: [ doc._id + '-channel1', doc._id + '-channel2' ]
      },
      {
        expectedUsers: doc.users,
        expectedChannels: [ doc._id + '-channel3' ]
      },
      {
        expectedRoles: doc.roles,
        expectedChannels: [ doc._id + '-channel4' ]
      }
    ];

    it('is applied when creating a valid document', function() {
      testHelper.verifyDocumentCreated(doc, 'write', expectedDynamicAssignments);
    });

    it('is applied when replacing an existing valid document', function() {
      var oldDoc = { _id: 'dynamicAccessDoc' };

      testHelper.verifyDocumentReplaced(doc, oldDoc, 'write', expectedDynamicAssignments);
    });

    it('is applied when deleting an existing document', function() {
      var oldDoc = { _id: 'dynamicAccessDoc' };

      var expectedDeleteAssignments = [
        {
          expectedUsers: null,
          expectedChannels: [ doc._id + '-channel3' ]
        },
        {
          expectedRoles: null,
          expectedChannels: [ doc._id + '-channel4' ]
        },
        {
          expectedUsers: null,
          expectedRoles: null,
          expectedChannels: [ doc._id + '-channel1', doc._id + '-channel2' ]
        }
      ];

      testHelper.verifyDocumentDeleted(oldDoc, 'write', expectedDeleteAssignments);
    });

    it('is NOT applied when creating an invalid document', function() {
      var doc = {
        _id: 'dynamicAccessDoc',
        users: [ 'user1' ],
        roles: [ 'role1' ],
        invalidProperty: 'foobar'
      };

      expect(syncFunction).withArgs(doc).to.throwException(function(ex) {
        expect(access.callCount).to.be(0);
      });
    });
<commit_msg>Issue #24: Scramble access assignment user, role and channel lists<commit_after>var expect = require('expect.js');
var simple = require('simple-mock');
var fs = require('fs');
var testHelper = require('../etc/test-helper.js');
var errorFormatter = testHelper.validationErrorFormatter;

// Load the contents of the sync function file into a global variable called syncFunction
/*jslint evil: true */
eval('var syncFunction = ' + fs.readFileSync('build/sync-functions/test-access-assignment-sync-function.js').toString());
/*jslint evil: false */

// Placeholders for stubbing built-in Sync Gateway support functions.
// More info: http://developer.couchbase.com/mobile/develop/guides/sync-gateway/sync-function-api-guide/index.html
var requireAccess;
var channel;
var access;

describe('User and role access assignment:', function() {
  beforeEach(function() {
    testHelper.init('build/sync-functions/test-access-assignment-sync-function.js');

    requireAccess = simple.stub();
    channel = simple.stub();
    access = simple.stub();
  });

  describe('Static assignment of channels to users and roles', function() {
    var expectedStaticAssignments = [
      {
        expectedUsers: [ 'user2', 'user1' ],
        expectedRoles: [ 'role2', 'role1' ],
        expectedChannels: [ 'channel1', 'channel2' ]
      },
      {
        expectedUsers: 'user3',
        expectedChannels: 'channel3'
      },
      {
        expectedRoles: 'role3',
        expectedChannels: 'channel4'
      }
    ];

    it('is applied when creating a valid document', function() {
      var doc = { _id: 'staticAccessDoc' };

      testHelper.verifyDocumentCreated(doc, 'write', expectedStaticAssignments);
    });

    it('is applied when replacing an existing valid document', function() {
      var doc = { _id: 'staticAccessDoc' };
      var oldDoc = { _id: 'staticAccessDoc' };

      testHelper.verifyDocumentReplaced(doc, oldDoc, 'write', expectedStaticAssignments);
    });

    it('is applied when deleting an existing document', function() {
      var oldDoc = { _id: 'staticAccessDoc' };

      testHelper.verifyDocumentDeleted(oldDoc, 'write', expectedStaticAssignments);
    });

    it('is NOT applied when creating an invalid document', function() {
      var doc = {
        _id: 'staticAccessDoc',
        invalidProperty: 'foobar'
      };

      expect(syncFunction).withArgs(doc).to.throwException(function(ex) {
        expect(access.callCount).to.be(0);
      });
    });

    it('is NOT applied when replacing an invalid document', function() {
      var doc = {
        _id: 'staticAccessDoc',
        invalidProperty: 'foobar'
      };
      var oldDoc = { _id: 'staticAccessDoc' };

      expect(syncFunction).withArgs(doc, oldDoc).to.throwException(function(ex) {
        expect(access.callCount).to.be(0);
      });
    });
  });

  describe('Dynamic assignment of channels to users and roles', function() {
    var doc = {
      _id: 'dynamicAccessDoc',
      users: [ 'user1', 'user2' ],
      roles: [ 'role1', 'role2' ]
    };
    var expectedDynamicAssignments = [
      {
        expectedUsers: doc.users,
        expectedRoles: doc.roles,
        expectedChannels: [ doc._id + '-channel1', doc._id + '-channel2' ]
      },
      {
        expectedUsers: doc.users,
        expectedChannels: [ doc._id + '-channel3' ]
      },
      {
        expectedRoles: doc.roles,
        expectedChannels: [ doc._id + '-channel4' ]
      }
    ];

    it('is applied when creating a valid document', function() {
      testHelper.verifyDocumentCreated(doc, 'write', expectedDynamicAssignments);
    });

    it('is applied when replacing an existing valid document', function() {
      var oldDoc = { _id: 'dynamicAccessDoc' };

      testHelper.verifyDocumentReplaced(doc, oldDoc, 'write', expectedDynamicAssignments);
    });

    it('is applied when deleting an existing document', function() {
      var oldDoc = { _id: 'dynamicAccessDoc' };

      var expectedDeleteAssignments = [
        {
          expectedUsers: null,
          expectedChannels: [ doc._id + '-channel3' ]
        },
        {
          expectedRoles: null,
          expectedChannels: [ doc._id + '-channel4' ]
        },
        {
          expectedUsers: null,
          expectedRoles: null,
          expectedChannels: [ doc._id + '-channel2', doc._id + '-channel1' ]
        }
      ];

      testHelper.verifyDocumentDeleted(oldDoc, 'write', expectedDeleteAssignments);
    });

    it('is NOT applied when creating an invalid document', function() {
      var doc = {
        _id: 'dynamicAccessDoc',
        users: [ 'user1' ],
        roles: [ 'role1' ],
        invalidProperty: 'foobar'
      };

      expect(syncFunction).withArgs(doc).to.throwException(function(ex) {
        expect(access.callCount).to.be(0);
      });
    });

{'fraction_non_alphanumeric': 0.1341323606044815, 'fraction_numerical': 0.00583637311099531, 'mean_word_length': 2.763137254901961, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '947108', 'n_tokens_mistral': 2777, 'n_tokens_neox': 2723, 'n_words': 633}