Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: integ test auth dynamic sign in #15

Merged
merged 2 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
218CFE182C5AD66D009D70B9 /* CreateTodo.graphql in Resources */ = {isa = PBXBuildFile; fileRef = 218CFE142C5AD66D009D70B9 /* CreateTodo.graphql */; };
218CFE1A2C5AD6B1009D70B9 /* Network.swift in Sources */ = {isa = PBXBuildFile; fileRef = 218CFE192C5AD6B1009D70B9 /* Network.swift */; };
218CFE242C5AD806009D70B9 /* AppSyncAPI in Frameworks */ = {isa = PBXBuildFile; productRef = 218CFE232C5AD806009D70B9 /* AppSyncAPI */; };
21A12D082C88EC160003B0C5 /* AuthSignInHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21A12D072C88EC160003B0C5 /* AuthSignInHelper.swift */; };
21B8F2D22C62978D0042981F /* AuthTokenTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21B8F2D12C62978D0042981F /* AuthTokenTests.swift */; };
21B8F2D42C6298580042981F /* IAMTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21B8F2D32C6298580042981F /* IAMTests.swift */; };
21CFD7C92C76641E0071C70F /* AWSAppSyncApolloExtensions in Frameworks */ = {isa = PBXBuildFile; productRef = 21CFD7C82C76641E0071C70F /* AWSAppSyncApolloExtensions */; };
Expand Down Expand Up @@ -55,6 +56,7 @@
218CFE132C5AD66D009D70B9 /* SubscribeTodo.graphql */ = {isa = PBXFileReference; lastKnownFileType = text; name = SubscribeTodo.graphql; path = IntegrationTestApp/graphql/SubscribeTodo.graphql; sourceTree = SOURCE_ROOT; };
218CFE142C5AD66D009D70B9 /* CreateTodo.graphql */ = {isa = PBXFileReference; lastKnownFileType = text; name = CreateTodo.graphql; path = IntegrationTestApp/graphql/CreateTodo.graphql; sourceTree = SOURCE_ROOT; };
218CFE192C5AD6B1009D70B9 /* Network.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Network.swift; sourceTree = "<group>"; };
21A12D072C88EC160003B0C5 /* AuthSignInHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthSignInHelper.swift; sourceTree = "<group>"; };
21B8F2D12C62978D0042981F /* AuthTokenTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthTokenTests.swift; sourceTree = "<group>"; };
21B8F2D32C6298580042981F /* IAMTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IAMTests.swift; sourceTree = "<group>"; };
21FDF39B2C62B20200481EA0 /* APIKeyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = APIKeyTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -136,6 +138,7 @@
21B8F2D32C6298580042981F /* IAMTests.swift */,
21FDF39B2C62B20200481EA0 /* APIKeyTests.swift */,
21FDF39D2C62B3E500481EA0 /* IntegrationTestBase.swift */,
21A12D072C88EC160003B0C5 /* AuthSignInHelper.swift */,
);
path = IntegrationTestAppTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -292,6 +295,7 @@
21FDF39C2C62B20200481EA0 /* APIKeyTests.swift in Sources */,
21B8F2D22C62978D0042981F /* AuthTokenTests.swift in Sources */,
21B8F2D42C6298580042981F /* IAMTests.swift in Sources */,
21A12D082C88EC160003B0C5 /* AuthSignInHelper.swift in Sources */,
21FDF39E2C62B3E500481EA0 /* IntegrationTestBase.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import Amplify
import XCTest

enum AuthSignInHelper {

static func signOut() async {
let session = try? await Amplify.Auth.fetchAuthSession()
if session?.isSignedIn ?? false {
lawmicha marked this conversation as resolved.
Show resolved Hide resolved
_ = await Amplify.Auth.signOut()
}
}

static func signUpUser(
username: String,
password: String,
email: String,
phoneNumber: String? = nil) async throws -> Bool {

var userAttributes = [
AuthUserAttribute(.email, value: email)
]

if let phoneNumber = phoneNumber {
userAttributes.append(AuthUserAttribute(.phoneNumber, value: phoneNumber))
}

let options = AuthSignUpRequest.Options(
userAttributes: userAttributes)
let result = try await Amplify.Auth.signUp(username: username, password: password, options: options)
return result.isSignUpComplete
}

static func signInUser(username: String, password: String) async throws -> AuthSignInResult {
return try await Amplify.Auth.signIn(username: username, password: password, options: nil)
}

static func registerAndSignInUser(
username: String,
password: String,
email: String,
phoneNumber: String? = nil) async throws -> Bool {
let signedUp = try await AuthSignInHelper.signUpUser(
username: username,
password: password,
email: email,
phoneNumber: phoneNumber)
guard signedUp else {
throw AuthError.invalidState("Auth sign up failed", "", nil)
}
let result = try await AuthSignInHelper.signInUser(username: username, password: password)
return result.isSignedIn
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import AWSAppSyncApolloExtensions
import XCTest

class IntegrationTestBase: XCTestCase {
let username = "integTest\(UUID().uuidString)"
let password = "P123@\(UUID().uuidString)"
var defaultTestEmail = "test-\(UUID().uuidString)@amazon.com"
override func setUp() async throws {
AppSyncApolloLogger.logLevel = .verbose
}
Expand All @@ -19,6 +22,6 @@ class IntegrationTestBase: XCTestCase {
return
}

// Sign in user either dynamically or pull from credentials file.
_ = try await AuthSignInHelper.registerAndSignInUser(username: username, password: password, email: defaultTestEmail)
}
}
61 changes: 59 additions & 2 deletions Tests/IntegrationTestApp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,76 @@ export const data = defineData({
});
```

`auth/resource.ts`
Update `auth/resource.ts`

```
import { defineAuth } from '@aws-amplify/backend';
import { defineAuth, defineFunction } from '@aws-amplify/backend';

export const auth = defineAuth({
loginWith: {
email: true,
},
triggers: {
// configure a trigger to point to a function definition
preSignUp: defineFunction({
entry: './pre-sign-up-handler.ts'
})
}
});

```

Add `auth/pre-sign-up-handler.ts`

```ts
import type { PreSignUpTriggerHandler } from 'aws-lambda';

export const handler: PreSignUpTriggerHandler = async (event) => {
// your code here
event.response.autoConfirmUser = true
return event;
};
```


Update `backend.ts`

```ts
import { defineBackend } from '@aws-amplify/backend';
import { auth } from './auth/resource';
import { data } from './data/resource';

/**
* @see https://docs.amplify.aws/react/build-a-backend/ to add storage, functions, and more
*/
const backend = defineBackend({
auth,
data,
});

// Override sign in with username as the username

const { cfnUserPool } = backend.auth.resources.cfnResources
cfnUserPool.usernameAttributes = []

cfnUserPool.addPropertyOverride(
"Policies",
{
PasswordPolicy: {
MinimumLength: 10,
RequireLowercase: false,
RequireNumbers: true,
RequireSymbols: true,
RequireUppercase: true,
TemporaryPasswordValidityDays: 20,
},
}
);

```

Run `npx ampx sandbox` to deploy your backend.

Once deployed, copy the `amplify_outputs.json` over to IntegrationTestApp folder (Tests/IntegrationTestApp/IntegrationTestApp).

## Generating AppSyncAPI
Expand Down
Loading