Skip to content

Commit

Permalink
(wip) modify tests to run on Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
sebsto committed Oct 30, 2024
1 parent 87c412f commit 8961472
Show file tree
Hide file tree
Showing 22 changed files with 100 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import CLIlib
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

// swiftlint:disable all
/*
Expand Down Expand Up @@ -103,61 +106,61 @@ struct MFAType: Codable {
}

extension AppleAuthenticator {

// call MFAType API and return the number of digit required for PIN
func handleTwoFactorAuthentication() async throws -> Int {

guard let data = try? await getMFAType(),
let mfaType = try? JSONDecoder().decode(MFAType?.self, from: data)
let mfaType = try? JSONDecoder().decode(MFAType?.self, from: data)
else {
throw AuthenticationError.canNotReadMFATypes
}

// FIXME: - add support for SMS fallback in case there is no trusted device

// I should first understand and handle case where there is a 'trustedDevices' in the answer according to
// https://github.com/fastlane/fastlane/blob/master/spaceship/lib/spaceship/two_step_or_factor_client.rb#L18
// when there is no 'trustedDevices', we are supposed to fallback to SMS to a phone number
// but for my account, the API return no 'trustedDevices' but I still receive the code on my mac and iphone

guard let count = mfaType.trustedPhoneNumbers?.count,
count > 0,
let securityCodeLength = mfaType.securityCode?.length
count > 0,
let securityCodeLength = mfaType.securityCode?.length
else {
log.warning("⚠️ No Security code length provided in answer")
throw AuthenticationError.requires2FATrustedPhoneNumber
}

return securityCodeLength

}

func twoFactorAuthentication(pin: String) async throws {

struct TFACode: Codable {
let code: String
}
struct TFABody: Codable {
let securityCode: TFACode
}

let codeType = "trusteddevice"
let body = TFABody(securityCode: TFACode(code: pin))
let requestHeader = ["X-Apple-Id-Session-Id": session.xAppleIdSessionId!]

let (_, response) = try await apiCall(
url: "https://idmsa.apple.com/appleauth/auth/verify/\(codeType)/securitycode", // swiftlint:disable:this line_length
method: .POST,
body: try JSONEncoder().encode(body),
headers: requestHeader,
validResponse: .range(200..<413)
)

switch response.statusCode {
case 400:
// authentication failed
throw AuthenticationError.invalidPinCode

case 412:
// upgrade account and repair with repair token
// see https://my.diffend.io/gems/fastlane/2.170.0/2.175.0/page/9#d2h-629314-4781
Expand All @@ -169,75 +172,31 @@ extension AppleAuthenticator {
repairToken: "secret"
) // X-Apple-Repair-Session-Token
}

case 200, 204:
// success
try await self.saveSession(response: response, session: session)

default:
// unknown error, fail gracefully
throw AuthenticationError.unexpectedHTTPReturnCode(code: response.statusCode)

}

// should we save additional cookies ?
// return (try await getDESCookie(), session)

}

// by OOP design it should be private. Make it internal (default) for testing
func getMFAType() async throws -> Data? {

let (data, _) = try await apiCall(
url: "https://idmsa.apple.com/appleauth/auth",
validResponse: .range(200..<400)
)

return data

}

// swiftlint:disable all
/*
Tell iTC that we are trustworthy (obviously)
This will update our local cookies to something new
They probably have a longer time to live than the other poor cookies
Changed Keys
- myacinfo
- DES5c148586dfd451e55afb0175f62418f91
We actually only care about the DES value
*/
// @available(OSX 10.12, *)
// private func getDESCookie() async throws -> String? {
//
// if #available(OSX 12.0, *) {
//
// let headers = [ "X-Apple-Id-Session-Id" : session.x_apple_id_session_id!,
// "X-Apple-Widget-Key" : session.itc_service_key!.authServiceKey,
// "Accept" : "application/json",
// "scnt" : session.scnt!]
// let request = request(for: "https://idmsa.apple.com/appleauth/auth/2sv/trust", withHeaders: headers)
//
// log(request: request)
//
// let (data, response) = try await httpClient.data(for: request)
// guard let httpResponse = response as? HTTPURLResponse, (200..<400).contains(httpResponse.statusCode) else {
// logger.debug("URLREsponse = \(response)")
// throw URLError(.badServerResponse)
//
// }
//
// log(response: httpResponse, data: data, error: nil)
//
// guard let cookies = httpResponse.value(forHTTPHeaderField: "Set-Cookie") else {
// return nil
// }
// return cookies
//
// } else {
// logger.critcal("Only works on macOS 10.12 or more recent")
// exit(-1)
// }
// }
// swiftlint:enable all
}
3 changes: 3 additions & 0 deletions Sources/xcodeinstall/API/Authentication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import CLIlib
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

// MARK: - Module Internal structures and data

Expand Down
3 changes: 3 additions & 0 deletions Sources/xcodeinstall/API/DownloadDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import CLIlib
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

// delegate class to receive download progress
class DownloadDelegate: NSObject, URLSessionDownloadDelegate {
Expand Down
3 changes: 3 additions & 0 deletions Sources/xcodeinstall/API/HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import CLIlib
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

/*
This fil contains code to make our APICall testable.
Expand Down
4 changes: 4 additions & 0 deletions Sources/xcodeinstall/API/URLLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import CLIlib
import Foundation
import Logging

#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

// FIXME consider using Swift 5.7 regexp
// https://github.com/apple/swift-evolution/blob/main/proposals/0350-regex-type-overview.md
func filterPassword(_ input: String) -> String {
Expand Down
3 changes: 3 additions & 0 deletions Sources/xcodeinstall/API/URLRequestExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
// Copyright © 2020. All rights reserved.
//
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

extension URLRequest {
public func cURL(pretty: Bool = false) -> String {
Expand Down
3 changes: 3 additions & 0 deletions Sources/xcodeinstall/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import CLIlib
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

/**

Expand Down
3 changes: 3 additions & 0 deletions Sources/xcodeinstall/Secrets/AWSSecretsHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import CLIlib
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

// the errors thrown by the SecretsManager class
enum AWSSecretsHandlerError: Error {
Expand Down
3 changes: 3 additions & 0 deletions Sources/xcodeinstall/Secrets/FileSecretsHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import CLIlib
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

// store secrets on files in $HOME/.xcodeinstaller
struct FileSecretsHandler: SecretsHandlerProtocol {
Expand Down
3 changes: 3 additions & 0 deletions Sources/xcodeinstall/Secrets/SecretsHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
//

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

protocol Secrets {
func data() throws -> Data
Expand Down
3 changes: 3 additions & 0 deletions Tests/xcodeinstallTests/API/AuthenticationTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
//

import XCTest
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

@testable import xcodeinstall

Expand Down
3 changes: 3 additions & 0 deletions Tests/xcodeinstallTests/API/HTTPClientTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
//

import XCTest
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

@testable import xcodeinstall

Expand Down
3 changes: 3 additions & 0 deletions Tests/xcodeinstallTests/API/ListTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
//

import XCTest
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

@testable import xcodeinstall

Expand Down
3 changes: 3 additions & 0 deletions Tests/xcodeinstallTests/API/MFAuthenticationTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
//

import XCTest
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

@testable import xcodeinstall

Expand Down
3 changes: 3 additions & 0 deletions Tests/xcodeinstallTests/API/MockedNetworkClasses.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import CLIlib
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

@testable import xcodeinstall

Expand Down
3 changes: 3 additions & 0 deletions Tests/xcodeinstallTests/API/URLRequestCurlTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
//

import XCTest
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

@testable import xcodeinstall

Expand Down
3 changes: 3 additions & 0 deletions Tests/xcodeinstallTests/CLI/CLIAuthTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import ArgumentParser
import XCTest
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

@testable import xcodeinstall

Expand Down
3 changes: 3 additions & 0 deletions Tests/xcodeinstallTests/Secrets/AWSSecretsHandlerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
//

import XCTest
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

@testable import xcodeinstall

Expand Down
4 changes: 4 additions & 0 deletions Tests/xcodeinstallTests/Secrets/AppleSessionSecretTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

import XCTest

#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

@testable import xcodeinstall

final class AppleSessionSecretTest: XCTestCase {
Expand Down
3 changes: 3 additions & 0 deletions Tests/xcodeinstallTests/Secrets/FileSecretsHandlerTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
//

import XCTest
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

@testable import xcodeinstall

Expand Down
3 changes: 3 additions & 0 deletions Tests/xcodeinstallTests/Secrets/MockedSecretsHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
//

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

@testable import xcodeinstall

Expand Down
Loading

0 comments on commit 8961472

Please sign in to comment.