Skip to content

Commit

Permalink
Introduce initial GitHub Action for unit testing (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikenachbaur-okta authored Nov 4, 2021
1 parent 50aeee5 commit a63482f
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 75 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/okta-idx.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: OktaIdx

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
UnitTests:
runs-on: macos-11
strategy:
matrix:
xcode: [ "13.1", "12.4" ]
env:
DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode }}.app/Contents/Developer
steps:
- uses: actions/checkout@v2
- name: Xcode iOS Unit Tests
run: xcodebuild -workspace okta-idx.xcworkspace -scheme "okta-idx-ios" -destination "platform=iOS Simulator,OS=latest,name=iPhone 11" clean test
- name: Swift Tests
run: swift test -v
PackageValidation:
runs-on: macos-11
env:
DEVELOPER_DIR: /Applications/Xcode_13.1.app/Contents/Developer
steps:
- uses: actions/checkout@v2
- name: Cocoapods
run: pod lib lint
28 changes: 0 additions & 28 deletions .travis.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,26 @@ internal extension Data {
/// Produces a SHA256 hash of the supplied data.
/// - Returns: SHA256 representation of the data.
func sha256() -> Data {
#if canImport(CryptoKit)
if #available(iOS 13.0, macCatalyst 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) {
return Data(SHA256.hash(data: self))
} else {
var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
self.withUnsafeBytes {
_ = CC_SHA256($0.baseAddress, CC_LONG(self.count), &hash)
}
return Data(hash)
return commonCryptoSHA256()
}
#else
return commonCryptoSHA256()
#endif
}

private func commonCryptoSHA256() -> Data {
var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
self.withUnsafeBytes {
_ = CC_SHA256($0.baseAddress, CC_LONG(self.count), &hash)
}
return Data(hash)
}


/// Encodes the data as a URL-safe Base64 string.
/// - Returns: Base64 URL-encoded string.
func base64URLEncodedString() -> String {
Expand Down
4 changes: 2 additions & 2 deletions Sources/OktaIdx/Internal/Utilities/DebugDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ struct DebugDescription<T: Any> {
extension String {
func indentingNewlines(by spaceCount: Int) -> String {
let spaces = String(repeating: " ", count: spaceCount)
let components = components(separatedBy: "\n")
let items = components(separatedBy: "\n")

return String(components.map { "\n" + spaces + $0 }.joined().dropFirst())
return String(items.map { "\n" + spaces + $0 }.joined().dropFirst())
}
}
29 changes: 15 additions & 14 deletions Tests/OktaIdxTests/IDXClientDelegateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,21 @@ class IDXClientDelegateTests: XCTestCase {
client = IDXClient(context: context, api: api)
client.delegate = delegate

remediationOption = .init(client: client,
name: "cancel",
method: "GET",
href: URL(string: "some://url")!,
accepts: "application/json",
form: IDXClient.Remediation.Form(fields: [
IDXClient.Remediation.Form.Field(name: "foo",
visible: false,
mutable: true,
required: false,
secret: false)
])!,
refresh: nil,
relatesTo: nil)
remediationOption = IDXClient.Remediation(
client: client,
name: "cancel",
method: "GET",
href: URL(string: "some://url")!,
accepts: "application/json",
form: IDXClient.Remediation.Form(fields: [
IDXClient.Remediation.Form.Field(name: "foo",
visible: false,
mutable: true,
required: false,
secret: false)
])!,
refresh: nil,
relatesTo: nil)
token = IDXClient.Token(accessToken: "access",
refreshToken: "refresh",
expiresIn: 10,
Expand Down
39 changes: 14 additions & 25 deletions Tests/TestCommon/Mocks/IDXClientAPIMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ class MockBase {
}

func result<T>(for name: String) -> Result<T, IDXClientError> {
let response = response(for: name)
if let result = response?["response"] as? T {
let responseData = response(for: name)
if let result = responseData?["response"] as? T {
return .success(result)
}

let error = response?["error"] as? IDXClientError ?? IDXClientError.invalidClient
let error = responseData?["error"] as? IDXClientError ?? IDXClientError.invalidClient
return .failure(error)
}
}
Expand All @@ -54,35 +54,31 @@ class IDXClientAPIMock: MockBase, IDXClientAPI {
func resume(completion: IDXClient.ResponseResult?) {
recordedCalls.append(RecordedCall(function: #function,
arguments: [:]))
let result: Result<IDXClient.Response, IDXClientError> = result(for: #function)
completion?(result)
completion?(result(for: #function))
}

func proceed(remediation option: IDXClient.Remediation, completion: IDXClient.ResponseResult?) {
recordedCalls.append(RecordedCall(function: #function,
arguments: [
"remediation": option as Any,
]))
let result: Result<IDXClient.Response, IDXClientError> = result(for: #function)
completion?(result)
completion?(result(for: #function))
}

func exchangeCode(redirect url: URL, completion: IDXClient.TokenResult?) {
recordedCalls.append(RecordedCall(function: #function,
arguments: [
"redirect": url as Any
]))
let result: Result<IDXClient.Token, IDXClientError> = result(for: #function)
completion?(result)
completion?(result(for: #function))
}

func exchangeCode(using remediation: IDXClient.Remediation, completion: IDXClient.TokenResult?) {
recordedCalls.append(RecordedCall(function: #function,
arguments: [
"using": response as Any
]))
let result: Result<IDXClient.Token, IDXClientError> = result(for: #function)
completion?(result)
completion?(result(for: #function))
}

func redirectResult(for url: URL) -> IDXClient.RedirectResult {
Expand All @@ -109,23 +105,20 @@ class IDXClientAPIv1Mock: MockBase, IDXClientAPIImpl {
arguments: [
"state": state as Any
]))
let result: Result<IDXClient.Context, IDXClientError> = result(for: #function)
completion(result)
completion(result(for: #function))
}

func resume(completion: @escaping (Result<IDXClient.Response, IDXClientError>) -> Void) {
recordedCalls.append(RecordedCall(function: #function, arguments: nil))
let result: Result<IDXClient.Response, IDXClientError> = result(for: #function)
completion(result)
completion(result(for: #function))
}

func proceed(remediation option: IDXClient.Remediation, completion: @escaping (Result<IDXClient.Response, IDXClientError>) -> Void) {
recordedCalls.append(RecordedCall(function: #function,
arguments: [
"remediation": option as Any,
]))
let result: Result<IDXClient.Response, IDXClientError> = result(for: #function)
completion(result)
completion(result(for: #function))
}

func redirectResult(for url: URL) -> IDXClient.RedirectResult {
Expand All @@ -142,17 +135,15 @@ class IDXClientAPIv1Mock: MockBase, IDXClientAPIImpl {
arguments: [
"redirect": url as Any
]))
let result: Result<IDXClient.Token, IDXClientError> = result(for: #function)
completion(result)
completion(result(for: #function))
}

func exchangeCode(using remediation: IDXClient.Remediation, completion: @escaping (Result<IDXClient.Token, IDXClientError>) -> Void) {
recordedCalls.append(RecordedCall(function: #function,
arguments: [
"using": remediation as Any
]))
let result: Result<IDXClient.Token, IDXClientError> = result(for: #function)
completion(result)
completion(result(for: #function))
}

func revoke(token: String, type: String, completion: @escaping (Result<Void, IDXClientError>) -> Void) {
Expand All @@ -161,16 +152,14 @@ class IDXClientAPIv1Mock: MockBase, IDXClientAPIImpl {
"token": token as Any,
"type": type as Any
]))
let result: Result<Void, IDXClientError> = result(for: #function)
completion(result)
completion(result(for: #function))
}

func refresh(token: IDXClient.Token, completion: @escaping (Result<IDXClient.Token, IDXClientError>) -> Void) {
recordedCalls.append(RecordedCall(function: #function,
arguments: [
"token": token as Any
]))
let result: Result<IDXClient.Token, IDXClientError> = result(for: #function)
completion(result)
completion(result(for: #function))
}
}
9 changes: 8 additions & 1 deletion Tests/TestCommon/Utilities/Bundle+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ extension Bundle {
}

static func testResource(folderName: String? = nil, fileName: String) -> URL {
var path = resourcesPath.appendingPathComponent("Resources")
var path = resourcesPath

// Handle differences when run in macOS targets.
if FileManager.default.fileExists(atPath: "\(path.path)/Contents/Resources") {
path.appendPathComponent("Contents/Resources")
}

path.appendPathComponent("Resources")
if let folderName = folderName {
path.appendPathComponent(folderName)
}
Expand Down

0 comments on commit a63482f

Please sign in to comment.