Skip to content

Commit

Permalink
test: test previously ignored errors
Browse files Browse the repository at this point in the history
Update tests and mocks to account for failures from previously ignored
errors
  • Loading branch information
flexjdev committed Feb 1, 2024
1 parent 9be957d commit 4966b17
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ import ProtonCoreNetworking
@testable import ProtonVPN

fileprivate let testData = MockTestData()
fileprivate let testAuthCredentials = AuthCredentials(username: "username", accessToken: "", refreshToken: "", sessionId: "", userId: "", scopes: [])
fileprivate let testVPNCredentials = VpnKeychainMock.vpnCredentials(accountPlan: .plus, maxTier: CoreAppConstants.VpnTiers.plus)
fileprivate func mockAuthCredentials(username: String) -> AuthCredentials {
return AuthCredentials(username: username, accessToken: "", refreshToken: "", sessionId: "", userId: "", scopes: [])
}
fileprivate var testAuthCredentials: AuthCredentials = mockAuthCredentials(username: "username")

fileprivate let subuserWithoutSessionsResponseError = ResponseError(httpCode: HttpStatusCode.accessForbidden,
responseCode: ApiErrorCode.subuserWithoutSessions,
Expand All @@ -49,36 +51,34 @@ final class AppSessionManagerImplementationTests: XCTestCase {

let asyncTimeout: TimeInterval = 5

var mockVPNAPIService: VpnApiService {
networking = NetworkingMock()
networkingDelegate = FullNetworkingMockDelegate()
networking.delegate = networkingDelegate

return VpnApiService(
networking: networking,
vpnKeychain: VpnKeychainMock(),
countryCodeProvider: CountryCodeProviderImplementation(),
authKeychain: authKeychain
)
}

override func setUp() {
super.setUp()
propertiesManager = PropertiesManagerMock()
networking = NetworkingMock()
networkingDelegate = FullNetworkingMockDelegate()
networking.delegate = networkingDelegate
authKeychain = AuthKeychainHandleMock()
unauthKeychain = UnauthKeychainMock()
vpnKeychain = VpnKeychainMock()
alertService = AppSessionManagerAlertServiceMock()
appStateManager = AppStateManagerMock()


networkingDelegate = FullNetworkingMockDelegate()
let freeCreds = VpnKeychainMock.vpnCredentials(accountPlan: .free, maxTier: CoreAppConstants.VpnTiers.free)
networkingDelegate.apiCredentials = freeCreds
networking.delegate = networkingDelegate

let mockAPIService = VpnApiService(
networking: networking,
vpnKeychain: VpnKeychainMock(),
countryCodeProvider: CountryCodeProviderImplementation(),
authKeychain: authKeychain
)

manager = withDependencies {
$0.date = .constant(Date())
} operation: {
let factory = ManagerFactoryMock(
vpnAPIService: mockVPNAPIService,
vpnAPIService: mockAPIService,
authKeychain: authKeychain,
unauthKeychain: unauthKeychain,
vpnKeychain: vpnKeychain,
Expand Down Expand Up @@ -110,7 +110,7 @@ final class AppSessionManagerImplementationTests: XCTestCase {
networkingDelegate.apiClientConfig = testData.defaultClientConfig

manager.finishLogin(
authCredentials: testAuthCredentials,
authCredentials: mockAuthCredentials(username: "bob"),
success: {
loginExpectation.fulfill()
XCTAssertTrue(self.manager.loggedIn)
Expand All @@ -122,6 +122,8 @@ final class AppSessionManagerImplementationTests: XCTestCase {
)

wait(for: [loginExpectation], timeout: asyncTimeout)

XCTAssertEqual(authKeychain.username, "bob", "Username should have been updated in auth keychain")
}

func testSuccessfulSilentLoginLogsIn() throws {
Expand Down Expand Up @@ -220,9 +222,6 @@ final class AppSessionManagerImplementationTests: XCTestCase {
appStateManager.state = .connected(differentUserServerDescriptor)
networkingDelegate.apiVpnLocation = .mock
networkingDelegate.apiClientConfig = testData.defaultClientConfig
let freeCreds = VpnKeychainMock.vpnCredentials(accountPlan: .free,
maxTier: CoreAppConstants.VpnTiers.free)
networkingDelegate.apiCredentials = freeCreds
authKeychain.credentials = testAuthCredentials
alertService.addAlertHandler(for: ActiveSessionWarningAlert.self, handler: { alert in
activeSessionAlertExpectation.fulfill()
Expand All @@ -246,9 +245,6 @@ final class AppSessionManagerImplementationTests: XCTestCase {
appStateManager.state = .connected(differentUserServerDescriptor)
networkingDelegate.apiVpnLocation = .mock
networkingDelegate.apiClientConfig = testData.defaultClientConfig
let freeCreds = VpnKeychainMock.vpnCredentials(accountPlan: .free,
maxTier: CoreAppConstants.VpnTiers.free)
networkingDelegate.apiCredentials = freeCreds
authKeychain.credentials = testAuthCredentials
alertService.addAlertHandler(for: ActiveSessionWarningAlert.self, handler: { alert in
activeSessionAlertExpectation.fulfill()
Expand Down Expand Up @@ -451,8 +447,12 @@ class AuthKeychainHandleMock: AuthKeychainHandle {
var username: String?
var userId: String?

func saveToCache(_ credentials: VPNShared.AuthCredentials?) { }
func store(_ credentials: VPNShared.AuthCredentials, forContext: VPNShared.AppContext?) throws { }
func saveToCache(_ credentials: VPNShared.AuthCredentials?) {
self.credentials = credentials
}
func store(_ credentials: VPNShared.AuthCredentials, forContext: VPNShared.AppContext?) throws {
self.credentials = credentials
}
func fetch(forContext: AppContext?) -> AuthCredentials? { return credentials }
func clear() { }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import ProtonCoreFoundations
import VPNShared
import ProtonCoreEnvironment

import XCTestDynamicOverlay

public final class NetworkingMock {
public weak var delegate: NetworkingMockDelegate?

Expand Down Expand Up @@ -229,7 +231,7 @@ public class FullNetworkingMockDelegate: NetworkingMockDelegate {
do {
return try handleMockNetworkingRequestThrowingOnUnexpectedError(request)
} catch {
assertionFailure("Unexpected error occurred: \(error)")
XCTFail("Unexpected error occurred: \(error)")
return .failure(error)
}
}
Expand All @@ -248,16 +250,17 @@ public class FullNetworkingMockDelegate: NetworkingMockDelegate {
defer { didHitRoute?(route) }
switch route {
case .vpn:
if let apiCredentialsResponseError {
return .failure(apiCredentialsResponseError)
}
// for fetching client credentials
guard let apiCredentials = apiCredentials else {
guard let apiCredentialsResponseError else {
return .failure(ResponseError(httpCode: HttpStatusCode.badRequest,
responseCode: 2000,
userFacingMessage: nil,
underlyingError: nil)
)
}
return .failure(apiCredentialsResponseError)
return .failure(ResponseError(
httpCode: HttpStatusCode.badRequest,
responseCode: 2000,
userFacingMessage: nil,
underlyingError: nil
))
}

let data = try JSONSerialization.data(withJSONObject: apiCredentials.asDict)
Expand Down

0 comments on commit 4966b17

Please sign in to comment.