Skip to content

Commit 2ba2dbf

Browse files
authored
chore(api): fix outdated integration tests (#1252)
* chore(api): fix outdated integration tests * chore(api): fix GraphQLWithUserPoolIntegrationTests * chore(api): fix GraphQLWithUserPoolIntegrationTests
1 parent 052ca58 commit 2ba2dbf

File tree

5 files changed

+122
-76
lines changed

5 files changed

+122
-76
lines changed

AmplifyPlugins/API/AWSAPICategoryPluginIntegrationTests/GraphQL/GraphQLWithUserPoolIntegrationTests/AuthDirective/GraphQLAuthDirectiveIntegrationTests.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,8 @@ class GraphQLAuthDirectiveIntegrationTests: XCTestCase {
188188
signIn(username: user1.username, password: user1.password)
189189
let connectedInvoked = expectation(description: "Connection established")
190190
let progressInvoked = expectation(description: "Progress invoked")
191-
let ownerId = getUserSub()
192191
let request = GraphQLRequest<MutationSyncResult>.subscription(to: SocialNote.self,
193-
subscriptionType: .onCreate,
194-
ownerId: ownerId)
192+
subscriptionType: .onCreate)
195193
let operation = Amplify.API.subscribe(
196194
request: request,
197195
valueListener: { graphQLResponse in

AmplifyPlugins/API/AWSAPICategoryPluginIntegrationTests/GraphQL/GraphQLWithUserPoolIntegrationTests/GraphQLWithUserPoolIntegrationTests.swift

Lines changed: 113 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -6,69 +6,60 @@
66
//
77

88
import XCTest
9-
@testable import Amplify
10-
import AWSMobileClient
9+
import Foundation
10+
import AmplifyPlugins
1111
import AWSAPICategoryPlugin
12+
13+
@testable import Amplify
1214
@testable import AWSAPICategoryPluginTestCommon
1315
@testable import AmplifyTestCommon
1416

1517
// swiftlint:disable type_body_length
1618
class GraphQLWithUserPoolIntegrationTests: XCTestCase {
19+
struct User {
20+
let username: String
21+
let password: String
22+
}
1723

18-
static let amplifyConfiguration = "GraphQLWithUserPoolIntegrationTests-amplifyconfiguration"
19-
static let awsconfiguration = "GraphQLWithUserPoolIntegrationTests-awsconfiguration"
20-
static let credentials = "GraphQLWithUserPoolIntegrationTests-credentials"
21-
static var user1: String!
22-
static var password: String!
24+
let amplifyConfigurationFile = "GraphQLWithUserPoolIntegrationTests-amplifyconfiguration"
25+
let credentialsFile = "GraphQLWithUserPoolIntegrationTests-credentials"
26+
var user: User!
2327

24-
static override func setUp() {
28+
override func setUp() {
2529
do {
30+
let credentials = try TestConfigHelper.retrieveCredentials(forResource: credentialsFile)
2631

27-
let credentials = try TestConfigHelper.retrieveCredentials(
28-
forResource: GraphQLWithUserPoolIntegrationTests.credentials)
29-
30-
guard let user1 = credentials["user1"], let password = credentials["password"] else {
32+
guard let username = credentials["user1"],
33+
let password = credentials["password"] else {
3134
XCTFail("Missing credentials.json data")
3235
return
3336
}
3437

35-
GraphQLWithUserPoolIntegrationTests.user1 = user1
36-
GraphQLWithUserPoolIntegrationTests.password = password
37-
38-
let awsConfiguration = try TestConfigHelper.retrieveAWSConfiguration(
39-
forResource: GraphQLWithUserPoolIntegrationTests.awsconfiguration)
40-
AWSInfo.configureDefaultAWSInfo(awsConfiguration)
41-
} catch {
42-
XCTFail("Error during setup: \(error)")
43-
}
44-
}
45-
46-
override func setUp() {
47-
do {
48-
AuthHelper.initializeMobileClient()
49-
50-
Amplify.reset()
38+
user = User(username: username, password: password)
5139

5240
try Amplify.add(plugin: AWSAPIPlugin())
41+
try Amplify.add(plugin: AWSCognitoAuthPlugin())
5342

5443
let amplifyConfig = try TestConfigHelper.retrieveAmplifyConfiguration(
55-
forResource: GraphQLWithUserPoolIntegrationTests.amplifyConfiguration)
44+
forResource: amplifyConfigurationFile)
5645
try Amplify.configure(amplifyConfig)
5746
} catch {
5847
XCTFail("Error during setup: \(error)")
5948
}
49+
signOut()
6050
}
6151

6252
override func tearDown() {
53+
signOut()
6354
Amplify.reset()
6455
}
6556

6657
/// Given: A CreateTodo mutation request, and user signed in, graphql has userpools as auth mode.
6758
/// When: Call mutate API
6859
/// Then: The operation completes successfully with no errors and todo in response
6960
func testCreateTodoMutationWithUserPoolWithSignedInUser() {
70-
AuthHelper.signIn(username: GraphQLWithUserPoolIntegrationTests.user1,
71-
password: GraphQLWithUserPoolIntegrationTests.password)
61+
signIn(username: user.username,
62+
password: user.password)
7263
let completeInvoked = expectation(description: "request completed")
7364
let expectedId = UUID().uuidString
7465
let expectedName = "testCreateTodoMutationName"
@@ -108,7 +99,7 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
10899
/// When: Call mutate API
109100
/// Then: The operation fails with error, user not signed in.
110101
func testCreateTodoMutationWithUserPoolWithoutSignedInUserFailsWithError() {
111-
AuthHelper.signOut()
102+
signOut()
112103
let failedInvoked = expectation(description: "request failed")
113104
let expectedId = UUID().uuidString
114105
let expectedName = "testCreateTodoMutationName"
@@ -136,8 +127,8 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
136127
/// When: Call mutate API
137128
/// Then: The operation creates a Todo successfully, Todo object is returned, and empty errors array
138129
func testCreateTodoMutation() {
139-
AuthHelper.signIn(username: GraphQLWithUserPoolIntegrationTests.user1,
140-
password: GraphQLWithUserPoolIntegrationTests.password)
130+
signIn(username: user.username,
131+
password: user.password)
141132
let completeInvoked = expectation(description: "request completed")
142133

143134
let expectedId = UUID().uuidString
@@ -181,32 +172,38 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
181172
/// When: Call mutate API
182173
/// Then: The mutation operation completes successfully with errors in graphQLResponse
183174
func testCreateTodoMutationWithMissingInputFromVariables() {
184-
AuthHelper.signIn(username: GraphQLWithUserPoolIntegrationTests.user1,
185-
password: GraphQLWithUserPoolIntegrationTests.password)
175+
signIn(username: user.username,
176+
password: user.password)
186177
let completeInvoked = expectation(description: "request completed")
187178
let uuid = UUID().uuidString
188-
let description = "testCreateTodoMutationWithMissingInputFromVariables"
179+
180+
// create a Todo mutation with a missing/invalid "description" variable value
189181
let request = GraphQLRequest(document: CreateTodoMutation.document,
190182
variables: CreateTodoMutation.variables(id: uuid,
191183
name: "",
192-
description: description),
184+
description: nil),
193185
responseType: AWSAPICategoryPluginTestCommon.Todo?.self,
194186
decodePath: CreateTodoMutation.decodePath)
195187
let operation = Amplify.API.mutate(request: request) { event in
196188
switch event {
197189
case .success(let graphQLResponse):
198190
guard case let .failure(graphQLResponseError) = graphQLResponse else {
199-
XCTFail("Missing failure")
191+
XCTFail("Unexpected response success \(graphQLResponse)")
200192
return
201193
}
202194

203-
guard case let .partial(todo, error) = graphQLResponseError else {
204-
XCTFail("Missing partial response")
195+
guard case let .transformationError(rawResponse, error) = graphQLResponseError else {
196+
XCTFail("Missing transformation error")
197+
return
198+
}
199+
guard case let .operationError(operationErrorDescription, _, operationError) = error else {
200+
XCTFail("Unexpected error type \(error)")
205201
return
206202
}
207-
print(graphQLResponseError.errorDescription)
208-
XCTAssertNil(todo)
209-
XCTAssertNotNil(error)
203+
XCTAssertNotNil(rawResponse)
204+
XCTAssertNotNil(operationError)
205+
XCTAssertEqual(operationErrorDescription, "valueNotFound")
206+
210207
completeInvoked.fulfill()
211208
case .failure(let error):
212209
XCTFail("Unexpected .failed event: \(error)")
@@ -220,8 +217,8 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
220217
/// When: Call mutate API
221218
/// Then: The mutation operation fails with APIError
222219
func testCreateTodoMutationWithInvalidResponseType() {
223-
AuthHelper.signIn(username: GraphQLWithUserPoolIntegrationTests.user1,
224-
password: GraphQLWithUserPoolIntegrationTests.password)
220+
signIn(username: user.username,
221+
password: user.password)
225222
let transformationErrorInvoked = expectation(description: "transform error invoked")
226223

227224
let expectedId = UUID().uuidString
@@ -257,8 +254,8 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
257254
/// When: Call query API for that Todo
258255
/// Then: The query operation returns successfully with the Todo object and empty errors
259256
func testGetTodoQuery() {
260-
AuthHelper.signIn(username: GraphQLWithUserPoolIntegrationTests.user1,
261-
password: GraphQLWithUserPoolIntegrationTests.password)
257+
signIn(username: user.username,
258+
password: user.password)
262259
let uuid = UUID().uuidString
263260
let testMethodName = String("\(#function)".dropLast(2))
264261
let name = testMethodName + "Name"
@@ -302,8 +299,8 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
302299
/// When: Call query API
303300
/// Then: The query operation successfully with no errors and empty Todo object
304301
func testGetTodoQueryForMissingTodo() {
305-
AuthHelper.signIn(username: GraphQLWithUserPoolIntegrationTests.user1,
306-
password: GraphQLWithUserPoolIntegrationTests.password)
302+
signIn(username: user.username,
303+
password: user.password)
307304
let uuid = UUID().uuidString
308305

309306
let completeInvoked = expectation(description: "request completed")
@@ -332,8 +329,8 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
332329
/// When: Call mutate API
333330
/// Then: The operation updates the Todo successfully and the Todo object is returned
334331
func testUpdateTodoMutation() {
335-
AuthHelper.signIn(username: GraphQLWithUserPoolIntegrationTests.user1,
336-
password: GraphQLWithUserPoolIntegrationTests.password)
332+
signIn(username: user.username,
333+
password: user.password)
337334
let uuid = UUID().uuidString
338335
let testMethodName = String("\(#function)".dropLast(2))
339336
let name = testMethodName + "Name"
@@ -379,8 +376,8 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
379376
/// When: Call mutatate API with DeleteTodo mutation
380377
/// Then: The operation deletes the Todo successfully, Todo object is returned, and an query returns empty
381378
func testDeleteTodoMutation() {
382-
AuthHelper.signIn(username: GraphQLWithUserPoolIntegrationTests.user1,
383-
password: GraphQLWithUserPoolIntegrationTests.password)
379+
signIn(username: user.username,
380+
password: user.password)
384381
let uuid = UUID().uuidString
385382
let testMethodName = String("\(#function)".dropLast(2))
386383
let name = testMethodName + "Name"
@@ -447,8 +444,8 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
447444
/// When: Call query API with ListTodo mutation for all Todos
448445
/// Then: The operation completes successfully with list of Todos returned
449446
func testListTodosQuery() {
450-
AuthHelper.signIn(username: GraphQLWithUserPoolIntegrationTests.user1,
451-
password: GraphQLWithUserPoolIntegrationTests.password)
447+
signIn(username: user.username,
448+
password: user.password)
452449
let uuid = UUID().uuidString
453450
let testMethodName = String("\(#function)".dropLast(2))
454451
let name = testMethodName + "Name"
@@ -487,8 +484,8 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
487484
/// When: Call query API with ListTodo mutation with filter on the random Id
488485
/// Then: The operation completes successfully with no errors and empty list
489486
func testListTodosQueryWithNoResults() {
490-
AuthHelper.signIn(username: GraphQLWithUserPoolIntegrationTests.user1,
491-
password: GraphQLWithUserPoolIntegrationTests.password)
487+
signIn(username: user.username,
488+
password: user.password)
492489
let uuid = UUID().uuidString
493490
let filter = ["id": ["eq": uuid]]
494491
let variables = ListTodosQuery.variables(filter: filter, limit: 10)
@@ -522,8 +519,8 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
522519
/// When: Call mutate API on CreateTodo
523520
/// Then: The subscription handler is called and Todo object is returned
524521
func testOnCreateTodoSubscription() {
525-
AuthHelper.signIn(username: GraphQLWithUserPoolIntegrationTests.user1,
526-
password: GraphQLWithUserPoolIntegrationTests.password)
522+
signIn(username: user.username,
523+
password: user.password)
527524
let connectedInvoked = expectation(description: "Connection established")
528525
let disconnectedInvoked = expectation(description: "Connection disconnected")
529526
let completedInvoked = expectation(description: "Completed invoked")
@@ -584,8 +581,8 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
584581
/// When: Call mutate API on UpdateTodo
585582
/// Then: The subscription handler is called and Todo object is returned
586583
func testOnUpdateTodoSubscription() {
587-
AuthHelper.signIn(username: GraphQLWithUserPoolIntegrationTests.user1,
588-
password: GraphQLWithUserPoolIntegrationTests.password)
584+
signIn(username: user.username,
585+
password: user.password)
589586
let connectedInvoked = expectation(description: "Connection established")
590587
let disconnectedInvoked = expectation(description: "Connection disconnected")
591588
let completedInvoked = expectation(description: "Completed invoked")
@@ -651,8 +648,8 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
651648
/// When: Call mutate API on DeleteTodo
652649
/// Then: The subscription handler is called and Todo object is returned
653650
func testOnDeleteTodoSubscription() {
654-
AuthHelper.signIn(username: GraphQLWithUserPoolIntegrationTests.user1,
655-
password: GraphQLWithUserPoolIntegrationTests.password)
651+
signIn(username: user.username,
652+
password: user.password)
656653
let connectedInvoked = expectation(description: "Connection established")
657654
let disconnectedInvoked = expectation(description: "Connection disconnected")
658655
let completedInvoked = expectation(description: "Completed invoked")
@@ -697,7 +694,7 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
697694
return
698695
}
699696

700-
guard deleteTodo(id: todo.id, name: name + "Updated", description: description) != nil else {
697+
guard deleteTodo(id: todo.id) != nil else {
701698
XCTFail("Failed to update todo")
702699
return
703700
}
@@ -710,8 +707,8 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
710707

711708
// Query with two query documents, return two different objects.
712709
func testCreateMultipleSubscriptions() {
713-
AuthHelper.signIn(username: GraphQLWithUserPoolIntegrationTests.user1,
714-
password: GraphQLWithUserPoolIntegrationTests.password)
710+
signIn(username: user.username,
711+
password: user.password)
715712
let operations = [createTodoSubscription(),
716713
createTodoSubscription(),
717714
createTodoSubscription(),
@@ -737,7 +734,54 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
737734
}
738735
}
739736

740-
// MARK: Common functionality
737+
// MARK: - Helpers
738+
739+
func signIn(username: String, password: String) {
740+
let signInInvoked = expectation(description: "sign in completed")
741+
_ = Amplify.Auth.signIn(username: username, password: password) { event in
742+
switch event {
743+
case .success:
744+
signInInvoked.fulfill()
745+
case .failure(let error):
746+
XCTFail("Failed to Sign in user \(error)")
747+
}
748+
}
749+
wait(for: [signInInvoked], timeout: TestCommonConstants.networkTimeout)
750+
}
751+
752+
func isSignedIn() -> Bool {
753+
let checkIsSignedInCompleted = expectation(description: "retrieve auth session completed")
754+
var resultOptional: Bool?
755+
_ = Amplify.Auth.fetchAuthSession { event in
756+
switch event {
757+
case .success(let authSession):
758+
resultOptional = authSession.isSignedIn
759+
checkIsSignedInCompleted.fulfill()
760+
case .failure(let error):
761+
fatalError("Failed to get auth session \(error)")
762+
}
763+
}
764+
wait(for: [checkIsSignedInCompleted], timeout: TestCommonConstants.networkTimeout)
765+
guard let result = resultOptional else {
766+
fatalError("Could not get isSignedIn for user")
767+
}
768+
769+
return result
770+
}
771+
772+
func signOut() {
773+
let signOutCompleted = expectation(description: "sign out completed")
774+
_ = Amplify.Auth.signOut { event in
775+
switch event {
776+
case .success:
777+
signOutCompleted.fulfill()
778+
case .failure(let error):
779+
print("Could not sign out user \(error)")
780+
signOutCompleted.fulfill()
781+
}
782+
}
783+
wait(for: [signOutCompleted], timeout: TestCommonConstants.networkTimeout)
784+
}
741785

742786
func createTodo(id: String, name: String, description: String) -> AWSAPICategoryPluginTestCommon.Todo? {
743787
let completeInvoked = expectation(description: "Completd is invoked")
@@ -801,7 +845,7 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
801845
return todo
802846
}
803847

804-
func deleteTodo(id: String, name: String, description: String) -> AWSAPICategoryPluginTestCommon.Todo? {
848+
func deleteTodo(id: String) -> AWSAPICategoryPluginTestCommon.Todo? {
805849
let completeInvoked = expectation(description: "Completd is invoked")
806850
var todo: AWSAPICategoryPluginTestCommon.Todo?
807851

AmplifyPlugins/API/AWSAPICategoryPluginIntegrationTests/REST/RESTWithIAMIntegrationTests/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ The following steps show how to set up an API endpoint with APIGateway and Lambd
1414
? Choose a Lambda source `Create a new Lambda function`
1515
? Provide a friendly name for your resource to be used as a label for this category in the project: `restwithiamintegratie962cff0`
1616
? Provide the AWS Lambda function name: `restwithiamintegratie962cff0`
17-
? Choose the function template that you want to use: `Serverless express function (Integration with Amazon API Gateway)`
17+
? Choose the runtime that you want to use: NodeJS
18+
? Choose the function template that you want to use: `Serverless ExpressJS function (Integration with API Gateway)`
19+
? Do you want to configure advanced settings? `Yes`
1820
? Do you want to access other resources created in this project from your Lambda function? `No`
21+
? Do you want to invoke this function on a recurring schedule? `No`
22+
? Do you want to configure Lambda layers for this function? `No`
1923
? Do you want to edit the local lambda function now? `No`
2024
Succesfully added the Lambda function locally
2125
? Restrict API access `Yes`

0 commit comments

Comments
 (0)