<commit_before>const should = require('should');
const config = require('../../../lib/config');
const services = require('../../../lib/services');
const schemas = require('../../../lib/schemas');
const credentialService = services.credential;
const userService = services.user;
const db = require('../../../lib/db');

describe('Credential tests', () => {
  const username = 'someUser';
  const credential = {
    secret: 'password'
  };

  function insertCredential(type) {
    return credentialService.insertCredential(username, type, credential);
  }

  beforeEach(() => db.flushdb());

  describe("'oauth2' specific cases", () => {
    const policy = 'oauth2';

    it('should not insert a credential that already exists', () => {
      return insertCredential(policy).then(newCredential => {
        return should(insertCredential(policy)).be.rejected();
      });
    });

    it('should insert a credential without password specified if autoGeneratePassword is set to true', () => {
      return credentialService
        .insertCredential(username, policy, {})
        .then(newCredential => {
          should.exist(newCredential);
          should.exist(newCredential.secret);
          should(newCredential.secret.length).greaterThanOrEqual(10);
        });
    });

    it('should insert a credential with id but different type that already exists', () => {
      return insertCredential(policy)
        .then(() => credentialService.insertCredential(username, 'basic-auth', credential))
        .then(newCredential => {
          should.exist(newCredential);
          newCredential.isActive.should.eql(true);
        });
    });
  });

  describe('support for multiple credentials', () => {
    ['key-auth', 'jwt'].forEach(policy => {
      it(policy, () => {
        return Promise.all([
          insertCredential(policy),
          insertCredential(policy)
        ])
          .then(() => credentialService.getCredentials(username))
          .then(results => {
            results.should.be.instanceOf(Array).and.have.length(2);
          });
      });
    });
  });

  const tests = [
    { policy: 'oauth2', passwordKey: 'secret' },
    { policy: 'basic-auth', passwordKey: 'password' },
    { policy: 'key-auth' },
    { policy: 'jwt' }
  ];

  tests.forEach(({ policy, passwordKey }) => {
    describe(`policy: '${policy}'`, () => {
      it('should insert a credential', () => {
        return insertCredential(policy);
      });

      it("should get a credential and strip 'passwordKey' props", () => {
        return insertCredential(policy)
          .then(credential => credentialService.getCredential(credential.id, policy))
          .then(credential => {
            should.exist(credential);
            if (passwordKey) {
              should.not.exist(credential.passwordKey);
              should.not.exist(credential[passwordKey]);
            };
            credential.isActive.should.eql(true);
          });
      });

      it('should deactivate a credential', () => {
        return insertCredential(policy)
          .then(credential => {
            return credentialService.deactivateCredential(credential.id, policy).then(res => {
              should.exist(res);
              return credentialService.getCredential(credential.id, policy);
            });
          })
          .then(credential => {
            should.exist(credential);
            credential.isActive.should.eql(false);
          });
      });

      it('should reactivate a credential', () => {
        return insertCredential(policy)
          .then(credential => {
            return credentialService.activateCredential(credential.id, policy).then(res => {
              should.exist(res);
              return credentialService.getCredential(credential.id, policy);
            });
          })
          .then(credential => {
            should.exist(credential);
            credential.isActive.should.eql(true);
          });
      });
    });
  });
});

describe('Credential service tests', () => {
  describe('Credential Cascade Delete tests', () => {
    let user;

    before(() => {
      user = {
        username: 'irfanbaqui',
        firstname: 'irfan',
        lastname: 'baqui',
        email: '<EMAIL>'
      };

      return db.flushdb()
        .then(() => userService.insert(user))
        .then((newUser) => {
          user = newUser;
          return Promise.all([
            credentialService.insertCredential(user.id, 'oauth2').then((oauthCred) => should.exist(oauthCred.secret)),
            credentialService.insertCredential(user.id, 'basic-auth').then((basicAuthCred) => should.exist(basicAuthCred.password))
          ]);<commit_msg>rename `policy` param to `type`<commit_after>const should = require('should');
const config = require('../../../lib/config');
const services = require('../../../lib/services');
const schemas = require('../../../lib/schemas');
const credentialService = services.credential;
const userService = services.user;
const db = require('../../../lib/db');

describe('Credential tests', () => {
  const username = 'someUser';
  const credential = {
    secret: 'password'
  };

  function insertCredential(type) {
    return credentialService.insertCredential(username, type, credential);
  }

  beforeEach(() => db.flushdb());

  describe("'oauth2' specific cases", () => {
    const type = 'oauth2';

    it('should not insert a credential that already exists', () => {
      return insertCredential(type).then(newCredential => {
        return should(insertCredential(type)).be.rejected();
      });
    });

    it('should insert a credential without password specified if autoGeneratePassword is set to true', () => {
      return credentialService
        .insertCredential(username, type, {})
        .then(newCredential => {
          should.exist(newCredential);
          should.exist(newCredential.secret);
          should(newCredential.secret.length).greaterThanOrEqual(10);
        });
    });

    it('should insert a credential with id but different type that already exists', () => {
      return insertCredential(type)
        .then(() => credentialService.insertCredential(username, 'basic-auth', credential))
        .then(newCredential => {
          should.exist(newCredential);
          newCredential.isActive.should.eql(true);
        });
    });
  });

  describe('support for multiple credentials', () => {
    ['key-auth', 'jwt'].forEach(type => {
      it(type, () => {
        return Promise.all([
          insertCredential(type),
          insertCredential(type)
        ])
          .then(() => credentialService.getCredentials(username))
          .then(results => {
            results.should.be.instanceOf(Array).and.have.length(2);
          });
      });
    });
  });

  const tests = [
    { type: 'oauth2', passwordKey: 'secret' },
    { type: 'basic-auth', passwordKey: 'password' },
    { type: 'key-auth' },
    { type: 'jwt' }
  ];

  tests.forEach(({ type, passwordKey }) => {
    describe(`credential type: '${type}'`, () => {
      it('should insert a credential', () => {
        return insertCredential(type);
      });

      it("should get a credential and strip 'passwordKey' props", () => {
        return insertCredential(type)
          .then(credential => credentialService.getCredential(credential.id, type))
          .then(credential => {
            should.exist(credential);
            if (passwordKey) {
              should.not.exist(credential.passwordKey);
              should.not.exist(credential[passwordKey]);
            };
            credential.isActive.should.eql(true);
          });
      });

      it('should deactivate a credential', () => {
        return insertCredential(type)
          .then(credential => {
            return credentialService.deactivateCredential(credential.id, type).then(res => {
              should.exist(res);
              return credentialService.getCredential(credential.id, type);
            });
          })
          .then(credential => {
            should.exist(credential);
            credential.isActive.should.eql(false);
          });
      });

      it('should reactivate a credential', () => {
        return insertCredential(type)
          .then(credential => {
            return credentialService.activateCredential(credential.id, type).then(res => {
              should.exist(res);
              return credentialService.getCredential(credential.id, type);
            });
          })
          .then(credential => {
            should.exist(credential);
            credential.isActive.should.eql(true);
          });
      });
    });
  });
});

describe('Credential service tests', () => {
  describe('Credential Cascade Delete tests', () => {
    let user;

    before(() => {
      user = {
        username: 'irfanbaqui',
        firstname: 'irfan',
        lastname: 'baqui',
        email: '<EMAIL>'
      };

      return db.flushdb()
        .then(() => userService.insert(user))
        .then((newUser) => {
          user = newUser;
          return Promise.all([
            credentialService.insertCredential(user.id, 'oauth2').then((oauthCred) => should.exist(oauthCred.secret)),
            credentialService.insertCredential(user.id, 'basic-auth').then((basicAuthCred) => should.exist(basicAuthCred.password))
          ]);
{'fraction_non_alphanumeric': 0.1545211342964152, 'fraction_numerical': 0.00149812734082397, 'mean_word_length': 2.391146589259797, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 77, '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': '1081689', 'n_tokens_mistral': 2591, 'n_tokens_neox': 2555, 'n_words': 457}