Skip to content

Commit

Permalink
Add tests for global functions (#559)
Browse files Browse the repository at this point in the history
  • Loading branch information
Widcket authored Dec 1, 2021
1 parent c08e936 commit 6c4dbeb
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 69 deletions.
52 changes: 52 additions & 0 deletions Auth0/Auth0.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,58 @@ public func users(token: String, domain: String, session: URLSession = .shared)
return Management(token: token, url: .httpsURL(from: domain), session: session)
}

#if WEB_AUTH_PLATFORM
/**
Auth0 component for authenticating with web-based flow

```
Auth0.webAuth()
```

Auth0 domain is loaded from the file `Auth0.plist` in your main bundle with the following content:

```
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>ClientId</key>
<string>{YOUR_CLIENT_ID}</string>
<key>Domain</key>
<string>{YOUR_DOMAIN}</string>
</dict>
</plist>
```

- parameter session: instance of URLSession used for networking. By default it will use the shared URLSession
- parameter bundle: bundle used to locate the `Auth0.plist` file. By default is the main bundle

- returns: Auth0 WebAuth component
- important: Calling this method without a valid `Auth0.plist` will crash your application
*/
public func webAuth(session: URLSession = .shared, bundle: Bundle = Bundle.main) -> WebAuth {
let values = plistValues(bundle: bundle)!
return webAuth(clientId: values.clientId, domain: values.domain, session: session)
}

/**
Auth0 component for authenticating with web-based flow

```
Auth0.webAuth(clientId: clientId, domain: "samples.auth0.com")
```

- parameter clientId: Id of your Auth0 client
- parameter domain: name of your Auth0 domain
- parameter session: instance of URLSession used for networking. By default it will use the shared URLSession

- returns: Auth0 WebAuth component
*/
public func webAuth(clientId: String, domain: String, session: URLSession = .shared) -> WebAuth {
return Auth0WebAuth(clientId: clientId, url: .httpsURL(from: domain), session: session)
}
#endif

func plistValues(bundle: Bundle) -> (clientId: String, domain: String)? {
guard
let path = bundle.path(forResource: "Auth0", ofType: "plist"),
Expand Down
50 changes: 0 additions & 50 deletions Auth0/WebAuth.swift
Original file line number Diff line number Diff line change
@@ -1,56 +1,6 @@
#if WEB_AUTH_PLATFORM
import Foundation

/**
Auth0 component for authenticating with web-based flow

```
Auth0.webAuth()
```

Auth0 domain is loaded from the file `Auth0.plist` in your main bundle with the following content:

```
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>ClientId</key>
<string>{YOUR_CLIENT_ID}</string>
<key>Domain</key>
<string>{YOUR_DOMAIN}</string>
</dict>
</plist>
```

- parameter session: instance of URLSession used for networking. By default it will use the shared URLSession
- parameter bundle: bundle used to locate the `Auth0.plist` file. By default is the main bundle

- returns: Auth0 WebAuth component
- important: Calling this method without a valid `Auth0.plist` will crash your application
*/
public func webAuth(session: URLSession = .shared, bundle: Bundle = Bundle.main) -> WebAuth {
let values = plistValues(bundle: bundle)!
return webAuth(clientId: values.clientId, domain: values.domain, session: session)
}

/**
Auth0 component for authenticating with web-based flow

```
Auth0.webAuth(clientId: clientId, domain: "samples.auth0.com")
```

- parameter clientId: Id of your Auth0 client
- parameter domain: name of your Auth0 domain
- parameter session: instance of URLSession used for networking. By default it will use the shared URLSession

- returns: Auth0 WebAuth component
*/
public func webAuth(clientId: String, domain: String, session: URLSession = .shared) -> WebAuth {
return Auth0WebAuth(clientId: clientId, url: .httpsURL(from: domain), session: session)
}

/// WebAuth Authentication using Auth0
public protocol WebAuth: Trackable, Loggable {
var clientId: String { get }
Expand Down
123 changes: 104 additions & 19 deletions Auth0Tests/Auth0Spec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,113 @@ import OHHTTPStubs

private let ClientId = "CLIENT_ID"
private let Domain = "samples.auth0.com"
private let Token = "TOKEN"

class Auth0Spec: QuickSpec {

override func spec() {

describe("global functions") {

context("authentication") {

it("should return authentication client with client id & domain") {
let authentication = Auth0.authentication(clientId: ClientId,
domain: Domain) as! Auth0Authentication
expect(authentication.clientId) == ClientId
expect(authentication.url.absoluteString) == "https://\(Domain)/"
}

it("should return authentication client with client id, domain & session") {
let session = URLSession(configuration: URLSession.shared.configuration)
let authentication = Auth0.authentication(clientId: ClientId,
domain: Domain,
session: session) as! Auth0Authentication
expect(authentication.session).to(be(session))
}

}

context("users") {

it("should return users client with token & domain") {
let users = Auth0.users(token: Token, domain: Domain) as! Management
expect(users.token) == Token
expect(users.url.absoluteString) == "https://\(Domain)/"
}

it("should return users client with token, domain & session") {
let session = URLSession(configuration: URLSession.shared.configuration)
let users = Auth0.users(token: Token,
domain: Domain,
session: session) as! Management
expect(users.session).to(be(session))
}

}

#if WEB_AUTH_PLATFORM
context("web auth") {

it("should return web auth client with client id & domain") {
let webAuth = Auth0.webAuth(clientId: ClientId,
domain: Domain) as! Auth0WebAuth
expect(webAuth.clientId) == ClientId
expect(webAuth.url.absoluteString) == "https://\(Domain)/"
}

it("should return web auth client with client id, domain & session") {
let session = URLSession(configuration: URLSession.shared.configuration)
let webAuth = Auth0.webAuth(clientId: ClientId,
domain: Domain,
session: session) as! Auth0WebAuth
expect(webAuth.session).to(be(session))
}

}
#endif

#if !SWIFT_PACKAGE
describe("plist loading") {

let bundle = Bundle(for: Auth0Spec.self)

it("should return authentication client with bundle") {
let auth = Auth0.authentication(bundle: bundle)
expect(auth.url.absoluteString) == "https://\(Domain)/"
expect(auth.clientId) == ClientId
}

it("should return authentication client with bundle & session") {
let session = URLSession(configuration: URLSession.shared.configuration)
let authentication = Auth0.authentication(session: session, bundle: bundle) as! Auth0Authentication
expect(authentication.session).to(be(session))
}

it("should return users client with bundle") {
let users = Auth0.users(token: Token, bundle: bundle)
expect(users.url.absoluteString) == "https://\(Domain)/"
}

#if WEB_AUTH_PLATFORM
it("should return web auth client with bundle") {
let auth = Auth0.webAuth(bundle: bundle)
expect(auth.url.absoluteString) == "https://\(Domain)/"
expect(auth.clientId) == ClientId
}

it("should return web auth client with bundle & session") {
let session = URLSession(configuration: URLSession.shared.configuration)
let webAuth = Auth0.webAuth(session: session, bundle: bundle) as! Auth0WebAuth
expect(webAuth.session).to(be(session))
}
#endif

}
#endif

}

describe("logging") {

it("should have no logging for auth by default") {
Expand Down Expand Up @@ -118,26 +221,8 @@ class Auth0Spec: QuickSpec {

}

#if !SWIFT_PACKAGE
describe("plist loading") {

let bundle = Bundle(for: Auth0Spec.self)

it("should return authentication endpoint with account from plist") {
let auth = Auth0.authentication(bundle: bundle)
expect(auth.url.absoluteString) == "https://samples.auth0.com/"
expect(auth.clientId) == "CLIENT_ID"
}

it("should return users endpoint with domain from plist") {
let users = Auth0.users(token: "TOKEN", bundle: bundle)
expect(users.url.absoluteString) == "https://samples.auth0.com/"
}

}
#endif

}

}

class MockLogger: Logger {
Expand Down

0 comments on commit 6c4dbeb

Please sign in to comment.