Skip to content

Commit

Permalink
Add API tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino committed Aug 9, 2024
1 parent 1a01afe commit 7e631ed
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Sources/Orders/OrdersServiceCustom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ extension OrdersServiceCustom {
)

let zipFile = tmp.appendingPathComponent("\(UUID().uuidString).order")
try await FileManager.default.zipItem(at: root, to: zipFile, shouldKeepParent: false)
try FileManager.default.zipItem(at: root, to: zipFile, shouldKeepParent: false)
defer { _ = try? FileManager.default.removeItem(at: zipFile) }

return try Data(contentsOf: zipFile)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Passes/PassesServiceCustom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ extension PassesServiceCustom {
)

let zipFile = tmp.appendingPathComponent("\(UUID().uuidString).pkpass")
try await FileManager.default.zipItem(at: root, to: zipFile, shouldKeepParent: false)
try FileManager.default.zipItem(at: root, to: zipFile, shouldKeepParent: false)
defer { _ = try? FileManager.default.removeItem(at: zipFile) }

return try Data(contentsOf: zipFile)
Expand Down Expand Up @@ -587,7 +587,7 @@ extension PassesServiceCustom {
}

let zipFile = tmp.appendingPathComponent("\(UUID().uuidString).pkpasses")
try await FileManager.default.zipItem(at: root, to: zipFile, shouldKeepParent: false)
try FileManager.default.zipItem(at: root, to: zipFile, shouldKeepParent: false)
defer { _ = try? FileManager.default.removeItem(at: zipFile) }

return try Data(contentsOf: zipFile)
Expand Down
44 changes: 43 additions & 1 deletion Tests/OrdersTests/OrdersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import XCTVapor
import Fluent
import FluentSQLiteDriver
@testable import Orders
@testable import PassKit

final class OrdersTests: XCTestCase {
var app: Application!
let orderDelegate = TestOrdersDelegate()
let ordersURI = "/api/orders/v1/"
var ordersService: OrdersService!
var app: Application!

override func setUp() async throws {
self.app = try await Application.make(.testing)
Expand All @@ -27,4 +29,44 @@ final class OrdersTests: XCTestCase {
let data = try await ordersService.generateOrderContent(for: order, on: app.db)
XCTAssertNotNil(data)
}

func testAPIDeviceRegistration() async throws {
let orderData = OrderData(title: "Test Order")
try await orderData.create(on: app.db)
let order = try await orderData.$order.get(on: app.db)
let deviceLibraryIdentifier = "abcdefg"

try await app.test(
.POST,
"\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.orderTypeIdentifier)/\(order.requireID())",
headers: ["Authorization": "AppleOrder \(order.authenticationToken)"],
beforeRequest: { req async throws in
try req.content.encode(RegistrationDTO(pushToken: "1234567890"))
},
afterResponse: { res async throws in
XCTAssertEqual(res.status, .created)
}
)

try await app.test(
.POST,
"\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.orderTypeIdentifier)/\(order.requireID())",
headers: ["Authorization": "AppleOrder \(order.authenticationToken)"],
beforeRequest: { req async throws in
try req.content.encode(RegistrationDTO(pushToken: "1234567890"))
},
afterResponse: { res async throws in
XCTAssertEqual(res.status, .ok)
}
)

try await app.test(
.DELETE,
"\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.orderTypeIdentifier)/\(order.requireID())",
headers: ["Authorization": "AppleOrder \(order.authenticationToken)"],
afterResponse: { res async throws in
XCTAssertEqual(res.status, .ok)
}
)
}
}
44 changes: 43 additions & 1 deletion Tests/PassesTests/PassesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import XCTVapor
import Fluent
import FluentSQLiteDriver
@testable import Passes
@testable import PassKit

final class PassesTests: XCTestCase {
var app: Application!
let passDelegate = TestPassesDelegate()
let passesURI = "/api/passes/v1/"
var passesService: PassesService!
var app: Application!

override func setUp() async throws {
self.app = try await Application.make(.testing)
Expand Down Expand Up @@ -54,4 +56,44 @@ final class PassesTests: XCTestCase {

XCTAssertGreaterThan(dataPersonalize.count, data.count)
}

func testAPIDeviceRegistration() async throws {
let passData = PassData(title: "Test Pass")
try await passData.create(on: app.db)
let pass = try await passData.$pass.get(on: app.db)
let deviceLibraryIdentifier = "abcdefg"

try await app.test(
.POST,
"\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.passTypeIdentifier)/\(pass.requireID())",
headers: ["Authorization": "ApplePass \(pass.authenticationToken)"],
beforeRequest: { req async throws in
try req.content.encode(RegistrationDTO(pushToken: "1234567890"))
},
afterResponse: { res async throws in
XCTAssertEqual(res.status, .created)
}
)

try await app.test(
.POST,
"\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.passTypeIdentifier)/\(pass.requireID())",
headers: ["Authorization": "ApplePass \(pass.authenticationToken)"],
beforeRequest: { req async throws in
try req.content.encode(RegistrationDTO(pushToken: "1234567890"))
},
afterResponse: { res async throws in
XCTAssertEqual(res.status, .ok)
}
)

try await app.test(
.DELETE,
"\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.passTypeIdentifier)/\(pass.requireID())",
headers: ["Authorization": "ApplePass \(pass.authenticationToken)"],
afterResponse: { res async throws in
XCTAssertEqual(res.status, .ok)
}
)
}
}

0 comments on commit 7e631ed

Please sign in to comment.