6
6
//
7
7
8
8
import XCTest
9
- @ testable import Amplify
10
- import AWSMobileClient
9
+ import Foundation
10
+ import AmplifyPlugins
11
11
import AWSAPICategoryPlugin
12
+
13
+ @testable import Amplify
12
14
@testable import AWSAPICategoryPluginTestCommon
13
15
@testable import AmplifyTestCommon
14
16
15
17
// swiftlint:disable type_body_length
16
18
class GraphQLWithUserPoolIntegrationTests : XCTestCase {
19
+ struct User {
20
+ let username : String
21
+ let password : String
22
+ }
17
23
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 !
23
27
24
- static override func setUp( ) {
28
+ override func setUp( ) {
25
29
do {
30
+ let credentials = try TestConfigHelper . retrieveCredentials ( forResource: credentialsFile)
26
31
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 {
31
34
XCTFail ( " Missing credentials.json data " )
32
35
return
33
36
}
34
37
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)
51
39
52
40
try Amplify . add ( plugin: AWSAPIPlugin ( ) )
41
+ try Amplify . add ( plugin: AWSCognitoAuthPlugin ( ) )
53
42
54
43
let amplifyConfig = try TestConfigHelper . retrieveAmplifyConfiguration (
55
- forResource: GraphQLWithUserPoolIntegrationTests . amplifyConfiguration )
44
+ forResource: amplifyConfigurationFile )
56
45
try Amplify . configure ( amplifyConfig)
57
46
} catch {
58
47
XCTFail ( " Error during setup: \( error) " )
59
48
}
49
+ signOut ( )
60
50
}
61
51
62
52
override func tearDown( ) {
53
+ signOut ( )
63
54
Amplify . reset ( )
64
55
}
65
56
66
57
/// Given: A CreateTodo mutation request, and user signed in, graphql has userpools as auth mode.
67
58
/// When: Call mutate API
68
59
/// Then: The operation completes successfully with no errors and todo in response
69
60
func testCreateTodoMutationWithUserPoolWithSignedInUser( ) {
70
- AuthHelper . signIn ( username: GraphQLWithUserPoolIntegrationTests . user1 ,
71
- password: GraphQLWithUserPoolIntegrationTests . password)
61
+ signIn ( username: user . username ,
62
+ password: user . password)
72
63
let completeInvoked = expectation ( description: " request completed " )
73
64
let expectedId = UUID ( ) . uuidString
74
65
let expectedName = " testCreateTodoMutationName "
@@ -108,7 +99,7 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
108
99
/// When: Call mutate API
109
100
/// Then: The operation fails with error, user not signed in.
110
101
func testCreateTodoMutationWithUserPoolWithoutSignedInUserFailsWithError( ) {
111
- AuthHelper . signOut ( )
102
+ signOut ( )
112
103
let failedInvoked = expectation ( description: " request failed " )
113
104
let expectedId = UUID ( ) . uuidString
114
105
let expectedName = " testCreateTodoMutationName "
@@ -136,8 +127,8 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
136
127
/// When: Call mutate API
137
128
/// Then: The operation creates a Todo successfully, Todo object is returned, and empty errors array
138
129
func testCreateTodoMutation( ) {
139
- AuthHelper . signIn ( username: GraphQLWithUserPoolIntegrationTests . user1 ,
140
- password: GraphQLWithUserPoolIntegrationTests . password)
130
+ signIn ( username: user . username ,
131
+ password: user . password)
141
132
let completeInvoked = expectation ( description: " request completed " )
142
133
143
134
let expectedId = UUID ( ) . uuidString
@@ -181,32 +172,38 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
181
172
/// When: Call mutate API
182
173
/// Then: The mutation operation completes successfully with errors in graphQLResponse
183
174
func testCreateTodoMutationWithMissingInputFromVariables( ) {
184
- AuthHelper . signIn ( username: GraphQLWithUserPoolIntegrationTests . user1 ,
185
- password: GraphQLWithUserPoolIntegrationTests . password)
175
+ signIn ( username: user . username ,
176
+ password: user . password)
186
177
let completeInvoked = expectation ( description: " request completed " )
187
178
let uuid = UUID ( ) . uuidString
188
- let description = " testCreateTodoMutationWithMissingInputFromVariables "
179
+
180
+ // create a Todo mutation with a missing/invalid "description" variable value
189
181
let request = GraphQLRequest ( document: CreateTodoMutation . document,
190
182
variables: CreateTodoMutation . variables ( id: uuid,
191
183
name: " " ,
192
- description: description ) ,
184
+ description: nil ) ,
193
185
responseType: AWSAPICategoryPluginTestCommon . Todo? . self,
194
186
decodePath: CreateTodoMutation . decodePath)
195
187
let operation = Amplify . API. mutate ( request: request) { event in
196
188
switch event {
197
189
case . success( let graphQLResponse) :
198
190
guard case let . failure( graphQLResponseError) = graphQLResponse else {
199
- XCTFail ( " Missing failure " )
191
+ XCTFail ( " Unexpected response success \( graphQLResponse ) " )
200
192
return
201
193
}
202
194
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) " )
205
201
return
206
202
}
207
- print ( graphQLResponseError. errorDescription)
208
- XCTAssertNil ( todo)
209
- XCTAssertNotNil ( error)
203
+ XCTAssertNotNil ( rawResponse)
204
+ XCTAssertNotNil ( operationError)
205
+ XCTAssertEqual ( operationErrorDescription, " valueNotFound " )
206
+
210
207
completeInvoked. fulfill ( )
211
208
case . failure( let error) :
212
209
XCTFail ( " Unexpected .failed event: \( error) " )
@@ -220,8 +217,8 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
220
217
/// When: Call mutate API
221
218
/// Then: The mutation operation fails with APIError
222
219
func testCreateTodoMutationWithInvalidResponseType( ) {
223
- AuthHelper . signIn ( username: GraphQLWithUserPoolIntegrationTests . user1 ,
224
- password: GraphQLWithUserPoolIntegrationTests . password)
220
+ signIn ( username: user . username ,
221
+ password: user . password)
225
222
let transformationErrorInvoked = expectation ( description: " transform error invoked " )
226
223
227
224
let expectedId = UUID ( ) . uuidString
@@ -257,8 +254,8 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
257
254
/// When: Call query API for that Todo
258
255
/// Then: The query operation returns successfully with the Todo object and empty errors
259
256
func testGetTodoQuery( ) {
260
- AuthHelper . signIn ( username: GraphQLWithUserPoolIntegrationTests . user1 ,
261
- password: GraphQLWithUserPoolIntegrationTests . password)
257
+ signIn ( username: user . username ,
258
+ password: user . password)
262
259
let uuid = UUID ( ) . uuidString
263
260
let testMethodName = String ( " \( #function) " . dropLast ( 2 ) )
264
261
let name = testMethodName + " Name "
@@ -302,8 +299,8 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
302
299
/// When: Call query API
303
300
/// Then: The query operation successfully with no errors and empty Todo object
304
301
func testGetTodoQueryForMissingTodo( ) {
305
- AuthHelper . signIn ( username: GraphQLWithUserPoolIntegrationTests . user1 ,
306
- password: GraphQLWithUserPoolIntegrationTests . password)
302
+ signIn ( username: user . username ,
303
+ password: user . password)
307
304
let uuid = UUID ( ) . uuidString
308
305
309
306
let completeInvoked = expectation ( description: " request completed " )
@@ -332,8 +329,8 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
332
329
/// When: Call mutate API
333
330
/// Then: The operation updates the Todo successfully and the Todo object is returned
334
331
func testUpdateTodoMutation( ) {
335
- AuthHelper . signIn ( username: GraphQLWithUserPoolIntegrationTests . user1 ,
336
- password: GraphQLWithUserPoolIntegrationTests . password)
332
+ signIn ( username: user . username ,
333
+ password: user . password)
337
334
let uuid = UUID ( ) . uuidString
338
335
let testMethodName = String ( " \( #function) " . dropLast ( 2 ) )
339
336
let name = testMethodName + " Name "
@@ -379,8 +376,8 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
379
376
/// When: Call mutatate API with DeleteTodo mutation
380
377
/// Then: The operation deletes the Todo successfully, Todo object is returned, and an query returns empty
381
378
func testDeleteTodoMutation( ) {
382
- AuthHelper . signIn ( username: GraphQLWithUserPoolIntegrationTests . user1 ,
383
- password: GraphQLWithUserPoolIntegrationTests . password)
379
+ signIn ( username: user . username ,
380
+ password: user . password)
384
381
let uuid = UUID ( ) . uuidString
385
382
let testMethodName = String ( " \( #function) " . dropLast ( 2 ) )
386
383
let name = testMethodName + " Name "
@@ -447,8 +444,8 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
447
444
/// When: Call query API with ListTodo mutation for all Todos
448
445
/// Then: The operation completes successfully with list of Todos returned
449
446
func testListTodosQuery( ) {
450
- AuthHelper . signIn ( username: GraphQLWithUserPoolIntegrationTests . user1 ,
451
- password: GraphQLWithUserPoolIntegrationTests . password)
447
+ signIn ( username: user . username ,
448
+ password: user . password)
452
449
let uuid = UUID ( ) . uuidString
453
450
let testMethodName = String ( " \( #function) " . dropLast ( 2 ) )
454
451
let name = testMethodName + " Name "
@@ -487,8 +484,8 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
487
484
/// When: Call query API with ListTodo mutation with filter on the random Id
488
485
/// Then: The operation completes successfully with no errors and empty list
489
486
func testListTodosQueryWithNoResults( ) {
490
- AuthHelper . signIn ( username: GraphQLWithUserPoolIntegrationTests . user1 ,
491
- password: GraphQLWithUserPoolIntegrationTests . password)
487
+ signIn ( username: user . username ,
488
+ password: user . password)
492
489
let uuid = UUID ( ) . uuidString
493
490
let filter = [ " id " : [ " eq " : uuid] ]
494
491
let variables = ListTodosQuery . variables ( filter: filter, limit: 10 )
@@ -522,8 +519,8 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
522
519
/// When: Call mutate API on CreateTodo
523
520
/// Then: The subscription handler is called and Todo object is returned
524
521
func testOnCreateTodoSubscription( ) {
525
- AuthHelper . signIn ( username: GraphQLWithUserPoolIntegrationTests . user1 ,
526
- password: GraphQLWithUserPoolIntegrationTests . password)
522
+ signIn ( username: user . username ,
523
+ password: user . password)
527
524
let connectedInvoked = expectation ( description: " Connection established " )
528
525
let disconnectedInvoked = expectation ( description: " Connection disconnected " )
529
526
let completedInvoked = expectation ( description: " Completed invoked " )
@@ -584,8 +581,8 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
584
581
/// When: Call mutate API on UpdateTodo
585
582
/// Then: The subscription handler is called and Todo object is returned
586
583
func testOnUpdateTodoSubscription( ) {
587
- AuthHelper . signIn ( username: GraphQLWithUserPoolIntegrationTests . user1 ,
588
- password: GraphQLWithUserPoolIntegrationTests . password)
584
+ signIn ( username: user . username ,
585
+ password: user . password)
589
586
let connectedInvoked = expectation ( description: " Connection established " )
590
587
let disconnectedInvoked = expectation ( description: " Connection disconnected " )
591
588
let completedInvoked = expectation ( description: " Completed invoked " )
@@ -651,8 +648,8 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
651
648
/// When: Call mutate API on DeleteTodo
652
649
/// Then: The subscription handler is called and Todo object is returned
653
650
func testOnDeleteTodoSubscription( ) {
654
- AuthHelper . signIn ( username: GraphQLWithUserPoolIntegrationTests . user1 ,
655
- password: GraphQLWithUserPoolIntegrationTests . password)
651
+ signIn ( username: user . username ,
652
+ password: user . password)
656
653
let connectedInvoked = expectation ( description: " Connection established " )
657
654
let disconnectedInvoked = expectation ( description: " Connection disconnected " )
658
655
let completedInvoked = expectation ( description: " Completed invoked " )
@@ -697,7 +694,7 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
697
694
return
698
695
}
699
696
700
- guard deleteTodo ( id: todo. id, name : name + " Updated " , description : description ) != nil else {
697
+ guard deleteTodo ( id: todo. id) != nil else {
701
698
XCTFail ( " Failed to update todo " )
702
699
return
703
700
}
@@ -710,8 +707,8 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
710
707
711
708
// Query with two query documents, return two different objects.
712
709
func testCreateMultipleSubscriptions( ) {
713
- AuthHelper . signIn ( username: GraphQLWithUserPoolIntegrationTests . user1 ,
714
- password: GraphQLWithUserPoolIntegrationTests . password)
710
+ signIn ( username: user . username ,
711
+ password: user . password)
715
712
let operations = [ createTodoSubscription ( ) ,
716
713
createTodoSubscription ( ) ,
717
714
createTodoSubscription ( ) ,
@@ -737,7 +734,54 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
737
734
}
738
735
}
739
736
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
+ }
741
785
742
786
func createTodo( id: String , name: String , description: String ) -> AWSAPICategoryPluginTestCommon . Todo ? {
743
787
let completeInvoked = expectation ( description: " Completd is invoked " )
@@ -801,7 +845,7 @@ class GraphQLWithUserPoolIntegrationTests: XCTestCase {
801
845
return todo
802
846
}
803
847
804
- func deleteTodo( id: String , name : String , description : String ) -> AWSAPICategoryPluginTestCommon . Todo ? {
848
+ func deleteTodo( id: String ) -> AWSAPICategoryPluginTestCommon . Todo ? {
805
849
let completeInvoked = expectation ( description: " Completd is invoked " )
806
850
var todo : AWSAPICategoryPluginTestCommon . Todo ?
807
851
0 commit comments