Skip to content

Commit a30622b

Browse files
committed
Updates tests
1 parent 2cdbf62 commit a30622b

File tree

2 files changed

+99
-22
lines changed

2 files changed

+99
-22
lines changed

Auth0Tests/RequestSpec.swift

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ fileprivate extension Request where T == [String: Any], E == AuthenticationError
3030

3131
}
3232

33+
extension Requestable {
34+
var storedParameters: [String: Any] {
35+
(self as? Request<ResultType, ErrorType>)?.parameters ?? [:]
36+
}
37+
var storedHeaders: [String: String] {
38+
(self as? Request<ResultType, ErrorType>)?.headers ?? [:]
39+
}
40+
var dpop: DPoP? {
41+
(self as? Request<ResultType, ErrorType>)?.dpop
42+
}
43+
}
44+
3345
class RequestSpec: QuickSpec {
3446
override class func spec() {
3547

@@ -52,32 +64,32 @@ class RequestSpec: QuickSpec {
5264
}
5365

5466
it("should create a new request with extra parameters") {
55-
let request = Request().parameters(["foo": "bar"]) as! Request<[String: Any], AuthenticationError>
56-
expect(request.parameters["foo"] as? String) == "bar"
67+
let request = Request().parameters(["foo": "bar"])
68+
expect(request.storedParameters["foo"] as? String) == "bar"
5769
}
5870

5971
it("should merge extra parameters with existing parameters") {
60-
let request = Request(parameters: ["foo": "bar"]).parameters(["baz": "qux"]) as! Request<[String: Any], AuthenticationError>
61-
expect(request.parameters["foo"] as? String) == "bar"
62-
expect(request.parameters["baz"] as? String) == "qux"
72+
let request = Request(parameters: ["foo": "bar"]).parameters(["baz": "qux"])
73+
expect(request.storedParameters["foo"] as? String) == "bar"
74+
expect(request.storedParameters["baz"] as? String) == "qux"
6375
}
6476

6577
it("should overwrite existing parameters with extra parameters") {
66-
let request = Request(parameters: ["foo": "bar"]).parameters(["foo": "baz"]) as! Request<[String: Any], AuthenticationError>
67-
expect(request.parameters["foo"] as? String) == "baz"
78+
let request = Request(parameters: ["foo": "bar"]).parameters(["foo": "baz"])
79+
expect(request.storedParameters["foo"] as? String) == "baz"
6880
}
6981

7082
it("should create a new request and not mutate an existing request") {
7183
let request = Request(parameters: ["foo": "bar"])
72-
let updated = request.parameters(["foo": "baz"]) as! Request<[String: Any], AuthenticationError>
73-
expect(updated.parameters["foo"] as? String) == "baz"
84+
let updated = request.parameters(["foo": "baz"])
85+
expect(updated.storedParameters["foo"] as? String) == "baz"
7486
expect(request.parameters["foo"] as? String) == "bar"
7587
}
7688

7789
it("should enforce the openid scope when adding extra parameters") {
7890
let request = Request(parameters: ["foo": "bar"])
79-
let updated = request.parameters(["scope": "email phone"]) as! Request<[String: Any], AuthenticationError>
80-
expect(updated.parameters["scope"] as? String) == "openid email phone"
91+
let updated = request.parameters(["scope": "email phone"])
92+
expect(updated.storedParameters["scope"] as? String) == "openid email phone"
8193
}
8294

8395
it("should add the parameters as query parameters") {
@@ -113,25 +125,25 @@ class RequestSpec: QuickSpec {
113125
}
114126

115127
it("should create a new request with extra headers") {
116-
let request = Request().headers(["foo": "bar"]) as! Request<[String: Any], AuthenticationError>
117-
expect(request.headers["foo"]) == "bar"
128+
let request = Request().headers(["foo": "bar"])
129+
expect(request.storedHeaders["foo"]) == "bar"
118130
}
119131

120132
it("should merge extra headers with existing headers") {
121-
let request = Request(headers: ["foo": "bar"]).headers(["baz": "qux"]) as! Request<[String: Any], AuthenticationError>
122-
expect(request.headers["foo"]) == "bar"
123-
expect(request.headers["baz"]) == "qux"
133+
let request = Request(headers: ["foo": "bar"]).headers(["baz": "qux"])
134+
expect(request.storedHeaders["foo"]) == "bar"
135+
expect(request.storedHeaders["baz"]) == "qux"
124136
}
125137

126138
it("should overwrite existing headers with extra headers") {
127-
let request = Request(headers: ["foo": "bar"]).headers(["foo": "baz"]) as! Request<[String: Any], AuthenticationError>
128-
expect(request.headers["foo"]) == "baz"
139+
let request = Request(headers: ["foo": "bar"]).headers(["foo": "baz"])
140+
expect(request.storedHeaders["foo"]) == "baz"
129141
}
130142

131143
it("should create a new request and not mutate an existing request") {
132144
let request = Request(headers: ["foo": "bar"])
133-
let updated = request.headers(["foo": "baz"]) as! Request<[String: Any], AuthenticationError>
134-
expect(updated.headers["foo"]) == "baz"
145+
let updated = request.headers(["foo": "baz"])
146+
expect(updated.storedHeaders["foo"]) == "baz"
135147
expect(request.headers["foo"]) == "bar"
136148
}
137149

@@ -155,12 +167,12 @@ class RequestSpec: QuickSpec {
155167
}
156168

157169
it("should preserve dpop when adding parameters") {
158-
let request = Request(dpop: DPoP()).parameters(["foo": "bar"]) as! Request<[String: Any], AuthenticationError>
170+
let request = Request(dpop: DPoP()).parameters(["foo": "bar"])
159171
expect(request.dpop).toNot(beNil())
160172
}
161173

162174
it("should preserve dpop when adding headers") {
163-
let request = Request(dpop: DPoP()).headers(["foo": "bar"]) as! Request<[String: Any], AuthenticationError>
175+
let request = Request(dpop: DPoP()).headers(["foo": "bar"])
164176
expect(request.dpop).toNot(beNil())
165177
}
166178

V3_MIGRATION_GUIDE.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ As expected with a major release, Auth0.swift v3 contains breaking changes. Plea
3131
- [**API Changes**](#api-changes)
3232
+ [WebAuthError cases](#webautherror-cases)
3333
+ [Renamed APIs](#renamed-apis)
34+
+ [Request to Requestable](#requestable)
3435

3536
---
3637

@@ -369,5 +370,69 @@ let user: UserProfile = ...
369370
```
370371
</details>
371372

373+
## Request to Requestable
374+
375+
**Change:** API Clients Authentication, MFAClient, MyAccountAuthenticationMethods have been refactored to return Requestable instead of Request
376+
377+
**Impact:** With returning Requestable protocol instead of Request, developers can now mock Request in the api clients thus mocking Auth0 layer making writing tests supper easy
378+
379+
**Reason:** With existing Request object it was hard to mock Auth0 SDK layer. Only way to mock was using URLProtocol. With Requestable, developers can mock requests and write tests without heavy lifting of mocking url session layer
380+
381+
**example code**
382+
383+
```swift
384+
public protocol Authentication {
385+
func login(email: String, code: String, audience: String?, scope: String) -> Requestable
386+
}
387+
388+
class MockRequestable: Requestable {
389+
typealias ResultType = Credentials
390+
typealias ErrorType = AuthenticationError
391+
392+
let mockResult: Result<Credentials, AuthenticationError>
393+
394+
init(mockResult: Result<Credentials, AuthenticationError>) {
395+
self.mockResult = mockResult
396+
}
397+
398+
func start(_ callback: @escaping (Result<Credentials, AuthenticationError>) -> Void) {
399+
callback(mockResult)
400+
}
401+
}
402+
403+
class MockAuthentication: Authentication {
404+
let mockRequest: MockRequestable
405+
406+
init(mockResult: Result<Credentials, AuthenticationError>) {
407+
self.mockRequest = MockRequestable(mockResult: mockResult)
408+
}
409+
410+
func login(email: String, code: String, audience: String?, scope: String) -> Requestable {
411+
return mockRequest
412+
}
413+
}
414+
415+
class ClientAppOrSDK {
416+
private let auth: Authentication
417+
418+
func login(email: String, code: String, completion: @escaping (Result<Credentials, Error>) -> Void) {
419+
let requestable = auth.login(email: email, code: code, audience: nil, scope: "openid profile email")
420+
requestable.start { result in
421+
// Handle result
422+
}
423+
}
424+
}
425+
426+
func testLoginSuccess() {
427+
let mockCredentials = Credentials(accessToken: "token", tokenType: "Bearer", expiresIn: Date(), idToken: "id_token")
428+
let mockAuth = MockAuthentication(mockResult: .success(mockCredentials))
429+
let apporsdk = ClientAppOrSDK(auth: mockAuth)
430+
431+
apporsdk.login(email: "test@example.com", code: "123456") { result in
432+
// Assert success without network calls
433+
XCTAssertEqual(try? result.get().accessToken, "token")
434+
}
435+
}
436+
```
372437
---
373438
[Go up ⤴](#table-of-contents)

0 commit comments

Comments
 (0)