Skip to content

Commit 10184c3

Browse files
authored
fix(Auth): Hub event for successful signIn for webUI signIn (#472)
- Fix for hub event not being fired for successful signIn in webUi and social provider signIn - Fixed typo for webUI event - Added Unit tests
1 parent 987f825 commit 10184c3

File tree

6 files changed

+165
-3
lines changed

6 files changed

+165
-3
lines changed

Amplify/Categories/Auth/Operation/AuthSocialWebUISignInOperation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ public protocol AuthSocialWebUISignInOperation: AmplifyOperation<
1616
public extension HubPayload.EventName.Auth {
1717

1818
/// eventName for HubPayloads emitted by this operation
19-
static let socialWebUISgnIn = "Auth.socialWebUISignIn"
19+
static let socialWebUISignIn = "Auth.socialWebUISignIn"
2020
}

AmplifyPlugins/Auth/AWSAuthPlugin.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
B41D0F2024746ACC0049D08D /* amplifyconfiguration.json in Resources */ = {isa = PBXBuildFile; fileRef = B41B89672468DB0E0092F96D /* amplifyconfiguration.json */; };
2626
B41D0F2324746B6B0049D08D /* AuthSignUpTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B41D0F2224746B6B0049D08D /* AuthSignUpTests.swift */; };
2727
B41D0F26247476580049D08D /* AuthDeviceOperationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B41D0F25247476580049D08D /* AuthDeviceOperationTests.swift */; };
28+
B41D0F2924758D4C0049D08D /* AuthHubEventHandlerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B41D0F2824758D4C0049D08D /* AuthHubEventHandlerTests.swift */; };
2829
B42B3D38246ADDAE007211E0 /* AWSAuthService.swift in Sources */ = {isa = PBXBuildFile; fileRef = B42B3D37246ADDAE007211E0 /* AWSAuthService.swift */; };
2930
B42B3D3D246AE2B6007211E0 /* AWSAuthPlugin+InvalidateCredentials.swift in Sources */ = {isa = PBXBuildFile; fileRef = B42B3D3C246AE2B6007211E0 /* AWSAuthPlugin+InvalidateCredentials.swift */; };
3031
B439F2AB2448836500C30CBB /* AuthSignInRequest+Validate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B439F2AA2448836500C30CBB /* AuthSignInRequest+Validate.swift */; };
@@ -156,6 +157,7 @@
156157
B41B896A2468DB700092F96D /* SignedOutAuthSessionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignedOutAuthSessionTests.swift; sourceTree = "<group>"; };
157158
B41D0F2224746B6B0049D08D /* AuthSignUpTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthSignUpTests.swift; sourceTree = "<group>"; };
158159
B41D0F25247476580049D08D /* AuthDeviceOperationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthDeviceOperationTests.swift; sourceTree = "<group>"; };
160+
B41D0F2824758D4C0049D08D /* AuthHubEventHandlerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthHubEventHandlerTests.swift; sourceTree = "<group>"; };
159161
B42B3D37246ADDAE007211E0 /* AWSAuthService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AWSAuthService.swift; sourceTree = "<group>"; };
160162
B42B3D3C246AE2B6007211E0 /* AWSAuthPlugin+InvalidateCredentials.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AWSAuthPlugin+InvalidateCredentials.swift"; sourceTree = "<group>"; };
161163
B439F2AA2448836500C30CBB /* AuthSignInRequest+Validate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AuthSignInRequest+Validate.swift"; sourceTree = "<group>"; };
@@ -344,6 +346,14 @@
344346
path = AuthDeviceTests;
345347
sourceTree = "<group>";
346348
};
349+
B41D0F2724758D330049D08D /* HubEventTests */ = {
350+
isa = PBXGroup;
351+
children = (
352+
B41D0F2824758D4C0049D08D /* AuthHubEventHandlerTests.swift */,
353+
);
354+
path = HubEventTests;
355+
sourceTree = "<group>";
356+
};
347357
B439F2A92448835700C30CBB /* Request */ = {
348358
isa = PBXGroup;
349359
children = (
@@ -420,6 +430,7 @@
420430
B43DC7522410572400D40275 /* AWSAuthPluginTests */ = {
421431
isa = PBXGroup;
422432
children = (
433+
B41D0F2724758D330049D08D /* HubEventTests */,
423434
B4C8EDB6246B5ED800ED7484 /* Utils */,
424435
B43DC7532410572400D40275 /* AWSAuthPluginTests.swift */,
425436
B43DC7552410572400D40275 /* Info.plist */,
@@ -1216,6 +1227,7 @@
12161227
buildActionMask = 2147483647;
12171228
files = (
12181229
B43DC7542410572400D40275 /* AWSAuthPluginTests.swift in Sources */,
1230+
B41D0F2924758D4C0049D08D /* AuthHubEventHandlerTests.swift in Sources */,
12191231
B4C8EDB8246B5EFD00ED7484 /* AuthUserAttributeKeyTests.swift in Sources */,
12201232
);
12211233
runOnlyForDeploymentPostprocessing = 0;

AmplifyPlugins/Auth/AWSAuthPlugin/Dependency/AuthenticationProviderAdapter+SignOut.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extension AuthenticationProviderAdapter {
1212

1313
func signOut(request: AuthSignOutRequest, completionHandler: @escaping (Result<Void, AuthError>) -> Void) {
1414

15-
if (request.options.globalSignOut) {
15+
if request.options.globalSignOut {
1616
// If user is signed in through HostedUI the signout require UI to complete. So calling this in main thread.
1717
DispatchQueue.main.async { [weak self] in
1818
guard let self = self else {

AmplifyPlugins/Auth/AWSAuthPlugin/HubEvents/AuthHubEventHandler.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class AuthHubEventHandler: AuthHubEventBehavior {
2828
dispatchAuthEvent(HubPayload.EventName.Auth.sessionExpired)
2929
}
3030

31+
// swiftlint:disable cyclomatic_complexity
3132
private func setupHubEvents() {
3233

3334
_ = Amplify.Hub.listen(to: .auth) {[weak self] payload in
@@ -47,6 +48,20 @@ class AuthHubEventHandler: AuthHubEventBehavior {
4748
}
4849
self?.handleSignInEvent(result)
4950

51+
case HubPayload.EventName.Auth.webUISignIn:
52+
guard let event = payload.data as? AWSAuthWebUISignInOperation.OperationResult,
53+
case let .success(result) = event else {
54+
return
55+
}
56+
self?.handleSignInEvent(result)
57+
58+
case HubPayload.EventName.Auth.socialWebUISignIn:
59+
guard let event = payload.data as? AWSAuthSocialWebUISignInOperation.OperationResult,
60+
case let .success(result) = event else {
61+
return
62+
}
63+
self?.handleSignInEvent(result)
64+
5065
case HubPayload.EventName.Auth.signOut:
5166
guard let event = payload.data as? AWSAuthSignOutOperation.OperationResult,
5267
case .success(_) = event else {

AmplifyPlugins/Auth/AWSAuthPlugin/Operations/AWSAuthSocialWebUISignInOperation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class AWSAuthSocialWebUISignInOperation: AmplifyOperation<
2222

2323
self.authenticationProvider = authenticationProvider
2424
super.init(categoryType: .auth,
25-
eventName: HubPayload.EventName.Auth.socialWebUISgnIn,
25+
eventName: HubPayload.EventName.Auth.socialWebUISignIn,
2626
request: request,
2727
resultListener: resultListener)
2828
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
//
2+
// Copyright 2018-2020 Amazon.com,
3+
// Inc. or its affiliates. All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import XCTest
9+
@testable import Amplify
10+
@testable import AWSAuthPlugin
11+
12+
class AuthHubEventHandlerTests: XCTestCase {
13+
14+
var authHandler: AuthHubEventHandler!
15+
override func setUp() {
16+
try? Amplify.configure()
17+
authHandler = AuthHubEventHandler()
18+
}
19+
20+
override func tearDown() {
21+
Amplify.reset()
22+
authHandler = nil
23+
}
24+
25+
/// Test whether HubEvent emits a signedIn event for normal signIn operation
26+
///
27+
/// - Given: A listener to hub events
28+
/// - When:
29+
/// - I mock a succesful sign in operation event
30+
/// - Then:
31+
/// - I should receive a signedIn hub event
32+
///
33+
func testSignedInHubEvent() {
34+
35+
let hubEventExpectation = expectation(description: "Should receive the hub event")
36+
_ = Amplify.Hub.listen(to: .auth) { payload in
37+
switch payload.eventName {
38+
case HubPayload.EventName.Auth.signedIn:
39+
hubEventExpectation.fulfill()
40+
default:
41+
break
42+
}
43+
}
44+
mockSuccessfulSignedInEvent()
45+
wait(for: [hubEventExpectation], timeout: 10)
46+
}
47+
48+
/// Test whether HubEvent emits a signedIn event for webUI signIn
49+
///
50+
/// - Given: A listener to hub events
51+
/// - When:
52+
/// - I mock a succesful webui signIn operation event
53+
/// - Then:
54+
/// - I should receive a signedIn hub event
55+
///
56+
func testWebUISignedInHubEvent() {
57+
_ = AuthHubEventHandler()
58+
let hubEventExpectation = expectation(description: "Should receive the hub event")
59+
_ = Amplify.Hub.listen(to: .auth) { payload in
60+
switch payload.eventName {
61+
case HubPayload.EventName.Auth.signedIn:
62+
hubEventExpectation.fulfill()
63+
default:
64+
break
65+
}
66+
}
67+
mockSuccessfulWebUISignedInEvent()
68+
wait(for: [hubEventExpectation], timeout: 10)
69+
}
70+
71+
/// Test whether HubEvent emits a signedIn event for social provider signIn
72+
///
73+
/// - Given: A listener to hub events
74+
/// - When:
75+
/// - I mock a succesful social provider webui signIn operation event
76+
/// - Then:
77+
/// - I should receive a signedIn hub event
78+
///
79+
func testSocialWebUISignedInHubEvent() {
80+
_ = AuthHubEventHandler()
81+
let hubEventExpectation = expectation(description: "Should receive the hub event")
82+
_ = Amplify.Hub.listen(to: .auth) { payload in
83+
switch payload.eventName {
84+
case HubPayload.EventName.Auth.signedIn:
85+
hubEventExpectation.fulfill()
86+
default:
87+
break
88+
}
89+
}
90+
mockSuccessfulSocialWebUISignedInEvent()
91+
wait(for: [hubEventExpectation], timeout: 10)
92+
}
93+
94+
private func mockSuccessfulSocialWebUISignedInEvent() {
95+
let mockResult = AuthSignInResult(nextStep: .done)
96+
let mockEvent = AWSAuthSocialWebUISignInOperation.OperationResult.success(mockResult)
97+
let mockRequest = AuthWebUISignInRequest(presentationAnchor: UIWindow(),
98+
authProvider: .amazon,
99+
options: AuthWebUISignInRequest.Options())
100+
let mockContext = AmplifyOperationContext(operationId: UUID(), request: mockRequest)
101+
let mockPayload = HubPayload(eventName: HubPayload.EventName.Auth.socialWebUISignIn,
102+
context: mockContext,
103+
data: mockEvent)
104+
Amplify.Hub.dispatch(to: .auth, payload: mockPayload)
105+
106+
}
107+
108+
private func mockSuccessfulWebUISignedInEvent() {
109+
let mockResult = AuthSignInResult(nextStep: .done)
110+
let mockEvent = AWSAuthWebUISignInOperation.OperationResult.success(mockResult)
111+
let mockRequest = AuthWebUISignInRequest(presentationAnchor: UIWindow(),
112+
options: AuthWebUISignInRequest.Options())
113+
let mockContext = AmplifyOperationContext(operationId: UUID(), request: mockRequest)
114+
let mockPayload = HubPayload(eventName: HubPayload.EventName.Auth.webUISignIn,
115+
context: mockContext,
116+
data: mockEvent)
117+
Amplify.Hub.dispatch(to: .auth, payload: mockPayload)
118+
119+
}
120+
121+
private func mockSuccessfulSignedInEvent() {
122+
let mockResult = AuthSignInResult(nextStep: .done)
123+
let mockEvent = AWSAuthSignInOperation.OperationResult.success(mockResult)
124+
let mockRequest = AuthSignInRequest(username: "username",
125+
password: "password",
126+
options: AuthSignInRequest.Options())
127+
let mockContext = AmplifyOperationContext(operationId: UUID(), request: mockRequest)
128+
let mockPayload = HubPayload(eventName: HubPayload.EventName.Auth.signIn,
129+
context: mockContext,
130+
data: mockEvent)
131+
Amplify.Hub.dispatch(to: .auth, payload: mockPayload)
132+
133+
}
134+
135+
}

0 commit comments

Comments
 (0)