-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathAWSAuthServiceBehaviorTests.swift
128 lines (110 loc) · 4.99 KB
/
AWSAuthServiceBehaviorTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
import XCTest
@testable import Amplify
@testable import AWSPluginsCore
import AWSCore
class AWSAuthServiceBehaviorTests: XCTestCase {
// MARK: Tests for change from blocking to non-blocking
/**
Confirm that calling the non-blocking replacements of `getIdentityId()` (`getIdentityID(completion:)`)
and `getToken()` (`getUserPoolAccessToken(completion:)`) in `AWSAuthServiceBehavior` conforming
objects that have yet to implement the non-blocking methods, will act as a pass through to
the existing blocking methods.
- IMPORTANT: This tests can be removed after the deprecation process of
the blocking methods `getIdentityId` and `getToken` is complete
*/
func testNonBlockingDefaultImplementationSuccess() {
let mockAWSAuthService: AWSAuthServiceBehavior = _MockAWSAuthService.init(
identityID: .success("42"),
userPoolAccessToken: .success("25")
)
/// Calling `getIdentityID(completion:)` on an `AWSAuthServiceBehavior` conforming type
/// without an explicit implementation should use the now deprecated `getIdentityId()` method as a
/// default implementation.
mockAWSAuthService.getIdentityID {
switch $0 {
case .success(let id): XCTAssertEqual(id, "42")
case .failure: XCTFail("This instance of _MockAWSAuthService should return .success")
}
}
/// Calling `getUserPoolAccessToken(completion:)` on an `AWSAuthServiceBehavior` conforming type
/// without an explicit implementation should use the now deprecated `getToken()` method as a
/// default implementation.
mockAWSAuthService.getUserPoolAccessToken {
switch $0 {
case .success(let id): XCTAssertEqual(id, "25")
case .failure: XCTFail("This instance of _MockAWSAuthService should return .success")
}
}
}
func testNonBlockingDefaultImplementationFailure() {
let identityIDErrorDescription = "identityID_description"
let identityIDErrorRecovery = "identityID_recovery"
let userPoolAccessTokenIDErrorDescription = "userPoolAccessToken_description"
let userPoolAccessTokenIDErrorRecovery = "userPoolAccessToken_recovery"
let mockAWSAuthService: AWSAuthServiceBehavior = _MockAWSAuthService.init(
identityID: .failure(
.notAuthorized(
identityIDErrorDescription,
identityIDErrorRecovery
)
),
userPoolAccessToken: .failure(
.invalidState(
userPoolAccessTokenIDErrorDescription,
userPoolAccessTokenIDErrorRecovery
)
)
)
/// Calling `getIdentityID(completion:)` on an `AWSAuthServiceBehavior` conforming type
/// without an explicit implementation should use the now deprecated `getIdentityId()` method as a
/// default implementation.
mockAWSAuthService.getIdentityID {
switch $0 {
case let .failure(.notAuthorized(description, recovery, _)):
XCTAssertEqual(description, identityIDErrorDescription)
XCTAssertEqual(recovery, identityIDErrorRecovery)
default: XCTFail("This instance of _MockAWSAuthService should return .failure(.notAuthorized)")
}
}
/// Calling `getUserPoolAccessToken(completion:)` on an `AWSAuthServiceBehavior` conforming type
/// without an explicit implementation should use the now deprecated `getToken()` method as a
/// default implementation.
mockAWSAuthService.getUserPoolAccessToken {
switch $0 {
case let .failure(.invalidState(description, recovery, _)):
XCTAssertEqual(description, userPoolAccessTokenIDErrorDescription)
XCTAssertEqual(recovery, userPoolAccessTokenIDErrorRecovery)
default: XCTFail("This instance of _MockAWSAuthService should return .failure(.invalidState)")
}
}
}
}
private class _MockAWSAuthService: AWSAuthServiceBehavior {
let identityID: () -> Result<String, AuthError>
let userPoolAccessToken: () -> Result<String, AuthError>
init(
identityID: @escaping @autoclosure () -> Result<String, AuthError>,
userPoolAccessToken: @escaping @autoclosure () -> Result<String, AuthError>
) {
self.identityID = identityID
self.userPoolAccessToken = userPoolAccessToken
}
func getCredentialsProvider() -> AWSCredentialsProvider {
AWSCognitoCredentialsProvider()
}
func getIdentityId() -> Result<String, AuthError> {
identityID()
}
func getToken() -> Result<String, AuthError> {
userPoolAccessToken()
}
func getTokenClaims(tokenString: String) -> Result<[String: AnyObject], AuthError> {
.success([:])
}
}