Skip to content

Sanity Test #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ let package = Package(
),
.testTarget(name: "WebPushTests", dependencies: [
.product(name: "AsyncHTTPClient", package: "async-http-client"),
.product(name: "Crypto", package: "swift-crypto"),
.product(name: "Logging", package: "swift-log"),
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "ServiceLifecycle", package: "swift-service-lifecycle"),
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,23 @@ Please check the [releases](https://github.com/mochidev/swift-webpush/releases)
dependencies: [
.package(
url: "https://github.com/mochidev/swift-webPush.git",
.upToNextMinor(from: "0.1.4")
.upToNextMinor(from: "0.2.0")
),
],
...
targets: [
.target(
name: "MyPackage",
dependencies: [
"WebPush",
.product(name: "WebPush", package: "swift-webpush"),
...
]
),
.testTarget(
name: "MyPackageTests",
dependencies: [
"WebPushTesting",
.product(name: "WebPushTesting", package: "swift-webpush"),
...
]
),
]
Expand Down
1 change: 1 addition & 0 deletions Tests/WebPushTests/VAPIDConfiguration+Testing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Foundation
import WebPush

extension VAPID.Configuration {
/// Make a new configuration useful for testing against.
static func makeTesting() -> VAPID.Configuration {
VAPID.Configuration(
key: VAPID.Key(),
Expand Down
53 changes: 53 additions & 0 deletions Tests/WebPushTests/VAPIDConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,54 @@ struct VAPIDConfigurationTests {
"""
)
}

@Test func decodesIncompleteConfiguration() throws {
#expect(
try JSONDecoder().decode(VAPID.Configuration.self, from: Data(
"""
{
"contactInformation" : "mailto:test@example.com",
"expirationDuration" : 79200,
"primaryKey" : "FniTgSrf0l+BdfeC6LiblKXBbY4LQm0S+4STNCoJI+0=",
"validityDuration" : 72000
}
""".utf8
)) ==
VAPID.Configuration(
key: key1,
contactInformation: .email("test@example.com")
)
)
}

@Test func decodesWholeConfiguration() throws {
#expect(
try JSONDecoder().decode(VAPID.Configuration.self, from: Data(
"""
{
"contactInformation" : "mailto:test@example.com",
"deprecatedKeys" : [
"bcZgo/p2WFqXaKFzmYaDKO/gARjWvGi3oXyHM2QNlfE="
],
"expirationDuration" : 3600,
"keys" : [
"wyQaGWNwvXKzVmPIhkqVQvQ+FKx1SNqHJ+re8n2ORrk="
],
"primaryKey" : "FniTgSrf0l+BdfeC6LiblKXBbY4LQm0S+4STNCoJI+0=",
"validityDuration" : 36000
}
""".utf8
)) ==
VAPID.Configuration(
primaryKey: key1,
keys: [key2],
deprecatedKeys: [key1, key2, key3],
contactInformation: .email("test@example.com"),
expirationDuration: .hours(1),
validityDuration: .hours(10)
)
)
}
}

@Suite
Expand Down Expand Up @@ -303,6 +351,11 @@ struct VAPIDConfigurationTests {
#expect(VAPID.Configuration.Duration.seconds(175) > VAPID.Configuration.Duration.minutes(2))
}

@Test func addingToDates() {
let now = Date()
#expect(now.adding(.seconds(5)) == now.addingTimeInterval(5))
}

@Test func coding() throws {
#expect(String(decoding: try JSONEncoder().encode(VAPID.Configuration.Duration(60)), as: UTF8.self) == "60")

Expand Down
67 changes: 51 additions & 16 deletions Tests/WebPushTests/WebPushTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// Copyright © 2024 Mochi Development, Inc. All rights reserved.
//

import AsyncHTTPClient
@preconcurrency import Crypto
import Foundation
import Logging
import ServiceLifecycle
Expand All @@ -14,27 +16,60 @@ import Testing

@Suite("WebPush Manager")
struct WebPushManagerTests {
@Test func webPushManagerInitializesOnItsOwn() async throws {
let manager = WebPushManager(vapidConfiguration: .makeTesting())
await withThrowingTaskGroup(of: Void.self) { group in
group.addTask {
try await manager.run()
@Suite
struct Initialization {
@Test func managerInitializesOnItsOwn() async throws {
let manager = WebPushManager(vapidConfiguration: .makeTesting())
await withThrowingTaskGroup(of: Void.self) { group in
group.addTask {
try await manager.run()
}
group.cancelAll()
}
}

@Test func managerInitializesAsService() async throws {
let logger = Logger(label: "ServiceLogger", factory: { PrintLogHandler(label: $0, metadataProvider: $1) })
let manager = WebPushManager(
vapidConfiguration: .makeTesting(),
logger: logger
)
await withThrowingTaskGroup(of: Void.self) { group in
group.addTask {
try await ServiceGroup(services: [manager], logger: logger).run()
}
group.cancelAll()
}
group.cancelAll()
}
}

@Test func webPushManagerInitializesAsService() async throws {
let logger = Logger(label: "ServiceLogger", factory: { PrintLogHandler(label: $0, metadataProvider: $1) })
let manager = WebPushManager(
vapidConfiguration: .makeTesting(),
logger: logger
)
await withThrowingTaskGroup(of: Void.self) { group in
group.addTask {
try await ServiceGroup(services: [manager], logger: logger).run()
@Suite("Sending Messages")
struct SendingMessages {
@Test func sendSuccessfulTextMessage() async throws {
try await confirmation { requestWasMade in
let vapidConfiguration = VAPID.Configuration.makeTesting()

let subscriberPrivateKey = P256.KeyAgreement.PrivateKey(compactRepresentable: false)
var authenticationSecret: [UInt8] = Array(repeating: 0, count: 16)
for index in authenticationSecret.indices { authenticationSecret[index] = .random(in: .min ... .max) }

let subscriber = Subscriber(
endpoint: URL(string: "https://example.com/subscriber")!,
userAgentKeyMaterial: UserAgentKeyMaterial(publicKey: subscriberPrivateKey.publicKey, authenticationSecret: Data(authenticationSecret)),
vapidKeyID: vapidConfiguration.primaryKey!.id
)

let manager = WebPushManager(
vapidConfiguration: vapidConfiguration,
logger: Logger(label: "WebPushManagerTests", factory: { PrintLogHandler(label: $0, metadataProvider: $1) }),
executor: .httpClient(MockHTTPClient({ request in
requestWasMade()
return HTTPClientResponse(status: .created)
}))
)

try await manager.send(string: "hello", to: subscriber)
}
group.cancelAll()
}
}

Expand Down
Loading