Skip to content

Commit 08c32f7

Browse files
lawmicha5d
andauthored
chore: integ test auth dynamic sign in (#15)
* chore: integ test auth dynamic sign in * Update Tests/IntegrationTestApp/IntegrationTestAppTests/AuthSignInHelper.swift Co-authored-by: Di Wu <github@wudi.me> --------- Co-authored-by: Di Wu <github@wudi.me>
1 parent fbb869b commit 08c32f7

File tree

4 files changed

+127
-3
lines changed

4 files changed

+127
-3
lines changed

Tests/IntegrationTestApp/IntegrationTestApp.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
218CFE182C5AD66D009D70B9 /* CreateTodo.graphql in Resources */ = {isa = PBXBuildFile; fileRef = 218CFE142C5AD66D009D70B9 /* CreateTodo.graphql */; };
2525
218CFE1A2C5AD6B1009D70B9 /* Network.swift in Sources */ = {isa = PBXBuildFile; fileRef = 218CFE192C5AD6B1009D70B9 /* Network.swift */; };
2626
218CFE242C5AD806009D70B9 /* AppSyncAPI in Frameworks */ = {isa = PBXBuildFile; productRef = 218CFE232C5AD806009D70B9 /* AppSyncAPI */; };
27+
21A12D082C88EC160003B0C5 /* AuthSignInHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21A12D072C88EC160003B0C5 /* AuthSignInHelper.swift */; };
2728
21B8F2D22C62978D0042981F /* AuthTokenTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21B8F2D12C62978D0042981F /* AuthTokenTests.swift */; };
2829
21B8F2D42C6298580042981F /* IAMTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21B8F2D32C6298580042981F /* IAMTests.swift */; };
2930
21CFD7C92C76641E0071C70F /* AWSAppSyncApolloExtensions in Frameworks */ = {isa = PBXBuildFile; productRef = 21CFD7C82C76641E0071C70F /* AWSAppSyncApolloExtensions */; };
@@ -55,6 +56,7 @@
5556
218CFE132C5AD66D009D70B9 /* SubscribeTodo.graphql */ = {isa = PBXFileReference; lastKnownFileType = text; name = SubscribeTodo.graphql; path = IntegrationTestApp/graphql/SubscribeTodo.graphql; sourceTree = SOURCE_ROOT; };
5657
218CFE142C5AD66D009D70B9 /* CreateTodo.graphql */ = {isa = PBXFileReference; lastKnownFileType = text; name = CreateTodo.graphql; path = IntegrationTestApp/graphql/CreateTodo.graphql; sourceTree = SOURCE_ROOT; };
5758
218CFE192C5AD6B1009D70B9 /* Network.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Network.swift; sourceTree = "<group>"; };
59+
21A12D072C88EC160003B0C5 /* AuthSignInHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthSignInHelper.swift; sourceTree = "<group>"; };
5860
21B8F2D12C62978D0042981F /* AuthTokenTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthTokenTests.swift; sourceTree = "<group>"; };
5961
21B8F2D32C6298580042981F /* IAMTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IAMTests.swift; sourceTree = "<group>"; };
6062
21FDF39B2C62B20200481EA0 /* APIKeyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIKeyTests.swift; sourceTree = "<group>"; };
@@ -136,6 +138,7 @@
136138
21B8F2D32C6298580042981F /* IAMTests.swift */,
137139
21FDF39B2C62B20200481EA0 /* APIKeyTests.swift */,
138140
21FDF39D2C62B3E500481EA0 /* IntegrationTestBase.swift */,
141+
21A12D072C88EC160003B0C5 /* AuthSignInHelper.swift */,
139142
);
140143
path = IntegrationTestAppTests;
141144
sourceTree = "<group>";
@@ -292,6 +295,7 @@
292295
21FDF39C2C62B20200481EA0 /* APIKeyTests.swift in Sources */,
293296
21B8F2D22C62978D0042981F /* AuthTokenTests.swift in Sources */,
294297
21B8F2D42C6298580042981F /* IAMTests.swift in Sources */,
298+
21A12D082C88EC160003B0C5 /* AuthSignInHelper.swift in Sources */,
295299
21FDF39E2C62B3E500481EA0 /* IntegrationTestBase.swift in Sources */,
296300
);
297301
runOnlyForDeploymentPostprocessing = 0;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import Amplify
9+
import XCTest
10+
11+
enum AuthSignInHelper {
12+
13+
static func signOut() async {
14+
let session = try? await Amplify.Auth.fetchAuthSession()
15+
if session?.isSignedIn == true {
16+
_ = await Amplify.Auth.signOut()
17+
}
18+
}
19+
20+
static func signUpUser(
21+
username: String,
22+
password: String,
23+
email: String,
24+
phoneNumber: String? = nil) async throws -> Bool {
25+
26+
var userAttributes = [
27+
AuthUserAttribute(.email, value: email)
28+
]
29+
30+
if let phoneNumber = phoneNumber {
31+
userAttributes.append(AuthUserAttribute(.phoneNumber, value: phoneNumber))
32+
}
33+
34+
let options = AuthSignUpRequest.Options(
35+
userAttributes: userAttributes)
36+
let result = try await Amplify.Auth.signUp(username: username, password: password, options: options)
37+
return result.isSignUpComplete
38+
}
39+
40+
static func signInUser(username: String, password: String) async throws -> AuthSignInResult {
41+
return try await Amplify.Auth.signIn(username: username, password: password, options: nil)
42+
}
43+
44+
static func registerAndSignInUser(
45+
username: String,
46+
password: String,
47+
email: String,
48+
phoneNumber: String? = nil) async throws -> Bool {
49+
let signedUp = try await AuthSignInHelper.signUpUser(
50+
username: username,
51+
password: password,
52+
email: email,
53+
phoneNumber: phoneNumber)
54+
guard signedUp else {
55+
throw AuthError.invalidState("Auth sign up failed", "", nil)
56+
}
57+
let result = try await AuthSignInHelper.signInUser(username: username, password: password)
58+
return result.isSignedIn
59+
}
60+
}

Tests/IntegrationTestApp/IntegrationTestAppTests/IntegrationTestBase.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import AWSAppSyncApolloExtensions
1010
import XCTest
1111

1212
class IntegrationTestBase: XCTestCase {
13+
let username = "integTest\(UUID().uuidString)"
14+
let password = "P123@\(UUID().uuidString)"
15+
var defaultTestEmail = "test-\(UUID().uuidString)@amazon.com"
1316
override func setUp() async throws {
1417
AppSyncApolloLogger.logLevel = .verbose
1518
}
@@ -19,6 +22,6 @@ class IntegrationTestBase: XCTestCase {
1922
return
2023
}
2124

22-
// Sign in user either dynamically or pull from credentials file.
25+
_ = try await AuthSignInHelper.registerAndSignInUser(username: username, password: password, email: defaultTestEmail)
2326
}
2427
}

Tests/IntegrationTestApp/README.md

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,76 @@ export const data = defineData({
3535
});
3636
```
3737

38-
`auth/resource.ts`
38+
Update `auth/resource.ts`
3939

4040
```
41-
import { defineAuth } from '@aws-amplify/backend';
41+
import { defineAuth, defineFunction } from '@aws-amplify/backend';
4242
4343
export const auth = defineAuth({
4444
loginWith: {
4545
email: true,
4646
},
47+
triggers: {
48+
// configure a trigger to point to a function definition
49+
preSignUp: defineFunction({
50+
entry: './pre-sign-up-handler.ts'
51+
})
52+
}
53+
});
54+
55+
```
56+
57+
Add `auth/pre-sign-up-handler.ts`
58+
59+
```ts
60+
import type { PreSignUpTriggerHandler } from 'aws-lambda';
61+
62+
export const handler: PreSignUpTriggerHandler = async (event) => {
63+
// your code here
64+
event.response.autoConfirmUser = true
65+
return event;
66+
};
67+
```
68+
69+
70+
Update `backend.ts`
71+
72+
```ts
73+
import { defineBackend } from '@aws-amplify/backend';
74+
import { auth } from './auth/resource';
75+
import { data } from './data/resource';
76+
77+
/**
78+
* @see https://docs.amplify.aws/react/build-a-backend/ to add storage, functions, and more
79+
*/
80+
const backend = defineBackend({
81+
auth,
82+
data,
4783
});
4884

85+
// Override sign in with username as the username
86+
87+
const { cfnUserPool } = backend.auth.resources.cfnResources
88+
cfnUserPool.usernameAttributes = []
89+
90+
cfnUserPool.addPropertyOverride(
91+
"Policies",
92+
{
93+
PasswordPolicy: {
94+
MinimumLength: 10,
95+
RequireLowercase: false,
96+
RequireNumbers: true,
97+
RequireSymbols: true,
98+
RequireUppercase: true,
99+
TemporaryPasswordValidityDays: 20,
100+
},
101+
}
102+
);
103+
49104
```
50105

106+
Run `npx ampx sandbox` to deploy your backend.
107+
51108
Once deployed, copy the `amplify_outputs.json` over to IntegrationTestApp folder (Tests/IntegrationTestApp/IntegrationTestApp).
52109

53110
## Generating AppSyncAPI

0 commit comments

Comments
 (0)