-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: have
AuthenticationAPI
depend on NetworkService
directly - …
…WPB-16179 (#2556)
- Loading branch information
1 parent
6b15af1
commit b016ab9
Showing
7 changed files
with
270 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,13 +23,13 @@ import XCTest | |
|
||
final class AuthenticationAPITests: XCTestCase { | ||
|
||
private var apiSnapshotHelper: APIServiceSnapshotHelper<any AuthenticationAPI>! | ||
private var apiSnapshotHelper: NetworkServiceSnapshotHelper<any AuthenticationAPI>! | ||
|
||
// MARK: - Setup | ||
|
||
override func setUp() { | ||
apiSnapshotHelper = APIServiceSnapshotHelper<any AuthenticationAPI> { apiService, apiVersion in | ||
AuthenticationAPIBuilder(apiService: apiService) | ||
apiSnapshotHelper = NetworkServiceSnapshotHelper<any AuthenticationAPI> { networkService, apiVersion in | ||
AuthenticationAPIBuilder(networkService: networkService) | ||
.makeAPI(for: apiVersion) | ||
} | ||
} | ||
|
@@ -42,8 +42,8 @@ final class AuthenticationAPITests: XCTestCase { | |
|
||
func testGetDomainRegistration_V0_To_V7() async throws { | ||
// Given | ||
let apiService = MockAPIServiceProtocol() | ||
let builder = AuthenticationAPIBuilder(apiService: apiService) | ||
let networkService = MockNetworkServiceProtocol() | ||
let builder = AuthenticationAPIBuilder(networkService: networkService) | ||
|
||
for apiVersion in [APIVersion.v0, .v1, .v2, .v3, .v4, .v5, .v6, .v7] { | ||
let sut = builder.makeAPI(for: apiVersion) | ||
|
@@ -93,11 +93,11 @@ final class AuthenticationAPITests: XCTestCase { | |
|
||
func testGetDomainRegistration_Response_Handling_V8_Success() async throws { | ||
// Given | ||
let apiService = MockAPIServiceProtocol.withResponses([ | ||
let networkService = MockNetworkServiceProtocol.withResponses([ | ||
(.ok, "GetDomainRegistrationSuccessResponseV8") | ||
]) | ||
|
||
let sut = AuthenticationAPIV8(apiService: apiService) | ||
let sut = AuthenticationAPIV8(networkService: networkService) | ||
|
||
// When | ||
let response = try await sut.getDomainRegistration(forEmail: "[email protected]") | ||
|
@@ -116,11 +116,11 @@ final class AuthenticationAPITests: XCTestCase { | |
|
||
func testGetDomainRegistration_ResponseWithNullValues_Handling_V8_Success() async throws { | ||
// Given | ||
let apiService = MockAPIServiceProtocol.withResponses([ | ||
let networkService = MockNetworkServiceProtocol.withResponses([ | ||
(.ok, "GetDomainRegistrationSuccessResponse_WithNullValuesV8") | ||
]) | ||
|
||
let sut = AuthenticationAPIV8(apiService: apiService) | ||
let sut = AuthenticationAPIV8(networkService: networkService) | ||
|
||
// When | ||
let response = try await sut.getDomainRegistration(forEmail: "[email protected]") | ||
|
@@ -139,11 +139,11 @@ final class AuthenticationAPITests: XCTestCase { | |
|
||
func testGetDomainRegistration_Response_Handling_V8_Invalid_Domain() async throws { | ||
// Given | ||
let apiService = MockAPIServiceProtocol.withResponses([ | ||
let networkService = MockNetworkServiceProtocol.withResponses([ | ||
(.badRequest, "GetDomainRegistrationErrorResponse_InvalidDomainV8") | ||
]) | ||
|
||
let sut = AuthenticationAPIV8(apiService: apiService) | ||
let sut = AuthenticationAPIV8(networkService: networkService) | ||
|
||
// Then | ||
await XCTAssertThrowsErrorAsync(AuthenticationAPIError.invalidDomain) { | ||
|
@@ -154,11 +154,11 @@ final class AuthenticationAPITests: XCTestCase { | |
|
||
func testGetOnPremConfigURL_Response_Handling_Success() async throws { | ||
// Given | ||
let apiService = MockAPIServiceProtocol.withResponses([ | ||
let networkService = MockNetworkServiceProtocol.withResponses([ | ||
(.ok, "GetOnPremConfigURLSuccessResponseV0") | ||
]) | ||
|
||
let sut = AuthenticationAPIV8(apiService: apiService) | ||
let sut = AuthenticationAPIV8(networkService: networkService) | ||
|
||
// When | ||
let response = try await sut.getOnPremConfigURL(forDomain: "example.com") | ||
|
@@ -172,11 +172,11 @@ final class AuthenticationAPITests: XCTestCase { | |
|
||
func testGetOnPremConfigURL_Response_Handling_Custom_Backend_Not_Found() async throws { | ||
// Given | ||
let apiService = MockAPIServiceProtocol.withResponses([ | ||
let networkService = MockNetworkServiceProtocol.withResponses([ | ||
(.notFound, "GetOnPremConfigURLErrorResponse_CustomBackendNotFound_V0") | ||
]) | ||
|
||
let sut = AuthenticationAPIV8(apiService: apiService) | ||
let sut = AuthenticationAPIV8(networkService: networkService) | ||
|
||
// Then | ||
await XCTAssertThrowsErrorAsync(AuthenticationAPIError.configNotFound) { | ||
|
@@ -187,11 +187,11 @@ final class AuthenticationAPITests: XCTestCase { | |
|
||
func testLoginViaEmail_Response_Handling_Success() async throws { | ||
// Given | ||
let apiService = MockAPIServiceProtocol.withResponses([ | ||
let networkService = MockNetworkServiceProtocol.withResponses([ | ||
(.ok, "LoginViaEmailSuccessResponseV0") | ||
]) | ||
|
||
let sut = AuthenticationAPIV8(apiService: apiService) | ||
let sut = AuthenticationAPIV8(networkService: networkService) | ||
|
||
// When | ||
let response = try await sut.login( | ||
|
@@ -219,11 +219,11 @@ final class AuthenticationAPITests: XCTestCase { | |
|
||
func testLoginViaEmail_Response_Handling_Custom_Backend_Not_Found() async throws { | ||
// Given | ||
let apiService = MockAPIServiceProtocol.withResponses([ | ||
let networkService = MockNetworkServiceProtocol.withResponses([ | ||
(.notFound, "LoginViaEmailErrorResponse_CodeAuthenticationRequired_V0") | ||
]) | ||
|
||
let sut = AuthenticationAPIV8(apiService: apiService) | ||
let sut = AuthenticationAPIV8(networkService: networkService) | ||
|
||
// Then | ||
await XCTAssertThrowsErrorAsync(AuthenticationAPIError.twoFactorAuthenticationRequired) { | ||
|
70 changes: 70 additions & 0 deletions
70
WireAPI/Tests/WireAPITests/Helpers/MockNetworkServiceProtocol+Helpers.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// | ||
// Wire | ||
// Copyright (C) 2025 Wire Swiss GmbH | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see http://www.gnu.org/licenses/. | ||
// | ||
|
||
import Foundation | ||
|
||
@testable import WireAPI | ||
@testable import WireAPISupport | ||
|
||
extension MockNetworkServiceProtocol { | ||
|
||
typealias Response = (statusCode: HTTPStatusCode, resourceName: String?) | ||
|
||
/// Create a mock network service that returns zero or more responses. | ||
/// | ||
/// Some ways you can use this: | ||
/// - Mock a series of paged responses | ||
/// - Mock a series or responses for repeated requests | ||
/// - Mock a single response | ||
/// | ||
/// - Parameter responses: The responses to return, one per request received. | ||
/// - Returns: A mock network service. | ||
|
||
static func withResponses(_ responses: [Response]) -> MockNetworkServiceProtocol { | ||
let networkService = MockNetworkServiceProtocol() | ||
var responses = responses | ||
|
||
networkService.executeRequest_MockMethod = { request in | ||
guard !responses.isEmpty else { | ||
throw "no response" | ||
} | ||
|
||
let response = responses.removeFirst() | ||
|
||
return try request.mockResponse( | ||
statusCode: response.statusCode, | ||
jsonResourceName: response.resourceName | ||
) | ||
} | ||
|
||
return networkService | ||
} | ||
|
||
static func withError(statusCode: HTTPStatusCode, label: String = "") -> MockNetworkServiceProtocol { | ||
let networkService = MockNetworkServiceProtocol() | ||
networkService.executeRequest_MockMethod = { request in | ||
try request.mockErrorResponse( | ||
statusCode: statusCode, | ||
label: label | ||
) | ||
} | ||
|
||
return networkService | ||
} | ||
|
||
} |
Oops, something went wrong.