<commit_before>/*!
 * Copyright (c) 2015-2017 Cisco Systems, Inc. See LICENSE file.
 */

import '@ciscospark/internal-plugin-encryption';

import {assert} from '@ciscospark/test-helper-chai';
import sinon from '@ciscospark/test-helper-sinon';
import CiscoSpark from '@ciscospark/spark-core';
import testUsers from '@ciscospark/test-helper-test-users';
import uuid from 'uuid';

describe(`Encryption`, function() {
  this.timeout(30000);
  describe(`KMS`, () => {
    let mccoy, spark, spock;

    before(`create test user`, () => testUsers.create({count: 2})
      .then((users) => {
        spock = users[0];
        spark = new CiscoSpark({
          credentials: {
            authorization: spock.token
          }
        });
        assert.isTrue(spark.canAuthorize);

        mccoy = users[1];
        mccoy.spark = new CiscoSpark({
          credentials: {
            authorization: mccoy.token
          }
        });

        assert.isTrue(mccoy.spark.canAuthorize);
        return mccoy.spark.internal.device.register();
      }));

    after(() => spark && spark.internal.mercury.disconnect());

    describe(`#createResource()`, () => {
      it(`creates a kms resource object`, () => spark.internal.encryption.kms.createUnboundKeys({count: 1})
        .then(([key]) => spark.internal.encryption.kms.createResource({
          userIds: [spark.internal.device.userId],
          key
        })
          .then((kro) => {
            assert.property(kro, `uri`);
            assert.property(kro, `keyUris`);
            assert.lengthOf(kro.keyUris, 1);
            assert.include(kro.keyUris, key.uri);
            assert.property(kro, `authorizationUris`);
            assert.lengthOf(kro.authorizationUris, 1);
          })));
    });

    describe(`#addAuthorization()`, () => {
      let boundedKeyUri, kro, otherKro;
      before(`create a resource`, () => spark.internal.encryption.kms.createUnboundKeys({count: 1})
        .then(([key]) => spark.internal.encryption.kms.createResource({
          key
        }))
        .then((k) => {
          kro = k;
          boundedKeyUri = kro.keyUris[0];
          assert.lengthOf(kro.authorizationUris, 1);
        }));

      it(`authorizes a user to a key`, () => spark.internal.encryption.kms.addAuthorization({
        userIds: [mccoy.spark.internal.device.userId],
        kroUri: kro.uri
      })
        .then(([auth]) => {
          assert.equal(auth.resourceUri, kro.uri);
          assert.equal(auth.authId, mccoy.spark.internal.device.userId);
          return assert.isFulfilled(mccoy.spark.internal.encryption.kms.fetchKey({uri: boundedKeyUri}));
        }));

      it(`authorizes a resource to a key`, () => spark.internal.encryption.kms.createUnboundKeys({count: 1})
        .then(([key]) => spark.internal.encryption.kms.createResource({key}))
        .then((k) => {
          otherKro = k;
          return spark.internal.encryption.kms.addAuthorization({
            authIds: [otherKro.uri],
            kro
          });
        })
        .then(([auth]) => {
          assert.equal(auth.resourceUri, kro.uri);
          assert.equal(auth.authId, otherKro.uri);
        }));
    });

    /**
     *Test listAuthorizations function
     *Setup: Create a resource, then authorize a user to a key, then authorize a resource to a key
     *Test:
     *1)Invoke listAuthorizations on the resource, verify that the function returns the array that contains two authorized items
     *2)Remove the authorized user from the resource, then invoke listAuthorizations again, verify that the returned array doesn't have the user.
     *3)Remove the authorized resource from the resource again, then invoke listAuthorizations again, verify that he returned array doesn't have the resource.
     */
    describe(`#listAuthorizations()`, () => {
      let boundedKeyUri, kro, otherKro;
      let testResourceId;

      before(`creates a resource`, () => spark.internal.encryption.kms.createUnboundKeys({count: 1})
        .then(([key]) => spark.internal.encryption.kms.createResource({
          key
        }))
        .then((k) => {
          kro = k;
          boundedKeyUri = kro.keyUris[0];
          assert.lengthOf(kro.authorizationUris, 1);
        }));

      before(`authorizes a user to a key`, () => spark.internal.encryption.kms.addAuthorization({
        userIds: [mccoy.spark.internal.device.userId],
        kroUri: kro.uri
      })
        .then(([auth]) => {
          assert.equal(auth.resourceUri, kro.uri);
          assert.equal(auth.authId, mccoy.spark.internal.device.userId);
          return assert.isFulfilled(mccoy.spark.internal.encryption.kms.fetchKey({uri: boundedKeyUri}));
        }));

      before(`authorizes a resource to a key`, () => spark.internal.encryption.kms.createUnboundKeys({count: 1})
        .then(([key]) => spark.internal.encryption.kms.createResource({key}))
        .then((k) => {
          otherKro = k;
          testResourceId = otherKro.uri;
          return spark.internal.encryption.kms.addAuthorization({
            authIds: [testResourceId],
            kro
          });
        })
        .then(([auth]) => {
          assert.equal(auth.resourceUri, kro.uri);
          assert.equal(auth.authId, otherKro.uri);
        }));

      it(`list authorizations`, () => spark.internal.encryption.kms.listAuthorizations({kroUri: kro.uri})
        .then((authorizations) => {
          assert.equal(authorizations.length, 2);
          assert.include(authorizations.map((a) => a.authId), mccoy.spark.internal.device.userId);
          assert.include(authorizations.map((a) => a.authId), testResourceId);
        }));

      it(`remove the user and verify this user is not in the authorization list `, () => spark.internal.encryption.kms.removeAuthorization({
        userId: mccoy.spark.internal.device.userId,
        kroUri: kro.uri
      })
        .then(([auth]) => {
          assert.equal(auth.resourceUri, kro.uri);
          assert.equal(auth.authId, mccoy.spark.internal.device.userId);
          return spark.internal.encryption.kms.listAuthorizations({kroUri: kro.uri})
            .then((authorizations) => {
              assert.equal(authorizations.length, 1);
              assert.include(authorizations.map((a) => a.authId), testResourceId);
            });
        }));

      it(`remove the resource and verify this resource is not in the authorizaiton list`, () => spark.internal.encryption.kms.removeAuthorization({
        userId: testResourceId,
        kroUri: kro.uri
      })
        .then(([auth]) => {
          assert.equal(auth.resourceUri, kro.uri);
          assert.equal(auth.authId, testResourceId);
          return spark.internal.encryption.kms.listAuthorizations({kroUri: kro.uri})
            .then((authorizations) => {
              assert.equal(authorizations.length, 0);
            });
        }));
    });

    describe(`#removeAuthorization()`, () => {
      let boundedKeyUri, kro, otherKro;
      before(`create resource`, () => spark.internal.encryption.kms.createUnboundKeys({count: 1})
        .then(([key]) => spark.internal.encryption.kms.createResource({
          key
        }))
        .then((k) => {
          kro = k;
          boundedKeyUri = kro.keyUris[0];
          assert.lengthOf(kro.authorizationUris, 1);
        }));

      before(`create another resource`, () => spark.internal.encryption.kms.createUnboundKeys({count: 1})<commit_msg>feat(@ciscospark/i-p-encryption): get auth list of the resource fixed tests<commit_after>/*!
 * Copyright (c) 2015-2017 Cisco Systems, Inc. See LICENSE file.
 */

import '@ciscospark/internal-plugin-encryption';

import {assert} from '@ciscospark/test-helper-chai';
import sinon from '@ciscospark/test-helper-sinon';
import CiscoSpark from '@ciscospark/spark-core';
import testUsers from '@ciscospark/test-helper-test-users';
import uuid from 'uuid';

describe(`Encryption`, function() {
  this.timeout(30000);
  describe(`KMS`, () => {
    let mccoy, spark, spock;

    before(`create test user`, () => testUsers.create({count: 2})
      .then((users) => {
        spock = users[0];
        spark = new CiscoSpark({
          credentials: {
            authorization: spock.token
          }
        });
        assert.isTrue(spark.canAuthorize);

        mccoy = users[1];
        mccoy.spark = new CiscoSpark({
          credentials: {
            authorization: mccoy.token
          }
        });

        assert.isTrue(mccoy.spark.canAuthorize);
        return mccoy.spark.internal.device.register();
      }));

    after(() => spark && spark.internal.mercury.disconnect());

    describe(`#createResource()`, () => {
      it(`creates a kms resource object`, () => spark.internal.encryption.kms.createUnboundKeys({count: 1})
        .then(([key]) => spark.internal.encryption.kms.createResource({
          userIds: [spark.internal.device.userId],
          key
        })
          .then((kro) => {
            assert.property(kro, `uri`);
            assert.property(kro, `keyUris`);
            assert.lengthOf(kro.keyUris, 1);
            assert.include(kro.keyUris, key.uri);
            assert.property(kro, `authorizationUris`);
            assert.lengthOf(kro.authorizationUris, 1);
          })));
    });

    describe(`#addAuthorization()`, () => {
      let boundedKeyUri, kro, otherKro;
      before(`create a resource`, () => spark.internal.encryption.kms.createUnboundKeys({count: 1})
        .then(([key]) => spark.internal.encryption.kms.createResource({
          key
        }))
        .then((k) => {
          kro = k;
          boundedKeyUri = kro.keyUris[0];
          assert.lengthOf(kro.authorizationUris, 1);
        }));

      it(`authorizes a user to a key`, () => spark.internal.encryption.kms.addAuthorization({
        userIds: [mccoy.spark.internal.device.userId],
        kroUri: kro.uri
      })
        .then(([auth]) => {
          assert.equal(auth.resourceUri, kro.uri);
          assert.equal(auth.authId, mccoy.spark.internal.device.userId);
          return assert.isFulfilled(mccoy.spark.internal.encryption.kms.fetchKey({uri: boundedKeyUri}));
        }));

      it(`authorizes a resource to a key`, () => spark.internal.encryption.kms.createUnboundKeys({count: 1})
        .then(([key]) => spark.internal.encryption.kms.createResource({key}))
        .then((k) => {
          otherKro = k;
          return spark.internal.encryption.kms.addAuthorization({
            authIds: [otherKro.uri],
            kro
          });
        })
        .then(([auth]) => {
          assert.equal(auth.resourceUri, kro.uri);
          assert.equal(auth.authId, otherKro.uri);
        }));
    });

    /**
     *Test listAuthorizations function
     *Setup: Create a resource, then authorize a user to a key, then authorize a resource to a key
     *Test:
     *1)Invoke listAuthorizations on the resource, verify that the function returns the array that contains two authorized items
     *2)Remove the authorized user from the resource, then invoke listAuthorizations again, verify that the returned array doesn't have the user.
     *3)Remove the authorized resource from the resource again, then invoke listAuthorizations again, verify that he returned array doesn't have the resource.
     */
    describe(`#listAuthorizations()`, () => {
      let boundedKeyUri, kro, otherKro;
      let testResourceId;

      before(`creates a resource`, () => spark.internal.encryption.kms.createUnboundKeys({count: 1})
        .then(([key]) => spark.internal.encryption.kms.createResource({
          key
        }))
        .then((k) => {
          kro = k;
          boundedKeyUri = kro.keyUris[0];
          assert.lengthOf(kro.authorizationUris, 1);
        }));

      before(`authorizes a user to a key`, () => spark.internal.encryption.kms.addAuthorization({
        userIds: [mccoy.spark.internal.device.userId],
        kroUri: kro.uri
      })
        .then(([auth]) => {
          assert.equal(auth.resourceUri, kro.uri);
          assert.equal(auth.authId, mccoy.spark.internal.device.userId);
          return assert.isFulfilled(mccoy.spark.internal.encryption.kms.fetchKey({uri: boundedKeyUri}));
        }));

      before(`authorizes a resource to a key`, () => spark.internal.encryption.kms.createUnboundKeys({count: 1})
        .then(([key]) => spark.internal.encryption.kms.createResource({key}))
        .then((k) => {
          otherKro = k;
          testResourceId = otherKro.uri;
          return spark.internal.encryption.kms.addAuthorization({
            authIds: [testResourceId],
            kro
          });
        })
        .then(([auth]) => {
          assert.equal(auth.resourceUri, kro.uri);
          assert.equal(auth.authId, otherKro.uri);
        }));

      it(`list authorizations`, () => spark.internal.encryption.kms.listAuthorizations({kroUri: kro.uri})
        .then((authorizations) => {
          assert.equal(authorizations.length, 3);
          assert.include(authorizations.map((a) => a.authId), mccoy.spark.internal.device.userId);
          assert.include(authorizations.map((a) => a.authId), spock.id);
          assert.include(authorizations.map((a) => a.resourceUri), testResourceId);
          assert.include(authorizations.map((a) => a.resourceUri), kro.uri);
        }));

      it(`remove the user and verify this user is not in the authorization list `, () => spark.internal.encryption.kms.removeAuthorization({
        userId: mccoy.spark.internal.device.userId,
        kroUri: kro.uri
      })
        .then(([auth]) => {
          assert.equal(auth.resourceUri, kro.uri);
          assert.equal(auth.authId, mccoy.spark.internal.device.userId);
          return spark.internal.encryption.kms.listAuthorizations({kroUri: kro.uri})
            .then((authorizations) => {
              assert.equal(authorizations.length, 2);
              assert.include(authorizations.map((a) => a.authId), spock.id);
              assert.include(authorizations.map((a) => a.resourceUri), testResourceId);
              assert.include(authorizations.map((a) => a.resourceUri), kro.uri);
            });
        }));

      it(`remove the resource and verify this resource is not in the authorizaiton list`, () => spark.internal.encryption.kms.removeAuthorization({
        userId: testResourceId,
        kroUri: kro.uri
      })
        .then(([auth]) => {
          assert.equal(auth.resourceUri, kro.uri);
          assert.equal(auth.authId, testResourceId);
          return spark.internal.encryption.kms.listAuthorizations({kroUri: kro.uri})
            .then((authorizations) => {
              assert.equal(authorizations.length, 1);
              assert.include(authorizations.map((a) => a.authId), spock.id);
              assert.include(authorizations.map((a) => a.resourceUri), kro.uri);
            });
        }));
    });

    describe(`#removeAuthorization()`, () => {
      let boundedKeyUri, kro, otherKro;
      before(`create resource`, () => spark.internal.encryption.kms.createUnboundKeys({count: 1})
        .then(([key]) => spark.internal.encryption.kms.createResource({
          key
        }))
        .then((k) => {
          kro = k;
          boundedKeyUri = kro.keyUris[0];
          assert.lengthOf(kro.authorizationUris, 1);
        }));

      before(`create another resource`, () => spark.internal.encryption.kms.createUnboundKeys({count: 1})
{'fraction_non_alphanumeric': 0.14725003250552593, 'fraction_numerical': 0.004810817839032635, 'mean_word_length': 2.4740289069557364, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 97, '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': '423874', 'n_tokens_mistral': 4672, 'n_tokens_neox': 4601, 'n_words': 827}