From 52ccc59377ea82c40a1c5391c7b28dacdeb7f94e Mon Sep 17 00:00:00 2001 From: Martin Walsh Date: Wed, 17 May 2017 09:58:55 +0100 Subject: [PATCH] Streamline CredentialManagerErrors Update Tests --- Auth0/CredentialsManager.swift | 10 +++++----- Auth0/CredentialsManagerError.swift | 3 +-- Auth0Tests/CredentialsManagerSpec.swift | 21 +++++++++++++++++---- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/Auth0/CredentialsManager.swift b/Auth0/CredentialsManager.swift index d674354ed..7971103c2 100644 --- a/Auth0/CredentialsManager.swift +++ b/Auth0/CredentialsManager.swift @@ -46,7 +46,7 @@ public struct CredentialsManager { return self.storage.setData(NSKeyedArchiver.archivedData(withRootObject: credentials), forKey: storeKey) } - /// Retrieve credentials from keychain and yield new credentials if the accessToken has expired + /// Retrieve credentials from keychain and yield new credentials using refreshToken if accessToken has expired /// otherwise the retrieved credentails will be returned as they have not expired. /// /// - seeAlso: [Auth0 Refresh Tokens Docs](https://auth0.com/docs/tokens/refresh-token) @@ -67,16 +67,16 @@ public struct CredentialsManager { let data = self.storage.data(forKey:self.storeKey), let credentials = NSKeyedUnarchiver.unarchiveObject(with: data) as? Credentials else { return callback(.noCredentials, nil) } - guard let refreshToken = credentials.refreshToken else { return callback(.noRefreshToken, nil) } - guard let expiresIn = credentials.expiresIn else { return callback(.noExpiresIn, nil) } + guard let expiresIn = credentials.expiresIn else { return callback(.failedRefresh, nil) } guard expiresIn < Date() else { return callback(nil, credentials) } + guard let refreshToken = credentials.refreshToken else { return callback(.noRefreshToken, nil) } self.authentication.renew(withRefreshToken: refreshToken, scope: scope).start { switch $0 { case .success(let credentials): callback(nil, credentials) - case .failure(let error): - callback(.renewFailed(error), nil) + case .failure(_): + callback(.failedRefresh, nil) } } } diff --git a/Auth0/CredentialsManagerError.swift b/Auth0/CredentialsManagerError.swift index 0576d6be9..a1a40e065 100644 --- a/Auth0/CredentialsManagerError.swift +++ b/Auth0/CredentialsManagerError.swift @@ -25,6 +25,5 @@ import Foundation public enum CredentialsManagerError: Error { case noCredentials case noRefreshToken - case noExpiresIn - case renewFailed(Error) + case failedRefresh } diff --git a/Auth0Tests/CredentialsManagerSpec.swift b/Auth0Tests/CredentialsManagerSpec.swift index 209670e39..acf515bcd 100644 --- a/Auth0Tests/CredentialsManagerSpec.swift +++ b/Auth0Tests/CredentialsManagerSpec.swift @@ -64,7 +64,7 @@ class CredentialsManagerSpec: QuickSpec { beforeEach { error = nil newCredentials = nil - stub(condition: isToken(Domain) && hasAtLeast(["refresh_token": RefreshToken])) { _ in return authResponse(accessToken: AccessToken) }.name = "refresh_token login" + stub(condition: isToken(Domain) && hasAtLeast(["refresh_token": RefreshToken])) { _ in return authResponse(accessToken: AccessToken) }.name = "renew success" } afterEach { @@ -78,8 +78,8 @@ class CredentialsManagerSpec: QuickSpec { expect(newCredentials).toEventually(beNil()) } - it("should error when no refresh_token present") { - credentials = Credentials(accessToken: AccessToken, tokenType: TokenType, idToken: IdToken, refreshToken: nil, expiresIn: Date(timeIntervalSinceNow: ExpiresIn)) + it("should error when no refresh_token present and token expired") { + credentials = Credentials(accessToken: AccessToken, tokenType: TokenType, idToken: IdToken, refreshToken: nil, expiresIn: Date(timeIntervalSinceNow: -ExpiresIn)) _ = credentialsManager.store(credentials: credentials) credentialsManager.credentials { error = $0; newCredentials = $1 } expect(error).to(matchError(CredentialsManagerError.noRefreshToken)) @@ -90,7 +90,7 @@ class CredentialsManagerSpec: QuickSpec { credentials = Credentials(accessToken: AccessToken, tokenType: TokenType, idToken: IdToken, refreshToken: RefreshToken, expiresIn: nil) _ = credentialsManager.store(credentials: credentials) credentialsManager.credentials { error = $0; newCredentials = $1 } - expect(error).to(matchError(CredentialsManagerError.noExpiresIn)) + expect(error).to(matchError(CredentialsManagerError.failedRefresh)) expect(newCredentials).toEventually(beNil()) } @@ -112,6 +112,19 @@ class CredentialsManagerSpec: QuickSpec { } } } + + it("should yield error on failed renew") { + stub(condition: isToken(Domain) && hasAtLeast(["refresh_token": RefreshToken])) { _ in return authFailure(code: "invalid_request", description: "missing_params") }.name = "renew failed" + credentials = Credentials(accessToken: AccessToken, tokenType: TokenType, idToken: IdToken, refreshToken: RefreshToken, expiresIn: Date(timeIntervalSinceNow: -3600)) + _ = credentialsManager.store(credentials: credentials) + waitUntil(timeout: 2) { done in + credentialsManager.credentials { error = $0; newCredentials = $1 + expect(error).to(matchError(CredentialsManagerError.failedRefresh)) + expect(newCredentials).to(beNil()) + done() + } + } + } } }