Skip to content

Commit

Permalink
More functional test refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebroberts committed Sep 16, 2024
1 parent fd93e17 commit badb704
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 359 deletions.
126 changes: 28 additions & 98 deletions test/local/functional/domain/github/crawler/crawlUsers.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, test } from 'vitest'
import { test } from 'vitest'
import { FakeAppState } from '../../../../../testSupport/fakes/fakeAppState'
import { FakeGithubInstallationClient } from '../../../../../testSupport/fakes/fakeGithubInstallationClient'
import {
Expand All @@ -15,6 +15,15 @@ import example_personal_account_user from '../../../../../examples/github/person
import example_org_users from '../../../../../examples/github/org/api/users.json'
import { crawlUsers } from '../../../../../../src/app/domain/github/crawler/crawlUsers'
import { stubQueryAccountMembershipsByAccount } from '../../../../../testSupport/fakes/tableRecordReadStubs'
import {
expectBatchWrites,
expectBatchWritesLength
} from '../../../../../testSupport/fakes/dynamoDB/fakeDynamoDBInterfaceExpectations'
import {
expectedBatchDeleteGithubMemberships,
expectedBatchWriteGithubMemberships,
expectedBatchWriteGithubUsers
} from '../../../../../testSupport/fakes/tableRecordExpectedWrites'

test('user-crawler-for-personal-account-installation', async () => {
// A
Expand All @@ -27,42 +36,11 @@ test('user-crawler-for-personal-account-installation', async () => {
await crawlUsers(appState, testPersonalInstallation)

// A
expect(appState.dynamoDB.batchWrites.length).toEqual(2)
expect(appState.dynamoDB.batchWrites[0]).toEqual({
RequestItems: {
fakeGithubUsersTable: [
{
PutRequest: {
Item: {
PK: 'USER#162360409',
_et: 'githubUser',
_lastUpdated: '2024-02-02T19:00:00.000Z',
...testTestUser
}
}
}
]
}
})
expect(appState.dynamoDB.batchWrites[1]).toEqual({
RequestItems: {
fakeGithubAccountMemberships: [
{
PutRequest: {
Item: {
GSI1PK: 'USER#162360409',
GSI1SK: 'ACCOUNT#162360409',
PK: 'ACCOUNT#162360409',
SK: 'USER#162360409',
_et: 'githubAccountMembership',
_lastUpdated: '2024-02-02T19:00:00.000Z',
...testTestUserMembershipOfPersonalInstallation
}
}
}
]
}
})
expectBatchWritesLength(appState).toEqual(2)
expectBatchWrites(appState, 0).toEqual(expectedBatchWriteGithubUsers([testTestUser]))
expectBatchWrites(appState, 1).toEqual(
expectedBatchWriteGithubMemberships([testTestUserMembershipOfPersonalInstallation])
)
})

test('user-crawler-for-org-installation', async () => {
Expand All @@ -82,66 +60,18 @@ test('user-crawler-for-org-installation', async () => {
await crawlUsers(appState, testOrgInstallation)

// A
expect(appState.dynamoDB.batchWrites.length).toEqual(3)
expect(appState.dynamoDB.batchWrites[0]).toEqual({
RequestItems: {
fakeGithubUsersTable: [
{
PutRequest: {
Item: {
PK: 'USER#162360409',
_et: 'githubUser',
_lastUpdated: '2024-02-02T19:00:00.000Z',
...testTestUser
}
}
},
{
PutRequest: {
Item: {
PK: 'USER#49635',
_et: 'githubUser',
_lastUpdated: '2024-02-02T19:00:00.000Z',
...testMikeRobertsUser
}
}
}
]
}
})
// Previous membership for testTestUserMembershipOfOrg can remain unchanged
expect(appState.dynamoDB.batchWrites[1]).toEqual({
RequestItems: {
fakeGithubAccountMemberships: [
{
PutRequest: {
Item: {
GSI1PK: 'USER#49635',
GSI1SK: 'ACCOUNT#162483619',
PK: 'ACCOUNT#162483619',
SK: 'USER#49635',
_et: 'githubAccountMembership',
_lastUpdated: '2024-02-02T19:00:00.000Z',
...testMikeRobertsUserMembershipOfOrg
}
}
}
]
}
})
expectBatchWritesLength(appState).toEqual(3)
expectBatchWrites(appState, 0).toEqual(expectedBatchWriteGithubUsers([testTestUser, testMikeRobertsUser]))
expectBatchWrites(appState, 1).toEqual(
expectedBatchWriteGithubMemberships([testMikeRobertsUserMembershipOfOrg])
)
// No longer a member
expect(appState.dynamoDB.batchWrites[2]).toEqual({
RequestItems: {
fakeGithubAccountMemberships: [
{
DeleteRequest: {
Key: {
PK: 'ACCOUNT#162483619',
SK: 'USER#9786'
}
}
}
]
}
})
expectBatchWrites(appState, 2).toEqual(
expectedBatchDeleteGithubMemberships([
{
accountId: 162483619,
userId: 9786
}
])
)
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import { testTestUserPushSubscription } from '../../../../../examples/cicada/web
import { handleApiMessage } from '../../../../../../src/app/lambdaFunctions/authenticatedApi/lambda'
import { createAPIGatewayProxyWithLambdaAuthorizerEvent } from '../../../../../testSupport/fakes/awsStubs'
import { HttpMethod } from 'aws-cdk-lib/aws-apigatewayv2'
import {
expectDelete,
expectDeletesLength,
expectPut,
expectPutsLength
} from '../../../../../testSupport/fakes/dynamoDB/fakeDynamoDBInterfaceExpectations'
import {
expectedDeleteWebPushSubscription,
expectedPutWebPushSubscription
} from '../../../../../testSupport/fakes/tableRecordExpectedWrites'

test('web push test', async () => {
const appState = new FakeAppState()
Expand Down Expand Up @@ -55,17 +65,8 @@ test('web push subscribe', async () => {
},
statusCode: 200
})
expect(appState.dynamoDB.puts.length).toEqual(1)
expect(appState.dynamoDB.puts[0]).toEqual({
Item: {
PK: 'USER#162360409',
SK: 'ENDPOINT#https://web.push.apple.com/TestOne',
_et: 'webPushSubscription',
_lastUpdated: '2024-02-02T19:00:00.000Z',
...testTestUserPushSubscription
},
TableName: 'fakeWebPushSubscriptions'
})
expectPutsLength(appState).toEqual(1)
expectPut(appState).toEqual(expectedPutWebPushSubscription(testTestUserPushSubscription))
})

test('web push unsubscribe', async () => {
Expand Down Expand Up @@ -93,12 +94,6 @@ test('web push unsubscribe', async () => {
},
statusCode: 200
})
expect(appState.dynamoDB.deletes.length).toEqual(1)
expect(appState.dynamoDB.deletes[0]).toEqual({
Key: {
PK: 'USER#162360409',
SK: 'ENDPOINT#https://web.push.apple.com/TestOne'
},
TableName: 'fakeWebPushSubscriptions'
})
expectDeletesLength(appState).toEqual(1)
expectDelete(appState).toEqual(expectedDeleteWebPushSubscription(testTestUserPushSubscription))
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { githubWebhookInstallationProcessor } from '../../../../../../src/app/do

import example_installation_created from '../../../../../examples/github/org/webhook/installationCreated.json'
import { testOrgInstallation } from '../../../../../examples/cicada/githubDomainObjects'
import {
expectPut,
expectPutsLength
} from '../../../../../testSupport/fakes/dynamoDB/fakeDynamoDBInterfaceExpectations'
import { expectedPutGithubInstallation } from '../../../../../testSupport/fakes/tableRecordExpectedWrites'

test('installation-webhook-for-org-account-installation', async () => {
// A
Expand All @@ -17,16 +22,8 @@ test('installation-webhook-for-org-account-installation', async () => {
await githubWebhookInstallationProcessor(appState, JSON.stringify(example_installation_created))

// A
expect(appState.dynamoDB.puts.length).toEqual(1)
expect(appState.dynamoDB.puts[0]).toEqual({
Item: {
PK: 'ACCOUNT#162483619',
_et: 'githubInstallation',
_lastUpdated: '2024-02-02T19:00:00.000Z',
...testOrgInstallation
},
TableName: 'fakeGithubInstallationsTable'
})
expectPutsLength(appState).toEqual(1)
expectPut(appState).toEqual(expectedPutGithubInstallation(testOrgInstallation))
expect(appState.eventBridgeBus.sentEvents.length).toEqual(1)
expect(appState.eventBridgeBus.sentEvents[0]).toEqual({
detailType: 'InstallationUpdated',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { expect, test } from 'vitest'
import { FakeAppState } from '../../../../../testSupport/fakes/fakeAppState'
import example_workflow_run from '../../../../../examples/github/org/webhook/workflowRunCompleted.json'
import { testOrgTestRepoOneWorkflowRunThree } from '../../../../../examples/cicada/githubDomainObjects'
import {
createSignatureHeader,
processWebhookFromS3Event
} from '../../../../../../src/app/domain/github/webhookProcessor/githubWebhookProcessor'
import {
expectPut,
expectPutsLength
} from '../../../../../testSupport/fakes/dynamoDB/fakeDynamoDBInterfaceExpectations'
import {
expectedPutGithubWorkflowRun,
expectedPutGithubWorkflowRunEvent,
expectedPutLatestGithubWorkflowRunEvent
} from '../../../../../testSupport/fakes/tableRecordExpectedWrites'

test('run-event', async () => {
// Arrange
const appState = new FakeAppState()
const rawBody = JSON.stringify(example_workflow_run)
appState.s3.getObjectsAsString.addResponse(
{ bucket: 'fake-bucket', key: 'fake-key' },
JSON.stringify({
'X-Hub-Signature-256': createSignatureHeader(rawBody, appState.config.fakeGithubConfig.webhookSecret),
'X-GitHub-Event': 'workflow_run',
body: rawBody
})
)

// Act
await processWebhookFromS3Event(appState, {
detail: {
bucket: {
name: 'fake-bucket'
},
object: {
key: 'fake-key'
}
},
'detail-type': '',
account: '',
id: '',
region: '',
resources: [],
source: '',
time: '',
version: ''
})

// Assert
expectPutsLength(appState).toEqual(3)
expectPut(appState, 0).toEqual(expectedPutGithubWorkflowRunEvent(testOrgTestRepoOneWorkflowRunThree))
expectPut(appState, 1).toEqual(expectedPutGithubWorkflowRun(testOrgTestRepoOneWorkflowRunThree))
expectPut(appState, 2).toEqual(expectedPutLatestGithubWorkflowRunEvent(testOrgTestRepoOneWorkflowRunThree))

expect(appState.eventBridgeBus.sentEvents.length).toEqual(1)
expect(appState.eventBridgeBus.sentEvents[0].detailType).toEqual('GithubNewWorkflowRunEvent')
expect(JSON.parse(appState.eventBridgeBus.sentEvents[0].detail)).toEqual({
data: testOrgTestRepoOneWorkflowRunThree
})
})

This file was deleted.

Loading

0 comments on commit badb704

Please sign in to comment.