Skip to content

Commit 765b986

Browse files
Refactored mocked key definitions to live publicly in WebPushTesting
1 parent e7fac1c commit 765b986

File tree

3 files changed

+94
-81
lines changed

3 files changed

+94
-81
lines changed

Sources/WebPushTesting/VAPIDConfiguration+Testing.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,5 @@ import WebPush
1212

1313
extension VAPID.Configuration {
1414
/// A mocked configuration useful when testing with the library, since the mocked manager doesn't make use of it anyways.
15-
public static var mocked: Self {
16-
/// Generated using `P256.Signing.PrivateKey(compactRepresentable: false).x963Representation.base64EncodedString()`.
17-
let privateKey = try! P256.Signing.PrivateKey(x963Representation: Data(base64Encoded: "BGEhWik09/s/JNkl0OAcTIdRTb7AoLRZQQG4C96OhlcFVQYH5kMWUML3MZBG3gPXxN1Njn6uXulDysPGMDBR47SurTnyXnbuaJ7VDm3UsVYUs5kFoZM8VB5QtoKpgE7WyQ==")!)
18-
return VAPID.Configuration(
19-
key: .init(privateKey: privateKey),
20-
contactInformation: .email("test@example.com")
21-
)
22-
}
15+
public static let mocked = VAPID.Configuration(key: .mockedKey1, contactInformation: .email("test@example.com"))
2316
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// VAPIDKey+Testing.swift
3+
// swift-webpush
4+
//
5+
// Created by Dimitri Bouniol on 2024-12-18.
6+
// Copyright © 2024 Mochi Development, Inc. All rights reserved.
7+
//
8+
9+
import WebPush
10+
11+
extension VAPID.Key {
12+
/// A mocked key guaranteed to not conflict with ``mockedKey2``, ``mockedKey3``, and ``mockedKey4``.
13+
public static let mockedKey1 = try! VAPID.Key(base64URLEncoded: "FniTgSrf0l+BdfeC6LiblKXBbY4LQm0S+4STNCoJI+0=")
14+
/// A mocked key guaranteed to not conflict with ``mockedKey1``, ``mockedKey3``, and ``mockedKey4``.
15+
public static let mockedKey2 = try! VAPID.Key(base64URLEncoded: "wyQaGWNwvXKzVmPIhkqVQvQ+FKx1SNqHJ+re8n2ORrk=")
16+
/// A mocked key guaranteed to not conflict with ``mockedKey1``, ``mockedKey2``, and ``mockedKey4``.
17+
public static let mockedKey3 = try! VAPID.Key(base64URLEncoded: "bcZgo/p2WFqXaKFzmYaDKO/gARjWvGi3oXyHM2QNlfE=")
18+
/// A mocked key guaranteed to not conflict with ``mockedKey1``, ``mockedKey2``, and ``mockedKey3``.
19+
public static let mockedKey4 = try! VAPID.Key(base64URLEncoded: "BGEhWik09/s/JNkl0OAcTIdRTb7AoLRZQQG4C96Ohlc=")
20+
}
21+
22+
extension VAPID.Key.ID {
23+
/// A mocked key ID that matches ``/VAPID/Key/mockedKey1``.
24+
public static let mockedKeyID1 = VAPID.Key.mockedKey1.id
25+
/// A mocked key ID that matches ``/VAPID/Key/mockedKey2``.
26+
public static let mockedKeyID2 = VAPID.Key.mockedKey2.id
27+
/// A mocked key ID that matches ``/VAPID/Key/mockedKey3``.
28+
public static let mockedKeyID3 = VAPID.Key.mockedKey3.id
29+
/// A mocked key ID that matches ``/VAPID/Key/mockedKey4``.
30+
public static let mockedKeyID4 = VAPID.Key.mockedKey4.id
31+
}

Tests/WebPushTests/VAPIDConfigurationTests.swift

Lines changed: 62 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,19 @@ import Crypto
1010
import Foundation
1111
import Testing
1212
@testable import WebPush
13+
import WebPushTesting
1314

1415
@Suite("VAPID Configuration Tests")
1516
struct VAPIDConfigurationTests {
1617
@Suite
1718
struct Initialization {
18-
let key1 = try! VAPID.Key(base64URLEncoded: "FniTgSrf0l+BdfeC6LiblKXBbY4LQm0S+4STNCoJI+0=")
19-
let key2 = try! VAPID.Key(base64URLEncoded: "wyQaGWNwvXKzVmPIhkqVQvQ+FKx1SNqHJ+re8n2ORrk=")
20-
let key3 = try! VAPID.Key(base64URLEncoded: "bcZgo/p2WFqXaKFzmYaDKO/gARjWvGi3oXyHM2QNlfE=")
21-
2219
@Test func primaryKeyOnly() {
2320
let config = VAPID.Configuration(
24-
key: key1,
21+
key: .mockedKey1,
2522
contactInformation: .email("test@email.com")
2623
)
27-
#expect(config.primaryKey == key1)
28-
#expect(config.keys == [key1])
24+
#expect(config.primaryKey == .mockedKey1)
25+
#expect(config.keys == [.mockedKey1])
2926
#expect(config.deprecatedKeys == nil)
3027
#expect(config.contactInformation == .email("test@email.com"))
3128
#expect(config.expirationDuration == .hours(22))
@@ -34,14 +31,14 @@ struct VAPIDConfigurationTests {
3431

3532
@Test func emptyDeprecatedKeys() {
3633
let config = VAPID.Configuration(
37-
key: key1,
34+
key: .mockedKey1,
3835
deprecatedKeys: [],
3936
contactInformation: .url(URL(string: "https://example.com")!),
4037
expirationDuration: .hours(24),
4138
validityDuration: .hours(12)
4239
)
43-
#expect(config.primaryKey == key1)
44-
#expect(config.keys == [key1])
40+
#expect(config.primaryKey == .mockedKey1)
41+
#expect(config.keys == [.mockedKey1])
4542
#expect(config.deprecatedKeys == nil)
4643
#expect(config.contactInformation == .url(URL(string: "https://example.com")!))
4744
#expect(config.expirationDuration == .hours(24))
@@ -50,29 +47,29 @@ struct VAPIDConfigurationTests {
5047

5148
@Test func deprecatedKeys() {
5249
let config = VAPID.Configuration(
53-
key: key1,
54-
deprecatedKeys: [key2, key3],
50+
key: .mockedKey1,
51+
deprecatedKeys: [.mockedKey2, .mockedKey3],
5552
contactInformation: .email("test@email.com")
5653
)
57-
#expect(config.primaryKey == key1)
58-
#expect(config.keys == [key1])
59-
#expect(config.deprecatedKeys == [key2, key3])
54+
#expect(config.primaryKey == .mockedKey1)
55+
#expect(config.keys == [.mockedKey1])
56+
#expect(config.deprecatedKeys == [.mockedKey2, .mockedKey3])
6057
#expect(config.contactInformation == .email("test@email.com"))
6158
#expect(config.expirationDuration == .hours(22))
6259
#expect(config.validityDuration == .hours(20))
6360
}
6461

6562
@Test func deprecatedAndPrimaryKeys() {
6663
let config = VAPID.Configuration(
67-
key: key1,
68-
deprecatedKeys: [key2, key3, key1],
64+
key: .mockedKey1,
65+
deprecatedKeys: [.mockedKey2, .mockedKey3, .mockedKey1],
6966
contactInformation: .url(URL(string: "https://example.com")!),
7067
expirationDuration: .hours(24),
7168
validityDuration: .hours(12)
7269
)
73-
#expect(config.primaryKey == key1)
74-
#expect(config.keys == [key1])
75-
#expect(config.deprecatedKeys == [key2, key3])
70+
#expect(config.primaryKey == .mockedKey1)
71+
#expect(config.keys == [.mockedKey1])
72+
#expect(config.deprecatedKeys == [.mockedKey2, .mockedKey3])
7673
#expect(config.contactInformation == .url(URL(string: "https://example.com")!))
7774
#expect(config.expirationDuration == .hours(24))
7875
#expect(config.validityDuration == .hours(12))
@@ -81,12 +78,12 @@ struct VAPIDConfigurationTests {
8178
@Test func multipleKeys() throws {
8279
let config = try VAPID.Configuration(
8380
primaryKey: nil,
84-
keys: [key1, key2],
81+
keys: [.mockedKey1, .mockedKey2],
8582
deprecatedKeys: nil,
8683
contactInformation: .email("test@email.com")
8784
)
8885
#expect(config.primaryKey == nil)
89-
#expect(config.keys == [key1, key2])
86+
#expect(config.keys == [.mockedKey1, .mockedKey2])
9087
#expect(config.deprecatedKeys == nil)
9188
#expect(config.contactInformation == .email("test@email.com"))
9289
#expect(config.expirationDuration == .hours(22))
@@ -98,7 +95,7 @@ struct VAPIDConfigurationTests {
9895
try VAPID.Configuration(
9996
primaryKey: nil,
10097
keys: [],
101-
deprecatedKeys: [key2, key3],
98+
deprecatedKeys: [.mockedKey2, .mockedKey3],
10299
contactInformation: .email("test@email.com")
103100
)
104101
}
@@ -107,12 +104,12 @@ struct VAPIDConfigurationTests {
107104
@Test func multipleAndDeprecatedKeys() throws {
108105
let config = try VAPID.Configuration(
109106
primaryKey: nil,
110-
keys: [key1, key2],
111-
deprecatedKeys: [key2],
107+
keys: [.mockedKey1, .mockedKey2],
108+
deprecatedKeys: [.mockedKey2],
112109
contactInformation: .email("test@email.com")
113110
)
114111
#expect(config.primaryKey == nil)
115-
#expect(config.keys == [key1, key2])
112+
#expect(config.keys == [.mockedKey1, .mockedKey2])
116113
#expect(config.deprecatedKeys == nil)
117114
#expect(config.contactInformation == .email("test@email.com"))
118115
#expect(config.expirationDuration == .hours(22))
@@ -121,16 +118,16 @@ struct VAPIDConfigurationTests {
121118

122119
@Test func multipleAndPrimaryKeys() throws {
123120
let config = try VAPID.Configuration(
124-
primaryKey: key1,
125-
keys: [key2],
126-
deprecatedKeys: [key2, key3, key1],
121+
primaryKey: .mockedKey1,
122+
keys: [.mockedKey2],
123+
deprecatedKeys: [.mockedKey2, .mockedKey3, .mockedKey1],
127124
contactInformation: .url(URL(string: "https://example.com")!),
128125
expirationDuration: .hours(24),
129126
validityDuration: .hours(12)
130127
)
131-
#expect(config.primaryKey == key1)
132-
#expect(config.keys == [key1, key2])
133-
#expect(config.deprecatedKeys == [key3])
128+
#expect(config.primaryKey == .mockedKey1)
129+
#expect(config.keys == [.mockedKey1, .mockedKey2])
130+
#expect(config.deprecatedKeys == [.mockedKey3])
134131
#expect(config.contactInformation == .url(URL(string: "https://example.com")!))
135132
#expect(config.expirationDuration == .hours(24))
136133
#expect(config.validityDuration == .hours(12))
@@ -139,81 +136,73 @@ struct VAPIDConfigurationTests {
139136

140137
@Suite
141138
struct Updates {
142-
let key1 = try! VAPID.Key(base64URLEncoded: "FniTgSrf0l+BdfeC6LiblKXBbY4LQm0S+4STNCoJI+0=")
143-
let key2 = try! VAPID.Key(base64URLEncoded: "wyQaGWNwvXKzVmPIhkqVQvQ+FKx1SNqHJ+re8n2ORrk=")
144-
let key3 = try! VAPID.Key(base64URLEncoded: "bcZgo/p2WFqXaKFzmYaDKO/gARjWvGi3oXyHM2QNlfE=")
145-
146139
@Test func primaryKeyOnly() throws {
147-
var config = VAPID.Configuration(key: key1, contactInformation: .email("test@email.com"))
140+
var config = VAPID.Configuration(key: .mockedKey1, contactInformation: .email("test@email.com"))
148141

149-
try config.updateKeys(primaryKey: key2, keys: [], deprecatedKeys: nil)
150-
#expect(config.primaryKey == key2)
151-
#expect(config.keys == [key2])
142+
try config.updateKeys(primaryKey: .mockedKey2, keys: [], deprecatedKeys: nil)
143+
#expect(config.primaryKey == .mockedKey2)
144+
#expect(config.keys == [.mockedKey2])
152145
#expect(config.deprecatedKeys == nil)
153146
}
154147

155148
@Test func noKeys() throws {
156-
var config = VAPID.Configuration(key: key1, contactInformation: .email("test@email.com"))
149+
var config = VAPID.Configuration(key: .mockedKey1, contactInformation: .email("test@email.com"))
157150
#expect(throws: VAPID.ConfigurationError.keysNotProvided) {
158151
try config.updateKeys(primaryKey: nil, keys: [], deprecatedKeys: nil)
159152
}
160153
#expect(throws: VAPID.ConfigurationError.keysNotProvided) {
161154
try config.updateKeys(primaryKey: nil, keys: [], deprecatedKeys: [])
162155
}
163156
#expect(throws: VAPID.ConfigurationError.keysNotProvided) {
164-
try config.updateKeys(primaryKey: nil, keys: [], deprecatedKeys: [key1])
157+
try config.updateKeys(primaryKey: nil, keys: [], deprecatedKeys: [.mockedKey1])
165158
}
166159
}
167160

168161
@Test func multipleKeys() throws {
169-
var config = VAPID.Configuration(key: key1, contactInformation: .email("test@email.com"))
162+
var config = VAPID.Configuration(key: .mockedKey1, contactInformation: .email("test@email.com"))
170163

171-
try config.updateKeys(primaryKey: nil, keys: [key2], deprecatedKeys: nil)
164+
try config.updateKeys(primaryKey: nil, keys: [.mockedKey2], deprecatedKeys: nil)
172165
#expect(config.primaryKey == nil)
173-
#expect(config.keys == [key2])
166+
#expect(config.keys == [.mockedKey2])
174167
#expect(config.deprecatedKeys == nil)
175168

176-
try config.updateKeys(primaryKey: nil, keys: [key2, key3], deprecatedKeys: nil)
169+
try config.updateKeys(primaryKey: nil, keys: [.mockedKey2, .mockedKey3], deprecatedKeys: nil)
177170
#expect(config.primaryKey == nil)
178-
#expect(config.keys == [key2, key3])
171+
#expect(config.keys == [.mockedKey2, .mockedKey3])
179172
#expect(config.deprecatedKeys == nil)
180173
}
181174

182175
@Test func multipleAndDeprecatedKeys() throws {
183-
var config = VAPID.Configuration(key: key1, contactInformation: .email("test@email.com"))
176+
var config = VAPID.Configuration(key: .mockedKey1, contactInformation: .email("test@email.com"))
184177

185-
try config.updateKeys(primaryKey: nil, keys: [key2], deprecatedKeys: [key2, key3])
178+
try config.updateKeys(primaryKey: nil, keys: [.mockedKey2], deprecatedKeys: [.mockedKey2, .mockedKey3])
186179
#expect(config.primaryKey == nil)
187-
#expect(config.keys == [key2])
188-
#expect(config.deprecatedKeys == [key3])
180+
#expect(config.keys == [.mockedKey2])
181+
#expect(config.deprecatedKeys == [.mockedKey3])
189182

190-
try config.updateKeys(primaryKey: nil, keys: [key2, key3], deprecatedKeys: [key2, key3])
183+
try config.updateKeys(primaryKey: nil, keys: [.mockedKey2, .mockedKey3], deprecatedKeys: [.mockedKey2, .mockedKey3])
191184
#expect(config.primaryKey == nil)
192-
#expect(config.keys == [key2, key3])
185+
#expect(config.keys == [.mockedKey2, .mockedKey3])
193186
#expect(config.deprecatedKeys == nil)
194187
}
195188

196189
@Test func multipleAndPrimaryKeys() throws {
197-
var config = VAPID.Configuration(key: key1, contactInformation: .email("test@email.com"))
190+
var config = VAPID.Configuration(key: .mockedKey1, contactInformation: .email("test@email.com"))
198191

199-
try config.updateKeys(primaryKey: key2, keys: [key3], deprecatedKeys: [key1, key2, key3])
200-
#expect(config.primaryKey == key2)
201-
#expect(config.keys == [key2, key3])
202-
#expect(config.deprecatedKeys == [key1])
192+
try config.updateKeys(primaryKey: .mockedKey2, keys: [.mockedKey3], deprecatedKeys: [.mockedKey1, .mockedKey2, .mockedKey3])
193+
#expect(config.primaryKey == .mockedKey2)
194+
#expect(config.keys == [.mockedKey2, .mockedKey3])
195+
#expect(config.deprecatedKeys == [.mockedKey1])
203196

204-
try config.updateKeys(primaryKey: key2, keys: [key3], deprecatedKeys: [key2, key3])
205-
#expect(config.primaryKey == key2)
206-
#expect(config.keys == [key2, key3])
197+
try config.updateKeys(primaryKey: .mockedKey2, keys: [.mockedKey3], deprecatedKeys: [.mockedKey2, .mockedKey3])
198+
#expect(config.primaryKey == .mockedKey2)
199+
#expect(config.keys == [.mockedKey2, .mockedKey3])
207200
#expect(config.deprecatedKeys == nil)
208201
}
209202
}
210203

211204
@Suite
212205
struct Coding {
213-
let key1 = try! VAPID.Key(base64URLEncoded: "FniTgSrf0l+BdfeC6LiblKXBbY4LQm0S+4STNCoJI+0=")
214-
let key2 = try! VAPID.Key(base64URLEncoded: "wyQaGWNwvXKzVmPIhkqVQvQ+FKx1SNqHJ+re8n2ORrk=")
215-
let key3 = try! VAPID.Key(base64URLEncoded: "bcZgo/p2WFqXaKFzmYaDKO/gARjWvGi3oXyHM2QNlfE=")
216-
217206
func encode(_ configuration: VAPID.Configuration) throws -> String {
218207
let encoder = JSONEncoder()
219208
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
@@ -222,7 +211,7 @@ struct VAPIDConfigurationTests {
222211

223212
@Test func encodesPrimaryKeyOnly() async throws {
224213
#expect(
225-
try encode(.init(key: key1, contactInformation: .email("test@example.com"))) ==
214+
try encode(.init(key: .mockedKey1, contactInformation: .email("test@example.com"))) ==
226215
"""
227216
{
228217
"contactInformation" : "mailto:test@example.com",
@@ -237,9 +226,9 @@ struct VAPIDConfigurationTests {
237226
@Test func encodesMultipleKeysWithoutDuplicates() async throws {
238227
#expect(
239228
try encode(.init(
240-
primaryKey: key1,
241-
keys: [key2],
242-
deprecatedKeys: [key1, key2, key3],
229+
primaryKey: .mockedKey1,
230+
keys: [.mockedKey2],
231+
deprecatedKeys: [.mockedKey1, .mockedKey2, .mockedKey3],
243232
contactInformation: .email("test@example.com"),
244233
expirationDuration: .hours(1),
245234
validityDuration: .hours(10)
@@ -274,7 +263,7 @@ struct VAPIDConfigurationTests {
274263
""".utf8
275264
)) ==
276265
VAPID.Configuration(
277-
key: key1,
266+
key: .mockedKey1,
278267
contactInformation: .email("test@example.com")
279268
)
280269
)
@@ -299,9 +288,9 @@ struct VAPIDConfigurationTests {
299288
""".utf8
300289
)) ==
301290
VAPID.Configuration(
302-
primaryKey: key1,
303-
keys: [key2],
304-
deprecatedKeys: [key1, key2, key3],
291+
primaryKey: .mockedKey1,
292+
keys: [.mockedKey2],
293+
deprecatedKeys: [.mockedKey1, .mockedKey2, .mockedKey3],
305294
contactInformation: .email("test@example.com"),
306295
expirationDuration: .hours(1),
307296
validityDuration: .hours(10)

0 commit comments

Comments
 (0)