Skip to content

Commit

Permalink
Add isAuthenticated to HTTPAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
mederic committed Aug 17, 2018
1 parent e320dd1 commit 381a5ae
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 7 deletions.
4 changes: 3 additions & 1 deletion Source/Admin/API/AdminCredential.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public struct AdminCredential: Credential {
public let userId: String
public var authenticationToken: String?

public func authentication() throws -> String? {
func isAuthenticated() -> Bool { return self.authenticationToken != nil }

func authentication() throws -> String? {
guard let authenticationToken = self.authenticationToken else {
throw OMGError.configuration(message: "Authentication token is required")
}
Expand Down
4 changes: 4 additions & 0 deletions Source/Client/API/ClientCredential.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public struct ClientCredential: Credential {
self.authenticationToken = authenticationToken
}

func isAuthenticated() -> Bool {
return self.authenticationToken != nil
}

func authentication() throws -> String? {
guard let authenticationToken = self.authenticationToken else { return nil }
return try CredentialEncoder.encode(value1: self.apiKey, value2: authenticationToken, scheme: "OMGClient")
Expand Down
1 change: 1 addition & 0 deletions Source/Core/API/Credential.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/// Contains the required functions that a configuration object would need to authenticate an API call if needed
protocol Credential {
func isAuthenticated() -> Bool
func authentication() throws -> String?
mutating func update(withAuthenticationToken authenticationToken: AuthenticationToken)
mutating func invalidate()
Expand Down
3 changes: 3 additions & 0 deletions Source/Core/API/HTTPAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public class HTTPAPI {
self.config = config
}

/// A boolean indicating if the client is authenticated and allowed to make authenticated requests
public var isAuthenticated: Bool { return self.config.credentials.isAuthenticated() }

@discardableResult
func request<ResultType>(toEndpoint endpoint: APIEndpoint,
callback: Request<ResultType>.Callback?) -> Request<ResultType>? {
Expand Down
10 changes: 10 additions & 0 deletions Tests/Client/APITests/ClientCredentialTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
import XCTest

class ClientCredentialTests: XCTestCase {
func testIsAuthenticatedIsFalseWhenNoAuthenticationToken() {
let credentials = ClientCredential(apiKey: "api_key")
XCTAssertFalse(credentials.isAuthenticated())
}

func testIsAuthenticatedIsFalseWithAnAuthenticationToken() {
let credentials = ClientCredential(apiKey: "api_key", authenticationToken: "123")
XCTAssertTrue(credentials.isAuthenticated())
}

func testUpdateCredentialSuccessfully() {
var credentials = ClientCredential(apiKey: "api_key")
XCTAssertNil(credentials.authenticationToken)
Expand Down
12 changes: 12 additions & 0 deletions Tests/Core/APITests/HTTPClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@
import XCTest

class HTTPClientTests: XCTestCase {
func testIsAuthenticatedIsTrueWhenAuthenticated() {
let config = TestConfiguration(baseURL: "http://localhost:4000", credentials: TestCredential(authenticated: true))
let client = HTTPAPI(config: config)
XCTAssertTrue(client.isAuthenticated)
}

func testIsAuthenticatedIsFalseWhenNotAuthenticated() {
let config = TestConfiguration(baseURL: "http://localhost:4000", credentials: TestCredential(authenticated: false))
let client = HTTPAPI(config: config)
XCTAssertFalse(client.isAuthenticated)
}

func testInvalidURL() {
let expectation = self.expectation(description: "Invalid url")
let config = TestConfiguration(baseURL: "invalid url @")
Expand Down
4 changes: 2 additions & 2 deletions Tests/Core/APITests/RequestBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import XCTest

class RequestBuilderTests: XCTestCase {
var httpConfig = TestConfiguration(credentials: TestCredential())
var socketConfig = TestConfiguration(credentials: TestCredential())
var httpConfig = TestConfiguration(credentials: TestCredential(authenticated: true))
var socketConfig = TestConfiguration(credentials: TestCredential(authenticated: true))
var requestBuilder: RequestBuilder!

override func setUp() {
Expand Down
2 changes: 1 addition & 1 deletion Tests/Core/TestMocks/TestConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct TestConfiguration: Configuration {

let debugLog: Bool = false

init(baseURL: String = "http://localhost:4000", credentials: TestCredential = TestCredential()) {
init(baseURL: String = "http://localhost:4000", credentials: TestCredential = TestCredential(authenticated: true)) {
self.baseURL = baseURL
self.credentials = credentials
}
Expand Down
20 changes: 17 additions & 3 deletions Tests/Core/TestMocks/TestCredential.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,25 @@
@testable import OmiseGO

struct TestCredential: Credential {
mutating func update(withAuthenticationToken _: AuthenticationToken) {}
var authenticated: Bool

init(authenticated: Bool) {
self.authenticated = authenticated
}

func isAuthenticated() -> Bool {
return self.authenticated
}

mutating func update(withAuthenticationToken _: AuthenticationToken) {
self.authenticated = true
}

func authentication() throws -> String? {
return "OMGClient \("123:123".data(using: .utf8)!.base64EncodedString())"
return self.authenticated ? "OMGClient \("123:123".data(using: .utf8)!.base64EncodedString())" : nil
}

mutating func invalidate() {}
mutating func invalidate() {
self.authenticated = false
}
}

0 comments on commit 381a5ae

Please sign in to comment.