From be9822ae1a1c231c61065f546b69d5529001b2e4 Mon Sep 17 00:00:00 2001 From: Martin Walsh Date: Wed, 10 May 2017 13:16:34 +0100 Subject: [PATCH] Added Tests Add SimpleKeyChain to PodSpec --- Auth0.podspec | 3 +- Auth0.xcodeproj/project.pbxproj | 4 + Auth0Tests/CredentialsManagerSpec.swift | 101 ++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 Auth0Tests/CredentialsManagerSpec.swift diff --git a/Auth0.podspec b/Auth0.podspec index 60f3940bf..fd91a7066 100644 --- a/Auth0.podspec +++ b/Auth0.podspec @@ -23,7 +23,7 @@ Pod::Spec.new do |s| DESC s.homepage = 'https://github.com/auth0/Auth0.swift' s.license = 'MIT' - s.author = { 'Auth0' => 'oss@auth0.com', 'Hernan Zalazar' => 'hernan@auth0.com' } + s.authors = { "Auth0" => "support@auth0.com" }, { "Hernan Zalazar" => "hernan@auth0.com" }, { "Martin Walsh" => "martin.walsh@auth0.com" } s.source = { :git => 'https://github.com/auth0/Auth0.swift.git', :tag => s.version.to_s } s.social_media_url = 'https://twitter.com/auth0' @@ -35,6 +35,7 @@ Pod::Spec.new do |s| s.ios.source_files = 'Auth0/*.{swift,h,m}' s.ios.frameworks = 'UIKit', 'SafariServices' + s.ios.dependency 'SimpleKeychain' s.osx.source_files = 'Auth0/*.swift' s.osx.exclude_files = web_auth_files s.watchos.source_files = 'Auth0/*.swift' diff --git a/Auth0.xcodeproj/project.pbxproj b/Auth0.xcodeproj/project.pbxproj index 00e27ef72..f13c1ef69 100644 --- a/Auth0.xcodeproj/project.pbxproj +++ b/Auth0.xcodeproj/project.pbxproj @@ -15,6 +15,7 @@ 5BEDE15C1EC202960007300D /* SimpleKeychain.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5BEDE15B1EC202960007300D /* SimpleKeychain.framework */; }; 5BEDE17F1EC2138C0007300D /* SimpleKeychain.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5BEDE15B1EC202960007300D /* SimpleKeychain.framework */; }; 5BEDE18A1EC21B040007300D /* CredentialsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BEDE1891EC21B040007300D /* CredentialsManager.swift */; }; + 5BEDE1951EC333380007300D /* CredentialsManagerSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BEDE1931EC3331A0007300D /* CredentialsManagerSpec.swift */; }; 5F06DDA51CC451540011842B /* Auth0.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5F06DD781CC448B10011842B /* Auth0.framework */; }; 5F06DDB41CC451700011842B /* Auth0.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5F06DD851CC448C90011842B /* Auth0.framework */; }; 5F06DDC91CC66B710011842B /* Auth0.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F06DDC81CC66B710011842B /* Auth0.swift */; }; @@ -299,6 +300,7 @@ 5BD4A9CD1DEC6EFA00D6D7AE /* ResponseType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ResponseType.swift; path = Auth0/ResponseType.swift; sourceTree = SOURCE_ROOT; }; 5BEDE15B1EC202960007300D /* SimpleKeychain.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SimpleKeychain.framework; path = Carthage/Build/iOS/SimpleKeychain.framework; sourceTree = ""; }; 5BEDE1891EC21B040007300D /* CredentialsManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CredentialsManager.swift; sourceTree = ""; }; + 5BEDE1931EC3331A0007300D /* CredentialsManagerSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = CredentialsManagerSpec.swift; path = Auth0Tests/CredentialsManagerSpec.swift; sourceTree = SOURCE_ROOT; }; 5F049B6C1CB42C29006F6C05 /* Auth0.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = Auth0.h; path = Auth0/Auth0.h; sourceTree = ""; }; 5F049B6E1CB42C29006F6C05 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = Info.plist; path = Auth0/Info.plist; sourceTree = ""; }; 5F06DD781CC448B10011842B /* Auth0.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Auth0.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -696,6 +698,7 @@ 5FBBF0331CC95FA40024D2AF /* Utils */ = { isa = PBXGroup; children = ( + 5BEDE1931EC3331A0007300D /* CredentialsManagerSpec.swift */, 5FBBF0371CC964BC0024D2AF /* Matchers.swift */, 5FBBF03A1CC96AA70024D2AF /* Responses.swift */, ); @@ -1363,6 +1366,7 @@ 5F28B4671D8300D50000EB23 /* LoggerSpec.swift in Sources */, 5FBBF0431CCA90300024D2AF /* AuthenticationSpec.swift in Sources */, 5FCAB16B1D07AC3500331C84 /* OAuth2GrantSpec.swift in Sources */, + 5BEDE1951EC333380007300D /* CredentialsManagerSpec.swift in Sources */, 5FD255B11D14A9E000387ECB /* AuthenticationErrorSpec.swift in Sources */, 5FCAB16C1D07AC3500331C84 /* SafariSessionSpec.swift in Sources */, 5FE2F8C61CD1522F003628F4 /* ProfileSpec.swift in Sources */, diff --git a/Auth0Tests/CredentialsManagerSpec.swift b/Auth0Tests/CredentialsManagerSpec.swift new file mode 100644 index 000000000..7b55a57ef --- /dev/null +++ b/Auth0Tests/CredentialsManagerSpec.swift @@ -0,0 +1,101 @@ +// CredentialsManagerSpec.swift +// +// Copyright (c) 2016 Auth0 (http://auth0.com) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +import Quick +import Nimble +import OHHTTPStubs +import SimpleKeychain + +@testable import Auth0 + +private let AccessToken = UUID().uuidString.replacingOccurrences(of: "-", with: "") +private let TokenType = "bearer" +private let IdToken = UUID().uuidString.replacingOccurrences(of: "-", with: "") +private let RefreshToken = UUID().uuidString.replacingOccurrences(of: "-", with: "") +private let ExpiresIn: TimeInterval = 3600 +private let ClientId = "CLIENT_ID" +private let Domain = "samples.auth0.com" + +class CredentialsManagerSpec: QuickSpec { + + override func spec() { + + let authentication = Auth0.authentication(clientId: ClientId, domain: Domain) + var credentialsManager: CredentialsManager! + var storage: A0SimpleKeychain! + var credentials: Credentials! + + beforeEach { + storage = A0SimpleKeychain() + credentialsManager = CredentialsManager(storage: storage, authentication: authentication, storeKey: "credentials") + credentials = Credentials(accessToken: AccessToken, tokenType: TokenType, idToken: IdToken, refreshToken: RefreshToken, expiresIn: Date(timeIntervalSinceNow: ExpiresIn)) + } + + describe("storage") { + + it("should not retrieve credentials with no keychain entry") { + expect(credentialsManager.retrieve()).to(beNil()) + } + + it("should store credentials in keychain") { + expect(credentialsManager.store(credentials)).to(beTrue()) + } + + it("should retrieve stored credentials from keychain") { + expect(credentialsManager.retrieve()).toNot(beNil()) + storage.clearAll() + } + + } + + describe("renewal") { + + var error: Error? + var newCredentials: Credentials? + + beforeEach { + error = nil + newCredentials = nil + stub(condition: isToken(Domain) && hasAtLeast(["refresh_token": RefreshToken])) { _ in return authResponse(accessToken: AccessToken) }.name = "refresh_token login" + } + + it("should error when no refresh_token present") { + credentials = Credentials(accessToken: AccessToken, tokenType: TokenType, idToken: IdToken, refreshToken: nil, expiresIn: Date(timeIntervalSinceNow: ExpiresIn)) + credentialsManager.renew(credentials) { error = $0; newCredentials = $1 } + expect(error).toEventuallyNot(beNil()) + expect(newCredentials).toEventually(beNil()) + } + + it("should yield new credentials") { + waitUntil(timeout: 2) { done in + credentialsManager.renew(credentials) { error = $0; newCredentials = $1 + expect(error).to(beNil()) + expect(newCredentials?.accessToken) == AccessToken + done() + } + } + } + + } + } +} +