Skip to content

Commit

Permalink
Remove OAuth2Authenticatable defaulting extension
Browse files Browse the repository at this point in the history
Ensure loginHint param always specified
Update Testing
  • Loading branch information
cocojoe committed Mar 20, 2017
1 parent e95f84e commit 26494e3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Lock/AuthPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class AuthPresenter: Presentable, Loggable {

private func newView(withInsets insets: UIEdgeInsets, mode: AuthCollectionView.Mode) -> AuthCollectionView {
let view = AuthCollectionView(connections: self.connections, mode: mode, insets: insets, customStyle: self.customStyle) { name in
self.interactor.login(name) { error in
self.interactor.login(name, loginHint: nil) { error in
guard let error = error else { return }
self.messagePresenter?.showError(error)
}
Expand Down
6 changes: 0 additions & 6 deletions Lock/OAuth2Authenticatable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ protocol OAuth2Authenticatable {
func login(_ connection: String, loginHint: String?, callback: @escaping (OAuth2AuthenticatableError?) -> Void)
}

extension OAuth2Authenticatable {
func login(_ connection: String, loginHint: String? = nil, callback: @escaping (OAuth2AuthenticatableError?) -> Void) {
self.login(connection, loginHint: loginHint, callback: callback)
}
}

enum OAuth2AuthenticatableError: Error, LocalizableError {
case noConnectionAvailable
case couldNotAuthenticate
Expand Down
28 changes: 14 additions & 14 deletions LockTests/Interactors/Auth0OAuth2InteractorSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,76 +69,76 @@ class Auth0OAuth2InteractorSpec: QuickSpec {
}

it("should set connection") {
interactor.login("facebook", callback: { _ in })
interactor.login("facebook", loginHint: nil, callback: { _ in })
expect(authentication.webAuth.connection) == "facebook"
}

it("should set scope") {
interactor.login("facebook", callback: { _ in })
interactor.login("facebook", loginHint: nil, callback: { _ in })
expect(authentication.webAuth.scope) == "openid"
}

it("should set connection scope for specified connection") {
options.connectionScope = ["facebook": "user_friends,email"]
interactor = Auth0OAuth2Interactor(authentication: authentication, dispatcher: dispatcher, options: options, nativeHandlers: nativeHandlers)
interactor.login("facebook", callback: { _ in })
interactor.login("facebook", loginHint: nil, callback: { _ in })
expect(authentication.webAuth.parameters["connection_scope"]) == "user_friends,email"
}

it("should set connection scope for matching connections only") {
options.connectionScope = ["facebook": "user_friends,email",
"google-oauth2": "gmail,ads"]
interactor = Auth0OAuth2Interactor(authentication: authentication, dispatcher: dispatcher, options: options, nativeHandlers: nativeHandlers)
interactor.login("facebook", callback: { _ in })
interactor.login("facebook", loginHint: nil, callback: { _ in })
expect(authentication.webAuth.parameters["connection_scope"]) == "user_friends,email"
interactor.login("google-oauth2", callback: { _ in })
interactor.login("google-oauth2", loginHint: nil, callback: { _ in })
expect(authentication.webAuth.parameters["connection_scope"]) == "gmail,ads"
}

it("should not set audience if nil") {
options.audience = nil
interactor = Auth0OAuth2Interactor(authentication: authentication, dispatcher: dispatcher, options: options, nativeHandlers: nativeHandlers)
interactor.login("facebook", callback: { _ in })
interactor.login("facebook", loginHint: nil, callback: { _ in })
expect(authentication.webAuth.audience).to(beNil())
}

it("should set audience") {
options.audience = "https://myapi.com/v1"
interactor = Auth0OAuth2Interactor(authentication: authentication, dispatcher: dispatcher, options: options, nativeHandlers: nativeHandlers)
interactor.login("facebook", callback: { _ in })
interactor.login("facebook", loginHint: nil, callback: { _ in })
expect(authentication.webAuth.audience) == "https://myapi.com/v1"
}

it("should set parameters") {
let state = UUID().uuidString
options.parameters = ["state": state as Any]
interactor = Auth0OAuth2Interactor(authentication: authentication, dispatcher: dispatcher, options: options, nativeHandlers: nativeHandlers)
interactor.login("facebook", callback: { _ in })
interactor.login("facebook", loginHint: nil, callback: { _ in })
expect(authentication.webAuth.parameters["state"]) == state
}

it("should not yield error on success") {
authentication.webAuthResult = { return .success(result: mockCredentials()) }
interactor.login("facebook") { error = $0 }
interactor.login("facebook", loginHint: nil) { error = $0 }
expect(error).toEventually(beNil())
}

it("should call credentials callback") {
let expected = mockCredentials()
authentication.webAuthResult = { return .success(result: expected) }
interactor.login("facebook") { error = $0 }
interactor.login("facebook", loginHint: nil) { error = $0 }
expect(credentials).toEventually(equal(expected))
}

it("should handle cancel error") {
authentication.webAuthResult = { return .failure(error: WebAuthError.userCancelled) }
interactor.login("facebook") { error = $0 }
interactor.login("facebook", loginHint: nil) { error = $0 }
expect(error).toEventually(equal(OAuth2AuthenticatableError.cancelled))
}

it("should handle generic error") {
authentication.webAuthResult = { return .failure(error: WebAuthError.noBundleIdentifierFound) }
interactor.login("facebook") { error = $0 }
interactor.login("facebook", loginHint: nil) { error = $0 }
expect(error).toEventually(equal(OAuth2AuthenticatableError.couldNotAuthenticate))
}

Expand All @@ -159,7 +159,7 @@ class Auth0OAuth2InteractorSpec: QuickSpec {
it("should yield no error on success") {
stub(condition: isOAuthAccessToken(domain) && hasAtLeast(["access_token": "SocialToken", "connection": "facebook"])) { _ in return Auth0Stubs.authentication() }.name = "Social Auth"
waitUntil(timeout: Timeout) { done in
interactor.login("facebook") { error in
interactor.login("facebook", loginHint: nil) { error in
expect(error).to(beNil())
done()
}
Expand All @@ -170,7 +170,7 @@ class Auth0OAuth2InteractorSpec: QuickSpec {
it("should yield error on failure") {
stub(condition: isOAuthAccessToken(domain) && hasAtLeast(["access_token": "SocialToken", "connection": "facebook"])) { _ in return Auth0Stubs.failure() }.name = "Social Auth Fail"
waitUntil(timeout: Timeout) { done in
interactor.login("facebook") { error in
interactor.login("facebook", loginHint: nil) { error in
expect(error).toNot(beNil())
done()
}
Expand Down
2 changes: 1 addition & 1 deletion LockTests/Utils/Mocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class MockMultifactorInteractor: MultifactorAuthenticatable {
}

class MockAuthInteractor: OAuth2Authenticatable {
func login(_ connection: String, parameters: [String: String], callback: @escaping (OAuth2AuthenticatableError?) -> ()) {
func login(_ connection: String, loginHint: String? = nil, callback: @escaping (OAuth2AuthenticatableError?) -> ()) {
}
}

Expand Down

0 comments on commit 26494e3

Please sign in to comment.