<commit_before>'use strict';

const chai = require('chai');
const AWS = require('aws-sdk-mock');
const testData = require('./test-data.json');
const emptyData = require('./empty-data.json');
const VPCPlugin = require('../index.js');

const expect = chai.expect;

// Used for changing what to test
const vpc = 'ci';
const subnets = [
  'test_subnet_1',
  'test_subnet_2',
  'test_subnet_3',
];
const securityGroups = ['test_group_1'];
const vpcId = 'vpc-test';

// This will create a mock plugin to be used for testing
const constructPlugin = (vpcConfig) => {
  const serverless = {
    service: {
      provider: {
        region: 'us-moon-1',
      },
      custom: {
        vpc: vpcConfig,
      },
    },
    cli: {
      log() {
      },
    },
  };
  return new VPCPlugin(serverless);
};

describe('serverless-vpc-plugin', () => {
  it('registers hooks', () => {
    const plugin = constructPlugin({}, {});
    expect(plugin.hooks['before:deploy:initialize']).to.be.a('function');
  });
});


describe('Given a vpc,', () => {
  it('function updates vpc', () => {
    AWS.mock('EC2', 'describeVpcs', testData);
    AWS.mock('EC2', 'describeSubnets', testData);
    AWS.mock('EC2', 'describeSecurityGroups', testData);

    const plugin = constructPlugin({
      vpcName: vpc,
      subnetNames: subnets,
      securityGroupNames: securityGroups,
    });

    return plugin.updateVpcConfig().then((data) => {
      expect(data).to.eql({
        securityGroupIds: ['sg-test'],
        subnetIds: ['subnet-test-1', 'subnet-test-2', 'subnet-test-3'],
      });
    });
  });

  it('vpc option given does not exist', () => {
    AWS.mock('EC2', 'describeVpcs', emptyData);
    const plugin = constructPlugin({});

    return plugin.getVpcId('not_a_vpc_name').then(() => {
      throw new Error('No error thrown for invalid VPC options');<commit_msg>Fixed unit tests and added unit tests to check if AWS.config was updated<commit_after>'use strict';

const chai = require('chai');
const AWS = require('aws-sdk-mock');
const aws = require('aws-sdk');
const testData = require('./test-data.json');
const emptyData = require('./empty-data.json');
const VPCPlugin = require('../index.js');

const expect = chai.expect;

// Used for changing what to test
const testCreds = {
  accessKeyId: 'test_key',
  secretAccessKey: 'test_secret',
  sessionToken: '<PASSWORD>'
};
const vpc = 'ci';
const subnets = [
  'test_subnet_1',
  'test_subnet_2',
  'test_subnet_3',
];
const securityGroups = ['test_group_1'];
const vpcId = 'vpc-test';

// This will create a mock plugin to be used for testing
const constructPlugin = (vpcConfig, test_credentials) => {
  const serverless = {
    service: {
      provider: {
        region: 'us-moon-1',
      },
      custom: {
        vpc: vpcConfig,
      },
    },
    cli: {
      log() {
      },
    },
    providers: {
      aws: {
        getCredentials: function () {
          return new aws.Credentials (testCreds);
        }
      }
    }
  };
  return new VPCPlugin(serverless);
};

describe('serverless-vpc-plugin', () => {
  it('check aws config', () => {
    const plugin = constructPlugin({}, 'tests');
    const returnedCreds = plugin.ec2.config.credentials;
    expect(returnedCreds['accessKeyId']).to.equal(testCreds['accessKeyId']);
    expect(returnedCreds['sessionToken']).to.equal(testCreds['sessionToken']);
  });

  it('registers hooks', () => {
    const plugin = constructPlugin({});
    expect(plugin.hooks['before:deploy:initialize']).to.be.a('function');
  });
});


describe('Given a vpc,', () => {
  it('function updates vpc', () => {
    AWS.mock('EC2', 'describeVpcs', testData);
    AWS.mock('EC2', 'describeSubnets', testData);
    AWS.mock('EC2', 'describeSecurityGroups', testData);

    const plugin = constructPlugin({
      vpcName: vpc,
      subnetNames: subnets,
      securityGroupNames: securityGroups,
    });

    return plugin.updateVpcConfig().then((data) => {
      expect(data).to.eql({
        securityGroupIds: ['sg-test'],
        subnetIds: ['subnet-test-1', 'subnet-test-2', 'subnet-test-3'],
      });
    });
  });

  it('vpc option given does not exist', () => {
    AWS.mock('EC2', 'describeVpcs', emptyData);
    const plugin = constructPlugin({});

    return plugin.getVpcId('not_a_vpc_name').then(() => {
      throw new Error('No error thrown for invalid VPC options');
{'fraction_non_alphanumeric': 0.17477064220183486, 'fraction_numerical': 0.005733944954128441, 'mean_word_length': 3.2923228346456694, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 21, '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': '1157866', 'n_tokens_mistral': 1453, 'n_tokens_neox': 1433, 'n_words': 305}