Skip to content

Commit da86e72

Browse files
authored
Add snapshot tests for Requests (#72)
1 parent af64679 commit da86e72

File tree

5 files changed

+169
-17
lines changed

5 files changed

+169
-17
lines changed

Package.swift

+10-3
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,21 @@ let package = Package(
4343
targets: ["OpenPassObjC"]
4444
)
4545
],
46-
dependencies: [],
46+
dependencies: [
47+
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.18.0")
48+
],
4749
targets: [
4850
.target(
4951
name: "OpenPass",
5052
dependencies: []
5153
),
5254
.testTarget(
5355
name: "OpenPassTests",
54-
dependencies: ["OpenPass"],
56+
dependencies: [
57+
"OpenPass",
58+
.product(name: "InlineSnapshotTesting", package: "swift-snapshot-testing"),
59+
.product(name: "SnapshotTesting", package: "swift-snapshot-testing"),
60+
],
5561
resources: [
5662
.copy("TestData")
5763
]
@@ -72,4 +78,5 @@ let package = Package(
7278
path: "Tests/ObjCTestHelpers"
7379
)
7480
],
75-
swiftLanguageVersions: [.v5])
81+
swiftLanguageVersions: [.v5]
82+
)

Sources/OpenPass/OpenPassClient.swift

+6-5
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,23 @@ internal final class OpenPassClient {
4646
convenience init(configuration: OpenPassConfiguration) {
4747
self.init(
4848
baseURL: configuration.baseURL,
49-
sdkName: configuration.sdkName,
50-
sdkVersion: configuration.sdkVersion,
49+
baseRequestParameters: BaseRequestParameters(
50+
sdkName: configuration.sdkName,
51+
sdkVersion: configuration.sdkVersion
52+
),
5153
clientId: configuration.clientId,
5254
isLoggingEnabled: configuration.isLoggingEnabled
5355
)
5456
}
5557

5658
init(
5759
baseURL: String,
58-
sdkName: String,
59-
sdkVersion: String = openPassSdkVersion,
60+
baseRequestParameters: BaseRequestParameters,
6061
clientId: String,
6162
isLoggingEnabled: Bool
6263
) {
6364
self.baseURL = baseURL
64-
self.baseRequestParameters = BaseRequestParameters(sdkName: sdkName, sdkVersion: sdkVersion)
65+
self.baseRequestParameters = baseRequestParameters
6566
self.clientId = clientId
6667
self.log = isLoggingEnabled
6768
? .init(subsystem: "com.myopenpass", category: "OpenPassClient")

Tests/OpenPassTests/OpenPassManagerTests.swift

+22-8
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ final class OpenPassManagerTests: XCTestCase {
4545
@MainActor
4646
/// Test helper. Runs the sign in flow with default, 'success' parameters. Override parameters to test failure scenarios.
4747
private func _testSignInUXFlow(
48-
configuration: OpenPassConfiguration? = nil,
48+
client: OpenPassClient? = nil,
4949
authenticationState: String = "state123",
5050
authenticationSession: TestAuthenticationSession? = nil,
5151
overrideFixtures: [String:(String, Int)] = [:],
@@ -55,15 +55,12 @@ final class OpenPassManagerTests: XCTestCase {
5555
"/v1/api/token": ("openpasstokens-200", 200),
5656
"/.well-known/jwks": ("jwks", 200),
5757
]
58-
let defaultConfiguration = OpenPassConfiguration.testConfiguration
5958
// overrides in fixtures replace defaults
6059
let fixtures = defaultFixtures.merging(overrideFixtures) { $1 }
6160
try HTTPStub.shared.stub(fixtures: fixtures)
6261

6362
let flow = SignInFlow(
64-
openPassClient: OpenPassClient(
65-
configuration: configuration ?? defaultConfiguration
66-
),
63+
openPassClient: client ?? OpenPassClient(configuration: OpenPassConfiguration.testConfiguration),
6764
tokenValidator: tokenValidator,
6865
redirectHost: "com.openpass",
6966
isLoggingEnabled: false,
@@ -250,7 +247,7 @@ final class OpenPassManagerTests: XCTestCase {
250247
XCTAssertEqual(components.host, "auth.myopenpass.com")
251248
XCTAssertEqual(components.path, "/v1/api/authorize")
252249

253-
let ignoredQueryItems: Set = ["code_challenge", "device_model", "device_platform", "device_platform_version", "sdk_version"]
250+
let ignoredQueryItems: Set = ["code_challenge"]
254251
let allQueryItemNames = (components.queryItems ?? [])
255252
.map(\.name)
256253
let allQueryItemNamesSet = Set(allQueryItemNames)
@@ -265,16 +262,33 @@ final class OpenPassManagerTests: XCTestCase {
265262
.init(name: "client_id", value: "test-client"),
266263
.init(name: "code_challenge_method", value: "S256"),
267264
.init(name: "device_manufacturer", value: "Apple"),
265+
.init(name: "device_model", value: "iPhone"),
266+
.init(name: "device_platform", value: "iOS"),
267+
.init(name: "device_platform_version", value: "13.0.0"),
268268
.init(name: "redirect_uri", value: "com.myopenpass.auth.test-client://com.openpass"),
269269
.init(name: "response_type", value: "code"),
270270
.init(name: "scope", value: "openid"),
271271
.init(name: "sdk_name", value: "openpass-ios-sdk"),
272+
.init(name: "sdk_version", value: "1.0.0"),
272273
.init(name: "state", value: "state123"),
273274
])
274275

275276
return Self.defaultAuthenticationCallbackURL
276277
}
277278
let _ = try await _testSignInUXFlow(
279+
client: OpenPassClient(
280+
baseURL: "https://auth.myopenpass.com/",
281+
baseRequestParameters: .init(
282+
sdkName: "openpass-ios-sdk",
283+
sdkVersion: "1.0.0",
284+
devicePlatform: "iOS",
285+
devicePlatformVersion: "13.0.0",
286+
deviceManufacturer: "Apple",
287+
deviceModel: "iPhone"
288+
),
289+
clientId: "test-client",
290+
isLoggingEnabled: false
291+
),
278292
authenticationSession: session,
279293
tokenValidator: IDTokenValidationStub.valid
280294
)
@@ -290,12 +304,12 @@ final class OpenPassManagerTests: XCTestCase {
290304
return Self.defaultAuthenticationCallbackURL
291305
}
292306
let _ = try await _testSignInUXFlow(
293-
configuration: .init(
307+
client: OpenPassClient(configuration: .init(
294308
clientId: "test-client",
295309
redirectHost: "",
296310
isLoggingEnabled: false,
297311
sdkNameSuffix: "-test-suffix"
298-
),
312+
)),
299313
authenticationSession: session,
300314
tokenValidator: IDTokenValidationStub.valid
301315
)
+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
//
2+
// RequestTests.swift
3+
//
4+
// MIT License
5+
//
6+
// Copyright (c) 2025 The Trade Desk (https://www.thetradedesk.com/)
7+
//
8+
// Permission is hereby granted, free of charge, to any person obtaining a copy
9+
// of this software and associated documentation files (the "Software"), to deal
10+
// in the Software without restriction, including without limitation the rights
11+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
// copies of the Software, and to permit persons to whom the Software is
13+
// furnished to do so, subject to the following conditions:
14+
//
15+
// The above copyright notice and this permission notice shall be included in all
16+
// copies or substantial portions of the Software.
17+
//
18+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
// SOFTWARE.
25+
//
26+
27+
import Foundation
28+
import InlineSnapshotTesting
29+
@testable import OpenPass
30+
import XCTest
31+
32+
final class RequestTests: XCTestCase {
33+
34+
private let client = OpenPassClient(
35+
baseURL: OpenPassConfiguration.defaultBaseURL,
36+
baseRequestParameters: BaseRequestParameters(
37+
sdkName: OpenPassConfiguration.defaultSdkName,
38+
sdkVersion: "1.0.0",
39+
devicePlatform: "iOS",
40+
devicePlatformVersion: "18.0",
41+
deviceManufacturer: "Apple",
42+
deviceModel: "iPhone"
43+
),
44+
clientId: "test-client-id",
45+
isLoggingEnabled: false
46+
)
47+
48+
func testAuthorizationCodeRequest() {
49+
let request = Request.authorizationCode(
50+
clientId: "client_id_5",
51+
code: "code-123",
52+
codeVerifier: "code-verifier-456",
53+
redirectUri: "redirect-uri-789"
54+
)
55+
assertInlineSnapshot(of: client.urlRequest(request), as: .raw(pretty: true)) {
56+
"""
57+
POST https://auth.myopenpass.com/v1/api/token
58+
Content-Type: application/x-www-form-urlencoded
59+
Device-Manufacturer: Apple
60+
Device-Model: iPhone
61+
Device-Platform-Version: 18.0
62+
Device-Platform: iOS
63+
SDK-Name: openpass-ios-sdk
64+
SDK-Version: 1.0.0
65+
66+
client_id=client_id_5&code_verifier=code-verifier-456&code=code-123&grant_type=authorization_code&redirect_uri=redirect-uri-789
67+
"""
68+
}
69+
}
70+
71+
func testAuthorizeDeviceRequest() {
72+
let request = Request.authorizeDevice(clientId: "client-id-7")
73+
assertInlineSnapshot(of: client.urlRequest(request), as: .raw(pretty: true)) {
74+
"""
75+
POST https://auth.myopenpass.com/v1/api/authorize-device
76+
Content-Type: application/x-www-form-urlencoded
77+
Device-Manufacturer: Apple
78+
Device-Model: iPhone
79+
Device-Platform-Version: 18.0
80+
Device-Platform: iOS
81+
SDK-Name: openpass-ios-sdk
82+
SDK-Version: 1.0.0
83+
84+
client_id=client-id-7&scope=openid
85+
"""
86+
}
87+
}
88+
89+
func testDeviceTokenRequest() {
90+
let request = Request.deviceToken(
91+
clientId: "client-id-0",
92+
deviceCode: "device-code-1"
93+
)
94+
assertInlineSnapshot(of: client.urlRequest(request), as: .raw(pretty: true)) {
95+
"""
96+
POST https://auth.myopenpass.com/v1/api/device-token
97+
Content-Type: application/x-www-form-urlencoded
98+
Device-Manufacturer: Apple
99+
Device-Model: iPhone
100+
Device-Platform-Version: 18.0
101+
Device-Platform: iOS
102+
SDK-Name: openpass-ios-sdk
103+
SDK-Version: 1.0.0
104+
105+
client_id=client-id-0&device_code=device-code-1&grant_type=urn:ietf:params:oauth:grant-type:device_code
106+
"""
107+
}
108+
}
109+
110+
func testRefreshRequest() {
111+
let request = Request.refresh(
112+
clientId: "client_id_3",
113+
refreshToken: "refresh-token-value"
114+
)
115+
assertInlineSnapshot(of: client.urlRequest(request), as: .raw(pretty: true)) {
116+
"""
117+
POST https://auth.myopenpass.com/v1/api/token
118+
Content-Type: application/x-www-form-urlencoded
119+
Device-Manufacturer: Apple
120+
Device-Model: iPhone
121+
Device-Platform-Version: 18.0
122+
Device-Platform: iOS
123+
SDK-Name: openpass-ios-sdk
124+
SDK-Version: 1.0.0
125+
126+
client_id=client_id_3&grant_type=refresh_token&refresh_token=refresh-token-value
127+
"""
128+
}
129+
}
130+
}

Tests/OpenPassTests/TestExtensions/OpenPassClient+TestExtensions.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
extension OpenPassClient {
3131
static let test = OpenPassClient(
3232
baseURL: "https://auth.myopenpass.com/",
33-
sdkName: "OpenPassTest",
33+
baseRequestParameters: BaseRequestParameters(sdkName: "OpenPassTest", sdkVersion: "1.0.0"),
3434
clientId: "test-client",
3535
isLoggingEnabled: false
3636
)

0 commit comments

Comments
 (0)