Skip to content

Commit 8dd00b4

Browse files
authored
fix(auth): Update the Auth plugin to enforce ordering of api calls (#1549)
1 parent 9e82f73 commit 8dd00b4

File tree

5 files changed

+69
-11
lines changed

5 files changed

+69
-11
lines changed

AmplifyPlugins/Auth/AWSCognitoAuthPlugin/AWSCognitoAuthPlugin+Configure.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@ extension AWSCognitoAuthPlugin {
4242
let userService = AuthUserServiceAdapter(awsMobileClient: awsMobileClient)
4343
let deviceService = AuthDeviceServiceAdapter(awsMobileClient: awsMobileClient)
4444
let hubEventHandler = AuthHubEventHandler()
45+
let operationQueue = OperationQueue()
46+
operationQueue.maxConcurrentOperationCount = 1
4547
configure(authenticationProvider: authenticationProvider,
4648
authorizationProvider: authorizationProvider,
4749
userService: userService,
4850
deviceService: deviceService,
49-
hubEventHandler: hubEventHandler)
51+
hubEventHandler: hubEventHandler,
52+
queue: operationQueue)
5053
} catch let authError as AuthError {
5154
throw authError
5255

AmplifyPlugins/Auth/AWSCognitoAuthPluginIntegrationTests/AWSAuthBaseTest.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import AWSCognitoAuthPlugin
1111
@testable import AmplifyTestCommon
1212

1313
class AWSAuthBaseTest: XCTestCase {
14-
14+
1515
let networkTimeout = TimeInterval(10)
1616
var email = UUID().uuidString + "@" + UUID().uuidString + ".com"
1717
var email2 = UUID().uuidString + "@" + UUID().uuidString + ".com"
18-
18+
1919
let amplifyConfigurationFile = "testconfiguration/AWSCognitoAuthPluginIntegrationTests-amplifyconfiguration"
2020
let credentialsFile = "testconfiguration/AWSCognitoAuthPluginIntegrationTests-credentials"
21-
21+
2222
func initializeAmplify() {
2323
do {
2424
let credentialsConfiguration = try TestConfigHelper.retrieveCredentials(forResource: credentialsFile)

AmplifyPlugins/Auth/AWSCognitoAuthPluginTests/ConfigurationTests/AWSCognitoAuthPluginConfigTests.swift

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,11 @@ class AWSCognitoAuthPluginConfigTests: XCTestCase {
146146
"awsCognitoAuthPlugin": [
147147
"CredentialsProvider": ["CognitoIdentity": ["Default":
148148
["xx": "xx",
149-
"xx": "us-east-1"]
149+
"xx2": "us-east-1"]
150150
]],
151151
"CognitoUserPool": ["Default": [
152152
"xx": "xx",
153-
"xx": "us-east-1"
153+
"xx2": "us-east-1"
154154
]]
155155
]
156156
])
@@ -188,4 +188,61 @@ class AWSCognitoAuthPluginConfigTests: XCTestCase {
188188
}
189189
}
190190

191+
/// Test Auth configuration order of execution of apis
192+
///
193+
/// - Given: Given valid config for user pool and identity pool
194+
/// - When:
195+
/// - I call different auth apis after configuration
196+
/// - Then:
197+
/// - I should get the result back based on the order in which the calls where made.
198+
///
199+
func testAPIExecutionOrder() throws {
200+
let plugin = AWSCognitoAuthPlugin()
201+
try Amplify.add(plugin: plugin)
202+
203+
let categoryConfig = AuthCategoryConfiguration(plugins: [
204+
"awsCognitoAuthPlugin": [
205+
"CredentialsProvider": ["CognitoIdentity": ["Default":
206+
["PoolId": "xx",
207+
"Region": "us-east-1"]
208+
]],
209+
"CognitoUserPool": ["Default": [
210+
"PoolId": "xx",
211+
"Region": "us-east-1",
212+
"AppClientId": "xx",
213+
"AppClientSecret": "xx"]]
214+
]
215+
])
216+
let amplifyConfig = AmplifyConfiguration(auth: categoryConfig)
217+
do {
218+
try Amplify.configure(amplifyConfig)
219+
} catch {
220+
XCTFail("Should not throw error. \(error)")
221+
}
222+
223+
let signUpExpectation = expectation(description: "Should receive a result")
224+
_ = plugin.signUp(username: "mockUsername", password: "", options: nil) { _ in
225+
signUpExpectation.fulfill()
226+
}
227+
228+
var expectationList = [signUpExpectation]
229+
230+
for _ in 1 ... 50 {
231+
let fetchSessionExpectation = expectation(description: "Should receive a result")
232+
_ = plugin.fetchAuthSession(options: nil) { _ in
233+
fetchSessionExpectation.fulfill()
234+
}
235+
expectationList.append(fetchSessionExpectation)
236+
}
237+
238+
let signUpExpectation2 = expectation(description: "Should receive a result")
239+
DispatchQueue.global().async {
240+
241+
_ = plugin.signUp(username: "mockUsername", password: "", options: nil) { _ in
242+
signUpExpectation2.fulfill()
243+
}
244+
}
245+
expectationList.append(signUpExpectation2)
246+
wait(for: expectationList, timeout: 10, enforceOrder: true)
247+
}
191248
}

AmplifyPlugins/Auth/AWSCognitoAuthPluginTests/Mocks/MockAuthHubEventBehavior.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,4 @@ class MockAuthHubEventBehavior: AuthHubEventBehavior {
2020
func sendSessionExpiredEvent() {
2121
// Incomplete implementation
2222
}
23-
24-
2523
}

AmplifyPlugins/Auth/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ PODS:
2929
- CwlPreconditionTesting (1.1.1):
3030
- CwlCatchException
3131
- SwiftFormat/CLI (0.44.17)
32-
- SwiftLint (0.45.0)
32+
- SwiftLint (0.45.1)
3333

3434
DEPENDENCIES:
3535
- Amplify (from `../../`)
@@ -85,8 +85,8 @@ SPEC CHECKSUMS:
8585
CwlCatchException: 70a52ae44ea5d46db7bd385f801a94942420cd8c
8686
CwlPreconditionTesting: d33a4e4f285c0b885fddcae5dfedfbb34d4f3961
8787
SwiftFormat: 3b5caa6389b2b9adbc00e133b3ccc8c6e687a6a4
88-
SwiftLint: e5c7f1fba68eccfc51509d5b2ce1699f5502e0c7
88+
SwiftLint: 06ac37e4d38c7068e0935bb30cda95f093bec761
8989

9090
PODFILE CHECKSUM: 371cf67fe35ebb5167d0880bad12b01618a0fb0e
9191

92-
COCOAPODS: 1.10.1
92+
COCOAPODS: 1.11.2

0 commit comments

Comments
 (0)