-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(data): Gen2 data customize auth doc example testing (#3705)
* test(data): Gen2 data customize auth doc example testing * address PR comments
- Loading branch information
Showing
33 changed files
with
1,601 additions
and
19 deletions.
There are no files selected for viewing
166 changes: 165 additions & 1 deletion
166
AmplifyPlugins/API/Tests/APIHostApp/APIHostApp.xcodeproj/project.pbxproj
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginGen2GraphQLTests/AuthSignInHelper.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// | ||
// 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 { | ||
_ = 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 { | ||
await signOut() | ||
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 | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...lugins/API/Tests/APIHostApp/AWSAPIPluginGen2GraphQLTests/Gen2_11/GraphQLPost11Tests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
import XCTest | ||
@testable import Amplify | ||
|
||
final class GraphQLPost11Tests: AWSAPIPluginGen2GraphQLBaseTest { | ||
|
||
// Code Snippet for | ||
// https://docs.amplify.aws/swift/build-a-backend/data/customize-authz/#configure-multiple-authorization-rules | ||
func testCodeSnippet() async throws { | ||
await setup(withModels: Post11Models(), withAuthPlugin: true) | ||
let username = "integTest\(UUID().uuidString)" | ||
let password = "P123@\(UUID().uuidString)" | ||
do { | ||
_ = try await AuthSignInHelper.registerAndSignInUser( | ||
username: username, | ||
password: password, | ||
email: defaultTestEmail) | ||
} catch { | ||
XCTFail("Could not sign up and sign in user \(error)") | ||
} | ||
|
||
// Code Snippet begins | ||
do { | ||
let post = Post(title: "Hello World") | ||
let createdTodo = try await Amplify.API.mutate(request: .create( | ||
post, | ||
authMode: .amazonCognitoUserPools)).get() | ||
} catch { | ||
print("Failed to create post", error) | ||
// Code Snippet Ends | ||
XCTFail("Failed to create post \(error)") | ||
// Code Snippet Begins | ||
} | ||
|
||
// Code Snippet ends | ||
await AuthSignInHelper.signOut() | ||
// Code Snippet begins | ||
|
||
do { | ||
let queriedPosts = try await Amplify.API.query(request: .list( | ||
Post.self, | ||
authMode: .awsIAM)).get() | ||
print("Number of posts:", queriedPosts.count) | ||
|
||
// Code Snippet Ends | ||
XCTAssertTrue(queriedPosts.count > 0 || queriedPosts.hasNextPage()) | ||
// Code Snippet Begins | ||
} catch { | ||
print("Failed to list posts", error) | ||
// Code Snippet Ends | ||
XCTFail("Failed to list posts \(error)") | ||
// Code Snippet Begins | ||
} | ||
} | ||
} | ||
|
||
extension GraphQLPost11Tests: DefaultLogger { } | ||
|
||
extension GraphQLPost11Tests { | ||
typealias Post = Post11 | ||
|
||
struct Post11Models: AmplifyModelRegistration { | ||
public let version: String = "version" | ||
func registerModels(registry: ModelRegistry.Type) { | ||
ModelRegistry.register(modelType: Post11.self) | ||
} | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginGen2GraphQLTests/Gen2_11/Post11+Schema.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// swiftlint:disable all | ||
import Amplify | ||
import Foundation | ||
|
||
extension Post11 { | ||
// MARK: - CodingKeys | ||
public enum CodingKeys: String, ModelKey { | ||
case id | ||
case title | ||
case content | ||
case createdAt | ||
case updatedAt | ||
} | ||
|
||
public static let keys = CodingKeys.self | ||
// MARK: - ModelSchema | ||
|
||
public static let schema = defineSchema { model in | ||
let post11 = Post11.keys | ||
|
||
model.authRules = [ | ||
rule(allow: .public, provider: .iam, operations: [.read]), | ||
rule(allow: .owner, ownerField: "owner", identityClaim: "cognito:username", provider: .userPools, operations: [.create, .update, .delete, .read]) | ||
] | ||
|
||
model.listPluralName = "Post11s" | ||
model.syncPluralName = "Post11s" | ||
|
||
model.attributes( | ||
.primaryKey(fields: [post11.id]) | ||
) | ||
|
||
model.fields( | ||
.field(post11.id, is: .required, ofType: .string), | ||
.field(post11.title, is: .optional, ofType: .string), | ||
.field(post11.content, is: .optional, ofType: .string), | ||
.field(post11.createdAt, is: .optional, isReadOnly: true, ofType: .dateTime), | ||
.field(post11.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) | ||
) | ||
} | ||
public class Path: ModelPath<Post11> { } | ||
|
||
public static var rootPath: PropertyContainerPath? { Path() } | ||
} | ||
|
||
extension Post11: ModelIdentifiable { | ||
public typealias IdentifierFormat = ModelIdentifierFormat.Default | ||
public typealias IdentifierProtocol = DefaultModelIdentifier<Self> | ||
} | ||
extension ModelPath where ModelType == Post11 { | ||
public var id: FieldPath<String> { | ||
string("id") | ||
} | ||
public var title: FieldPath<String> { | ||
string("title") | ||
} | ||
public var content: FieldPath<String> { | ||
string("content") | ||
} | ||
public var createdAt: FieldPath<Temporal.DateTime> { | ||
datetime("createdAt") | ||
} | ||
public var updatedAt: FieldPath<Temporal.DateTime> { | ||
datetime("updatedAt") | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginGen2GraphQLTests/Gen2_11/Post11.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// swiftlint:disable all | ||
import Amplify | ||
import Foundation | ||
|
||
public struct Post11: Model { | ||
public let id: String | ||
public var title: String? | ||
public var content: String? | ||
public var createdAt: Temporal.DateTime? | ||
public var updatedAt: Temporal.DateTime? | ||
|
||
public init(id: String = UUID().uuidString, | ||
title: String? = nil, | ||
content: String? = nil) { | ||
self.init(id: id, | ||
title: title, | ||
content: content, | ||
createdAt: nil, | ||
updatedAt: nil) | ||
} | ||
internal init(id: String = UUID().uuidString, | ||
title: String? = nil, | ||
content: String? = nil, | ||
createdAt: Temporal.DateTime? = nil, | ||
updatedAt: Temporal.DateTime? = nil) { | ||
self.id = id | ||
self.title = title | ||
self.content = content | ||
self.createdAt = createdAt | ||
self.updatedAt = updatedAt | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...lugins/API/Tests/APIHostApp/AWSAPIPluginGen2GraphQLTests/Gen2_12/GraphQLTodo12Tests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// Copyright Amazon.com Inc. or its affiliates. | ||
// All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
import XCTest | ||
@testable import Amplify | ||
|
||
final class GraphQLTodo12Tests: AWSAPIPluginGen2GraphQLBaseTest { | ||
|
||
// Code Snippet for | ||
// https://docs.amplify.aws/react/build-a-backend/data/customize-authz/public-data-access/#add-public-authorization-rule-using-api-key-based-authentication | ||
func testCodeSnippet() async throws { | ||
await setup(withModels: Todo12Models()) | ||
|
||
// Code Snippet begins | ||
do { | ||
let todo = Todo(content: "My new todo") | ||
let createdTodo = try await Amplify.API.mutate(request: .create( | ||
todo, | ||
authMode: .apiKey)).get() | ||
} catch { | ||
print("Failed to create todo", error) | ||
// Code Snippet Ends | ||
XCTFail("Failed to create todo \(error)") | ||
// Code Snippet Begins | ||
} | ||
} | ||
} | ||
|
||
extension GraphQLTodo12Tests: DefaultLogger { } | ||
|
||
extension GraphQLTodo12Tests { | ||
typealias Todo = Todo12 | ||
|
||
struct Todo12Models: AmplifyModelRegistration { | ||
public let version: String = "version" | ||
func registerModels(registry: ModelRegistry.Type) { | ||
ModelRegistry.register(modelType: Todo12.self) | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginGen2GraphQLTests/Gen2_12/Todo12+Schema.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// swiftlint:disable all | ||
import Amplify | ||
import Foundation | ||
|
||
extension Todo12 { | ||
// MARK: - CodingKeys | ||
public enum CodingKeys: String, ModelKey { | ||
case id | ||
case content | ||
case createdAt | ||
case updatedAt | ||
} | ||
|
||
public static let keys = CodingKeys.self | ||
// MARK: - ModelSchema | ||
|
||
public static let schema = defineSchema { model in | ||
let todo12 = Todo12.keys | ||
|
||
model.authRules = [ | ||
rule(allow: .public, provider: .apiKey, operations: [.create, .update, .delete, .read]) | ||
] | ||
|
||
model.listPluralName = "Todo12s" | ||
model.syncPluralName = "Todo12s" | ||
|
||
model.attributes( | ||
.primaryKey(fields: [todo12.id]) | ||
) | ||
|
||
model.fields( | ||
.field(todo12.id, is: .required, ofType: .string), | ||
.field(todo12.content, is: .optional, ofType: .string), | ||
.field(todo12.createdAt, is: .optional, isReadOnly: true, ofType: .dateTime), | ||
.field(todo12.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime) | ||
) | ||
} | ||
public class Path: ModelPath<Todo12> { } | ||
|
||
public static var rootPath: PropertyContainerPath? { Path() } | ||
} | ||
|
||
extension Todo12: ModelIdentifiable { | ||
public typealias IdentifierFormat = ModelIdentifierFormat.Default | ||
public typealias IdentifierProtocol = DefaultModelIdentifier<Self> | ||
} | ||
extension ModelPath where ModelType == Todo12 { | ||
public var id: FieldPath<String> { | ||
string("id") | ||
} | ||
public var content: FieldPath<String> { | ||
string("content") | ||
} | ||
public var createdAt: FieldPath<Temporal.DateTime> { | ||
datetime("createdAt") | ||
} | ||
public var updatedAt: FieldPath<Temporal.DateTime> { | ||
datetime("updatedAt") | ||
} | ||
} |
Oops, something went wrong.