<commit_before>const assert = require('assert');
const PassThrough = require('stream').PassThrough;
const helpers = require('yeoman-test');
const adminHelper = require('../../common/admin-helper')();
const idGen = require('uuid-base62');
const environment = require('../../fixtures/cli/environment');
const namespace = 'express-gateway:credentials:create';

describe('eg credentials create', () => {
  let program, env, user;

  before(() => {
    ({ program, env } = environment.bootstrap());
    return adminHelper.start();
  });
  after(() => adminHelper.stop());

  beforeEach(() => {
    env.prepareHijack();
    return adminHelper.admin.users.create({
      username: idGen.v4(),
      firstname: 'La',
      lastname: 'Deeda'
    })
    .then(createdUser => {
      user = createdUser;
    });
  });

  afterEach(() => {
    env.resetHijack();
  });

  it('creates a credential from prompts', done => {
    env.hijack(namespace, generator => {
      let output = null;
      let text = null;

      generator.once('run', () => {
        generator.log.error = message => {
          done(new Error(message));
        };
        generator.log.ok = message => {
          text = message;
        };
        generator.stdout = message => {
          output = message;
        };

        helpers.mockPrompt(generator, {});
      });

      generator.once('end', () => {
        const loggedCred = JSON.parse(output);
        assert.equal(text, 'Created ' + loggedCred.keyId);

        return adminHelper.admin.credentials.info(loggedCred.keyId, 'key-auth')
              .then(cred => {
                assert.ok(cred.keyId);
                assert.ok(cred.keySecret);
                assert.ok(cred.isActive);
                assert.equal(cred.consumerId, user.username);
                done();
              }).catch(done);
      });
    });

    env.argv = program.parse('credentials create -t key-auth -c ' + user.username);
  });

  it('creates a user from stdin', done => {
    const cmd = { consumer: user.username, type: 'key-auth' };

    env.hijack(namespace, generator => {
      let output = null;
      let text = null;
      generator.once('run', () => {
        generator.log.error = message => {
          done(new Error(message));
        };
        generator.stdout = message => {
          output = message;
        };

        generator.log.ok = message => {
          text = message;
        };

        generator.stdin = new PassThrough();
        generator.stdin.write(JSON.stringify(cmd), 'utf8');
        generator.stdin.end();
      });

      generator.once('end', () => {
        const loggedCred = JSON.parse(output);
        assert.equal(text, 'Created ' + loggedCred.keyId);
        return adminHelper.admin.credentials.info(loggedCred.keyId, 'key-auth')
              .then(cred => {
                assert.ok(cred.keyId);
                assert.ok(cred.keySecret);
                assert.ok(cred.isActive);
                assert.equal(cred.consumerId, user.username);
                done();
              }).catch(done);
      });
    });

    env.argv = program.parse('credentials create --stdin');
  });

  it('prints only the API key id when using the --quiet flag', done => {
    env.hijack(namespace, generator => {
      let output = null;

      generator.once('run', () => {
        generator.log.error = message => {
          done(new Error(message));
        };
        generator.stdout = message => {
          output = message;
        };
      });

      generator.once('end', () => {
        const loggedCred = output.split(':')[0];
        return adminHelper.admin.credentials.info(loggedCred, 'key-auth')
              .then(cred => {
                assert.ok(cred.keyId);
                assert.ok(cred.keySecret);
                assert.ok(cred.isActive);
                assert.equal(cred.consumerId, user.username);
                assert.equal(output, cred.keyId + ':' + cred.keySecret);

                done();
              }).catch(done);
      });
    });

    env.argv = program.parse('credentials create -t key-auth -c ' + user.username + ' -q');
  });

  it('prints error on invalid username from stdin', done => {
    const credential = {};

    env.hijack(namespace, generator => {
      let output = null;
      let error = null;

      generator.once('run', () => {
        generator.log = message => {
          output = message;
        };
        generator.log.error = message => {
          error = message;<commit_msg>Fix credential creations test<commit_after>const assert = require('assert');
const PassThrough = require('stream').PassThrough;
const helpers = require('yeoman-test');
const adminHelper = require('../../common/admin-helper')();
const idGen = require('uuid-base62');
const environment = require('../../fixtures/cli/environment');
const namespace = 'express-gateway:credentials:create';

describe('eg credentials create', () => {
  let program, env, user;

  before(() => {
    ({ program, env } = environment.bootstrap());
    return adminHelper.start();
  });
  after(() => adminHelper.stop());

  beforeEach(() => {
    env.prepareHijack();
    return adminHelper.admin.users.create({
      username: idGen.v4(),
      firstname: 'La',
      lastname: 'Deeda'
    })
      .then(createdUser => {
        user = createdUser;
      });
  });

  afterEach(() => {
    env.resetHijack();
  });

  it('creates a credential from prompts', done => {
    env.hijack(namespace, generator => {
      let output = null;
      let text = null;

      generator.once('run', () => {
        generator.log.error = message => {
          done(new Error(message));
        };
        generator.log.ok = message => {
          text = message;
        };
        generator.stdout = message => {
          output = message;
        };

        helpers.mockPrompt(generator, {});
      });

      generator.once('end', () => {
        const loggedCred = JSON.parse(output);
        assert.equal(text, 'Created ' + loggedCred.keyId);

        return adminHelper.admin.credentials.info(loggedCred.keyId, 'key-auth')
          .then(cred => {
            assert.ok(cred.keyId);
            assert.ok(cred.keySecret);
            assert.ok(cred.isActive);
            assert.equal(cred.consumerId, user.id);
            done();
          }).catch(done);
      });
    });

    env.argv = program.parse('credentials create -t key-auth -c ' + user.username);
  });

  it('creates a user from stdin', done => {
    const cmd = { consumer: user.username, type: 'key-auth' };

    env.hijack(namespace, generator => {
      let output = null;
      let text = null;
      generator.once('run', () => {
        generator.log.error = message => {
          done(new Error(message));
        };
        generator.stdout = message => {
          output = message;
        };

        generator.log.ok = message => {
          text = message;
        };

        generator.stdin = new PassThrough();
        generator.stdin.write(JSON.stringify(cmd), 'utf8');
        generator.stdin.end();
      });

      generator.once('end', () => {
        const loggedCred = JSON.parse(output);
        assert.equal(text, 'Created ' + loggedCred.keyId);
        return adminHelper.admin.credentials.info(loggedCred.keyId, 'key-auth')
          .then(cred => {
            assert.ok(cred.keyId);
            assert.ok(cred.keySecret);
            assert.ok(cred.isActive);
            assert.equal(cred.consumerId, user.id);
            done();
          }).catch(done);
      });
    });

    env.argv = program.parse('credentials create --stdin');
  });

  it('prints only the API key id when using the --quiet flag', done => {
    env.hijack(namespace, generator => {
      let output = null;

      generator.once('run', () => {
        generator.log.error = message => {
          done(new Error(message));
        };
        generator.stdout = message => {
          output = message;
        };
      });

      generator.once('end', () => {
        const loggedCred = output.split(':')[0];
        return adminHelper.admin.credentials.info(loggedCred, 'key-auth')
          .then(cred => {
            assert.ok(cred.keyId);
            assert.ok(cred.keySecret);
            assert.ok(cred.isActive);
            assert.equal(cred.consumerId, user.id);
            assert.equal(output, cred.keyId + ':' + cred.keySecret);

            done();
          }).catch(done);
      });
    });

    env.argv = program.parse('credentials create -t key-auth -c ' + user.username + ' -q');
  });

  it('prints error on invalid username from stdin', done => {
    const credential = {};

    env.hijack(namespace, generator => {
      let output = null;
      let error = null;

      generator.once('run', () => {
        generator.log = message => {
          output = message;
        };
        generator.log.error = message => {
          error = message;
{'fraction_non_alphanumeric': 0.16026936026936026, 'fraction_numerical': 0.001122334455667789, 'mean_word_length': 2.022727272727273, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 71, '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': '1348402', 'n_tokens_mistral': 2592, 'n_tokens_neox': 2507, 'n_words': 490}