Skip to content

Commit

Permalink
Fix how test mock resource bundles are found to work across platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
mikenachbaur-okta committed Oct 30, 2024
1 parent d37de8f commit d09f527
Show file tree
Hide file tree
Showing 32 changed files with 252 additions and 70 deletions.
19 changes: 19 additions & 0 deletions Tests/APIClientTestCommon/MockResponses.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Copyright (c) 2024-Present, Okta, Inc. and/or its affiliates. All rights reserved.
// The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
//
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and limitations under the License.
//

import Foundation

extension Bundle {
public static var apiClientTestCommon: Bundle {
.module
}
}
4 changes: 4 additions & 0 deletions Tests/AuthFoundationTests/CredentialRefreshTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ final class CredentialRefreshTests: XCTestCase, OAuth2ClientDelegate {
return credential
}

static override func setUp() {
registerMock(bundles: .authFoundationTests)
}

override func setUpWithError() throws {
delegate = CredentialRefreshDelegate()
coordinator = MockCredentialCoordinator()
Expand Down
4 changes: 4 additions & 0 deletions Tests/AuthFoundationTests/CredentialRevokeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ final class CredentialTests: XCTestCase {
scopes: "openid"),
clientSettings: [ "client_id": "foo" ]))

static override func setUp() {
registerMock(bundles: .authFoundationTests)
}

override func setUpWithError() throws {
coordinator = MockCredentialCoordinator()
credential = coordinator.credentialDataSource.credential(for: token, coordinator: coordinator)
Expand Down
19 changes: 19 additions & 0 deletions Tests/AuthFoundationTests/MockResponses.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Copyright (c) 2024-Present, Okta, Inc. and/or its affiliates. All rights reserved.
// The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
//
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and limitations under the License.
//

import Foundation

extension Bundle {
public static var authFoundationTests: Bundle {
.module
}
}
6 changes: 5 additions & 1 deletion Tests/AuthFoundationTests/OAuth2ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ final class OAuth2ClientTests: XCTestCase {
clientId: "clientid",
scopes: "openid")
var token: Token!


static override func setUp() {
registerMock(bundles: .authFoundationTests)
}

override func setUpWithError() throws {
Credential.tokenStorage = MockTokenStorage()
Credential.credentialDataSource = MockCredentialDataSource()
Expand Down
4 changes: 4 additions & 0 deletions Tests/AuthFoundationTests/OIDCLegacyMigratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ final class OIDCLegacyMigratorTests: XCTestCase {
let issuer = URL(string: "https://example.com")!
let redirectUri = URL(string: "my-app:/")!

static override func setUp() {
registerMock(bundles: .authFoundationTests)
}

override func setUp() {
keychain = MockKeychain()
Keychain.implementation = keychain
Expand Down
4 changes: 4 additions & 0 deletions Tests/AuthFoundationTests/TokenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ final class TokenTests: XCTestCase {
clientId: "clientid",
scopes: "openid")

static override func setUp() {
registerMock(bundles: .authFoundationTests)
}

override func setUpWithError() throws {
JWK.validator = MockJWKValidator()
Token.idTokenValidator = MockIDTokenValidator()
Expand Down
4 changes: 4 additions & 0 deletions Tests/JWTTests/DefaultJWKValidatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ final class DefaultJWKValidatorTests: XCTestCase {
"""
var validator: DefaultJWKValidator!

static override func setUp() {
registerMock(bundles: .jwtTests)
}

override func setUpWithError() throws {
validator = DefaultJWKValidator()
}
Expand Down
4 changes: 4 additions & 0 deletions Tests/JWTTests/DefaultTokenHashValidatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ final class DefaultTokenHashValidatorTests: XCTestCase {

let validAccessToken = "VGhpc0lzQVJlYWxseUdyZWF0QWNjZXNzVG9rZW4sIERvbid0WW91VGhpbms_"

static override func setUp() {
registerMock(bundles: .jwtTests)
}

override func setUpWithError() throws {
validator = DefaultTokenHashValidator(hashKey: .accessToken)
}
Expand Down
4 changes: 4 additions & 0 deletions Tests/JWTTests/JSONValueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class JSONTests: XCTestCase {
let decoder = JSONDecoder()
let encoder = JSONEncoder()

static override func setUp() {
registerMock(bundles: .jwtTests)
}

func testString() throws {
let value = JSON.string("Test String")
XCTAssertNotNil(value)
Expand Down
4 changes: 4 additions & 0 deletions Tests/JWTTests/JWKTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import TestCommon
@testable import JWT

final class JWKTests: XCTestCase {
static override func setUp() {
registerMock(bundles: .jwtTests)
}

func testKeySets() throws {
let keyData = try data(filename: "keys")
let jwks = try JSONDecoder().decode(JWKS.self, from: keyData)
Expand Down
19 changes: 19 additions & 0 deletions Tests/JWTTests/MockResponses.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Copyright (c) 2024-Present, Okta, Inc. and/or its affiliates. All rights reserved.
// The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
//
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and limitations under the License.
//

import Foundation

extension Bundle {
public static var jwtTests: Bundle {
.module
}
}
4 changes: 4 additions & 0 deletions Tests/OktaDirectAuthTests/DirectAuth1FATests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ final class DirectAuth1FATests: XCTestCase {
var client: OAuth2Client!
var flow: DirectAuthenticationFlow!

static override func setUp() {
registerMock(bundles: .oktaDirectAuthTests)
}

override func setUpWithError() throws {
urlSession = URLSessionMock()
client = OAuth2Client(baseURL: issuer,
Expand Down
4 changes: 4 additions & 0 deletions Tests/OktaDirectAuthTests/DirectAuth2FATests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ final class DirectAuth2FATests: XCTestCase {
var client: OAuth2Client!
var flow: DirectAuthenticationFlow!

static override func setUp() {
registerMock(bundles: .oktaDirectAuthTests)
}

override func setUpWithError() throws {
urlSession = URLSessionMock()
client = OAuth2Client(baseURL: issuer,
Expand Down
19 changes: 19 additions & 0 deletions Tests/OktaDirectAuthTests/MockResponses.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Copyright (c) 2024-Present, Okta, Inc. and/or its affiliates. All rights reserved.
// The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
//
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and limitations under the License.
//

import Foundation

extension Bundle {
public static var oktaDirectAuthTests: Bundle {
.module
}
}
10 changes: 7 additions & 3 deletions Tests/OktaOAuth2Tests/AuthorizationCodeFlowSuccessTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ final class AuthorizationCodeFlowSuccessTests: XCTestCase {
var client: OAuth2Client!
var flow: AuthorizationCodeFlow!

static override func setUp() {
registerMock(bundles: .oktaOAuth2Tests)
}

override func setUpWithError() throws {
client = OAuth2Client(baseURL: issuer,
clientId: "clientId",
Expand All @@ -67,13 +71,13 @@ final class AuthorizationCodeFlowSuccessTests: XCTestCase {
Token.accessTokenValidator = MockTokenHashValidator()

urlSession.expect("https://example.com/oauth2/default/.well-known/openid-configuration",
data: try data(filename: "openid-configuration", matching: "OktaOAuth2Tests"),
data: try data(filename: "openid-configuration"),
contentType: "application/json")
urlSession.expect("https://example.okta.com/oauth2/v1/token",
data: try data(filename: "token", matching: "OktaOAuth2Tests"),
data: try data(filename: "token"),
contentType: "application/json")
urlSession.expect("https://example.okta.com/oauth2/v1/keys?client_id=clientId",
data: try data(filename: "keys", matching: "OktaOAuth2Tests"),
data: try data(filename: "keys"),
contentType: "application/json")

flow = client.authorizationCodeFlow(redirectUri: redirectUri,
Expand Down
16 changes: 10 additions & 6 deletions Tests/OktaOAuth2Tests/DeviceAuthorizationFlowErrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ final class DeviceAuthorizationFlowErrorTests: XCTestCase {
var client: OAuth2Client!
var flow: DeviceAuthorizationFlow!

static override func setUp() {
registerMock(bundles: .oktaOAuth2Tests)
}

override func setUpWithError() throws {
client = OAuth2Client(baseURL: issuer,
clientId: "clientId",
Expand All @@ -42,7 +46,7 @@ final class DeviceAuthorizationFlowErrorTests: XCTestCase {

func testSlowDown() throws {
urlSession.expect("https://example.com/.well-known/openid-configuration",
data: try data(filename: "openid-configuration", matching: "OktaOAuth2Tests"),
data: try data(filename: "openid-configuration"),
contentType: "application/json")
urlSession.expect("https://example.okta.com/oauth2/v1/device/authorize",
data: try data(filename: "device-authorize"),
Expand All @@ -52,10 +56,10 @@ final class DeviceAuthorizationFlowErrorTests: XCTestCase {
statusCode: 400,
contentType: "application/json")
urlSession.expect("https://example.okta.com/oauth2/v1/token",
data: try data(filename: "token", matching: "OktaOAuth2Tests"),
data: try data(filename: "token"),
contentType: "application/json")
urlSession.expect("https://example.okta.com/oauth2/v1/keys?client_id=clientId",
data: try data(filename: "keys", matching: "OktaOAuth2Tests"),
data: try data(filename: "keys"),
contentType: "application/json")
DeviceAuthorizationFlow.slowDownInterval = 1

Expand All @@ -64,7 +68,7 @@ final class DeviceAuthorizationFlowErrorTests: XCTestCase {

func testAuthorizationPending() throws {
urlSession.expect("https://example.com/.well-known/openid-configuration",
data: try data(filename: "openid-configuration", matching: "OktaOAuth2Tests"),
data: try data(filename: "openid-configuration"),
contentType: "application/json")
urlSession.expect("https://example.okta.com/oauth2/v1/device/authorize",
data: try data(filename: "device-authorize"),
Expand All @@ -74,10 +78,10 @@ final class DeviceAuthorizationFlowErrorTests: XCTestCase {
statusCode: 400,
contentType: "application/json")
urlSession.expect("https://example.okta.com/oauth2/v1/token",
data: try data(filename: "token", matching: "OktaOAuth2Tests"),
data: try data(filename: "token"),
contentType: "application/json")
urlSession.expect("https://example.okta.com/oauth2/v1/keys?client_id=clientId",
data: try data(filename: "keys", matching: "OktaOAuth2Tests"),
data: try data(filename: "keys"),
contentType: "application/json")
DeviceAuthorizationFlow.slowDownInterval = 1

Expand Down
10 changes: 7 additions & 3 deletions Tests/OktaOAuth2Tests/DeviceAuthorizationFlowSuccessTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ final class DeviceAuthorizationFlowSuccessTests: XCTestCase {
var client: OAuth2Client!
var flow: DeviceAuthorizationFlow!

static override func setUp() {
registerMock(bundles: .oktaOAuth2Tests)
}

override func setUpWithError() throws {
client = OAuth2Client(baseURL: issuer,
clientId: "clientId",
Expand All @@ -34,16 +38,16 @@ final class DeviceAuthorizationFlowSuccessTests: XCTestCase {
Token.accessTokenValidator = MockTokenHashValidator()

urlSession.expect("https://example.com/.well-known/openid-configuration",
data: try data(filename: "openid-configuration", matching: "OktaOAuth2Tests"),
data: try data(filename: "openid-configuration"),
contentType: "application/json")
urlSession.expect("https://example.okta.com/oauth2/v1/device/authorize",
data: try data(filename: "device-authorize"),
contentType: "application/json")
urlSession.expect("https://example.okta.com/oauth2/v1/token",
data: try data(filename: "token", matching: "OktaOAuth2Tests"),
data: try data(filename: "token"),
contentType: "application/json")
urlSession.expect("https://example.okta.com/oauth2/v1/keys?client_id=clientId",
data: try data(filename: "keys", matching: "OktaOAuth2Tests"),
data: try data(filename: "keys"),
contentType: "application/json")
flow = client.deviceAuthorizationFlow()
}
Expand Down
10 changes: 7 additions & 3 deletions Tests/OktaOAuth2Tests/JWTAuthorizationFlowTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ final class JWTAuthorizationFlowTests: XCTestCase {
var flow: JWTAuthorizationFlow!
var jwt: JWT!

static override func setUp() {
registerMock(bundles: .oktaOAuth2Tests)
}

override func setUpWithError() throws {
client = OAuth2Client(baseURL: issuer,
clientId: "clientId",
Expand All @@ -63,13 +67,13 @@ final class JWTAuthorizationFlowTests: XCTestCase {
Token.accessTokenValidator = MockTokenHashValidator()

urlSession.expect("https://example.okta.com/.well-known/openid-configuration",
data: try data(filename: "openid-configuration", matching: "OktaOAuth2Tests"),
data: try data(filename: "openid-configuration"),
contentType: "application/json")
urlSession.expect("https://example.okta.com/oauth2/v1/keys?client_id=clientId",
data: try data(filename: "keys", matching: "OktaOAuth2Tests"),
data: try data(filename: "keys"),
contentType: "application/json")
urlSession.expect("https://example.okta.com/oauth2/v1/token",
data: try data(filename: "token", matching: "OktaOAuth2Tests"),
data: try data(filename: "token"),
contentType: "application/json")

flow = client.jwtAuthorizationFlow()
Expand Down
19 changes: 19 additions & 0 deletions Tests/OktaOAuth2Tests/MockResponses.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Copyright (c) 2024-Present, Okta, Inc. and/or its affiliates. All rights reserved.
// The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
//
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and limitations under the License.
//

import Foundation

extension Bundle {
public static var oktaOAuth2Tests: Bundle {
.module
}
}
8 changes: 6 additions & 2 deletions Tests/OktaOAuth2Tests/OAuth2ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ final class OAuth2ClientTests: XCTestCase {
var urlSession: URLSessionMock!
var client: OAuth2Client!

static override func setUp() {
registerMock(bundles: .oktaOAuth2Tests)
}

override func setUpWithError() throws {
urlSession = URLSessionMock()
client = OAuth2Client(baseURL: issuer, clientId: "theClientId", scopes: "openid profile offline_access", session: urlSession)
Expand Down Expand Up @@ -53,10 +57,10 @@ final class OAuth2ClientTests: XCTestCase {
data: openIdData,
contentType: "application/json")
urlSession.expect("https://example.okta.com/oauth2/v1/keys?client_id=theClientId",
data: try data(filename: "keys", matching: "OktaOAuth2Tests"),
data: try data(filename: "keys"),
contentType: "application/json")
urlSession.expect("https://example.okta.com/oauth2/v1/token",
data: try data(filename: "token", matching: "OktaOAuth2Tests"),
data: try data(filename: "token"),
contentType: "application/json")

nonisolated(unsafe) var token: Token?
Expand Down
Loading

0 comments on commit d09f527

Please sign in to comment.