Skip to content

chore: refactor webauthn integration tests to use device id from booted simulator #3923

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

Closed
wants to merge 2 commits into from
Closed
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
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
8 changes: 8 additions & 0 deletions .github/workflows/integ_test_auth_webauthn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ jobs:
npm install
npm start &
shell: bash

- name: Verify server is running
run: |
curl --fail -X POST http://127.0.0.1:9293/boot || exit 1

- name: Run iOS Integration Tests
id: run-tests
Expand All @@ -85,6 +89,10 @@ jobs:
derived_data_path: ${{ github.workspace }}/Build
disable_package_resolution: ${{ steps.dependencies-cache.outputs.cache-hit }}

- name: Enrol Biometrics
run: |
curl --fail -X POST http://127.0.0.1:9293/enroll || exit 1

- name: Retry iOS Integration Tests
if: steps.run-tests.outcome=='failure'
id: retry-tests
Expand Down
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
Loading