Skip to content

Commit

Permalink
KM-4542: Update error thrown from failed login request (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
kp-laura-sempere authored Jul 8, 2024
1 parent 5ee445a commit 6b359cd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Sources/PIALibrary/Account/DefaultAccountProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ open class DefaultAccountProvider: AccountProvider, ConfigurationAccess, Databas

loginUseCase.login(with: credentials) { error in
DispatchQueue.main.async {
self.handleLoginResult(error: error, credentials: credentials, notificationToSend: notificationToSend, callback: callback)
self.handleLoginResult(error: error?.asClientError(), credentials: credentials, notificationToSend: notificationToSend, callback: callback)
}
}
}
Expand Down
17 changes: 11 additions & 6 deletions Sources/PIALibrary/Account/Domain/UseCases/LoginUseCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ private extension LoginUseCase {

guard let self else { return }

if let error {
completion(error)
if error != nil {

completion(.unauthorized)
} else if let dataResponse {
let shouldSaveToken = configuration.path == .login
self.handleDataResponse(dataResponse, shouldSaveTokenFromResponse: shouldSaveToken, completion: completion)
} else {
completion(NetworkRequestError.allConnectionAttemptsFailed())
completion(.unauthorized)
}
}
}
Expand All @@ -90,7 +91,7 @@ private extension LoginUseCase {

if shouldSaveTokenFromResponse {
guard let dataResponseContent = dataResponse.data else {
completion(NetworkRequestError.noDataContent)
completion(.unauthorized)
return
}
saveAPIToken(from: dataResponseContent, completion: completion)
Expand All @@ -105,11 +106,15 @@ private extension LoginUseCase {
try apiTokenProvider.saveAPIToken(from: data)
// Refresh the Vpn token after successfully login
refreshVpnTokenUseCase() { error in
completion(error)
if error != nil {
completion(.unauthorized)
} else {
completion(nil)
}
}

} catch {
completion(NetworkRequestError.unableToSaveAPIToken)
completion(.unauthorized)
}
}
}
15 changes: 10 additions & 5 deletions Tests/PIALibraryTests/Accounts/LoginUseCaseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ class LoginUseCaseTests: XCTestCase {
// AND the Vpn token is NOT refreshed
XCTAssertEqual(fixture.refreshVpnTokenUseCaseMock.callAsFunctionCalledAttempt, 0)

// AND an error is returned
// AND an 'unauthorized' error is returned
XCTAssertNotNil(capturedError)
XCTAssertEqual(capturedError!, .unauthorized)

}

Expand Down Expand Up @@ -177,8 +178,9 @@ class LoginUseCaseTests: XCTestCase {
// AND the Vpn token is NOT refreshed
XCTAssertEqual(fixture.refreshVpnTokenUseCaseMock.callAsFunctionCalledAttempt, 0)

// AND an error is returned
// AND an 'unauthorized' error is returned
XCTAssertNotNil(capturedError)
XCTAssertEqual(capturedError!, .unauthorized)

}

Expand Down Expand Up @@ -244,8 +246,9 @@ class LoginUseCaseTests: XCTestCase {
// AND the Vpn token is NOT refreshed
XCTAssertEqual(fixture.refreshVpnTokenUseCaseMock.callAsFunctionCalledAttempt, 0)

// AND an error is returned
// AND an 'unauthorized' error is returned
XCTAssertNotNil(capturedError)
XCTAssertEqual(capturedError!, .unauthorized)

}

Expand Down Expand Up @@ -275,8 +278,9 @@ class LoginUseCaseTests: XCTestCase {
// AND the Vpn token is NOT refreshed
XCTAssertEqual(fixture.refreshVpnTokenUseCaseMock.callAsFunctionCalledAttempt, 0)

// AND an error is returned
// AND an 'unauthorized' error is returned
XCTAssertNotNil(capturedError)
XCTAssertEqual(capturedError!, .unauthorized)

}

Expand Down Expand Up @@ -334,8 +338,9 @@ class LoginUseCaseTests: XCTestCase {
XCTAssertEqual(fixture.networkClientMock.executeRequestCalledAttempt, 1)
XCTAssertEqual(fixture.networkClientMock.executeRequestWithConfiguation?.path, RequestAPI.Path.loginLink)

// AND an error is returned
// AND an 'unauthorized' error is returned
XCTAssertNotNil(capturedError)
XCTAssertEqual(capturedError!, .unauthorized)

}

Expand Down

0 comments on commit 6b359cd

Please sign in to comment.