Skip to content

Commit 01cc218

Browse files
feat(data) AppSync Events WebSocket Unit Tests + Integration Tests (#3048)
Co-authored-by: Matt Creaser <[email protected]>
1 parent cd7c149 commit 01cc218

File tree

29 files changed

+36644
-163
lines changed

29 files changed

+36644
-163
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ __pycache__/
9898
#amplify
9999
.amplify/
100100
amplify/
101+
#keep amplify gen2 backend projects
102+
!**/androidTest/backend/amplify/
101103
build/
102104
dist/
103105
node_modules/

appsync/aws-sdk-appsync-amplify/build.gradle.kts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@ group = properties["POM_GROUP"].toString()
3232

3333
android {
3434
namespace = "com.amazonaws.sdk.appsync.amplify"
35-
defaultConfig {
36-
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
37-
}
38-
39-
testOptions {
40-
execution = "ANDROIDX_TEST_ORCHESTRATOR"
41-
}
4235
}
4336

4437
dependencies {

appsync/aws-sdk-appsync-events/build.gradle.kts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,17 @@ dependencies {
5555
testImplementation(libs.test.kotest.assertions)
5656
testImplementation(libs.test.kotest.assertions.json)
5757
testImplementation(libs.test.mockwebserver)
58+
testImplementation(libs.test.turbine)
59+
60+
androidTestApi(project(":aws-sdk-appsync-amplify"))
61+
androidTestImplementation(project(":aws-auth-cognito"))
62+
androidTestImplementation(project(":core-kotlin"))
63+
androidTestImplementation(project(":testutils"))
64+
androidTestImplementation(libs.test.androidx.runner)
65+
androidTestImplementation(libs.test.androidx.junit)
66+
androidTestImplementation(libs.test.kotlin.coroutines)
67+
androidTestImplementation(libs.test.kotest.assertions)
68+
androidTestImplementation(libs.test.turbine)
69+
70+
androidTestUtil(libs.test.androidx.orchestrator)
5871
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.amazonaws.sdk.appsync.events.test">
4+
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
</manifest>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## AWS Amplify + AWS AppSync Events Template
2+
3+
## Getting Started
4+
5+
1. Setup AWS Account with Amplify
6+
7+
https://docs.amplify.aws/android/start/account-setup/
8+
9+
2. Install dependencies:
10+
11+
```bash
12+
npm install
13+
```
14+
15+
3. Deploy Sandbox
16+
17+
```bash
18+
npx ampx sandbox
19+
```
20+
21+
4. Make note of the amplify_outputs.json file that was generated. You will need to copy this file to the test project.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
version: 1
2+
backend:
3+
phases:
4+
build:
5+
commands:
6+
- npm ci
7+
- npx ampx pipeline-deploy --branch $AWS_BRANCH --app-id $AWS_APP_ID
8+
frontend:
9+
phases:
10+
build:
11+
commands:
12+
- mkdir ./dist && touch ./dist/index.html
13+
artifacts:
14+
baseDirectory: dist
15+
files:
16+
- '**/*'
17+
cache:
18+
paths:
19+
- node_modules/**/*
20+
- .npm/**/*
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineAuth } from '@aws-amplify/backend';
2+
3+
/**
4+
* Define and configure your auth resource
5+
* @see https://docs.amplify.aws/gen2/build-a-backend/auth
6+
*/
7+
export const auth = defineAuth({
8+
loginWith: {
9+
email: true,
10+
},
11+
});
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { defineBackend } from '@aws-amplify/backend'
2+
import { auth } from './auth/resource'
3+
import {
4+
AuthorizationType,
5+
CfnApi,
6+
CfnChannelNamespace,
7+
CfnApiKey,
8+
} from 'aws-cdk-lib/aws-appsync'
9+
import { Policy, PolicyStatement } from 'aws-cdk-lib/aws-iam'
10+
11+
const backend = defineBackend({ auth })
12+
13+
const customResources = backend.createStack('custom-resources-appsync-events')
14+
15+
const cfnEventAPI = new CfnApi(customResources, 'cfnEventAPI', {
16+
name: 'appsync-events-integration-tests',
17+
eventConfig: {
18+
authProviders: [
19+
{ authType: AuthorizationType.API_KEY },
20+
{ authType: AuthorizationType.IAM },
21+
{
22+
authType: AuthorizationType.USER_POOL,
23+
cognitoConfig: {
24+
awsRegion: customResources.region,
25+
userPoolId: backend.auth.resources.userPool.userPoolId,
26+
},
27+
}
28+
],
29+
connectionAuthModes: [
30+
{ authType: AuthorizationType.API_KEY },
31+
{ authType: AuthorizationType.IAM },
32+
{ authType: AuthorizationType.USER_POOL }
33+
],
34+
defaultPublishAuthModes: [
35+
{ authType: AuthorizationType.API_KEY },
36+
{ authType: AuthorizationType.IAM },
37+
{ authType: AuthorizationType.USER_POOL }
38+
],
39+
defaultSubscribeAuthModes: [
40+
{ authType: AuthorizationType.API_KEY },
41+
{ authType: AuthorizationType.IAM },
42+
{ authType: AuthorizationType.USER_POOL }
43+
],
44+
},
45+
})
46+
47+
new CfnChannelNamespace(customResources, 'CfnEventsIntegrationTestsNamespace', {
48+
apiId: cfnEventAPI.attrApiId,
49+
name: 'default',
50+
})
51+
52+
new CfnChannelNamespace(customResources, 'CfnEventsIntegrationTestsCustomNamespace', {
53+
apiId: cfnEventAPI.attrApiId,
54+
name: 'custom',
55+
})
56+
57+
// attach a policy to the authenticated user role in our User Pool to grant access to the Event API:
58+
backend.auth.resources.authenticatedUserIamRole.attachInlinePolicy(
59+
new Policy(customResources, 'AuthAppSyncEventPolicy', {
60+
statements: [
61+
new PolicyStatement({
62+
actions: [
63+
'appsync:EventConnect',
64+
'appsync:EventSubscribe',
65+
'appsync:EventPublish',
66+
],
67+
resources: [`${cfnEventAPI.attrApiArn}/*`, `${cfnEventAPI.attrApiArn}`],
68+
}),
69+
],
70+
})
71+
);
72+
73+
// Add the policy as an inline policy (not `addToPrincialPolicy`) to avoid circular deps
74+
backend.auth.resources.unauthenticatedUserIamRole.attachInlinePolicy(
75+
new Policy(customResources, 'UnauthAppSyncEventPolicy', {
76+
statements: [
77+
new PolicyStatement({
78+
actions: [
79+
'appsync:EventConnect',
80+
'appsync:EventPublish',
81+
'appsync:EventSubscribe',
82+
],
83+
resources: [`${cfnEventAPI.attrApiArn}/*`, `${cfnEventAPI.attrApiArn}`],
84+
}),
85+
],
86+
})
87+
)
88+
89+
// Create an API key
90+
const apiKey = new CfnApiKey(customResources, 'EventApiKey', {
91+
apiId: cfnEventAPI.attrApiId,
92+
description: 'API Key for Event API',
93+
expires: Math.floor(Date.now() / 1000) + (37 * 24 * 60 * 60) // Set for 37 days
94+
})
95+
96+
backend.addOutput({
97+
custom: {
98+
events: {
99+
url: `https://${cfnEventAPI.getAtt('Dns.Http').toString()}/event`,
100+
aws_region: customResources.region,
101+
default_authorization_type: AuthorizationType.API_KEY,
102+
api_key: apiKey.attrApiKey
103+
},
104+
},
105+
})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "module"
3+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2022",
4+
"module": "es2022",
5+
"moduleResolution": "bundler",
6+
"resolveJsonModule": true,
7+
"esModuleInterop": true,
8+
"forceConsistentCasingInFileNames": true,
9+
"strict": true,
10+
"skipLibCheck": true,
11+
"paths": {
12+
"$amplify/*": [
13+
"../.amplify/generated/*"
14+
]
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)