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


/* istanbul ignore next */
if (typeof Promise === 'undefined') {
  // eslint-disable-next-line global-require
  require('es6-promise').polyfill();
}

/* eslint-env mocha */
/* eslint camelcase: [0] */

const _ = require('lodash');
const assert = require('assert');
const retry = require('@webex/test-helper-retry');

let TestUsers;

try {
  /* eslint global-require: [0] */
  TestUsers = require('@ciscospark/test-users-legacy');
}
catch (e) {
  // ignore
}

const allUsers = [];

if (after) {
  after(function () {
    /* eslint no-invalid-this: [0] */
    this.timeout(120000);
    allUsers.forEach((user) => user.webex && user.webex.internal.mercury && user.webex.internal.mercury.disconnect());

    return _remove(allUsers);
  });
}

/**
 * Helper
 * @param {Object} options
 * @private
 * @returns {Promise<Array<User>>}
 */
function _create(options) {
  options = options || {};
  let {count} = options;

  if (!count) {
    count = 1;
  }

  const promises = [];

  for (let i = 0; i < count; i += 1) {
    promises.push(retry(makeUser));
  }

  return Promise.all(promises);

  /**
   * Helper
   * @private
   * @returns {Promise<User>}
   */
  function makeUser() {
    const config = _.defaults({
      scopes: process.env.WEBEX_SCOPE
    }, options.config);

    return TestUsers.create(config)
      .then((user) => {
        allUsers.push(user);

        return user;
      });
  }
}

/**
 * Gets users from environment variable
 * @param {Object} options
 * @returns {Array<User>}
 */
function _extractFromEnv(options) {
  /* eslint complexity: [0] */
  let {count} = options;

  if (!count) {
    count = 1;
  }

  const users = [{
    id: process.env.WEBEX_ID || process.env.WEBEX_ID_0,
    token: {
      access_token: process.env.WEBEX_ACCESS_TOKEN || process.env.WEBEX_ACCESS_TOKEN_0,
      refresh_token: process.env.WEBEX_REFRESH_TOKEN || process.env.WEBEX_REFRESH_TOKEN_0
    },
    email: process.env.WEBEX_EMAIL || process.env.WEBEX_EMAIL_0,
    name: process.env.WEBEX_NAME || process.env.WEBEX_NAME_0,
    password: <PASSWORD> || <PASSWORD>
  }];

  for (let i = 1; i < count; i += 1) {
    users.push({
      id: process.env[`WEBEX_ID_${i}`],
      token: {
        access_token: process.env[`WEBEX_ACCESS_TOKEN_${i}`],
        refresh_token: process.env[`WEBEX_REFRESH_TOKEN_${i}`]
      },
      email: process.env[`WEBEX_EMAIL_${i}`],
      name: process.env[`WEBEX_NAME_${i}`],
      password: <PASSWORD>[`<PASSWORD>}`]
    });
  }

  for (let j = 0; j < count; j += 1) {
    assert(users[j].token.access_token, `No access token available for user${j}`);
  }

  return users;
}

/**
 * Helper
 * @param {Array<User>} users
 * @returns {Promise}
 */
function _remove(users) {
  return Promise.all(users.map((user) => {
    if (user.token && !user.token.authorization) {
      Reflect.deleteProperty(user, 'token');
    }
    TestUsers.remove(user)
      .catch((reason) => {
        console.warn('failed to delete test user', reason);
      });

    // Edge times out waiting for the delete calls to complete (and test user
    // deletion isn't really something we need to wait for anyway) so we'll just
    // give enough time for the requests to go out, then allow the browser to
    // close, even if the requests haven't returned.
    return new Promise((resolve) => {
      setTimeout(resolve, 500);
    });
  }));
}

module.exports = {
  create: function create(options) {
    assert(process.env.WEBEX_CLIENT_ID, 'WEBEX_CLIENT_ID must be defined');
    assert(process.env.WEBEX_CLIENT_SECRET, 'WEBEX_CLIENT_SECRET must be defined');

    return new Promise((resolve) => {
      resolve(TestUsers ? _create(options) : _extractFromEnv(options));
    });
  },

  remove: function remove(users) {
    if (!TestUsers) {
      return Promise.resolve();
    }

    return _remove(users);
  }
};<commit_msg>feat(test-helper-test-users): convert to test-users<commit_after>/*!
 * Copyright (c) 2015-2019 Cisco Systems, Inc. See LICENSE file.
 */

/* eslint-env mocha */
/* eslint camelcase: [0] */

const assert = require('assert');

const _ = require('lodash');
const retry = require('@webex/test-helper-retry');
const {createTestUser, removeTestUser} = require('@webex/test-users');

const allUsers = [];

if (after) {
  after(function () {
    /* eslint no-invalid-this: [0] */
    this.timeout(120000);
    allUsers.forEach((user) => user.webex && user.webex.internal.mercury && user.webex.internal.mercury.disconnect());

    return _remove(allUsers);
  });
}

/**
 * Helper
 * @param {Object} options
 * @private
 * @returns {Promise<Array<User>>}
 */
function _create(options) {
  options = options || {};
  let {count} = options;

  if (!count) {
    count = 1;
  }

  const promises = [];

  for (let i = 0; i < count; i += 1) {
    promises.push(retry(makeUser));
  }

  return Promise.all(promises);

  /**
   * Helper
   * @private
   * @returns {Promise<User>}
   */
  function makeUser() {
    const config = _.defaults({
      scopes: process.env.WEBEX_SCOPE
    }, options.config);

    return createTestUser(config)
      .then((user) => {
        allUsers.push(user);

        return user;
      });
  }
}

/**
 * Helper
 * @param {Array<User>} users
 * @returns {Promise}
 */
function _remove(users) {
  return Promise.all(users.map((user) => {
    if (user.token && !user.token.authorization) {
      Reflect.deleteProperty(user, 'token');
    }
    removeTestUser(user)
      .catch((reason) => {
        console.warn('failed to delete test user', reason);
      });

    // Edge times out waiting for the delete calls to complete (and test user
    // deletion isn't really something we need to wait for anyway) so we'll just
    // give enough time for the requests to go out, then allow the browser to
    // close, even if the requests haven't returned.
    return new Promise((resolve) => {
      setTimeout(resolve, 500);
    });
  }));
}

module.exports = {
  create: function create(options) {
    assert(process.env.WEBEX_CLIENT_ID, 'WEBEX_CLIENT_ID must be defined');
    assert(process.env.WEBEX_CLIENT_SECRET, 'WEBEX_CLIENT_SECRET must be defined');

    return _create(options);
  },

  remove: function remove(users) {
    return _remove(users);
  }
};
{'fraction_non_alphanumeric': 0.1580783256698906, 'fraction_numerical': 0.00903757729506897, 'mean_word_length': 3.0934458144062296, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 27, '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': '4043832', 'n_tokens_mistral': 2193, 'n_tokens_neox': 2142, 'n_words': 484}