Skip to content

Commit 7dc8bbc

Browse files
committed
add unit tests
1 parent 426cf99 commit 7dc8bbc

File tree

2 files changed

+153
-7
lines changed

2 files changed

+153
-7
lines changed

AmplifyPlugins/Auth/Sources/AWSCognitoAuthPlugin/Models/AuthFlowType.swift

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,14 +164,17 @@ extension AuthFlowType: Codable {
164164
case "USER_PASSWORD_AUTH":
165165
self = .userPassword
166166
case "USER_AUTH":
167-
let preferredFirstFactorString = try container.decode(String.self, forKey: .preferredFirstFactor)
168-
if let preferredFirstFactor = AuthFactorType(rawValue: preferredFirstFactorString) {
169-
self = .userAuth(preferredFirstFactor: preferredFirstFactor)
167+
if let preferredFirstFactorString = try container.decodeIfPresent(String.self, forKey: .preferredFirstFactor) {
168+
if let preferredFirstFactor = AuthFactorType(rawValue: preferredFirstFactorString) {
169+
self = .userAuth(preferredFirstFactor: preferredFirstFactor)
170+
} else {
171+
throw DecodingError.dataCorruptedError(
172+
forKey: .preferredFirstFactor,
173+
in: container,
174+
debugDescription: "Unable to decode preferredFirstFactor value")
175+
}
170176
} else {
171-
throw DecodingError.dataCorruptedError(
172-
forKey: .preferredFirstFactor,
173-
in: container,
174-
debugDescription: "Unable to decode preferredFirstFactor value")
177+
self = .userAuth(preferredFirstFactor: nil)
175178
}
176179
default:
177180
throw DecodingError.dataCorruptedError(forKey: .type, in: container, debugDescription: "Invalid AuthFlowType value")
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
9+
import XCTest
10+
@testable import AWSCognitoAuthPlugin
11+
12+
class AuthFlowTypeTests: XCTestCase {
13+
14+
func testRawValue() {
15+
XCTAssertEqual(AuthFlowType.userSRP.rawValue, "USER_SRP_AUTH")
16+
XCTAssertEqual(AuthFlowType.customWithSRP.rawValue, "CUSTOM_AUTH_WITH_SRP")
17+
XCTAssertEqual(AuthFlowType.customWithoutSRP.rawValue, "CUSTOM_AUTH_WITHOUT_SRP")
18+
XCTAssertEqual(AuthFlowType.userPassword.rawValue, "USER_PASSWORD_AUTH")
19+
XCTAssertEqual(AuthFlowType.userAuth(preferredFirstFactor: nil).rawValue, "USER_AUTH")
20+
}
21+
22+
func testInitWithRawValue() {
23+
XCTAssertEqual(AuthFlowType(rawValue: "USER_SRP_AUTH"), .userSRP)
24+
XCTAssertEqual(AuthFlowType(rawValue: "CUSTOM_AUTH"), .customWithSRP)
25+
XCTAssertEqual(AuthFlowType(rawValue: "CUSTOM_AUTH_WITH_SRP"), .customWithSRP)
26+
XCTAssertEqual(AuthFlowType(rawValue: "CUSTOM_AUTH_WITHOUT_SRP"), .customWithoutSRP)
27+
XCTAssertEqual(AuthFlowType(rawValue: "USER_PASSWORD_AUTH"), .userPassword)
28+
XCTAssertEqual(AuthFlowType(rawValue: "USER_AUTH"), .userAuth(preferredFirstFactor: nil))
29+
XCTAssertNil(AuthFlowType(rawValue: "INVALID_AUTH"))
30+
}
31+
32+
func testDeprecatedCustom() {
33+
// This test is to ensure the deprecated case is still functional
34+
XCTAssertEqual(AuthFlowType.custom.rawValue, "CUSTOM_AUTH_WITH_SRP")
35+
}
36+
37+
func testEncoding() throws {
38+
let encoder = JSONEncoder()
39+
let userSRP = try encoder.encode(AuthFlowType.userSRP)
40+
XCTAssertEqual(String(data: userSRP, encoding: .utf8), "{\"type\":\"USER_SRP_AUTH\"}")
41+
42+
let customWithSRP = try encoder.encode(AuthFlowType.customWithSRP)
43+
XCTAssertEqual(String(data: customWithSRP, encoding: .utf8), "{\"type\":\"CUSTOM_AUTH_WITH_SRP\"}")
44+
45+
let customWithoutSRP = try encoder.encode(AuthFlowType.customWithoutSRP)
46+
XCTAssertEqual(String(data: customWithoutSRP, encoding: .utf8), "{\"type\":\"CUSTOM_AUTH_WITHOUT_SRP\"}")
47+
48+
let userPassword = try encoder.encode(AuthFlowType.userPassword)
49+
XCTAssertEqual(String(data: userPassword, encoding: .utf8), "{\"type\":\"USER_PASSWORD_AUTH\"}")
50+
51+
let userAuth = try encoder.encode(AuthFlowType.userAuth(preferredFirstFactor: nil))
52+
XCTAssertEqual(String(data: userAuth, encoding: .utf8), "{\"preferredFirstFactor\":null,\"type\":\"USER_AUTH\"}")
53+
}
54+
55+
func testDecoding() throws {
56+
let decoder = JSONDecoder()
57+
let userSRP = try decoder.decode(AuthFlowType.self, from: "{\"type\":\"USER_SRP_AUTH\"}".data(using: .utf8)!)
58+
XCTAssertEqual(userSRP, .userSRP)
59+
60+
let customWithSRP = try decoder.decode(AuthFlowType.self, from: "{\"type\":\"CUSTOM_AUTH_WITH_SRP\"}".data(using: .utf8)!)
61+
XCTAssertEqual(customWithSRP, .customWithSRP)
62+
63+
let customWithoutSRP = try decoder.decode(AuthFlowType.self, from: "{\"type\":\"CUSTOM_AUTH_WITHOUT_SRP\"}".data(using: .utf8)!)
64+
XCTAssertEqual(customWithoutSRP, .customWithoutSRP)
65+
66+
let userPassword = try decoder.decode(AuthFlowType.self, from: "{\"type\":\"USER_PASSWORD_AUTH\"}".data(using: .utf8)!)
67+
XCTAssertEqual(userPassword, .userPassword)
68+
69+
let userAuth = try decoder.decode(AuthFlowType.self, from: "{\"type\":\"USER_AUTH\"}".data(using: .utf8)!)
70+
XCTAssertEqual(userAuth, .userAuth(preferredFirstFactor: nil))
71+
}
72+
73+
func testDecodingWithPreferredFirstFactor() throws {
74+
let decoder = JSONDecoder()
75+
let json = """
76+
{
77+
"type": "USER_AUTH",
78+
"preferredFirstFactor": "SMS_OTP"
79+
}
80+
""".data(using: .utf8)!
81+
let authFlowType = try decoder.decode(AuthFlowType.self, from: json)
82+
XCTAssertEqual(authFlowType, .userAuth(preferredFirstFactor: .smsOTP))
83+
}
84+
85+
func testDecodingLegacyStructure() throws {
86+
let decoder = JSONDecoder()
87+
var legacyJson = "\"userSRP\"".data(using: .utf8)!
88+
var authFlowType = try decoder.decode(AuthFlowType.self, from: legacyJson)
89+
XCTAssertEqual(authFlowType, .userSRP)
90+
91+
legacyJson = "\"userPassword\"".data(using: .utf8)!
92+
authFlowType = try decoder.decode(AuthFlowType.self, from: legacyJson)
93+
XCTAssertEqual(authFlowType, .userPassword)
94+
95+
legacyJson = "\"customWithSRP\"".data(using: .utf8)!
96+
authFlowType = try decoder.decode(AuthFlowType.self, from: legacyJson)
97+
XCTAssertEqual(authFlowType, .customWithSRP)
98+
99+
legacyJson = "\"customWithoutSRP\"".data(using: .utf8)!
100+
authFlowType = try decoder.decode(AuthFlowType.self, from: legacyJson)
101+
XCTAssertEqual(authFlowType, .customWithoutSRP)
102+
103+
legacyJson = "\"custom\"".data(using: .utf8)!
104+
authFlowType = try decoder.decode(AuthFlowType.self, from: legacyJson)
105+
XCTAssertEqual(authFlowType, .custom)
106+
}
107+
108+
func testDecodingInvalidType() {
109+
let decoder = JSONDecoder()
110+
let invalidJson = "{\"type\":\"INVALID_AUTH\"}".data(using: .utf8)!
111+
XCTAssertThrowsError(try decoder.decode(AuthFlowType.self, from: invalidJson)) { error in
112+
guard case DecodingError.dataCorrupted(let context) = error else {
113+
return XCTFail("Expected dataCorrupted error")
114+
}
115+
XCTAssertEqual(context.debugDescription, "Invalid AuthFlowType value")
116+
}
117+
}
118+
119+
func testDecodingInvalidPreferredFirstFactor() {
120+
let decoder = JSONDecoder()
121+
let invalidJson = """
122+
{
123+
"type": "USER_AUTH",
124+
"preferredFirstFactor": "INVALID_FACTOR"
125+
}
126+
""".data(using: .utf8)!
127+
XCTAssertThrowsError(try decoder.decode(AuthFlowType.self, from: invalidJson)) { error in
128+
guard case DecodingError.dataCorrupted(let context) = error else {
129+
return XCTFail("Expected dataCorrupted error")
130+
}
131+
XCTAssertEqual(context.debugDescription, "Unable to decode preferredFirstFactor value")
132+
}
133+
}
134+
135+
func testGetClientFlowType() {
136+
XCTAssertEqual(AuthFlowType.custom.getClientFlowType(), .customAuth)
137+
XCTAssertEqual(AuthFlowType.customWithSRP.getClientFlowType(), .customAuth)
138+
XCTAssertEqual(AuthFlowType.customWithoutSRP.getClientFlowType(), .customAuth)
139+
XCTAssertEqual(AuthFlowType.userSRP.getClientFlowType(), .userSrpAuth)
140+
XCTAssertEqual(AuthFlowType.userPassword.getClientFlowType(), .userPasswordAuth)
141+
XCTAssertEqual(AuthFlowType.userAuth(preferredFirstFactor: nil).getClientFlowType(), .userAuth)
142+
}
143+
}

0 commit comments

Comments
 (0)