Skip to content

Commit

Permalink
Add test coverage for CopyUsers class
Browse files Browse the repository at this point in the history
  • Loading branch information
mjgiarlo committed May 2, 2019
1 parent 7c3cd49 commit 6ce1065
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 3 deletions.
1 change: 1 addition & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ version: "2"

exclude_patterns:
- "__tests__/"
- "__mocks__/"
- 'eslintrc.js'
- 'jest.config.js'
- '**/node_modules/'
Expand Down
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.git
.node-version
Dockerfile
__mocks__
__tests__
node_modules
npm-debug.log
5 changes: 5 additions & 0 deletions __mocks__/s3-error-fake.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default class S3ErrorFake {
putObject(_, callback) {
callback('uh oh')
}
}
5 changes: 5 additions & 0 deletions __mocks__/s3-success-fake.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default class S3SuccessFake {
putObject(_, callback) {
callback()
}
}
65 changes: 63 additions & 2 deletions __tests__/CopyUsers.test.js
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')
})
})
})
})
2 changes: 1 addition & 1 deletion config/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ module.exports = {
awsAccessSecret: 'secret',
awsRegion: 'my-region-1',
s3Bucket: 'my-bucket-name',
userPoolId: 'my-region_1_uSeRPoOlId'
userPoolId: 'my-region-1_uSeRPoOlId'
}

0 comments on commit 6ce1065

Please sign in to comment.