Skip to content

Commit

Permalink
Update OrdersTests to Swift Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino committed Oct 11, 2024
1 parent 805a194 commit 2e13021
Show file tree
Hide file tree
Showing 4 changed files with 487 additions and 507 deletions.
179 changes: 76 additions & 103 deletions Tests/OrdersTests/EncryptedOrdersTests.swift
Original file line number Diff line number Diff line change
@@ -1,116 +1,89 @@
import Fluent
import FluentSQLiteDriver
import FluentKit
import PassKit
import Testing
import XCTVapor
import Zip

@testable import Orders

final class EncryptedOrdersTests: XCTestCase {
struct EncryptedOrdersTests {
let delegate = EncryptedOrdersDelegate()
let ordersURI = "/api/orders/v1/"
var ordersService: OrdersService!
var app: Application!

override func setUp() async throws {
self.app = try await Application.make(.testing)
app.databases.use(.sqlite(.memory), as: .sqlite)

OrdersService.register(migrations: app.migrations)
app.migrations.add(CreateOrderData())
ordersService = try OrdersService(
app: app,
delegate: delegate,
pushRoutesMiddleware: SecretMiddleware(secret: "foo"),
logger: app.logger
)
app.databases.middleware.use(OrderDataMiddleware(service: ordersService), on: .sqlite)

try await app.autoMigrate()

Zip.addCustomFileExtension("order")
}

override func tearDown() async throws {
try await app.autoRevert()
try await self.app.asyncShutdown()
self.app = nil
@Test func orderGeneration() async throws {
try await withApp(delegate: delegate) { app, ordersService in
let orderData = OrderData(title: "Test Order")
try await orderData.create(on: app.db)
let order = try await orderData.$order.get(on: app.db)
let data = try await ordersService.generateOrderContent(for: order, on: app.db)
let orderURL = FileManager.default.temporaryDirectory.appendingPathComponent("test.order")
try data.write(to: orderURL)
let orderFolder = try Zip.quickUnzipFile(orderURL)

#expect(FileManager.default.fileExists(atPath: orderFolder.path.appending("/signature")))

let passJSONData = try String(contentsOfFile: orderFolder.path.appending("/order.json")).data(using: .utf8)
let passJSON = try JSONSerialization.jsonObject(with: passJSONData!) as! [String: Any]
#expect(passJSON["authenticationToken"] as? String == order.authenticationToken)
let orderID = try order.requireID().uuidString
#expect(passJSON["orderIdentifier"] as? String == orderID)

let manifestJSONData = try String(contentsOfFile: orderFolder.path.appending("/manifest.json")).data(using: .utf8)
let manifestJSON = try JSONSerialization.jsonObject(with: manifestJSONData!) as! [String: Any]
let iconData = try Data(contentsOf: orderFolder.appendingPathComponent("/icon.png"))
let iconHash = Array(SHA256.hash(data: iconData)).hex
#expect(manifestJSON["icon.png"] as? String == iconHash)
}
}

func testOrderGeneration() 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 data = try await ordersService.generateOrderContent(for: order, on: app.db)
let orderURL = FileManager.default.temporaryDirectory.appendingPathComponent("test.order")
try data.write(to: orderURL)
let orderFolder = try Zip.quickUnzipFile(orderURL)

XCTAssert(FileManager.default.fileExists(atPath: orderFolder.path.appending("/signature")))

let passJSONData = try String(contentsOfFile: orderFolder.path.appending("/order.json"))
.data(using: .utf8)
let passJSON = try JSONSerialization.jsonObject(with: passJSONData!) as! [String: Any]
XCTAssertEqual(passJSON["authenticationToken"] as? String, order.authenticationToken)
try XCTAssertEqual(passJSON["orderIdentifier"] as? String, order.requireID().uuidString)

let manifestJSONData = try String(
contentsOfFile: orderFolder.path.appending("/manifest.json")
).data(using: .utf8)
let manifestJSON =
try JSONSerialization.jsonObject(with: manifestJSONData!) as! [String: Any]
let iconData = try Data(contentsOf: orderFolder.appendingPathComponent("/icon.png"))
let iconHash = Array(SHA256.hash(data: iconData)).hex
XCTAssertEqual(manifestJSON["icon.png"] as? String, iconHash)
}

func testAPNSClient() async throws {
XCTAssertNotNil(app.apns.client(.init(string: "orders")))

let orderData = OrderData(title: "Test Order")
try await orderData.create(on: app.db)
let order = try await orderData._$order.get(on: app.db)

try await ordersService.sendPushNotificationsForOrder(
id: order.requireID(), of: order.orderTypeIdentifier, on: app.db)

let deviceLibraryIdentifier = "abcdefg"
let pushToken = "1234567890"

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

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: pushToken))
},
afterResponse: { res async throws in
XCTAssertEqual(res.status, .created)
}
)

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

// Test `OrderDataMiddleware` update method
orderData.title = "Test Order 2"
do {
try await orderData.update(on: app.db)
} catch {}
@Test func apnsClient() async throws {
try await withApp(delegate: delegate) { app, ordersService in
#expect(app.apns.client(.init(string: "orders")) != nil)

let orderData = OrderData(title: "Test Order")
try await orderData.create(on: app.db)
let order = try await orderData._$order.get(on: app.db)

try await ordersService.sendPushNotificationsForOrder(id: order.requireID(), of: order.orderTypeIdentifier, on: app.db)

let deviceLibraryIdentifier = "abcdefg"
let pushToken = "1234567890"

try await app.test(
.POST,
"\(ordersURI)push/\(order.orderTypeIdentifier)/\(order.requireID())",
headers: ["X-Secret": "foo"],
afterResponse: { res async throws in
#expect(res.status == .noContent)
}
)

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: pushToken))
},
afterResponse: { res async throws in
#expect(res.status == .created)
}
)

try await app.test(
.POST,
"\(ordersURI)push/\(order.orderTypeIdentifier)/\(order.requireID())",
headers: ["X-Secret": "foo"],
afterResponse: { res async throws in
#expect(res.status == .internalServerError)
}
)

// Test `OrderDataMiddleware` update method
orderData.title = "Test Order 2"
do {
try await orderData.update(on: app.db)
} catch {}
}
}
}
Loading

0 comments on commit 2e13021

Please sign in to comment.