Skip to content

Commit

Permalink
Enable more tests, work around CI timing problems
Browse files Browse the repository at this point in the history
  • Loading branch information
mikenachbaur-okta committed Oct 30, 2024
1 parent 3de733f commit 7f6e664
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 295 deletions.
4 changes: 0 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,6 @@ var package = Package(
.target(name: "TestCommon")
]),

// .target(name: "AuthFoundationTestCommon",
// dependencies: ["AuthFoundation"],
// path: "Tests/AuthFoundationTestCommon"),

// Abstract API Client
.target(name: "APIClient",
dependencies: [
Expand Down
43 changes: 0 additions & 43 deletions Sources/JWT/JSONPayload.swift

This file was deleted.

30 changes: 17 additions & 13 deletions Tests/APIClientTestCommon/MockApiRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@ import Foundation
import FoundationNetworking
#endif

//struct MockApiRequest: APIRequest {
// var url: URL
// var cachePolicy: URLRequest.CachePolicy
//
// typealias ResponseType = Token
//
// init(url: URL,
// cachePolicy: URLRequest.CachePolicy = .reloadIgnoringLocalAndRemoteCacheData)
// {
// self.url = url
// self.cachePolicy = cachePolicy
// }
//}
struct SampleResponse: Decodable, Sendable {
let message: String
}

struct MockApiRequest<ResponseType: Decodable & Sendable>: APIRequest {
typealias ResponseType = ResponseType

var url: URL
var cachePolicy: URLRequest.CachePolicy

init(url: URL,
cachePolicy: URLRequest.CachePolicy = .reloadIgnoringLocalAndRemoteCacheData)
{
self.url = url
self.cachePolicy = cachePolicy
}
}
108 changes: 56 additions & 52 deletions Tests/APIClientTests/APIClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,55 +29,59 @@ struct MockApiParsingContext: APIParsingContext {
}
}

//class APIClientTests: XCTestCase {
// var client: MockApiClient!
// let baseUrl = URL(string: "https://example.okta.com/oauth2/v1/token")!
// var configuration: MockAPIClientConfiguration!
// let urlSession = URLSessionMock()
// let requestId = UUID().uuidString
//
// override func setUpWithError() throws {
// configuration = MockAPIClientConfiguration(baseURL: baseUrl,
// clientId: "clientid",
// scopes: "openid")
// client = MockApiClient(configuration: configuration,
// session: urlSession,
// baseURL: baseUrl)
// }
//
// @MainActor
// func testOverrideRequestResult() throws {
// client = MockApiClient(configuration: configuration,
// session: urlSession,
// baseURL: baseUrl,
// shouldRetry: .doNotRetry)
//
// urlSession.expect("https://example.okta.com/oauth2/v1/token",
// data: try data(filename: "token",
// matching: "APIClientTestCommon"),
// statusCode: 400,
// contentType: "application/json",
// headerFields: ["x-rate-limit-limit": "0",
// "x-rate-limit-remaining": "0",
// "x-rate-limit-reset": "1609459200",
// "Date": "Fri, 09 Sep 2022 02:22:14 GMT",
// "x-okta-request-id": requestId])
//
// let apiRequest = MockApiRequest(url: baseUrl)
// let context = MockApiParsingContext(result: .success)
//
// let expect = expectation(description: "network request")
// apiRequest.send(to: client, parsing: context, completion: { result in
// switch result {
// case .success(let response):
// XCTAssertEqual(response.statusCode, 400)
// case .failure(_):
// XCTFail("Did not expect the request to fail")
// }
// expect.fulfill()
// })
// waitForExpectations(timeout: 9.0) { error in
// XCTAssertNil(error)
// }
// }
//}
class APIClientTests: XCTestCase {
var client: MockApiClient!
let baseUrl = URL(string: "https://example.okta.com/oauth2/v1/token")!
var configuration: MockAPIClientConfiguration!
let urlSession = URLSessionMock()
let requestId = UUID().uuidString

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

override func setUpWithError() throws {
configuration = MockAPIClientConfiguration(baseURL: baseUrl,
clientId: "clientid",
scopes: "openid")
client = MockApiClient(configuration: configuration,
session: urlSession,
baseURL: baseUrl)
}

func testOverrideRequestResult() throws {
client = MockApiClient(configuration: configuration,
session: urlSession,
baseURL: baseUrl,
shouldRetry: .doNotRetry)

urlSession.expect("https://example.okta.com/oauth2/v1/token",
data: data(for: """
{ "message": "Hello world!" }
"""),
statusCode: 400,
contentType: "application/json",
headerFields: ["x-rate-limit-limit": "0",
"x-rate-limit-remaining": "0",
"x-rate-limit-reset": "1609459200",
"Date": "Fri, 09 Sep 2022 02:22:14 GMT",
"x-okta-request-id": requestId])

let apiRequest: MockApiRequest<SampleResponse> = MockApiRequest(url: baseUrl)
let context = MockApiParsingContext(result: .success)

let expect = expectation(description: "network request")
apiRequest.send(to: client, parsing: context, completion: { result in
switch result {
case .success(let response):
XCTAssertEqual(response.statusCode, 400)
case .failure(_):
XCTFail("Did not expect the request to fail")
}
expect.fulfill()
})
waitForExpectations(timeout: 9.0) { error in
XCTAssertNil(error)
}
}
}
Loading

0 comments on commit 7f6e664

Please sign in to comment.