Skip to content

Commit

Permalink
chore: refactor webauthn integration tests to use device id from boot…
Browse files Browse the repository at this point in the history
…ed simulator
  • Loading branch information
harsh62 committed Nov 25, 2024
1 parent e4cf95c commit 3de49df
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/integ_test_auth.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ on:
required: true
default: true
type: boolean
webauthn-ios:
description: 'WebAuthn iOS'
required: true
default: true
type: boolean
workflow_call:

permissions:
Expand Down Expand Up @@ -60,6 +65,7 @@ jobs:
secrets: inherit

auth-webauthn-integration-test-iOS:
if: ${{ inputs.webauthn-ios != 'false' }}
name: Auth WebAuthn Integration Tests (iOS)
uses: ./.github/workflows/integ_test_auth_webauthn.yml
secrets: inherit
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,9 @@ final class AuthWebAuthnAppUITests: XCTestCase {
private var springboard: XCUIApplication!
private var continueButton: XCUIElement!

private lazy var deviceIdentifier: String = {
let paths = Bundle.main.bundleURL.pathComponents
guard let index = paths.firstIndex(where: { $0 == "Devices" }),
let identifier = paths.dropFirst(index + 1).first
else {
fatalError("Failed to get device identifier")
}

return identifier
}()

@MainActor
override func setUp() async throws {
continueAfterFailure = false
try await bootDevice()
try await enrollBiometrics()
if ProcessInfo.processInfo.arguments.contains("GEN2") {
app.launchArguments.append("GEN2")
Expand Down Expand Up @@ -162,25 +150,25 @@ final class AuthWebAuthnAppUITests: XCTestCase {
}

private func bootDevice() async throws {
let request = LocalServer.boot(deviceIdentifier).urlRequest
let request = LocalServer.boot.urlRequest
let (_, response) = try await URLSession.shared.data(for: request)
XCTAssertTrue((response as! HTTPURLResponse).statusCode < 300, "Failed to boot the device")
}

private func enrollBiometrics() async throws {
let request = LocalServer.enroll(deviceIdentifier).urlRequest
let request = LocalServer.enroll.urlRequest
let (_, response) = try await URLSession.shared.data(for: request)
XCTAssertTrue((response as! HTTPURLResponse).statusCode < 300, "Failed to enroll biometrics in the device")
}

private func matchBiometrics() async throws {
let request = LocalServer.match(deviceIdentifier).urlRequest
let request = LocalServer.match.urlRequest
let (_, response) = try await URLSession.shared.data(for: request)
XCTAssertTrue((response as! HTTPURLResponse).statusCode < 300, "Failed to match biometrics in the device")
}

private func uninstallApp() async throws {
let request = LocalServer.uninstall(deviceIdentifier).urlRequest
let request = LocalServer.uninstall.urlRequest
let (_, response) = try await URLSession.shared.data(for: request)
XCTAssertTrue((response as! HTTPURLResponse).statusCode < 300, "Failed to uninstall the App")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import Foundation
enum LocalServer {
static let endpoint = "http://127.0.0.1:9293"

case boot(String)
case enroll(String)
case match(String)
case uninstall(String)
case boot
case enroll
case match
case uninstall
}

extension LocalServer {
Expand All @@ -32,11 +32,11 @@ extension LocalServer {

var payload: Data? {
switch self {
case .boot(let deviceId),
.enroll(let deviceId),
.match(let deviceId),
.uninstall(let deviceId):
return try? JSONEncoder().encode(["deviceId": deviceId])
case .boot,
.enroll,
.match,
.uninstall:
return try? JSONEncoder().encode(["deviceId": ""])
}
}

Expand Down
26 changes: 17 additions & 9 deletions AmplifyPlugins/Auth/Tests/AuthWebAuthnApp/LocalServer/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,21 @@ const run = (cmd) => {
})
}

const getDeviceId = async () => {
const cmd = `xcrun simctl list | grep "iPhone" | grep "Booted" | awk -F '[()]' '{print $2}' | uniq`
try {
const deviceId = await run(cmd)
return deviceId.trim()
} catch (error) {
console.error("Failed to retrieve deviceId", error)
throw new Error("Failed to retrieve deviceId")
}
}

app.post('/uninstall', async (req, res) => {
console.log("POST /uninstall ")
const { deviceId } = req.body
try {
const deviceId = await getDeviceId()
const cmd = `xcrun simctl uninstall ${deviceId} ${bundleId}`
await run(cmd)
res.send("Done")
Expand All @@ -34,8 +45,8 @@ app.post('/uninstall', async (req, res) => {

app.post('/boot', async (req, res) => {
console.log("POST /boot ")
const { deviceId } = req.body
try {
const deviceId = await getDeviceId()
const cmd = `xcrun simctl bootstatus ${deviceId} -b`
await run(cmd)
res.send("Done")
Expand All @@ -47,8 +58,8 @@ app.post('/boot', async (req, res) => {

app.post('/enroll', async (req, res) => {
console.log("POST /enroll ")
const { deviceId } = req.body
try {
const deviceId = await getDeviceId()
const cmd = `xcrun simctl spawn ${deviceId} notifyutil -s com.apple.BiometricKit.enrollmentChanged '1' && xcrun simctl spawn ${deviceId} notifyutil -p com.apple.BiometricKit.enrollmentChanged`
await run(cmd)
res.send("Done")
Expand All @@ -58,20 +69,17 @@ app.post('/enroll', async (req, res) => {
}
})


app.post('/match', async (req, res) => {
console.log("POST /match ")
const { deviceId } = req.body
try {
const deviceId = await getDeviceId()
const cmd = `xcrun simctl spawn ${deviceId} notifyutil -p com.apple.BiometricKit_Sim.fingerTouch.match`
await run(cmd)
res.send("Done")
} catch (error) {
console.error("Failed to match biometrics", error)
console.error("Failed to match biometrics in the device", error)
res.sendStatus(500)
}
})

app.listen(9293, () => {
console.log("Simulator server started!")
})
export default app

0 comments on commit 3de49df

Please sign in to comment.