From 755ff557e2e28a7b41cb35c6dc08e0b1268e21a3 Mon Sep 17 00:00:00 2001
From: Francesco Paolo Severino <francescopaolo.severino@gmail.com>
Date: Fri, 9 Aug 2024 19:25:58 +0200
Subject: [PATCH] Add APNS tests

---
 Tests/OrdersTests/OrdersTests.swift | 11 +++++++++++
 Tests/PassesTests/PassesTests.swift | 11 +++++++++++
 2 files changed, 22 insertions(+)

diff --git a/Tests/OrdersTests/OrdersTests.swift b/Tests/OrdersTests/OrdersTests.swift
index 6558aa3..6e5554a 100644
--- a/Tests/OrdersTests/OrdersTests.swift
+++ b/Tests/OrdersTests/OrdersTests.swift
@@ -45,6 +45,7 @@ final class OrdersTests: XCTestCase {
                 try req.content.encode(RegistrationDTO(pushToken: pushToken))
             },
             afterResponse: { res async throws in
+                XCTAssertNotEqual(res.status, .unauthorized)
                 XCTAssertEqual(res.status, .created)
             }
         )
@@ -57,6 +58,7 @@ final class OrdersTests: XCTestCase {
                 try req.content.encode(RegistrationDTO(pushToken: pushToken))
             },
             afterResponse: { res async throws in
+                XCTAssertNotEqual(res.status, .unauthorized)
                 XCTAssertEqual(res.status, .ok)
             }
         )
@@ -91,6 +93,7 @@ final class OrdersTests: XCTestCase {
             "\(ordersURI)devices/\(deviceLibraryIdentifier)/registrations/\(order.orderTypeIdentifier)/\(order.requireID())",
             headers: ["Authorization": "AppleOrder \(order.authenticationToken)"],
             afterResponse: { res async throws in
+                XCTAssertNotEqual(res.status, .unauthorized)
                 XCTAssertEqual(res.status, .ok)
             }
         )
@@ -116,4 +119,12 @@ final class OrdersTests: XCTestCase {
         XCTAssertEqual(logs[0].message, log1)
         XCTAssertEqual(logs[1].message, log2)
     }
+
+    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.sendPushNotifications(for: order, on: app.db)
+    }
 }
diff --git a/Tests/PassesTests/PassesTests.swift b/Tests/PassesTests/PassesTests.swift
index 7cac819..bd489a5 100644
--- a/Tests/PassesTests/PassesTests.swift
+++ b/Tests/PassesTests/PassesTests.swift
@@ -72,6 +72,7 @@ final class PassesTests: XCTestCase {
                 try req.content.encode(RegistrationDTO(pushToken: pushToken))
             },
             afterResponse: { res async throws in
+                XCTAssertNotEqual(res.status, .unauthorized)
                 XCTAssertEqual(res.status, .created)
             }
         )
@@ -84,6 +85,7 @@ final class PassesTests: XCTestCase {
                 try req.content.encode(RegistrationDTO(pushToken: pushToken))
             },
             afterResponse: { res async throws in
+                XCTAssertNotEqual(res.status, .unauthorized)
                 XCTAssertEqual(res.status, .ok)
             }
         )
@@ -116,6 +118,7 @@ final class PassesTests: XCTestCase {
             "\(passesURI)devices/\(deviceLibraryIdentifier)/registrations/\(pass.passTypeIdentifier)/\(pass.requireID())",
             headers: ["Authorization": "ApplePass \(pass.authenticationToken)"],
             afterResponse: { res async throws in
+                XCTAssertNotEqual(res.status, .unauthorized)
                 XCTAssertEqual(res.status, .ok)
             }
         )
@@ -141,4 +144,12 @@ final class PassesTests: XCTestCase {
         XCTAssertEqual(logs[0].message, log1)
         XCTAssertEqual(logs[1].message, log2)
     }
+
+    func testAPNSClient() async throws {
+        XCTAssertNotNil(app.apns.client(.init(string: "passes")))
+        let passData = PassData(title: "Test Pass")
+        try await passData.create(on: app.db)
+        let pass = try await passData.$pass.get(on: app.db)
+        try await passesService.sendPushNotifications(for: pass, on: app.db)
+    }
 }