Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve unit tests, move hard coded URLs to one place, change test url #238

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import XCTest
class AA_05_DeviceBindingCallbackTest: CallbackBaseTest {

static var USERNAME: String = "sdkuser"
let options = FROptions(url: "https://openam-dbind.forgeblocks.com/am",
let options = FROptions(url: "https://openam-sdks.forgeblocks.com/am",
realm: "alpha",
enableCookie: true,
cookieName: "afef1acb448a873",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class AA_06_DeviceSigningVerifierCallbackTest: CallbackBaseTest {
static var USERNAME: String = "sdkuser"
static var APPLICATION_PIN: String = "1111"

let options = FROptions(url: "https://openam-dbind.forgeblocks.com/am",
let options = FROptions(url: "https://openam-sdks.forgeblocks.com/am",
realm: "alpha",
enableCookie: true,
cookieName: "afef1acb448a873",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// FRURLProtocolTests.swift
// FRAuthTests
//
// Copyright (c) 2020 ForgeRock. All rights reserved.
// Copyright (c) 2020-2023 ForgeRock. All rights reserved.
//
// This software may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
Expand Down Expand Up @@ -52,7 +52,7 @@ class FRURLProtocolTests: FRAuthBaseTest {

// When
let ex = self.expectation(description: "Making request")
urlSession.dataTask(with: URL(string: "https://httpbin.org/anything")!) { (data, response, error) in
urlSession.dataTask(with: URL(string: FRTestURL.anythingURL)!) { (data, response, error) in
let response = Response(data: data, response: response, error: error).parseReponse()

// Authorization header should have not been sent
Expand Down Expand Up @@ -92,13 +92,13 @@ class FRURLProtocolTests: FRAuthBaseTest {
let urlSession: URLSession = URLSession(configuration: config)

// Set TokenManagementPolicy, but with different URL for validation
let tokenManagementPolicy = TokenManagementPolicy(validatingURL: [URL(string: "https://httpbin.org/any")!], delegate: nil)
let tokenManagementPolicy = TokenManagementPolicy(validatingURL: [URL(string: "\(FRTestURL.baseURL)/any")!], delegate: nil)
FRURLProtocol.authorizationPolicy = nil
FRURLProtocol.tokenManagementPolicy = tokenManagementPolicy

// When
let ex = self.expectation(description: "Making request")
urlSession.dataTask(with: URL(string: "https://httpbin.org/anything")!) { (data, response, error) in
urlSession.dataTask(with: URL(string: FRTestURL.anythingURL)!) { (data, response, error) in
let response = Response(data: data, response: response, error: error).parseReponse()

// Authorization header should have not been sent
Expand Down Expand Up @@ -137,13 +137,13 @@ class FRURLProtocolTests: FRAuthBaseTest {
let urlSession: URLSession = URLSession(configuration: config)

// Set TokenManagementPolicy without delegate for default Authorization header, and validating URL
let tokenManagementPolicy = TokenManagementPolicy(validatingURL: [URL(string: "https://httpbin.org/anything")!], delegate: nil)
let tokenManagementPolicy = TokenManagementPolicy(validatingURL: [URL(string: FRTestURL.anythingURL)!], delegate: nil)
FRURLProtocol.authorizationPolicy = nil
FRURLProtocol.tokenManagementPolicy = tokenManagementPolicy

// When
let ex = self.expectation(description: "Making request")
urlSession.dataTask(with: URL(string: "https://httpbin.org/anything")!) { (data, response, error) in
urlSession.dataTask(with: URL(string: FRTestURL.anythingURL)!) { (data, response, error) in
let response = Response(data: data, response: response, error: error).parseReponse()

// Authorization header should have been sent
Expand Down Expand Up @@ -183,13 +183,13 @@ class FRURLProtocolTests: FRAuthBaseTest {
let urlSession: URLSession = URLSession(configuration: config)

// Set TokenManagementPolicy with delegate, and let delegation method to update request
let tokenManagementPolicy = TokenManagementPolicy(validatingURL: [URL(string: "https://httpbin.org/anything")!], delegate: self)
let tokenManagementPolicy = TokenManagementPolicy(validatingURL: [URL(string: FRTestURL.anythingURL)!], delegate: self)
FRURLProtocol.authorizationPolicy = nil
FRURLProtocol.tokenManagementPolicy = tokenManagementPolicy

// When
let ex = self.expectation(description: "Making request")
urlSession.dataTask(with: URL(string: "https://httpbin.org/anything")!) { (data, response, error) in
urlSession.dataTask(with: URL(string: FRTestURL.anythingURL)!) { (data, response, error) in
let response = Response(data: data, response: response, error: error).parseReponse()

// Authorization header should have been sent with customized header name in delegation method
Expand Down Expand Up @@ -233,13 +233,13 @@ class FRURLProtocolTests: FRAuthBaseTest {

// Set TokenManagementPolicy with delegate, and let delegation method to update request
self.evaluateTokenRefresh = true
let tokenManagementPolicy = TokenManagementPolicy(validatingURL: [URL(string: "https://httpbin.org/anything")!], delegate: self)
let tokenManagementPolicy = TokenManagementPolicy(validatingURL: [URL(string: FRTestURL.anythingURL)!], delegate: self)
FRURLProtocol.authorizationPolicy = nil
FRURLProtocol.tokenManagementPolicy = tokenManagementPolicy

// When
let ex = self.expectation(description: "Making request")
urlSession.dataTask(with: URL(string: "https://httpbin.org/anything")!) { (data, response, error) in
urlSession.dataTask(with: URL(string: FRTestURL.anythingURL)!) { (data, response, error) in
let response = Response(data: data, response: response, error: error).parseReponse()

// Authorization header should have been sent with customized header name in delegation method
Expand Down Expand Up @@ -293,13 +293,13 @@ class FRURLProtocolTests: FRAuthBaseTest {
let urlSession: URLSession = URLSession(configuration: config)

// Set TokenManagementPolicy with delegate, and let delegation method to handle evaluation for 401 status code
let tokenManagementPolicy = TokenManagementPolicy(validatingURL: [URL(string: "https://httpbin.org/status/401")!], delegate: self)
let tokenManagementPolicy = TokenManagementPolicy(validatingURL: [URL(string: FRTestURL.status401URL)!], delegate: self)
FRURLProtocol.authorizationPolicy = nil
FRURLProtocol.tokenManagementPolicy = tokenManagementPolicy

// When
let ex = self.expectation(description: "Making request")
urlSession.dataTask(with: URL(string: "https://httpbin.org/status/401")!) { (data, response, error) in
urlSession.dataTask(with: URL(string: FRTestURL.status401URL)!) { (data, response, error) in
let response = Response(data: data, response: response, error: error).parseReponse()

// Request must fail with status code 401, even though evaluationRefreshToken policy satsifies, it must receive 401 if it keeps failing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ class AuthorizationPolicyTests: FRAuthBaseTest {
let policy = AuthorizationPolicy(validatingURL: [URL(string: "https://openam.example.com/anything")!], delegate: self)
self.policyAdvice = PolicyAdvice(type: "TransactionConditionAdvice", value: "5afff42a-2715-40c8-98e7-919abc1b2dfc")
let session = URLSession(configuration: .default)
let task = session.dataTask(with: URL(string: "https://httpbin.org/anything")!)
let task = session.dataTask(with: URL(string: FRTestURL.anythingURL)!)
let advice = policy.evaluateAuthorizationPolicyWithRedirect(responseData: nil, session: URLSession(), task: task, willPerformHTTPRedirection: HTTPURLResponse(), newRequest: URLRequest(url: URL(string: "https://www.forgerock.com")!))
XCTAssertEqual(advice, self.policyAdvice)
XCTAssertEqual(self.list.count, 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SSLPinningTests.swift
// FRAuthTests
//
// Copyright (c) 2022 ForgeRock. All rights reserved.
// Copyright (c) 2022-2023 ForgeRock. All rights reserved.
//
// This software may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
Expand All @@ -27,7 +27,7 @@ class SSLPinningTests: FRAuthBaseTest {
// Init SDK
self.startSDK()

let request = Request(url: "https://httpbin.org/anything", method: .GET)
let request = Request(url: FRTestURL.anythingURL, method: .GET)
let ex = self.expectation(description: "Request submit")
var requestSucceeded = false

Expand Down Expand Up @@ -57,7 +57,7 @@ class SSLPinningTests: FRAuthBaseTest {
// Init SDK
self.startSDK()

let request = Request(url: "https://httpbin.org/anything", method: .GET)
let request = Request(url: FRTestURL.anythingURL, method: .GET)
let ex = self.expectation(description: "Request submit")
var requestSucceeded = false

Expand Down Expand Up @@ -117,7 +117,7 @@ class SSLPinningTests: FRAuthBaseTest {
// Init SDK
self.startSDK()

let request = Request(url: "https://httpbin.org/anything", method: .GET)
let request = Request(url: FRTestURL.anythingURL, method: .GET)
let ex = self.expectation(description: "Request submit")
var requestSucceeded = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// FRSecurityConfigurationTests.swift
// FRCoreTests
//
// Copyright (c) 2022 ForgeRock. All rights reserved.
// Copyright (c) 2022-2023 ForgeRock. All rights reserved.
//
// This software may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
Expand Down Expand Up @@ -40,7 +40,7 @@ class FRSecurityConfigurationTests: FRBaseTestCase {
//Validate with the correct public key hash
let frSecurityConfiguration1 = FRSecurityConfiguration(hashes: ["+KSzREQbAh9gqYLLGpfCG+cAy7Px3/Qmk/e8Egwyd7o="])

let validated1 = frSecurityConfiguration1.validate(serverTrust: trust, domain: "https://httpbin.org/")
let validated1 = frSecurityConfiguration1.validate(serverTrust: trust, domain: FRTestURL.baseURL)

XCTAssertTrue(validated1, "Certificate failed to validate with the correct public key hash")

Expand All @@ -66,7 +66,8 @@ class FRSecurityConfigurationTests: FRBaseTestCase {
//Validate with a wrong public key hash
let frSecurityConfiguration2 = FRSecurityConfiguration(hashes: ["GSHJImFNL2AkwaL7xE1K+LVGj/V4Dgl7QYrNHKF5g0U="])

let validated2 = frSecurityConfiguration2.validate(serverTrust: trust, domain: "https://httpbin.org/")
let validated2 = frSecurityConfiguration2.validate(serverTrust: trust, domain: FRTestURL.baseURL
)

XCTAssertFalse(validated2, "Certificate successfully validated with a wrong public key hash")
}
Expand Down
4 changes: 2 additions & 2 deletions FRCore/FRCoreTests/FRCore/Network/RequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RequestTest.swift
// FRCoreTests
//
// Copyright (c) 2020 ForgeRock. All rights reserved.
// Copyright (c) 2020-2023 ForgeRock. All rights reserved.
//
// This software may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
Expand All @@ -12,7 +12,7 @@ import XCTest

class RequestTests: FRBaseTestCase {

let testURL: String = "https://httpbin.org"
let testURL: String = FRTestURL.baseURL

/// Tests basic initialization method with valid URL string
func testBasicConstruction() {
Expand Down
4 changes: 2 additions & 2 deletions FRCore/FRCoreTests/FRCore/Network/RestClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class RestClientTests: FRBaseTestCase {

func test_02_test_200_get_request() {

let request = Request(url: "https://httpbin.org/get", method: .GET)
let request = Request(url: FRTestURL.getURL, method: .GET)
let expectation = self.expectation(description: "GET request: \(request.debugDescription)")

var response:[String: Any]?, urlResponse: URLResponse?, error: NetworkError?
Expand Down Expand Up @@ -88,7 +88,7 @@ class RestClientTests: FRBaseTestCase {

func test_03_test_invalid_response_image() {

let request = Request(url: "https://httpbin.org/image", method: .GET, headers: ["accept":"image/webp"])
let request = Request(url: FRTestURL.imageURL, method: .GET, headers: ["accept":"image/webp"])
let expectation = self.expectation(description: "GET request: \(request.debugDescription)")

var response:[String: Any]?, urlResponse: URLResponse?, error: NetworkError?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// RequestInterceptorTests.swift
// FRCoreTests
//
// Copyright (c) 2020 ForgeRock. All rights reserved.
// Copyright (c) 2020-2023 ForgeRock. All rights reserved.
//
// This software may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
Expand Down Expand Up @@ -36,7 +36,7 @@ class RequestInterceptorTests: FRBaseTestCase {

func test_01_request_captured_and_processed_interceptor() {

let request = Request(url: "https://httpbin.org/anything", method: .GET)
let request = Request(url: FRTestURL.anythingURL, method: .GET)
RequestInterceptorRegistry.shared.registerInterceptors(interceptors: [StartAuthenticateInterceptor()])
let ex = self.expectation(description: "Request submit")
RestClient.shared.invoke(request: request, action: Action(type: .START_AUTHENTICATE)) { (result) in
Expand Down Expand Up @@ -66,7 +66,7 @@ class RequestInterceptorTests: FRBaseTestCase {

func test_02_request_captured_and_should_not_process_interceptor_for_different_action() {

let request = Request(url: "https://httpbin.org/anything", method: .GET)
let request = Request(url: FRTestURL.anythingURL, method: .GET)
RequestInterceptorRegistry.shared.registerInterceptors(interceptors: [StartAuthenticateInterceptor()])
let ex = self.expectation(description: "Request submit")
RestClient.shared.invoke(request: request, action: Action(type: .AUTHORIZE)) { (result) in
Expand Down Expand Up @@ -96,7 +96,7 @@ class RequestInterceptorTests: FRBaseTestCase {

func test_03_request_captured_and_should_process_interceptors_in_order() {

let request = Request(url: "https://httpbin.org/anything", method: .GET)
let request = Request(url: FRTestURL.anythingURL, method: .GET)
RequestInterceptorRegistry.shared.registerInterceptors(interceptors: [DummyOne(), DummyTwo(), DummyThree(), DummyFour()])
let ex = self.expectation(description: "Request submit")
RestClient.shared.invoke(request: request, action: Action(type: .AUTHORIZE)) { (result) in
Expand Down Expand Up @@ -132,7 +132,7 @@ class RequestInterceptorTests: FRBaseTestCase {

func test_04_request_captured_and_should_not_process_interceptor_for_different_action_sync() {

let request = Request(url: "https://httpbin.org/anything", method: .GET)
let request = Request(url: FRTestURL.anythingURL, method: .GET)
RequestInterceptorRegistry.shared.registerInterceptors(interceptors: [StartAuthenticateInterceptor()])
let _ = RestClient.shared.invokeSync(request: request, action: Action(type: .AUTHORIZE))

Expand All @@ -152,7 +152,7 @@ class RequestInterceptorTests: FRBaseTestCase {

func test_05_request_captured_and_should_process_interceptors_in_order_sync() {

let request = Request(url: "https://httpbin.org/anything", method: .GET)
let request = Request(url: FRTestURL.anythingURL, method: .GET)
RequestInterceptorRegistry.shared.registerInterceptors(interceptors: [DummyOne(), DummyTwo(), DummyThree(), DummyFour()])
let _ = RestClient.shared.invokeSync(request: request, action: Action(type: .AUTHORIZE))

Expand All @@ -177,7 +177,7 @@ class RequestInterceptorTests: FRBaseTestCase {


func test_06_request_not_captured_when_no_action_defined() {
let request = Request(url: "https://httpbin.org/anything", method: .GET)
let request = Request(url: FRTestURL.anythingURL, method: .GET)
RequestInterceptorRegistry.shared.registerInterceptors(interceptors: [DummyOne(), DummyTwo(), DummyThree(), DummyFour()])
let ex = self.expectation(description: "Request submit")
RestClient.shared.invoke(request: request) { (result) in
Expand Down Expand Up @@ -211,7 +211,7 @@ class RequestInterceptorTests: FRBaseTestCase {

func test_07_request_captured_and_invoke_different_url() {

let request = Request(url: "https://httpbin.org/anything", method: .GET)
let request = Request(url: FRTestURL.anythingURL, method: .GET)
RequestInterceptorRegistry.shared.registerInterceptors(interceptors: [DifferentURLInterceptor()])
let ex = self.expectation(description: "Request submit")
RestClient.shared.invoke(request: request, action: Action(type: .AUTHENTICATE)) { (result) in
Expand Down Expand Up @@ -241,7 +241,7 @@ class RequestInterceptorTests: FRBaseTestCase {


func test_08_request_captured_in_seuqence_and_get_updated_request() {
let request = Request(url: "https://httpbin.org/anything", method: .GET)
let request = Request(url: FRTestURL.anythingURL, method: .GET)
RequestInterceptorRegistry.shared.registerInterceptors(interceptors: [InterceptorSequenceOne(), InterceptorSequenceTwo(), InterceptorSequenceThree()])
let ex = self.expectation(description: "Request submit")
RestClient.shared.invoke(request: request, action: Action(type: .AUTHENTICATE)) { (result) in
Expand Down Expand Up @@ -275,7 +275,7 @@ class RequestInterceptorTests: FRBaseTestCase {


func test_09_action_with_payload() {
let request = Request(url: "https://httpbin.org/anything", method: .GET)
let request = Request(url: FRTestURL.anythingURL, method: .GET)
RequestInterceptorRegistry.shared.registerInterceptors(interceptors: [PayloadInterceptor()])
let ex = self.expectation(description: "Request submit")
RestClient.shared.invoke(request: request, action: Action(type: .AUTHENTICATE, payload: ["testKey":"testVal"])) { (result) in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,6 @@
BlueprintName = "FRAuthTests"
ReferencedContainer = "container:../FRAuth/FRAuth.xcodeproj">
</BuildableReference>
<SkippedTests>
<Test
Identifier = "AA_05_DeviceBindingCallbackTest">
</Test>
<Test
Identifier = "AA_06_DeviceSigningVerifierCallbackTest">
</Test>
</SkippedTests>
</TestableReference>
<TestableReference
skipped = "NO">
Expand Down
2 changes: 1 addition & 1 deletion FRTestHost/FRTestHost/FRAuthConfigPKHash.plist
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<string>signUp</string>
<key>forgerock_ssl_pinning_public_key_hashes</key>
<array>
<string>j5bzD/UjYVE+0feXsngcrVs3i1vSaoOOtPgpLBb9Db8=</string>
<string>xuycWePFSVzZILxPmRYEM5D3KEnTExjc5n4BjsYBX0I=</string>
</array>
</dict>
</plist>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// FRBaseTestCase.swift
// FRAuthTests
//
// Copyright (c) 2020-2021 ForgeRock. All rights reserved.
// Copyright (c) 2020-2023 ForgeRock. All rights reserved.
//
// This software may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
Expand Down Expand Up @@ -156,3 +156,12 @@ class InternalRequestInterceptor: RequestInterceptor {
return request
}
}

public enum FRTestURL {
static let baseURL = "https://httpbin.org"
static let imageURL = baseURL + "/image"
static let anythingURL = baseURL + "/anything"
static let getURL = baseURL + "/get"
static let status401URL = baseURL + "/status/401"
}

Loading