Skip to content

Commit badb704

Browse files
committed
More functional test refactoring
1 parent fd93e17 commit badb704

9 files changed

+234
-359
lines changed
Lines changed: 28 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, test } from 'vitest'
1+
import { test } from 'vitest'
22
import { FakeAppState } from '../../../../../testSupport/fakes/fakeAppState'
33
import { FakeGithubInstallationClient } from '../../../../../testSupport/fakes/fakeGithubInstallationClient'
44
import {
@@ -15,6 +15,15 @@ import example_personal_account_user from '../../../../../examples/github/person
1515
import example_org_users from '../../../../../examples/github/org/api/users.json'
1616
import { crawlUsers } from '../../../../../../src/app/domain/github/crawler/crawlUsers'
1717
import { stubQueryAccountMembershipsByAccount } from '../../../../../testSupport/fakes/tableRecordReadStubs'
18+
import {
19+
expectBatchWrites,
20+
expectBatchWritesLength
21+
} from '../../../../../testSupport/fakes/dynamoDB/fakeDynamoDBInterfaceExpectations'
22+
import {
23+
expectedBatchDeleteGithubMemberships,
24+
expectedBatchWriteGithubMemberships,
25+
expectedBatchWriteGithubUsers
26+
} from '../../../../../testSupport/fakes/tableRecordExpectedWrites'
1827

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

2938
// A
30-
expect(appState.dynamoDB.batchWrites.length).toEqual(2)
31-
expect(appState.dynamoDB.batchWrites[0]).toEqual({
32-
RequestItems: {
33-
fakeGithubUsersTable: [
34-
{
35-
PutRequest: {
36-
Item: {
37-
PK: 'USER#162360409',
38-
_et: 'githubUser',
39-
_lastUpdated: '2024-02-02T19:00:00.000Z',
40-
...testTestUser
41-
}
42-
}
43-
}
44-
]
45-
}
46-
})
47-
expect(appState.dynamoDB.batchWrites[1]).toEqual({
48-
RequestItems: {
49-
fakeGithubAccountMemberships: [
50-
{
51-
PutRequest: {
52-
Item: {
53-
GSI1PK: 'USER#162360409',
54-
GSI1SK: 'ACCOUNT#162360409',
55-
PK: 'ACCOUNT#162360409',
56-
SK: 'USER#162360409',
57-
_et: 'githubAccountMembership',
58-
_lastUpdated: '2024-02-02T19:00:00.000Z',
59-
...testTestUserMembershipOfPersonalInstallation
60-
}
61-
}
62-
}
63-
]
64-
}
65-
})
39+
expectBatchWritesLength(appState).toEqual(2)
40+
expectBatchWrites(appState, 0).toEqual(expectedBatchWriteGithubUsers([testTestUser]))
41+
expectBatchWrites(appState, 1).toEqual(
42+
expectedBatchWriteGithubMemberships([testTestUserMembershipOfPersonalInstallation])
43+
)
6644
})
6745

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

8462
// A
85-
expect(appState.dynamoDB.batchWrites.length).toEqual(3)
86-
expect(appState.dynamoDB.batchWrites[0]).toEqual({
87-
RequestItems: {
88-
fakeGithubUsersTable: [
89-
{
90-
PutRequest: {
91-
Item: {
92-
PK: 'USER#162360409',
93-
_et: 'githubUser',
94-
_lastUpdated: '2024-02-02T19:00:00.000Z',
95-
...testTestUser
96-
}
97-
}
98-
},
99-
{
100-
PutRequest: {
101-
Item: {
102-
PK: 'USER#49635',
103-
_et: 'githubUser',
104-
_lastUpdated: '2024-02-02T19:00:00.000Z',
105-
...testMikeRobertsUser
106-
}
107-
}
108-
}
109-
]
110-
}
111-
})
112-
// Previous membership for testTestUserMembershipOfOrg can remain unchanged
113-
expect(appState.dynamoDB.batchWrites[1]).toEqual({
114-
RequestItems: {
115-
fakeGithubAccountMemberships: [
116-
{
117-
PutRequest: {
118-
Item: {
119-
GSI1PK: 'USER#49635',
120-
GSI1SK: 'ACCOUNT#162483619',
121-
PK: 'ACCOUNT#162483619',
122-
SK: 'USER#49635',
123-
_et: 'githubAccountMembership',
124-
_lastUpdated: '2024-02-02T19:00:00.000Z',
125-
...testMikeRobertsUserMembershipOfOrg
126-
}
127-
}
128-
}
129-
]
130-
}
131-
})
63+
expectBatchWritesLength(appState).toEqual(3)
64+
expectBatchWrites(appState, 0).toEqual(expectedBatchWriteGithubUsers([testTestUser, testMikeRobertsUser]))
65+
expectBatchWrites(appState, 1).toEqual(
66+
expectedBatchWriteGithubMemberships([testMikeRobertsUserMembershipOfOrg])
67+
)
13268
// No longer a member
133-
expect(appState.dynamoDB.batchWrites[2]).toEqual({
134-
RequestItems: {
135-
fakeGithubAccountMemberships: [
136-
{
137-
DeleteRequest: {
138-
Key: {
139-
PK: 'ACCOUNT#162483619',
140-
SK: 'USER#9786'
141-
}
142-
}
143-
}
144-
]
145-
}
146-
})
69+
expectBatchWrites(appState, 2).toEqual(
70+
expectedBatchDeleteGithubMemberships([
71+
{
72+
accountId: 162483619,
73+
userId: 9786
74+
}
75+
])
76+
)
14777
})

test/local/functional/domain/github/webPush/webPushSubscriptions.test.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ import { testTestUserPushSubscription } from '../../../../../examples/cicada/web
44
import { handleApiMessage } from '../../../../../../src/app/lambdaFunctions/authenticatedApi/lambda'
55
import { createAPIGatewayProxyWithLambdaAuthorizerEvent } from '../../../../../testSupport/fakes/awsStubs'
66
import { HttpMethod } from 'aws-cdk-lib/aws-apigatewayv2'
7+
import {
8+
expectDelete,
9+
expectDeletesLength,
10+
expectPut,
11+
expectPutsLength
12+
} from '../../../../../testSupport/fakes/dynamoDB/fakeDynamoDBInterfaceExpectations'
13+
import {
14+
expectedDeleteWebPushSubscription,
15+
expectedPutWebPushSubscription
16+
} from '../../../../../testSupport/fakes/tableRecordExpectedWrites'
717

818
test('web push test', async () => {
919
const appState = new FakeAppState()
@@ -55,17 +65,8 @@ test('web push subscribe', async () => {
5565
},
5666
statusCode: 200
5767
})
58-
expect(appState.dynamoDB.puts.length).toEqual(1)
59-
expect(appState.dynamoDB.puts[0]).toEqual({
60-
Item: {
61-
PK: 'USER#162360409',
62-
SK: 'ENDPOINT#https://web.push.apple.com/TestOne',
63-
_et: 'webPushSubscription',
64-
_lastUpdated: '2024-02-02T19:00:00.000Z',
65-
...testTestUserPushSubscription
66-
},
67-
TableName: 'fakeWebPushSubscriptions'
68-
})
68+
expectPutsLength(appState).toEqual(1)
69+
expectPut(appState).toEqual(expectedPutWebPushSubscription(testTestUserPushSubscription))
6970
})
7071

7172
test('web push unsubscribe', async () => {
@@ -93,12 +94,6 @@ test('web push unsubscribe', async () => {
9394
},
9495
statusCode: 200
9596
})
96-
expect(appState.dynamoDB.deletes.length).toEqual(1)
97-
expect(appState.dynamoDB.deletes[0]).toEqual({
98-
Key: {
99-
PK: 'USER#162360409',
100-
SK: 'ENDPOINT#https://web.push.apple.com/TestOne'
101-
},
102-
TableName: 'fakeWebPushSubscriptions'
103-
})
97+
expectDeletesLength(appState).toEqual(1)
98+
expectDelete(appState).toEqual(expectedDeleteWebPushSubscription(testTestUserPushSubscription))
10499
})

test/local/functional/domain/github/webhook/githubWebhookInstallationProcessor.test.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import { githubWebhookInstallationProcessor } from '../../../../../../src/app/do
44

55
import example_installation_created from '../../../../../examples/github/org/webhook/installationCreated.json'
66
import { testOrgInstallation } from '../../../../../examples/cicada/githubDomainObjects'
7+
import {
8+
expectPut,
9+
expectPutsLength
10+
} from '../../../../../testSupport/fakes/dynamoDB/fakeDynamoDBInterfaceExpectations'
11+
import { expectedPutGithubInstallation } from '../../../../../testSupport/fakes/tableRecordExpectedWrites'
712

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

1924
// A
20-
expect(appState.dynamoDB.puts.length).toEqual(1)
21-
expect(appState.dynamoDB.puts[0]).toEqual({
22-
Item: {
23-
PK: 'ACCOUNT#162483619',
24-
_et: 'githubInstallation',
25-
_lastUpdated: '2024-02-02T19:00:00.000Z',
26-
...testOrgInstallation
27-
},
28-
TableName: 'fakeGithubInstallationsTable'
29-
})
25+
expectPutsLength(appState).toEqual(1)
26+
expectPut(appState).toEqual(expectedPutGithubInstallation(testOrgInstallation))
3027
expect(appState.eventBridgeBus.sentEvents.length).toEqual(1)
3128
expect(appState.eventBridgeBus.sentEvents[0]).toEqual({
3229
detailType: 'InstallationUpdated',
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { expect, test } from 'vitest'
2+
import { FakeAppState } from '../../../../../testSupport/fakes/fakeAppState'
3+
import example_workflow_run from '../../../../../examples/github/org/webhook/workflowRunCompleted.json'
4+
import { testOrgTestRepoOneWorkflowRunThree } from '../../../../../examples/cicada/githubDomainObjects'
5+
import {
6+
createSignatureHeader,
7+
processWebhookFromS3Event
8+
} from '../../../../../../src/app/domain/github/webhookProcessor/githubWebhookProcessor'
9+
import {
10+
expectPut,
11+
expectPutsLength
12+
} from '../../../../../testSupport/fakes/dynamoDB/fakeDynamoDBInterfaceExpectations'
13+
import {
14+
expectedPutGithubWorkflowRun,
15+
expectedPutGithubWorkflowRunEvent,
16+
expectedPutLatestGithubWorkflowRunEvent
17+
} from '../../../../../testSupport/fakes/tableRecordExpectedWrites'
18+
19+
test('run-event', async () => {
20+
// Arrange
21+
const appState = new FakeAppState()
22+
const rawBody = JSON.stringify(example_workflow_run)
23+
appState.s3.getObjectsAsString.addResponse(
24+
{ bucket: 'fake-bucket', key: 'fake-key' },
25+
JSON.stringify({
26+
'X-Hub-Signature-256': createSignatureHeader(rawBody, appState.config.fakeGithubConfig.webhookSecret),
27+
'X-GitHub-Event': 'workflow_run',
28+
body: rawBody
29+
})
30+
)
31+
32+
// Act
33+
await processWebhookFromS3Event(appState, {
34+
detail: {
35+
bucket: {
36+
name: 'fake-bucket'
37+
},
38+
object: {
39+
key: 'fake-key'
40+
}
41+
},
42+
'detail-type': '',
43+
account: '',
44+
id: '',
45+
region: '',
46+
resources: [],
47+
source: '',
48+
time: '',
49+
version: ''
50+
})
51+
52+
// Assert
53+
expectPutsLength(appState).toEqual(3)
54+
expectPut(appState, 0).toEqual(expectedPutGithubWorkflowRunEvent(testOrgTestRepoOneWorkflowRunThree))
55+
expectPut(appState, 1).toEqual(expectedPutGithubWorkflowRun(testOrgTestRepoOneWorkflowRunThree))
56+
expectPut(appState, 2).toEqual(expectedPutLatestGithubWorkflowRunEvent(testOrgTestRepoOneWorkflowRunThree))
57+
58+
expect(appState.eventBridgeBus.sentEvents.length).toEqual(1)
59+
expect(appState.eventBridgeBus.sentEvents[0].detailType).toEqual('GithubNewWorkflowRunEvent')
60+
expect(JSON.parse(appState.eventBridgeBus.sentEvents[0].detail)).toEqual({
61+
data: testOrgTestRepoOneWorkflowRunThree
62+
})
63+
})

test/local/functional/domain/github/webhook/githubWebhookProcessor.ts

Lines changed: 0 additions & 83 deletions
This file was deleted.

0 commit comments

Comments
 (0)