|
| 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 | + XCTAssertTrue(String(data: userAuth, encoding: .utf8)?.contains("\"preferredFirstFactor\":null") == true) |
| 53 | + XCTAssertTrue(String(data: userAuth, encoding: .utf8)?.contains("\"type\":\"USER_AUTH\"") == true) |
| 54 | + } |
| 55 | + |
| 56 | + func testDecoding() throws { |
| 57 | + let decoder = JSONDecoder() |
| 58 | + let userSRP = try decoder.decode(AuthFlowType.self, from: "{\"type\":\"USER_SRP_AUTH\"}".data(using: .utf8)!) |
| 59 | + XCTAssertEqual(userSRP, .userSRP) |
| 60 | + |
| 61 | + let customWithSRP = try decoder.decode(AuthFlowType.self, from: "{\"type\":\"CUSTOM_AUTH_WITH_SRP\"}".data(using: .utf8)!) |
| 62 | + XCTAssertEqual(customWithSRP, .customWithSRP) |
| 63 | + |
| 64 | + let customWithoutSRP = try decoder.decode(AuthFlowType.self, from: "{\"type\":\"CUSTOM_AUTH_WITHOUT_SRP\"}".data(using: .utf8)!) |
| 65 | + XCTAssertEqual(customWithoutSRP, .customWithoutSRP) |
| 66 | + |
| 67 | + let userPassword = try decoder.decode(AuthFlowType.self, from: "{\"type\":\"USER_PASSWORD_AUTH\"}".data(using: .utf8)!) |
| 68 | + XCTAssertEqual(userPassword, .userPassword) |
| 69 | + |
| 70 | + let userAuth = try decoder.decode(AuthFlowType.self, from: "{\"type\":\"USER_AUTH\"}".data(using: .utf8)!) |
| 71 | + XCTAssertEqual(userAuth, .userAuth(preferredFirstFactor: nil)) |
| 72 | + } |
| 73 | + |
| 74 | + func testDecodingWithPreferredFirstFactor() throws { |
| 75 | + let decoder = JSONDecoder() |
| 76 | + let json = """ |
| 77 | + { |
| 78 | + "type": "USER_AUTH", |
| 79 | + "preferredFirstFactor": "SMS_OTP" |
| 80 | + } |
| 81 | + """.data(using: .utf8)! |
| 82 | + let authFlowType = try decoder.decode(AuthFlowType.self, from: json) |
| 83 | + XCTAssertEqual(authFlowType, .userAuth(preferredFirstFactor: .smsOTP)) |
| 84 | + } |
| 85 | + |
| 86 | + func testDecodingLegacyStructure() throws { |
| 87 | + let decoder = JSONDecoder() |
| 88 | + var legacyJson = "\"userSRP\"".data(using: .utf8)! |
| 89 | + var authFlowType = try decoder.decode(AuthFlowType.self, from: legacyJson) |
| 90 | + XCTAssertEqual(authFlowType, .userSRP) |
| 91 | + |
| 92 | + legacyJson = "\"userPassword\"".data(using: .utf8)! |
| 93 | + authFlowType = try decoder.decode(AuthFlowType.self, from: legacyJson) |
| 94 | + XCTAssertEqual(authFlowType, .userPassword) |
| 95 | + |
| 96 | + legacyJson = "\"customWithSRP\"".data(using: .utf8)! |
| 97 | + authFlowType = try decoder.decode(AuthFlowType.self, from: legacyJson) |
| 98 | + XCTAssertEqual(authFlowType, .customWithSRP) |
| 99 | + |
| 100 | + legacyJson = "\"customWithoutSRP\"".data(using: .utf8)! |
| 101 | + authFlowType = try decoder.decode(AuthFlowType.self, from: legacyJson) |
| 102 | + XCTAssertEqual(authFlowType, .customWithoutSRP) |
| 103 | + |
| 104 | + legacyJson = "\"custom\"".data(using: .utf8)! |
| 105 | + authFlowType = try decoder.decode(AuthFlowType.self, from: legacyJson) |
| 106 | + XCTAssertEqual(authFlowType, .custom) |
| 107 | + } |
| 108 | + |
| 109 | + func testDecodingInvalidType() { |
| 110 | + let decoder = JSONDecoder() |
| 111 | + let invalidJson = "{\"type\":\"INVALID_AUTH\"}".data(using: .utf8)! |
| 112 | + XCTAssertThrowsError(try decoder.decode(AuthFlowType.self, from: invalidJson)) { error in |
| 113 | + guard case DecodingError.dataCorrupted(let context) = error else { |
| 114 | + return XCTFail("Expected dataCorrupted error") |
| 115 | + } |
| 116 | + XCTAssertEqual(context.debugDescription, "Invalid AuthFlowType value") |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + func testDecodingInvalidPreferredFirstFactor() { |
| 121 | + let decoder = JSONDecoder() |
| 122 | + let invalidJson = """ |
| 123 | + { |
| 124 | + "type": "USER_AUTH", |
| 125 | + "preferredFirstFactor": "INVALID_FACTOR" |
| 126 | + } |
| 127 | + """.data(using: .utf8)! |
| 128 | + XCTAssertThrowsError(try decoder.decode(AuthFlowType.self, from: invalidJson)) { error in |
| 129 | + guard case DecodingError.dataCorrupted(let context) = error else { |
| 130 | + return XCTFail("Expected dataCorrupted error") |
| 131 | + } |
| 132 | + XCTAssertEqual(context.debugDescription, "Unable to decode preferredFirstFactor value") |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + func testGetClientFlowType() { |
| 137 | + XCTAssertEqual(AuthFlowType.custom.getClientFlowType(), .customAuth) |
| 138 | + XCTAssertEqual(AuthFlowType.customWithSRP.getClientFlowType(), .customAuth) |
| 139 | + XCTAssertEqual(AuthFlowType.customWithoutSRP.getClientFlowType(), .customAuth) |
| 140 | + XCTAssertEqual(AuthFlowType.userSRP.getClientFlowType(), .userSrpAuth) |
| 141 | + XCTAssertEqual(AuthFlowType.userPassword.getClientFlowType(), .userPasswordAuth) |
| 142 | + XCTAssertEqual(AuthFlowType.userAuth(preferredFirstFactor: nil).getClientFlowType(), .userAuth) |
| 143 | + } |
| 144 | +} |
0 commit comments