From d09f5272d8323a9d81595221f1ee24a3bfef37db Mon Sep 17 00:00:00 2001 From: Alex Nachbaur Date: Wed, 30 Oct 2024 11:26:54 -0700 Subject: [PATCH] Fix how test mock resource bundles are found to work across platforms --- Tests/APIClientTestCommon/MockResponses.swift | 19 +++++++ .../CredentialRefreshTests.swift | 4 ++ .../CredentialRevokeTests.swift | 4 ++ Tests/AuthFoundationTests/MockResponses.swift | 19 +++++++ .../OAuth2ClientTests.swift | 6 ++- .../OIDCLegacyMigratorTests.swift | 4 ++ Tests/AuthFoundationTests/TokenTests.swift | 4 ++ Tests/JWTTests/DefaultJWKValidatorTests.swift | 4 ++ .../DefaultTokenHashValidatorTests.swift | 4 ++ Tests/JWTTests/JSONValueTests.swift | 4 ++ Tests/JWTTests/JWKTests.swift | 4 ++ Tests/JWTTests/MockResponses.swift | 19 +++++++ .../DirectAuth1FATests.swift | 4 ++ .../DirectAuth2FATests.swift | 4 ++ Tests/OktaDirectAuthTests/MockResponses.swift | 19 +++++++ .../AuthorizationCodeFlowSuccessTests.swift | 10 ++-- .../DeviceAuthorizationFlowErrorTests.swift | 16 +++--- .../DeviceAuthorizationFlowSuccessTests.swift | 10 ++-- .../JWTAuthorizationFlowTests.swift | 10 ++-- Tests/OktaOAuth2Tests/MockResponses.swift | 19 +++++++ Tests/OktaOAuth2Tests/OAuth2ClientTests.swift | 8 ++- .../ResourceOwnerFlowTests.swift | 10 ++-- .../SessionLogoutFlowFailureTests.swift | 4 ++ .../SessionLogoutFlowSuccessTests.swift | 6 ++- .../SessionTokenFlowTests.swift | 10 ++-- .../TokenExchangeFlowTests.swift | 10 ++-- .../Utilities/XCTestCase+Extensions.swift | 2 +- Tests/TestCommon/XCTestCase+Extensions.swift | 49 ++++++------------- .../AuthenticationServicesProviderTests.swift | 4 ++ .../MockResponses.swift | 19 +++++++ .../ProviderTestBase.swift | 9 ++-- .../WebAuthenticationFlowTests.swift | 4 ++ 32 files changed, 252 insertions(+), 70 deletions(-) create mode 100644 Tests/APIClientTestCommon/MockResponses.swift create mode 100644 Tests/AuthFoundationTests/MockResponses.swift create mode 100644 Tests/JWTTests/MockResponses.swift create mode 100644 Tests/OktaDirectAuthTests/MockResponses.swift create mode 100644 Tests/OktaOAuth2Tests/MockResponses.swift create mode 100644 Tests/WebAuthenticationUITests/MockResponses.swift diff --git a/Tests/APIClientTestCommon/MockResponses.swift b/Tests/APIClientTestCommon/MockResponses.swift new file mode 100644 index 000000000..a45345028 --- /dev/null +++ b/Tests/APIClientTestCommon/MockResponses.swift @@ -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 + } +} diff --git a/Tests/AuthFoundationTests/CredentialRefreshTests.swift b/Tests/AuthFoundationTests/CredentialRefreshTests.swift index 32893bbc8..a6e94e534 100644 --- a/Tests/AuthFoundationTests/CredentialRefreshTests.swift +++ b/Tests/AuthFoundationTests/CredentialRefreshTests.swift @@ -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() diff --git a/Tests/AuthFoundationTests/CredentialRevokeTests.swift b/Tests/AuthFoundationTests/CredentialRevokeTests.swift index 46f96f1b5..e5941a2b6 100644 --- a/Tests/AuthFoundationTests/CredentialRevokeTests.swift +++ b/Tests/AuthFoundationTests/CredentialRevokeTests.swift @@ -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) diff --git a/Tests/AuthFoundationTests/MockResponses.swift b/Tests/AuthFoundationTests/MockResponses.swift new file mode 100644 index 000000000..df17ffece --- /dev/null +++ b/Tests/AuthFoundationTests/MockResponses.swift @@ -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 + } +} diff --git a/Tests/AuthFoundationTests/OAuth2ClientTests.swift b/Tests/AuthFoundationTests/OAuth2ClientTests.swift index 924241ecf..a03cc5b6d 100644 --- a/Tests/AuthFoundationTests/OAuth2ClientTests.swift +++ b/Tests/AuthFoundationTests/OAuth2ClientTests.swift @@ -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() diff --git a/Tests/AuthFoundationTests/OIDCLegacyMigratorTests.swift b/Tests/AuthFoundationTests/OIDCLegacyMigratorTests.swift index 9422204eb..1bb5584bb 100644 --- a/Tests/AuthFoundationTests/OIDCLegacyMigratorTests.swift +++ b/Tests/AuthFoundationTests/OIDCLegacyMigratorTests.swift @@ -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 diff --git a/Tests/AuthFoundationTests/TokenTests.swift b/Tests/AuthFoundationTests/TokenTests.swift index e98b10d6b..34a819c1c 100644 --- a/Tests/AuthFoundationTests/TokenTests.swift +++ b/Tests/AuthFoundationTests/TokenTests.swift @@ -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() diff --git a/Tests/JWTTests/DefaultJWKValidatorTests.swift b/Tests/JWTTests/DefaultJWKValidatorTests.swift index 9be9e5eac..af74d47dc 100644 --- a/Tests/JWTTests/DefaultJWKValidatorTests.swift +++ b/Tests/JWTTests/DefaultJWKValidatorTests.swift @@ -33,6 +33,10 @@ final class DefaultJWKValidatorTests: XCTestCase { """ var validator: DefaultJWKValidator! + static override func setUp() { + registerMock(bundles: .jwtTests) + } + override func setUpWithError() throws { validator = DefaultJWKValidator() } diff --git a/Tests/JWTTests/DefaultTokenHashValidatorTests.swift b/Tests/JWTTests/DefaultTokenHashValidatorTests.swift index b562471ea..9705eb855 100644 --- a/Tests/JWTTests/DefaultTokenHashValidatorTests.swift +++ b/Tests/JWTTests/DefaultTokenHashValidatorTests.swift @@ -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) } diff --git a/Tests/JWTTests/JSONValueTests.swift b/Tests/JWTTests/JSONValueTests.swift index 23ba9cb97..380ea46dc 100644 --- a/Tests/JWTTests/JSONValueTests.swift +++ b/Tests/JWTTests/JSONValueTests.swift @@ -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) diff --git a/Tests/JWTTests/JWKTests.swift b/Tests/JWTTests/JWKTests.swift index d0835ab75..4e71b4cfb 100644 --- a/Tests/JWTTests/JWKTests.swift +++ b/Tests/JWTTests/JWKTests.swift @@ -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) diff --git a/Tests/JWTTests/MockResponses.swift b/Tests/JWTTests/MockResponses.swift new file mode 100644 index 000000000..ee4cb2088 --- /dev/null +++ b/Tests/JWTTests/MockResponses.swift @@ -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 + } +} diff --git a/Tests/OktaDirectAuthTests/DirectAuth1FATests.swift b/Tests/OktaDirectAuthTests/DirectAuth1FATests.swift index aa9669bf0..cb24178b1 100644 --- a/Tests/OktaDirectAuthTests/DirectAuth1FATests.swift +++ b/Tests/OktaDirectAuthTests/DirectAuth1FATests.swift @@ -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, diff --git a/Tests/OktaDirectAuthTests/DirectAuth2FATests.swift b/Tests/OktaDirectAuthTests/DirectAuth2FATests.swift index a231f1de4..23f5b4074 100644 --- a/Tests/OktaDirectAuthTests/DirectAuth2FATests.swift +++ b/Tests/OktaDirectAuthTests/DirectAuth2FATests.swift @@ -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, diff --git a/Tests/OktaDirectAuthTests/MockResponses.swift b/Tests/OktaDirectAuthTests/MockResponses.swift new file mode 100644 index 000000000..6275b326c --- /dev/null +++ b/Tests/OktaDirectAuthTests/MockResponses.swift @@ -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 + } +} diff --git a/Tests/OktaOAuth2Tests/AuthorizationCodeFlowSuccessTests.swift b/Tests/OktaOAuth2Tests/AuthorizationCodeFlowSuccessTests.swift index c06355a50..9066eddb0 100644 --- a/Tests/OktaOAuth2Tests/AuthorizationCodeFlowSuccessTests.swift +++ b/Tests/OktaOAuth2Tests/AuthorizationCodeFlowSuccessTests.swift @@ -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", @@ -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, diff --git a/Tests/OktaOAuth2Tests/DeviceAuthorizationFlowErrorTests.swift b/Tests/OktaOAuth2Tests/DeviceAuthorizationFlowErrorTests.swift index 507a65bff..cce0734c8 100644 --- a/Tests/OktaOAuth2Tests/DeviceAuthorizationFlowErrorTests.swift +++ b/Tests/OktaOAuth2Tests/DeviceAuthorizationFlowErrorTests.swift @@ -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", @@ -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"), @@ -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 @@ -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"), @@ -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 diff --git a/Tests/OktaOAuth2Tests/DeviceAuthorizationFlowSuccessTests.swift b/Tests/OktaOAuth2Tests/DeviceAuthorizationFlowSuccessTests.swift index f28fd1b79..6e408bdc2 100644 --- a/Tests/OktaOAuth2Tests/DeviceAuthorizationFlowSuccessTests.swift +++ b/Tests/OktaOAuth2Tests/DeviceAuthorizationFlowSuccessTests.swift @@ -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", @@ -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() } diff --git a/Tests/OktaOAuth2Tests/JWTAuthorizationFlowTests.swift b/Tests/OktaOAuth2Tests/JWTAuthorizationFlowTests.swift index 490525b7f..750a8602c 100644 --- a/Tests/OktaOAuth2Tests/JWTAuthorizationFlowTests.swift +++ b/Tests/OktaOAuth2Tests/JWTAuthorizationFlowTests.swift @@ -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", @@ -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() diff --git a/Tests/OktaOAuth2Tests/MockResponses.swift b/Tests/OktaOAuth2Tests/MockResponses.swift new file mode 100644 index 000000000..45a3313d9 --- /dev/null +++ b/Tests/OktaOAuth2Tests/MockResponses.swift @@ -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 + } +} diff --git a/Tests/OktaOAuth2Tests/OAuth2ClientTests.swift b/Tests/OktaOAuth2Tests/OAuth2ClientTests.swift index b4c4af4bb..efcfdccb5 100644 --- a/Tests/OktaOAuth2Tests/OAuth2ClientTests.swift +++ b/Tests/OktaOAuth2Tests/OAuth2ClientTests.swift @@ -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) @@ -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? diff --git a/Tests/OktaOAuth2Tests/ResourceOwnerFlowTests.swift b/Tests/OktaOAuth2Tests/ResourceOwnerFlowTests.swift index d4baf9bfc..639ad8fab 100644 --- a/Tests/OktaOAuth2Tests/ResourceOwnerFlowTests.swift +++ b/Tests/OktaOAuth2Tests/ResourceOwnerFlowTests.swift @@ -47,6 +47,10 @@ final class ResourceOwnerFlowSuccessTests: XCTestCase { var client: OAuth2Client! var flow: ResourceOwnerFlow! + static override func setUp() { + registerMock(bundles: .oktaOAuth2Tests) + } + override func setUpWithError() throws { client = OAuth2Client(baseURL: issuer, clientId: "clientId", @@ -57,13 +61,13 @@ final class ResourceOwnerFlowSuccessTests: 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/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.resourceOwnerFlow() } diff --git a/Tests/OktaOAuth2Tests/SessionLogoutFlowFailureTests.swift b/Tests/OktaOAuth2Tests/SessionLogoutFlowFailureTests.swift index 58591343a..d0d600037 100644 --- a/Tests/OktaOAuth2Tests/SessionLogoutFlowFailureTests.swift +++ b/Tests/OktaOAuth2Tests/SessionLogoutFlowFailureTests.swift @@ -28,6 +28,10 @@ class SessionLogoutFlowFailureTests: XCTestCase { let logoutIDToken = "logoutIDToken" let state = "state" + static override func setUp() { + registerMock(bundles: .oktaOAuth2Tests) + } + override func setUpWithError() throws { client = OAuth2Client(baseURL: issuer, clientId: "clientId", scopes: "openid", session: urlSession) diff --git a/Tests/OktaOAuth2Tests/SessionLogoutFlowSuccessTests.swift b/Tests/OktaOAuth2Tests/SessionLogoutFlowSuccessTests.swift index 1a04c0599..f0f8fc59d 100644 --- a/Tests/OktaOAuth2Tests/SessionLogoutFlowSuccessTests.swift +++ b/Tests/OktaOAuth2Tests/SessionLogoutFlowSuccessTests.swift @@ -46,11 +46,15 @@ final class SessionLogoutFlowSuccessTests: XCTestCase { let logoutIDToken = "logoutIDToken" let state = "state" + static override func setUp() { + registerMock(bundles: .oktaOAuth2Tests) + } + override func setUpWithError() throws { client = OAuth2Client(baseURL: issuer, clientId: "clientId", scopes: "openid", session: urlSession) 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") flow = SessionLogoutFlow(logoutRedirectUri: logoutRedirectUri, client: client) diff --git a/Tests/OktaOAuth2Tests/SessionTokenFlowTests.swift b/Tests/OktaOAuth2Tests/SessionTokenFlowTests.swift index 8606bec47..71ebd2864 100644 --- a/Tests/OktaOAuth2Tests/SessionTokenFlowTests.swift +++ b/Tests/OktaOAuth2Tests/SessionTokenFlowTests.swift @@ -47,6 +47,10 @@ final class SessionTokenFlowSuccessTests: XCTestCase { var client: OAuth2Client! var flow: SessionTokenFlow! + static override func setUp() { + registerMock(bundles: .oktaOAuth2Tests) + } + override func setUpWithError() throws { client = OAuth2Client(baseURL: issuer, clientId: "clientId", @@ -58,13 +62,13 @@ final class SessionTokenFlowSuccessTests: XCTestCase { SessionTokenFlow.urlExchangeClass = MockSessionTokenFlowURLExchange.self 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/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.sessionTokenFlow(redirectUri: redirectUri, diff --git a/Tests/OktaOAuth2Tests/TokenExchangeFlowTests.swift b/Tests/OktaOAuth2Tests/TokenExchangeFlowTests.swift index dc7605a14..a1391f1c1 100644 --- a/Tests/OktaOAuth2Tests/TokenExchangeFlowTests.swift +++ b/Tests/OktaOAuth2Tests/TokenExchangeFlowTests.swift @@ -53,6 +53,10 @@ final class TokenExchangeFlowTests: XCTestCase { private let tokens: [TokenExchangeFlow.TokenType] = [.actor(type: .deviceSecret, value: "secret"), .subject(type: .idToken, value: "id_token")] + static override func setUp() { + registerMock(bundles: .oktaOAuth2Tests) + } + override func setUpWithError() throws { client = OAuth2Client(baseURL: issuer, clientId: "clientId", @@ -63,13 +67,13 @@ final class TokenExchangeFlowTests: 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.tokenExchangeFlow(audience: .default) diff --git a/Tests/OktaOAuth2Tests/Utilities/XCTestCase+Extensions.swift b/Tests/OktaOAuth2Tests/Utilities/XCTestCase+Extensions.swift index 790632f17..e9c17d419 100644 --- a/Tests/OktaOAuth2Tests/Utilities/XCTestCase+Extensions.swift +++ b/Tests/OktaOAuth2Tests/Utilities/XCTestCase+Extensions.swift @@ -16,7 +16,7 @@ import AuthFoundation extension XCTestCase { func openIdConfiguration(named: String = "openid-configuration") throws -> (OpenIdConfiguration, Data) { - let data = try data(filename: named, matching: "OktaOAuth2Tests") + let data = try data(filename: named) let configuration = try OpenIdConfiguration.jsonDecoder.decode(OpenIdConfiguration.self, from: data) return (configuration, data) diff --git a/Tests/TestCommon/XCTestCase+Extensions.swift b/Tests/TestCommon/XCTestCase+Extensions.swift index 2988a9a04..5e55d6619 100644 --- a/Tests/TestCommon/XCTestCase+Extensions.swift +++ b/Tests/TestCommon/XCTestCase+Extensions.swift @@ -17,55 +17,34 @@ enum TestError: Error { case noBundleResourceFound } -public extension Bundle { - func nestedBundles(suffixes: [String] = ["bundle", "xctest"]) -> [Bundle] { - var result: [Bundle] = [self] - - guard let resourcePath = resourcePath, - let enumerator = FileManager.default.enumerator(atPath: resourcePath) - else { - return result - } - - for case let path as String in enumerator { - guard suffixes.contains(where: { path.range(of: ".\($0)")?.isEmpty == false }), - let bundle = Bundle(path: "\(resourcePath)/\(path)"), - bundle.infoDictionary?["CFBundlePackageType"] as? String == "BNDL", - !result.contains(where: { $0.bundleIdentifier == bundle.bundleIdentifier }) - else { - continue - } - - result.append(contentsOf: bundle.nestedBundles(suffixes: suffixes)) - } - - return result +nonisolated(unsafe) fileprivate var testBundles: [Bundle] = [] +public extension XCTestCase { + static func registerMock(bundles: Bundle...) { + testBundles = bundles + } + + var mockBundles: [Bundle] { + var bundles: [Bundle] = testBundles + bundles.insert(Bundle(for: Self.self), at: 0) + return bundles } -} -public extension XCTestCase { func data(for json: String) -> Data { return json.data(using: .utf8)! } - func data(forClass testClass: XCTestCase.Type? = nil, filename: String, matching bundleName: String? = nil) throws -> Data { - let testClass = testClass ?? Self.self - return try data(from: Bundle(for: testClass), filename: filename, matching: bundleName) + func data(filename: String) throws -> Data { + return try data(from: mockBundles, filename: filename) } - func data(from bundle: Bundle, filename: String, matching bundleName: String? = nil) throws -> Data { + func data(from bundles: [Bundle], filename: String) throws -> Data { let file = (filename as NSString).deletingPathExtension var fileExtension = (filename as NSString).pathExtension if fileExtension == "" { fileExtension = "json" } - var bundles = bundle.nestedBundles() - if let bundleName = bundleName { - bundles = bundles.filter({ $0.bundleIdentifier?.range(of: bundleName)?.isEmpty == false }) - } - - guard let url = bundles.compactMap({ $0.url(forResource: file, withExtension: fileExtension) }).first + guard let url = testBundles.compactMap({ $0.url(forResource: file, withExtension: fileExtension) }).first else { throw TestError.noBundleResourceFound } diff --git a/Tests/WebAuthenticationUITests/AuthenticationServicesProviderTests.swift b/Tests/WebAuthenticationUITests/AuthenticationServicesProviderTests.swift index 54ee3b772..e57daa70c 100644 --- a/Tests/WebAuthenticationUITests/AuthenticationServicesProviderTests.swift +++ b/Tests/WebAuthenticationUITests/AuthenticationServicesProviderTests.swift @@ -68,6 +68,10 @@ struct TestAuthenticationSessionFactory: ASWebAuthenticationSessionFactory { class AuthenticationServicesProviderTests: ProviderTestBase { var provider: AuthenticationServicesProvider! + static override func setUp() { + registerMock(bundles: .webAuthenticationUITests) + } + override func setUpWithError() throws { try super.setUpWithError() diff --git a/Tests/WebAuthenticationUITests/MockResponses.swift b/Tests/WebAuthenticationUITests/MockResponses.swift new file mode 100644 index 000000000..db767bea1 --- /dev/null +++ b/Tests/WebAuthenticationUITests/MockResponses.swift @@ -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 webAuthenticationUITests: Bundle { + .module + } +} diff --git a/Tests/WebAuthenticationUITests/ProviderTestBase.swift b/Tests/WebAuthenticationUITests/ProviderTestBase.swift index a30c57da3..3f648d71d 100644 --- a/Tests/WebAuthenticationUITests/ProviderTestBase.swift +++ b/Tests/WebAuthenticationUITests/ProviderTestBase.swift @@ -97,16 +97,13 @@ class ProviderTestBase: XCTestCase, AuthorizationCodeFlowDelegate, SessionLogout logoutFlow.add(delegate: self) urlSession.expect("https://example.com/.well-known/openid-configuration", - data: try data(filename: "openid-configuration", - matching: "WebAuthenticationUITests"), + data: try data(filename: "openid-configuration"), contentType: "application/json") urlSession.expect("https://example.okta.com/oauth2/v1/token", - data: try data(filename: "token", - matching: "WebAuthenticationUITests"), + 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: "WebAuthenticationUITests"), + data: try data(filename: "keys"), contentType: "application/json") loginFlow = client.authorizationCodeFlow(redirectUri: redirectUri, additionalParameters: ["additional": "param"]) diff --git a/Tests/WebAuthenticationUITests/WebAuthenticationFlowTests.swift b/Tests/WebAuthenticationUITests/WebAuthenticationFlowTests.swift index ca86964a7..a2bbd89f8 100644 --- a/Tests/WebAuthenticationUITests/WebAuthenticationFlowTests.swift +++ b/Tests/WebAuthenticationUITests/WebAuthenticationFlowTests.swift @@ -30,6 +30,10 @@ class WebAuthenticationUITests: XCTestCase { private var logoutFlow: SessionLogoutFlow! private var client: OAuth2Client! + static override func setUp() { + registerMock(bundles: .webAuthenticationUITests) + } + override func setUpWithError() throws { client = OAuth2Client(baseURL: issuer, clientId: "clientId",