diff --git a/.babelrc b/.babelrc index f0daa17..686acbb 100644 --- a/.babelrc +++ b/.babelrc @@ -4,5 +4,10 @@ }]], "presets": [ "@babel/preset-env" - ] + ], + "env": { + "test": { + "plugins": ["@babel/plugin-transform-runtime"] + } + } } diff --git a/__mocks__/cognito-fake.js b/__mocks__/cognito-fake.js new file mode 100644 index 0000000..747aeb9 --- /dev/null +++ b/__mocks__/cognito-fake.js @@ -0,0 +1,18 @@ +export default class CognitoFake { + listUsers() { + return { + promise: async () => { + return new Promise(resolve => { + return resolve( + { + Users: [ + { Username: 'user1' }, + { Username: 'user2' } + ] + } + ) + }) + } + } + } +} diff --git a/__mocks__/cognito-pagination-fake.js b/__mocks__/cognito-pagination-fake.js new file mode 100644 index 0000000..1e1bc80 --- /dev/null +++ b/__mocks__/cognito-pagination-fake.js @@ -0,0 +1,33 @@ +export default class CognitoPaginationFake { + constructor() { + this.calledAlready = false + } + + listUsers() { + return { + promise: async () => { + return new Promise(resolve => { + if (this.calledAlready) { + return resolve( + { + Users: [ + { Username: 'user2' } + ] + } + ) + } else { + this.calledAlready= true + return resolve( + { + Users: [ + { Username: 'user1' } + ], + PaginationToken: 'token' + } + ) + } + }) + } + } + } +} diff --git a/__tests__/ExportUsers.test.js b/__tests__/ExportUsers.test.js index 2a6d1fd..e9b2d84 100644 --- a/__tests__/ExportUsers.test.js +++ b/__tests__/ExportUsers.test.js @@ -1,5 +1,51 @@ +import AWS from 'aws-sdk' +import ExportUsers from '../src/ExportUsers' +import CognitoFake from '../__mocks__/cognito-fake' +import CognitoPaginationFake from '../__mocks__/cognito-pagination-fake' + describe('ExportUsers', () => { - test('dummy test', () => { - expect(true).toEqual(true) + const exporter = new ExportUsers() + + describe('constructor()', () => { + it('sets cognito', () => { + expect(exporter.cognito).toBeInstanceOf(AWS.CognitoIdentityServiceProvider) + }) + }) + + describe('export()', () => { + const logSpy = jest.spyOn(console, 'log') + + describe('without pagination', () => { + beforeAll(() => { + exporter.cognito = new CognitoFake() + }) + + it('logs a message', async () => { + await exporter.export() + expect(logSpy).toHaveBeenCalledWith('Users exported from Cognito') + }) + + it('returns a list of users', async () => { + const userList = await exporter.export() + expect(userList).toEqual([{"Username":"user1"},{"Username":"user2"}]) + }) + }) + + describe('with pagination', () => { + // NOTE: Using beforeEach instead of beforeAll because the pagination fake stores the state of whether listUsers() has been called already + beforeEach(() => { + exporter.cognito = new CognitoPaginationFake() + }) + + it('logs a message', async () => { + await exporter.export() + expect(logSpy).toHaveBeenCalledWith('Users exported from Cognito') + }) + + it('returns a list of users', async () => { + const userList = await exporter.export() + expect(userList).toEqual([{"Username":"user1"},{"Username":"user2"}]) + }) + }) }) }) diff --git a/package-lock.json b/package-lock.json index a9aa5b1..f1fe39a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -570,6 +570,18 @@ "@babel/helper-plugin-utils": "^7.0.0" } }, + "@babel/plugin-transform-runtime": { + "version": "7.4.4", + "resolved": "https://registry.npmjs.org/@babel/plugin-transform-runtime/-/plugin-transform-runtime-7.4.4.tgz", + "integrity": "sha512-aMVojEjPszvau3NRg+TIH14ynZLvPewH4xhlCW1w6A3rkxTS1m4uwzRclYR9oS+rl/dr+kT+pzbfHuAWP/lc7Q==", + "dev": true, + "requires": { + "@babel/helper-module-imports": "^7.0.0", + "@babel/helper-plugin-utils": "^7.0.0", + "resolve": "^1.8.1", + "semver": "^5.5.1" + } + }, "@babel/plugin-transform-shorthand-properties": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/@babel/plugin-transform-shorthand-properties/-/plugin-transform-shorthand-properties-7.2.0.tgz", diff --git a/package.json b/package.json index d6b1063..bbb457a 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ } ], "devDependencies": { + "@babel/plugin-transform-runtime": "^7.4.4", "babel-eslint": "^10.0.1", "eslint": "^5.16.0", "eslint-plugin-import": "^2.17.2", diff --git a/src/ExportUsers.js b/src/ExportUsers.js index b24aa3e..ff284cd 100644 --- a/src/ExportUsers.js +++ b/src/ExportUsers.js @@ -13,11 +13,12 @@ export default class ExportUsers { const paginationCalls = async (exportedUsers = []) => { const { Users = [], PaginationToken } = await this.cognito.listUsers(params).promise() + const combinedUsers = exportedUsers.concat(Users) if (PaginationToken) { params.PaginationToken = PaginationToken - await paginationCalls(combinedUsers) + return await paginationCalls(combinedUsers) } return combinedUsers