-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test coverage for CopyUsers class
- Loading branch information
Showing
6 changed files
with
76 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
.git | ||
.node-version | ||
Dockerfile | ||
__mocks__ | ||
__tests__ | ||
node_modules | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default class S3ErrorFake { | ||
putObject(_, callback) { | ||
callback('uh oh') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default class S3SuccessFake { | ||
putObject(_, callback) { | ||
callback() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,66 @@ | ||
import AWS from 'aws-sdk' | ||
import CopyUsers from '../src/CopyUsers' | ||
import S3ErrorFake from '../__mocks__/s3-error-fake' | ||
import S3SuccessFake from '../__mocks__/s3-success-fake' | ||
|
||
describe('CopyUsers', () => { | ||
test('dummy test', () => { | ||
expect(true).toEqual(true) | ||
const userList = [ | ||
{ Username: 'user1' }, | ||
{ Username: 'user2' }, | ||
] | ||
const copier = new CopyUsers(userList) | ||
|
||
describe('constructor()', () => { | ||
it('sets userListString', () => { | ||
expect(copier.userListString).toEqual("[{\"Username\":\"user1\"},{\"Username\":\"user2\"}]") | ||
}) | ||
it('sets s3', () => { | ||
expect(copier.s3).toBeInstanceOf(AWS.S3) | ||
}) | ||
it('sets objectKey', () => { | ||
// Matches: {ISO DATE}/{AWS_REGION}_{USER_POOL_ID}.json | ||
expect(copier.objectKey).toMatch(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z\/my-region-1_uSeRPoOlId\.json/) | ||
}) | ||
}) | ||
|
||
describe('copy()', () => { | ||
const logSpy = jest.spyOn(console, 'log') | ||
const errorSpy = jest.spyOn(console, 'error') | ||
|
||
describe('when successful', () => { | ||
beforeAll(() => { | ||
copier.s3 = new S3SuccessFake() | ||
}) | ||
|
||
it('calls putObject() on this.s3 and logs a non-error message', () => { | ||
const s3Spy = jest.spyOn(copier.s3, 'putObject') | ||
copier.copy() | ||
expect(s3Spy).toHaveBeenCalledWith({ | ||
Body: "[{\"Username\":\"user1\"},{\"Username\":\"user2\"}]", | ||
Bucket: 'my-bucket-name', | ||
Key: expect.any(String) | ||
}, expect.any(Function)) | ||
expect(logSpy).toHaveBeenCalledWith('Users backed up to S3') | ||
expect(errorSpy).not.toHaveBeenCalled() | ||
}) | ||
}) | ||
|
||
describe('when it errors out', () => { | ||
beforeAll(() => { | ||
copier.s3 = new S3ErrorFake() | ||
}) | ||
|
||
it('calls putObject() on this.s3 and logs an error', () => { | ||
const s3Spy = jest.spyOn(copier.s3, 'putObject') | ||
copier.copy() | ||
expect(s3Spy).toHaveBeenCalledWith({ | ||
Body: "[{\"Username\":\"user1\"},{\"Username\":\"user2\"}]", | ||
Bucket: 'my-bucket-name', | ||
Key: expect.any(String) | ||
}, expect.any(Function)) | ||
expect(logSpy).not.toHaveBeenCalled() | ||
expect(errorSpy).toHaveBeenCalledWith('error copying backup to S3: uh oh') | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters