Skip to content

Commit

Permalink
WIP Fix some tests
Browse files Browse the repository at this point in the history
(The "ensureCustomerItem backfills UserPoolId" test has been removed
 since we no longer require backfilling UserPoolId.)
  • Loading branch information
Alex Chew committed Aug 30, 2019
1 parent 32a58df commit 02551e7
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 203 deletions.
2 changes: 1 addition & 1 deletion lambdas/backend/__tests__/admin-catalog-visibility-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,4 +357,4 @@ describe('deleteAdminCatalogVisibility', () => {
expect(mockResponseObject.status).toHaveBeenCalledWith(400)
expect(mockResponseObject.json).toHaveBeenCalledWith({ message: 'Invalid input' })
})
})
})
181 changes: 95 additions & 86 deletions lambdas/backend/__tests__/customers-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,95 +2,104 @@ const customers = require('dev-portal-common/customers-controller')
const promiser = require('../../setup-jest').promiser

describe('customersController', () => {
test('ensureCustomerItem verifies that DDB is up-to-date', async () => {
let error = jest.fn(),
callback = jest.fn(),
entry = {
Id: 'cognitoIdentityId',
UserPoolId: 'cognitoUserId',
ApiKeyId: 'keyId'
}

customers.dynamoDb.get = jest.fn().mockReturnValue(promiser({ Item: entry }))

let returnValue = await customers.ensureCustomerItem('cognitoIdentityId', 'cognitoUserId', 'keyId', error, callback)

expect(customers.dynamoDb.get).toHaveBeenCalledTimes(1)
expect(customers.dynamoDb.get).toHaveBeenCalledWith({
TableName: 'DevPortalCustomers',
Key: {
Id: 'cognitoIdentityId'
}
})

expect(returnValue).toEqual(entry)
test('ensureCustomerItem verifies that DDB is up-to-date', async () => {
let error = jest.fn(),
callback = jest.fn(),
entry = {
Id: 'cognitoIdentityId',
UserPoolId: 'cognitoUserId',
ApiKeyId: 'keyId',
}
process.env['PreLoginAccountsTableName'] = 'PreLoginAccountsTable'

customers.dynamoDb.get = jest.fn().mockImplementation(({ TableName }) => {
if (TableName === 'DevPortalCustomers') {
return promiser({ Item: entry })
} else {
return promiser({ Items: [] })
}
})

test('ensureCustomerItem fixes DDB if it is not up-to-date', async () => {
let error = jest.fn(),
callback = jest.fn(),
entry = {
Id: 'cognitoIdentityId',
UserPoolId: 'cognitoUserId',
ApiKeyId: 'keyId'
}

customers.dynamoDb.get = jest.fn().mockReturnValue(promiser({ Item: undefined }))

customers.dynamoDb.put = jest.fn().mockReturnValue(promiser({}))

let returnValue = await customers.ensureCustomerItem('cognitoIdentityId', 'cognitoUserId', 'keyId', error, callback)

expect(customers.dynamoDb.get).toHaveBeenCalledTimes(1)
expect(customers.dynamoDb.get).toHaveBeenCalledWith({
TableName: 'DevPortalCustomers',
Key: {
Id: 'cognitoIdentityId'
}
})

expect(customers.dynamoDb.put).toHaveBeenCalledTimes(1)
expect(customers.dynamoDb.put).toHaveBeenCalledWith({
TableName: 'DevPortalCustomers',
Item: entry
})

expect(returnValue).toEqual(entry)
let returnValue = await customers.ensureCustomerItem(
'cognitoIdentityId',
'cognitoUserId',
'keyId',
error,
callback,
)

expect(customers.dynamoDb.get).toHaveBeenCalledTimes(2)
expect(customers.dynamoDb.get).toHaveBeenCalledWith({
TableName: 'PreLoginAccountsTable',
Key: {
UserId: 'cognitoUserId',
},
})
expect(customers.dynamoDb.get).toHaveBeenCalledWith({
TableName: 'DevPortalCustomers',
Key: {
Id: 'cognitoIdentityId',
},
})

test('ensureCustomerItem backfills UserPoolId', async () => {
let error = jest.fn(),
callback = jest.fn(),
oldEntry = {
Id: 'cognitoIdentityId',
ApiKeyId: 'keyId'
},
entry = {
Id: 'cognitoIdentityId',
UserPoolId: 'cognitoUserId',
ApiKeyId: 'keyId'
}

customers.dynamoDb.get = jest.fn().mockReturnValue(promiser({ Item: oldEntry }))

customers.dynamoDb.put = jest.fn().mockReturnValue(promiser({}))

let returnValue = await customers.ensureCustomerItem('cognitoIdentityId', 'cognitoUserId', 'keyId', error, callback)

expect(customers.dynamoDb.get).toHaveBeenCalledTimes(1)
expect(customers.dynamoDb.get).toHaveBeenCalledWith({
TableName: 'DevPortalCustomers',
Key: {
Id: 'cognitoIdentityId'
}
})

expect(customers.dynamoDb.put).toHaveBeenCalledTimes(1)
expect(customers.dynamoDb.put).toHaveBeenCalledWith({
TableName: 'DevPortalCustomers',
Item: entry
})

expect(returnValue).toEqual(entry)
expect(returnValue).toEqual(entry)
})

test('ensureCustomerItem fixes DDB if it is not up-to-date', async () => {
let error = jest.fn()
let callback = jest.fn()

process.env['PreLoginAccountsTableName'] = 'PreLoginAccountsTable'

customers.dynamoDb.get = jest
.fn()
.mockImplementation(({ TableName }) => {
if (TableName === 'PreLoginAccountsTable') {
return promiser({
UserId: 'cognitoUserId',
RegistrationStatus: 'registered',
})
} else if (TableName === 'CustomersTable') {
return promiser({})
}
})

customers.dynamoDb.put = jest.fn().mockReturnValue(promiser({}))

let returnValue = await customers.ensureCustomerItem(
'cognitoIdentityId',
'cognitoUserId',
'keyId',
error,
callback,
)

// Once for PreLoginAccountsTable, once for CustomersTable
expect(customers.dynamoDb.get).toHaveBeenCalledTimes(2)
expect(customers.dynamoDb.get).toHaveBeenCalledWith({
TableName: 'PreLoginAccountsTable',
Key: {
UserId: 'cognitoUserId',
},
})
expect(customers.dynamoDb.get).toHaveBeenCalledWith({
TableName: 'DevPortalCustomers',
Key: {
Id: 'cognitoIdentityId',
},
})

const expectedPutItem = {
TableName: 'DevPortalCustomers',
Item: {
Id: 'cognitoIdentityId',
UserPoolId: 'cognitoUserId',
RegistrationStatus: 'registered',
},
}
expect(customers.dynamoDb.put).toHaveBeenCalledTimes(1)
expect(customers.dynamoDb.put).toHaveBeenCalledWith(expectedPutItem)

expect(returnValue).toEqual(expectedPutItem)
})
})
2 changes: 1 addition & 1 deletion lambdas/backend/__tests__/get-sdk-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,4 @@ describe('getSdk', () => {
expect(res.status().json).toHaveBeenCalledTimes(1)
expect(res.status().json).toHaveBeenCalledWith({ message: `API with ID (thisApi) and Stage (shouldNotGenerateSDKs) is not enabled for SDK generation.` })
})
})
})
19 changes: 9 additions & 10 deletions lambdas/cognito-pre-signup-trigger/__tests__/demo.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const confirmationStrategy = require('../index')
const index = require('../index')

test('should always confirm users', () => {
const mockCallback = jest.fn(),
event = {}

confirmationStrategy.handler(event, {}, mockCallback)

expect(mockCallback).toHaveBeenCalledTimes(1)
expect(mockCallback).toHaveBeenCalledWith(null, { response: { autoConfirmUser: true } })
});
test('should always confirm users', async () => {
const event = {
userName: 'username',
request: { userAttributes: {} },
}
const result = await index.handler(event)
expect(result).toEqual(event)
})

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 02551e7

Please sign in to comment.