Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino committed Sep 25, 2024
1 parent 051e24b commit 6924e03
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
16 changes: 12 additions & 4 deletions Sources/Passes/PassesError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
//

/// Errors that can be thrown by PassKit passes.
public struct PassesError: Error, Sendable {
public struct PassesError: Error, Sendable, Equatable {
/// The type of the errors that can be thrown by PassKit passes.
public struct ErrorType: Sendable, Hashable, CustomStringConvertible {
enum Base: String, Sendable {
public struct ErrorType: Sendable, Hashable, CustomStringConvertible, Equatable {
enum Base: String, Sendable, Equatable {
case templateNotDirectory
case pemCertificateMissing
case pemPrivateKeyMissing
Expand Down Expand Up @@ -40,12 +40,16 @@ public struct PassesError: Error, Sendable {
}
}

private struct Backing: Sendable {
private struct Backing: Sendable, Equatable {
fileprivate let errorType: ErrorType

init(errorType: ErrorType) {
self.errorType = errorType
}

static func == (lhs: PassesError.Backing, rhs: PassesError.Backing) -> Bool {
lhs.errorType == rhs.errorType
}
}

private var backing: Backing
Expand All @@ -71,6 +75,10 @@ public struct PassesError: Error, Sendable {

/// The number of passes to bundle is invalid.
public static let invalidNumberOfPasses = Self(errorType: .invalidNumberOfPasses)

public static func == (lhs: PassesError, rhs: PassesError) -> Bool {
lhs.backing == rhs.backing
}
}

extension PassesError: CustomStringConvertible {
Expand Down
18 changes: 18 additions & 0 deletions Tests/OrdersTests/OrdersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ final class OrdersTests: XCTestCase {
}
)

try await app.test(
.GET,
"\(ordersURI)push/\(order.orderTypeIdentifier)/\("not-a-uuid")",
headers: ["X-Secret": "foo"],
afterResponse: { res async throws in
XCTAssertEqual(res.status, .badRequest)
}
)

try await app.test(
.DELETE,
"\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.orderTypeIdentifier)/\(order.requireID())",
Expand Down Expand Up @@ -300,6 +309,15 @@ final class OrdersTests: XCTestCase {
}
)

try await app.test(
.POST,
"\(ordersURI)push/\(order.orderTypeIdentifier)/\("not-a-uuid")",
headers: ["X-Secret": "foo"],
afterResponse: { res async throws in
XCTAssertEqual(res.status, .badRequest)
}
)

// Test `OrderDataMiddleware` update method
orderData.title = "Test Order 2"
do {
Expand Down
25 changes: 25 additions & 0 deletions Tests/PassesTests/PassesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ final class PassesTests: XCTestCase {

let data = try await passesService.generatePassesContent(for: [pass1, pass2], on: app.db)
XCTAssertNotNil(data)

do {
let data = try await passesService.generatePassesContent(for: [pass1], on: app.db)
XCTFail("Expected error, got \(data)")
} catch let error as PassesError {
XCTAssertEqual(error, .invalidNumberOfPasses)
}
}

func testPersonalization() async throws {
Expand Down Expand Up @@ -336,6 +343,15 @@ final class PassesTests: XCTestCase {
}
)

try await app.test(
.GET,
"\(passesURI)push/\(pass.passTypeIdentifier)/\("not-a-uuid")",
headers: ["X-Secret": "foo"],
afterResponse: { res async throws in
XCTAssertEqual(res.status, .badRequest)
}
)

try await app.test(
.DELETE,
"\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.passTypeIdentifier)/\(pass.requireID())",
Expand Down Expand Up @@ -431,6 +447,15 @@ final class PassesTests: XCTestCase {
}
)

try await app.test(
.POST,
"\(passesURI)push/\(pass.passTypeIdentifier)/\("not-a-uuid")",
headers: ["X-Secret": "foo"],
afterResponse: { res async throws in
XCTAssertEqual(res.status, .badRequest)
}
)

// Test `PassDataMiddleware` update method
passData.title = "Test Pass 2"
do {
Expand Down

0 comments on commit 6924e03

Please sign in to comment.