|
6 | 6 | //
|
7 | 7 |
|
8 | 8 | import Foundation
|
| 9 | +import SafariServices |
9 | 10 |
|
10 | 11 | import XCTest
|
11 | 12 | @testable import Amplify
|
12 | 13 | @testable import AWSCognitoAuthPlugin
|
13 | 14 | @testable import AWSMobileClient
|
14 | 15 |
|
| 16 | +// swiftlint:disable:next type_name |
15 | 17 | class AuthenticationProviderSigninWithSocialWebUITests: BaseAuthenticationProviderTest {
|
| 18 | + |
| 19 | + var window: UIWindow { |
| 20 | + let window = UIWindow() |
| 21 | + window.rootViewController = MockRootUIViewController() |
| 22 | + return window |
| 23 | + } |
| 24 | + |
| 25 | + /// Test a signInWithWebUI with valid inputs |
| 26 | + /// |
| 27 | + /// - Given: an auth plugin with mocked service. |
| 28 | + /// |
| 29 | + /// - When: |
| 30 | + /// - I invoke signInWithWebUI with valid values |
| 31 | + /// - Then: |
| 32 | + /// - I should get a .done response |
| 33 | + /// |
| 34 | + func testSuccessfulSignIn() { |
| 35 | + |
| 36 | + let mockSigninResult = UserState.signedIn |
| 37 | + mockAWSMobileClient?.showSignInMockResult = .success(mockSigninResult) |
| 38 | + let options = AuthWebUISignInRequest.Options() |
| 39 | + |
| 40 | + let resultExpectation = expectation(description: "Should receive a result") |
| 41 | + _ = plugin.signInWithWebUI(for: .amazon, presentationAnchor: window, options: options) { result in |
| 42 | + defer { |
| 43 | + resultExpectation.fulfill() |
| 44 | + } |
| 45 | + switch result { |
| 46 | + case .success(let signinResult): |
| 47 | + guard case .done = signinResult.nextStep else { |
| 48 | + XCTFail("Result should be .done for next step") |
| 49 | + return |
| 50 | + } |
| 51 | + XCTAssertTrue(signinResult.isSignedIn, "Signin result should be complete") |
| 52 | + case .failure(let error): |
| 53 | + XCTFail("Received failure with error \(error)") |
| 54 | + } |
| 55 | + } |
| 56 | + wait(for: [resultExpectation], timeout: apiTimeout) |
| 57 | + } |
| 58 | + |
| 59 | + /// Test a signInWithWebUI that return invalid response |
| 60 | + /// |
| 61 | + /// - Given: an auth plugin with mocked service. |
| 62 | + /// |
| 63 | + /// - When: |
| 64 | + /// - I invoke signInWithWebUI with valid values |
| 65 | + /// - Mock service returns invalid response like `signedOut` |
| 66 | + /// - Then: |
| 67 | + /// - I should get a .unknown response |
| 68 | + /// |
| 69 | + func testSignInWithInvalidResponse() { |
| 70 | + |
| 71 | + let mockSigninResult = UserState.signedOut |
| 72 | + mockAWSMobileClient?.showSignInMockResult = .success(mockSigninResult) |
| 73 | + let options = AuthWebUISignInRequest.Options() |
| 74 | + |
| 75 | + let resultExpectation = expectation(description: "Should receive a result") |
| 76 | + _ = plugin.signInWithWebUI(for: .amazon, presentationAnchor: window, options: options) { result in |
| 77 | + defer { |
| 78 | + resultExpectation.fulfill() |
| 79 | + } |
| 80 | + switch result { |
| 81 | + case .success(let signinResult): |
| 82 | + XCTFail("Should throw user cancelled error instead - \(signinResult)") |
| 83 | + case .failure(let error): |
| 84 | + guard case .unknown = error else { |
| 85 | + XCTFail("Received failure with error \(error)") |
| 86 | + return |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + wait(for: [resultExpectation], timeout: apiTimeout) |
| 91 | + } |
| 92 | + |
| 93 | + /// Test a signInWithWebUI when the user cancel |
| 94 | + /// |
| 95 | + /// - Given: an auth plugin with mocked service. |
| 96 | + /// |
| 97 | + /// - When: |
| 98 | + /// - I invoke signInWithWebUI and mock cancel |
| 99 | + /// - Then: |
| 100 | + /// - I should get a SFAuthenticationError.canceledLogin error |
| 101 | + /// |
| 102 | + func testCancelSignIn() { |
| 103 | + let mockError = NSError(domain: "com.apple.SafariServices.Authentication", |
| 104 | + code: SFAuthenticationError.canceledLogin.rawValue, |
| 105 | + userInfo: nil) |
| 106 | + mockAWSMobileClient?.showSignInMockResult = .failure(mockError) |
| 107 | + let options = AuthWebUISignInRequest.Options() |
| 108 | + |
| 109 | + let resultExpectation = expectation(description: "Should receive a result") |
| 110 | + _ = plugin.signInWithWebUI(for: .amazon, presentationAnchor: window, options: options) { result in |
| 111 | + defer { |
| 112 | + resultExpectation.fulfill() |
| 113 | + } |
| 114 | + switch result { |
| 115 | + case .success(let signinResult): |
| 116 | + XCTFail("Should throw user cancelled error, instead - \(signinResult)") |
| 117 | + case .failure(let error): |
| 118 | + guard case .unknown(_, let underlyingError) = error, |
| 119 | + case .canceledLogin = (underlyingError as? SFAuthenticationError)?.code else { |
| 120 | + XCTFail("Should produce SFAuthenticationError error but instead produced \(error)") |
| 121 | + return |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + wait(for: [resultExpectation], timeout: apiTimeout) |
| 126 | + } |
| 127 | + |
| 128 | + /// Test a signInWithWebUI with secuirty failed error |
| 129 | + /// |
| 130 | + /// - Given: an auth plugin with mocked service. |
| 131 | + /// |
| 132 | + /// - When: |
| 133 | + /// - I invoke signInWithWebUI and mock securityFailed |
| 134 | + /// - Then: |
| 135 | + /// - I should get a .service error |
| 136 | + /// |
| 137 | + func testSignInSecurityFailed() { |
| 138 | + |
| 139 | + let error = AWSMobileClientError.securityFailed(message: "") |
| 140 | + mockAWSMobileClient?.showSignInMockResult = .failure(error) |
| 141 | + let options = AuthWebUISignInRequest.Options() |
| 142 | + |
| 143 | + let resultExpectation = expectation(description: "Should receive a result") |
| 144 | + _ = plugin.signInWithWebUI(for: .amazon, presentationAnchor: window, options: options) { result in |
| 145 | + defer { |
| 146 | + resultExpectation.fulfill() |
| 147 | + } |
| 148 | + switch result { |
| 149 | + case .success(let signinResult): |
| 150 | + XCTFail("Should throw user cancelled error, instead - \(signinResult)") |
| 151 | + case .failure(let error): |
| 152 | + guard case .service = error else { |
| 153 | + XCTFail("Should produce service error but instead produced \(error)") |
| 154 | + return |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + wait(for: [resultExpectation], timeout: apiTimeout) |
| 159 | + } |
| 160 | + |
| 161 | + /// Test a signInWithWebUI with bad request error |
| 162 | + /// |
| 163 | + /// - Given: an auth plugin with mocked service. |
| 164 | + /// |
| 165 | + /// - When: |
| 166 | + /// - I invoke signInWithWebUI and mock badRequest |
| 167 | + /// - Then: |
| 168 | + /// - I should get a .service error |
| 169 | + /// |
| 170 | + func testSignInBadRequest() { |
| 171 | + |
| 172 | + let error = AWSMobileClientError.badRequest(message: "") |
| 173 | + mockAWSMobileClient?.showSignInMockResult = .failure(error) |
| 174 | + let options = AuthWebUISignInRequest.Options() |
| 175 | + |
| 176 | + let resultExpectation = expectation(description: "Should receive a result") |
| 177 | + _ = plugin.signInWithWebUI(for: .amazon, presentationAnchor: window, options: options) { result in |
| 178 | + defer { |
| 179 | + resultExpectation.fulfill() |
| 180 | + } |
| 181 | + switch result { |
| 182 | + case .success(let signinResult): |
| 183 | + XCTFail("Should throw user cancelled error, instead - \(signinResult)") |
| 184 | + case .failure(let error): |
| 185 | + guard case .service = error else { |
| 186 | + XCTFail("Should produce service error but instead produced \(error)") |
| 187 | + return |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + wait(for: [resultExpectation], timeout: apiTimeout) |
| 192 | + } |
| 193 | + |
| 194 | + /// Test a signInWithWebUI with invalid id token error |
| 195 | + /// |
| 196 | + /// - Given: an auth plugin with mocked service. |
| 197 | + /// |
| 198 | + /// - When: |
| 199 | + /// - I invoke signInWithWebUI and mock idTokenAndAcceessTokenNotIssued |
| 200 | + /// - Then: |
| 201 | + /// - I should get a .unknown error |
| 202 | + /// |
| 203 | + func testSignInInvalidIdToken() { |
| 204 | + |
| 205 | + let error = AWSMobileClientError.idTokenAndAcceessTokenNotIssued(message: "") |
| 206 | + mockAWSMobileClient?.showSignInMockResult = .failure(error) |
| 207 | + let options = AuthWebUISignInRequest.Options() |
| 208 | + |
| 209 | + let resultExpectation = expectation(description: "Should receive a result") |
| 210 | + _ = plugin.signInWithWebUI(for: .amazon, presentationAnchor: window, options: options) { result in |
| 211 | + defer { |
| 212 | + resultExpectation.fulfill() |
| 213 | + } |
| 214 | + switch result { |
| 215 | + case .success(let signinResult): |
| 216 | + XCTFail("Should throw user cancelled error, instead - \(signinResult)") |
| 217 | + case .failure(let error): |
| 218 | + guard case .unknown = error else { |
| 219 | + XCTFail("Should produce unknown error but instead produced \(error)") |
| 220 | + return |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + wait(for: [resultExpectation], timeout: apiTimeout) |
| 225 | + } |
16 | 226 | }
|
0 commit comments