Skip to content

Commit

Permalink
Added Tests
Browse files Browse the repository at this point in the history
Add SimpleKeyChain to PodSpec
  • Loading branch information
cocojoe committed May 10, 2017
1 parent 8513718 commit be9822a
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Auth0.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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'
Expand Down
4 changes: 4 additions & 0 deletions Auth0.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -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 = "<group>"; };
5BEDE1891EC21B040007300D /* CredentialsManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CredentialsManager.swift; sourceTree = "<group>"; };
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 = "<group>"; };
5F049B6E1CB42C29006F6C05 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = Info.plist; path = Auth0/Info.plist; sourceTree = "<group>"; };
5F06DD781CC448B10011842B /* Auth0.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Auth0.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -696,6 +698,7 @@
5FBBF0331CC95FA40024D2AF /* Utils */ = {
isa = PBXGroup;
children = (
5BEDE1931EC3331A0007300D /* CredentialsManagerSpec.swift */,
5FBBF0371CC964BC0024D2AF /* Matchers.swift */,
5FBBF03A1CC96AA70024D2AF /* Responses.swift */,
);
Expand Down Expand Up @@ -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 */,
Expand Down
101 changes: 101 additions & 0 deletions Auth0Tests/CredentialsManagerSpec.swift
Original file line number Diff line number Diff line change
@@ -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()
}
}
}

}
}
}

0 comments on commit be9822a

Please sign in to comment.