|
| 1 | +import XCTest |
| 2 | +@testable import WebAuthn |
| 3 | + |
| 4 | +// swiftlint:disable line_length |
| 5 | + |
| 6 | +final class ParsedCredentialCreationResponseTests: XCTestCase { |
| 7 | + func testInitFromRawResponseFailsWithInvalidRawID() throws { |
| 8 | + let registrationCredential = createRegistrationCredential(rawID: "%") |
| 9 | + |
| 10 | + XCTAssertThrowsError(try ParsedCredentialCreationResponse(from: registrationCredential)) { error in |
| 11 | + XCTAssertEqual(error as? WebAuthnError, .invalidRawID) |
| 12 | + } |
| 13 | + } |
| 14 | + |
| 15 | + func testInitFromRawResponseFailsWithInvalidType() throws { |
| 16 | + let registrationCredential = createRegistrationCredential(type: "some invalid type") |
| 17 | + |
| 18 | + XCTAssertThrowsError(try ParsedCredentialCreationResponse(from: registrationCredential)) { error in |
| 19 | + XCTAssertEqual(error as? WebAuthnError, .invalidCredentialCreationType) |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + func testInitFromRawResponseSucceeds() throws { |
| 24 | + let registrationCredential = createRegistrationCredential() |
| 25 | + XCTAssertNoThrow(try ParsedCredentialCreationResponse(from: registrationCredential)) |
| 26 | + } |
| 27 | + |
| 28 | + private func createRegistrationCredential( |
| 29 | + id: String = "123", |
| 30 | + type: String = "public-key", |
| 31 | + rawID: String = "123", |
| 32 | + clientDataJSON: String = "eyJ0eXBlIjoid2ViYXV0aG4uY3JlYXRlIiwiY2hhbGxlbmdlIjoiY21GdVpHOXRVM1J5YVc1blJuSnZiVk5sY25abGNnIiwib3JpZ2luIjoiaHR0cDovL2xvY2FsaG9zdDo4MDgwIiwiY3Jvc3NPcmlnaW4iOmZhbHNlLCJvdGhlcl9rZXlzX2Nhbl9iZV9hZGRlZF9oZXJlIjoiZG8gbm90IGNvbXBhcmUgY2xpZW50RGF0YUpTT04gYWdhaW5zdCBhIHRlbXBsYXRlLiBTZWUgaHR0cHM6Ly9nb28uZ2wveWFiUGV4In0", |
| 33 | + attestationObject: String = "o2NmbXRmcGFja2VkZ2F0dFN0bXSiY2FsZyZjc2lnWEcwRQIgNTRtpI_SOOZVzU1pN_4cX-osqUPiHMOW48qqq91DXfUCIQC-MHiaIxt2OdIxgqYnyUDHceevNOMfPibenabQGvXgjGhhdXRoRGF0YVikSZYN5YgOjGh0NBcPZHZgW4_krrmihjLHmVzzuoMdl2NFAAAAAK3OAAI1vMYKZIsLJfHwVQMAIDo-5W3Kur7A7y9Lfw7ijhExfCz3_5coMEQNY_y6p-JrpQECAyYgASFYIJr_yLoYbYWgcf7aQcd7pcjUj-3o8biafWQH28WijQSvIlggPI2KqqRQ26KKuFaJ0yH7nouCBrzHu8qRONW-CPa9VDM" |
| 34 | + ) -> RegistrationCredential { |
| 35 | + RegistrationCredential( |
| 36 | + id: id, |
| 37 | + type: type, |
| 38 | + rawID: rawID, |
| 39 | + attestationResponse: .init(clientDataJSON: clientDataJSON, attestationObject: attestationObject) |
| 40 | + ) |
| 41 | + } |
| 42 | +} |
0 commit comments