|
| 1 | +import ConcurrencyExtras |
| 2 | +import Mocker |
| 3 | +import TestHelpers |
| 4 | +import XCTest |
| 5 | + |
| 6 | +@testable import Auth |
| 7 | + |
| 8 | +final class APIClientTests: XCTestCase { |
| 9 | + fileprivate var apiClient: APIClient! |
| 10 | + fileprivate var storage: InMemoryLocalStorage! |
| 11 | + fileprivate var sut: AuthClient! |
| 12 | + |
| 13 | + #if !os(Windows) && !os(Linux) && !os(Android) |
| 14 | + override func invokeTest() { |
| 15 | + withMainSerialExecutor { |
| 16 | + super.invokeTest() |
| 17 | + } |
| 18 | + } |
| 19 | + #endif |
| 20 | + |
| 21 | + override func setUp() { |
| 22 | + super.setUp() |
| 23 | + storage = InMemoryLocalStorage() |
| 24 | + sut = makeSUT() |
| 25 | + apiClient = APIClient(clientID: sut.clientID) |
| 26 | + } |
| 27 | + |
| 28 | + override func tearDown() { |
| 29 | + super.tearDown() |
| 30 | + Mocker.removeAll() |
| 31 | + sut = nil |
| 32 | + storage = nil |
| 33 | + apiClient = nil |
| 34 | + } |
| 35 | + |
| 36 | + // MARK: - Core APIClient Tests |
| 37 | + |
| 38 | + func testAPIClientInitialization() { |
| 39 | + // Given: A client ID |
| 40 | + let clientID = sut.clientID |
| 41 | + |
| 42 | + // When: Creating an API client |
| 43 | + let client = APIClient(clientID: clientID) |
| 44 | + |
| 45 | + // Then: Should be initialized |
| 46 | + XCTAssertNotNil(client) |
| 47 | + } |
| 48 | + |
| 49 | + func testAPIClientExecuteSuccess() async throws { |
| 50 | + // Given: A mock successful response |
| 51 | + let responseData = createValidSessionJSON() |
| 52 | + |
| 53 | + Mock( |
| 54 | + url: URL(string: "http://localhost:54321/auth/v1/token")!, |
| 55 | + ignoreQuery: true, |
| 56 | + statusCode: 200, |
| 57 | + data: [.post: responseData] |
| 58 | + ).register() |
| 59 | + |
| 60 | + // When: Executing a request |
| 61 | + let request = try apiClient.execute( |
| 62 | + URL(string: "http://localhost:54321/auth/v1/token")!, |
| 63 | + method: .post, |
| 64 | + headers: [:], |
| 65 | + query: nil, |
| 66 | + body: ["grant_type": "refresh_token"], |
| 67 | + encoder: nil |
| 68 | + ) |
| 69 | + |
| 70 | + // Then: Should not throw an error and return a valid response |
| 71 | + do { |
| 72 | + let result: Session = try await request.serializingDecodable(Session.self).value |
| 73 | + XCTAssertNotNil(result) |
| 74 | + XCTAssertNotNil(result.accessToken) |
| 75 | + XCTAssertNotNil(result.refreshToken) |
| 76 | + } catch { |
| 77 | + XCTFail("Expected successful response, got error: \(error)") |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + func testAPIClientExecuteFailure() async throws { |
| 82 | + // Given: A mock error response |
| 83 | + let errorResponse = """ |
| 84 | + { |
| 85 | + "error": "invalid_grant", |
| 86 | + "error_description": "Invalid refresh token" |
| 87 | + } |
| 88 | + """.data(using: .utf8)! |
| 89 | + |
| 90 | + Mock( |
| 91 | + url: URL(string: "http://localhost:54321/auth/v1/token")!, |
| 92 | + ignoreQuery: true, |
| 93 | + statusCode: 400, |
| 94 | + data: [.post: errorResponse] |
| 95 | + ).register() |
| 96 | + |
| 97 | + // When: Executing a request |
| 98 | + let request = try apiClient.execute( |
| 99 | + URL(string: "http://localhost:54321/auth/v1/token")!, |
| 100 | + method: .post, |
| 101 | + headers: [:], |
| 102 | + query: nil, |
| 103 | + body: ["grant_type": "refresh_token"], |
| 104 | + encoder: nil |
| 105 | + ) |
| 106 | + |
| 107 | + // Then: Should throw error |
| 108 | + do { |
| 109 | + let _: Session = try await request.serializingDecodable(Session.self).value |
| 110 | + XCTFail("Expected error to be thrown") |
| 111 | + } catch { |
| 112 | + let errorMessage = String(describing: error) |
| 113 | + XCTAssertTrue( |
| 114 | + errorMessage.contains("Invalid refresh token") |
| 115 | + || errorMessage.contains("invalid_grant")) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + func testAPIClientExecuteWithHeaders() async throws { |
| 120 | + // Given: A mock response |
| 121 | + let responseData = createValidSessionJSON() |
| 122 | + |
| 123 | + Mock( |
| 124 | + url: URL(string: "http://localhost:54321/auth/v1/token")!, |
| 125 | + ignoreQuery: true, |
| 126 | + statusCode: 200, |
| 127 | + data: [.post: responseData] |
| 128 | + ).register() |
| 129 | + |
| 130 | + // When: Executing a request with default headers |
| 131 | + let request = try apiClient.execute( |
| 132 | + URL(string: "http://localhost:54321/auth/v1/token")!, |
| 133 | + method: .post, |
| 134 | + headers: [:], |
| 135 | + query: nil, |
| 136 | + body: ["grant_type": "refresh_token"], |
| 137 | + encoder: nil |
| 138 | + ) |
| 139 | + |
| 140 | + // Then: Should not throw an error |
| 141 | + do { |
| 142 | + let result: Session = try await request.serializingDecodable(Session.self).value |
| 143 | + XCTAssertNotNil(result) |
| 144 | + } catch { |
| 145 | + XCTFail("Expected successful response, got error: \(error)") |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + func testAPIClientExecuteWithQueryParameters() async throws { |
| 150 | + // Given: A mock response |
| 151 | + let responseData = createValidSessionJSON() |
| 152 | + |
| 153 | + Mock( |
| 154 | + url: URL(string: "http://localhost:54321/auth/v1/token")!, |
| 155 | + ignoreQuery: true, |
| 156 | + statusCode: 200, |
| 157 | + data: [.post: responseData] |
| 158 | + ).register() |
| 159 | + |
| 160 | + // When: Executing a request with query parameters |
| 161 | + let query = ["client_id": "test_client", "response_type": "code"] |
| 162 | + let request = try apiClient.execute( |
| 163 | + URL(string: "http://localhost:54321/auth/v1/token")!, |
| 164 | + method: .post, |
| 165 | + headers: [:], |
| 166 | + query: query, |
| 167 | + body: ["grant_type": "refresh_token"], |
| 168 | + encoder: nil |
| 169 | + ) |
| 170 | + |
| 171 | + // Then: Should not throw an error |
| 172 | + do { |
| 173 | + let result: Session = try await request.serializingDecodable(Session.self).value |
| 174 | + XCTAssertNotNil(result) |
| 175 | + } catch { |
| 176 | + XCTFail("Expected successful response, got error: \(error)") |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + func testAPIClientExecuteWithDifferentMethods() async throws { |
| 181 | + // Given: Mock response for POST method |
| 182 | + let postResponse = createValidSessionJSON() |
| 183 | + |
| 184 | + Mock( |
| 185 | + url: URL(string: "http://localhost:54321/auth/v1/token")!, |
| 186 | + ignoreQuery: true, |
| 187 | + statusCode: 200, |
| 188 | + data: [.post: postResponse] |
| 189 | + ).register() |
| 190 | + |
| 191 | + // When: Executing POST request |
| 192 | + let postRequest = try apiClient.execute( |
| 193 | + URL(string: "http://localhost:54321/auth/v1/token")!, |
| 194 | + method: .post, |
| 195 | + headers: [:], |
| 196 | + query: nil, |
| 197 | + body: ["grant_type": "refresh_token"], |
| 198 | + encoder: nil |
| 199 | + ) |
| 200 | + |
| 201 | + // Then: Should not throw an error |
| 202 | + do { |
| 203 | + let postResult: Session = try await postRequest.serializingDecodable(Session.self).value |
| 204 | + XCTAssertNotNil(postResult) |
| 205 | + } catch { |
| 206 | + XCTFail("Expected successful response, got error: \(error)") |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + func testAPIClientExecuteWithNetworkError() async throws { |
| 211 | + // Given: No mock registered (will cause network error) |
| 212 | + |
| 213 | + // When: Executing a request |
| 214 | + let request = try apiClient.execute( |
| 215 | + URL(string: "http://localhost:54321/auth/v1/token")!, |
| 216 | + method: .post, |
| 217 | + headers: [:], |
| 218 | + query: nil, |
| 219 | + body: ["grant_type": "refresh_token"], |
| 220 | + encoder: nil |
| 221 | + ) |
| 222 | + |
| 223 | + // Then: Should throw network error |
| 224 | + do { |
| 225 | + let _: Session = try await request.serializingDecodable(Session.self).value |
| 226 | + XCTFail("Expected error to be thrown") |
| 227 | + } catch { |
| 228 | + // Network error is expected |
| 229 | + XCTAssertNotNil(error) |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + func testAPIClientExecuteWithTimeout() async throws { |
| 234 | + // Given: A mock response with delay |
| 235 | + let responseData = createValidSessionJSON() |
| 236 | + |
| 237 | + var mock = Mock( |
| 238 | + url: URL(string: "http://localhost:54321/auth/v1/token")!, |
| 239 | + ignoreQuery: true, |
| 240 | + statusCode: 200, |
| 241 | + data: [.post: responseData] |
| 242 | + ) |
| 243 | + mock.delay = DispatchTimeInterval.milliseconds(100) |
| 244 | + mock.register() |
| 245 | + |
| 246 | + // When: Executing a request |
| 247 | + let request = try apiClient.execute( |
| 248 | + URL(string: "http://localhost:54321/auth/v1/token")!, |
| 249 | + method: .post, |
| 250 | + headers: [:], |
| 251 | + query: nil, |
| 252 | + body: ["grant_type": "refresh_token"], |
| 253 | + encoder: nil |
| 254 | + ) |
| 255 | + |
| 256 | + // Then: Should not throw an error after delay |
| 257 | + do { |
| 258 | + let result: Session = try await request.serializingDecodable(Session.self).value |
| 259 | + XCTAssertNotNil(result) |
| 260 | + } catch { |
| 261 | + XCTFail("Expected successful response, got error: \(error)") |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + func testAPIClientExecuteWithLargeResponse() async throws { |
| 266 | + // Given: A mock response with large data |
| 267 | + let largeResponse = String(repeating: "a", count: 10000) |
| 268 | + let responseData = """ |
| 269 | + { |
| 270 | + "data": "\(largeResponse)", |
| 271 | + "access_token": "test_access_token" |
| 272 | + } |
| 273 | + """.data(using: .utf8)! |
| 274 | + |
| 275 | + Mock( |
| 276 | + url: URL(string: "http://localhost:54321/auth/v1/token")!, |
| 277 | + ignoreQuery: true, |
| 278 | + statusCode: 200, |
| 279 | + data: [.post: responseData] |
| 280 | + ).register() |
| 281 | + |
| 282 | + // When: Executing a request |
| 283 | + let request = try apiClient.execute( |
| 284 | + URL(string: "http://localhost:54321/auth/v1/token")!, |
| 285 | + method: .post, |
| 286 | + headers: [:], |
| 287 | + query: nil, |
| 288 | + body: ["grant_type": "refresh_token"], |
| 289 | + encoder: nil |
| 290 | + ) |
| 291 | + |
| 292 | + struct LargeResponse: Codable { |
| 293 | + let data: String |
| 294 | + let accessToken: String |
| 295 | + |
| 296 | + enum CodingKeys: String, CodingKey { |
| 297 | + case data |
| 298 | + case accessToken = "access_token" |
| 299 | + } |
| 300 | + } |
| 301 | + |
| 302 | + let result: LargeResponse = try await request.serializingDecodable(LargeResponse.self).value |
| 303 | + |
| 304 | + // Then: Should handle large response |
| 305 | + XCTAssertEqual(result.data.count, 10000) |
| 306 | + XCTAssertEqual(result.accessToken, "test_access_token") |
| 307 | + } |
| 308 | + |
| 309 | + // MARK: - Integration Tests |
| 310 | + |
| 311 | + func testAPIClientIntegrationWithAuthClient() async throws { |
| 312 | + // Given: A mock response for sign in |
| 313 | + let responseData = createValidSessionJSON() |
| 314 | + |
| 315 | + Mock( |
| 316 | + url: URL(string: "http://localhost:54321/auth/v1/token")!, |
| 317 | + ignoreQuery: true, |
| 318 | + statusCode: 200, |
| 319 | + data: [.post: responseData] |
| 320 | + ).register() |
| 321 | + |
| 322 | + // When: Using auth client to sign in |
| 323 | + let result = try await sut.signIn( |
| 324 | + |
| 325 | + password: "password123" |
| 326 | + ) |
| 327 | + |
| 328 | + // Then: Should return session |
| 329 | + assertValidSession(result) |
| 330 | + } |
| 331 | + |
| 332 | + // MARK: - Helper Methods |
| 333 | + |
| 334 | + private func createValidSessionJSON() -> Data { |
| 335 | + // Use the existing session.json file which has the correct format |
| 336 | + return json(named: "session") |
| 337 | + } |
| 338 | + |
| 339 | + private func createValidSessionResponse() -> Session { |
| 340 | + // Use the existing mock session which is guaranteed to work |
| 341 | + return Session.validSession |
| 342 | + } |
| 343 | + |
| 344 | + private func assertValidSession(_ session: Session) { |
| 345 | + XCTAssertEqual( |
| 346 | + session.accessToken, |
| 347 | + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJhdXRoZW50aWNhdGVkIiwiZXhwIjoxNjQ4NjQwMDIxLCJzdWIiOiJmMzNkM2VjOS1hMmVlLTQ3YzQtODBlMS01YmQ5MTlmM2Q4YjgiLCJlbWFpbCI6Imd1aWxoZXJtZTJAZ3Jkcy5kZXYiLCJwaG9uZSI6IiIsImFwcF9tZXRhZGF0YSI6eyJwcm92aWRlciI6ImVtYWlsIiwicHJvdmlkZXJzIjpbImVtYWlsIl19LCJ1c2VyX21ldGFkYXRhIjp7fSwicm9sZSI6ImF1dGhlbnRpY2F0ZWQifQ.4lMvmz2pJkWu1hMsBgXP98Fwz4rbvFYl4VA9joRv6kY" |
| 348 | + ) |
| 349 | + XCTAssertEqual(session.refreshToken, "GGduTeu95GraIXQ56jppkw") |
| 350 | + XCTAssertEqual(session.expiresIn, 3600) |
| 351 | + XCTAssertEqual(session.tokenType, "bearer") |
| 352 | + XCTAssertEqual(session .user .email , "[email protected]") |
| 353 | + } |
| 354 | + |
| 355 | + private func makeSUT(flowType: AuthFlowType = .pkce) -> AuthClient { |
| 356 | + let sessionConfiguration = URLSessionConfiguration.default |
| 357 | + sessionConfiguration.protocolClasses = [MockingURLProtocol.self] |
| 358 | + |
| 359 | + let encoder = AuthClient.Configuration.jsonEncoder |
| 360 | + encoder.outputFormatting = [.sortedKeys] |
| 361 | + |
| 362 | + let configuration = AuthClient.Configuration( |
| 363 | + url: clientURL, |
| 364 | + headers: [ |
| 365 | + "apikey": |
| 366 | + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0" |
| 367 | + ], |
| 368 | + flowType: flowType, |
| 369 | + localStorage: storage, |
| 370 | + logger: nil, |
| 371 | + encoder: encoder, |
| 372 | + session: .init(configuration: sessionConfiguration) |
| 373 | + ) |
| 374 | + |
| 375 | + let sut = AuthClient(configuration: configuration) |
| 376 | + |
| 377 | + Dependencies[sut.clientID].pkce.generateCodeVerifier = { |
| 378 | + "nt_xCJhJXUsIlTmbE_b0r3VHDKLxFTAwXYSj1xF3ZPaulO2gejNornLLiW_C3Ru4w-5lqIh1XE2LTOsSKrj7iA" |
| 379 | + } |
| 380 | + |
| 381 | + Dependencies[sut.clientID].pkce.generateCodeChallenge = { _ in |
| 382 | + "hgJeigklONUI1pKSS98MIAbtJGaNu0zJU1iSiFOn2lY" |
| 383 | + } |
| 384 | + |
| 385 | + return sut |
| 386 | + } |
| 387 | +} |
0 commit comments