Skip to content

Commit 81e0b74

Browse files
authored
Add testing for issue #43 (#114)
1 parent ac84c96 commit 81e0b74

File tree

3 files changed

+37
-29
lines changed

3 files changed

+37
-29
lines changed

Tests/ImperialTests/ImperialTests.swift

+26-26
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ struct ImperialTests {
2121
func auth0Route() async throws {
2222
try await withApp(service: Auth0.self) { app in
2323
try await app.test(
24-
.GET, "/service",
24+
.GET, apiAuthURL,
2525
afterResponse: { res async throws in
2626
#expect(res.status == .seeOther)
2727
}
2828
)
2929

3030
try await app.test(
31-
.GET, "/service-auth-complete?code=123",
31+
.GET, "\(apiCallbackURL)?code=123",
3232
afterResponse: { res async throws in
3333
// A real Auth0 domain is needed to test this route
3434
#expect(res.status == .internalServerError)
@@ -41,14 +41,14 @@ struct ImperialTests {
4141
func deviantArtRoute() async throws {
4242
try await withApp(service: DeviantArt.self) { app in
4343
try await app.test(
44-
.GET, "/service",
44+
.GET, apiAuthURL,
4545
afterResponse: { res async throws in
4646
#expect(res.status == .seeOther)
4747
}
4848
)
4949

5050
try await app.test(
51-
.GET, "/service-auth-complete?code=123",
51+
.GET, "\(apiCallbackURL)?code=123",
5252
afterResponse: { res async throws in
5353
// TODO: test this route
5454
#expect(res.status != .notFound)
@@ -61,14 +61,14 @@ struct ImperialTests {
6161
func discordRoute() async throws {
6262
try await withApp(service: Discord.self) { app in
6363
try await app.test(
64-
.GET, "/service",
64+
.GET, apiAuthURL,
6565
afterResponse: { res async throws in
6666
#expect(res.status == .seeOther)
6767
}
6868
)
6969

7070
try await app.test(
71-
.GET, "/service-auth-complete?code=123",
71+
.GET, "\(apiCallbackURL)?code=123",
7272
afterResponse: { res async throws in
7373
// Discord returns a 400 Bad Request error when the code is invalid with a JSON error message
7474
#expect(res.status == .badRequest)
@@ -81,14 +81,14 @@ struct ImperialTests {
8181
func dropboxRoute() async throws {
8282
try await withApp(service: Dropbox.self) { app in
8383
try await app.test(
84-
.GET, "/service",
84+
.GET, apiAuthURL,
8585
afterResponse: { res async throws in
8686
#expect(res.status == .seeOther)
8787
}
8888
)
8989

9090
try await app.test(
91-
.GET, "/service-auth-complete?code=123",
91+
.GET, "\(apiCallbackURL)?code=123",
9292
afterResponse: { res async throws in
9393
// Dropbox returns a 400 Bad Request error when the code is invalid with a JSON error message
9494
#expect(res.status == .badRequest)
@@ -101,14 +101,14 @@ struct ImperialTests {
101101
func facebookRoute() async throws {
102102
try await withApp(service: Facebook.self) { app in
103103
try await app.test(
104-
.GET, "/service",
104+
.GET, apiAuthURL,
105105
afterResponse: { res async throws in
106106
#expect(res.status == .seeOther)
107107
}
108108
)
109109

110110
try await app.test(
111-
.GET, "/service-auth-complete?code=123",
111+
.GET, "\(apiCallbackURL)?code=123",
112112
afterResponse: { res async throws in
113113
// The response is an JS, signaling an error with `redirect_uri`
114114
#expect(res.status == .unsupportedMediaType)
@@ -121,14 +121,14 @@ struct ImperialTests {
121121
func githubRoute() async throws {
122122
try await withApp(service: GitHub.self) { app in
123123
try await app.test(
124-
.GET, "/service",
124+
.GET, apiAuthURL,
125125
afterResponse: { res async throws in
126126
#expect(res.status == .seeOther)
127127
}
128128
)
129129

130130
try await app.test(
131-
.GET, "/service-auth-complete?code=123",
131+
.GET, "\(apiCallbackURL)?code=123",
132132
afterResponse: { res async throws in
133133
// The response is an HTML page likely signaling an error
134134
#expect(res.status == .unsupportedMediaType)
@@ -141,14 +141,14 @@ struct ImperialTests {
141141
func gitlabRoute() async throws {
142142
try await withApp(service: Gitlab.self) { app in
143143
try await app.test(
144-
.GET, "/service",
144+
.GET, apiAuthURL,
145145
afterResponse: { res async throws in
146146
#expect(res.status == .seeOther)
147147
}
148148
)
149149

150150
try await app.test(
151-
.GET, "/service-auth-complete?code=123",
151+
.GET, "\(apiCallbackURL)?code=123",
152152
afterResponse: { res async throws in
153153
// Gitlab returns a 400 Bad Request error when the code is invalid with a JSON error message
154154
#expect(res.status == .badRequest)
@@ -161,14 +161,14 @@ struct ImperialTests {
161161
func googleRoute() async throws {
162162
try await withApp(service: Google.self) { app in
163163
try await app.test(
164-
.GET, "/service",
164+
.GET, apiAuthURL,
165165
afterResponse: { res async throws in
166166
#expect(res.status == .seeOther)
167167
}
168168
)
169169

170170
try await app.test(
171-
.GET, "/service-auth-complete?code=123",
171+
.GET, "\(apiCallbackURL)?code=123",
172172
afterResponse: { res async throws in
173173
// Google returns a 400 Bad Request error when the code is invalid with a JSON error message
174174
#expect(res.status == .badRequest)
@@ -181,14 +181,14 @@ struct ImperialTests {
181181
func googleJWTRoute() async throws {
182182
try await withApp(service: GoogleJWT.self) { app in
183183
try await app.test(
184-
.GET, "/service",
184+
.GET, apiAuthURL,
185185
afterResponse: { res async throws in
186186
#expect(res.status == .seeOther)
187187
}
188188
)
189189

190190
try await app.test(
191-
.GET, "/service-auth-complete",
191+
.GET, apiCallbackURL,
192192
afterResponse: { res async throws in
193193
// We don't have a valid key to sign the JWT
194194
#expect(res.status == .internalServerError)
@@ -201,14 +201,14 @@ struct ImperialTests {
201201
func imgurRoute() async throws {
202202
try await withApp(service: Imgur.self) { app in
203203
try await app.test(
204-
.GET, "/service",
204+
.GET, apiAuthURL,
205205
afterResponse: { res async throws in
206206
#expect(res.status == .seeOther)
207207
}
208208
)
209209

210210
try await app.test(
211-
.GET, "/service-auth-complete?code=123",
211+
.GET, "\(apiCallbackURL)?code=123",
212212
afterResponse: { res async throws in
213213
// TODO: test this route
214214
#expect(res.status != .notFound)
@@ -221,14 +221,14 @@ struct ImperialTests {
221221
func keycloakRoute() async throws {
222222
try await withApp(service: Keycloak.self) { app in
223223
try await app.test(
224-
.GET, "/service",
224+
.GET, apiAuthURL,
225225
afterResponse: { res async throws in
226226
#expect(res.status == .seeOther)
227227
}
228228
)
229229

230230
try await app.test(
231-
.GET, "/service-auth-complete?code=123",
231+
.GET, "\(apiCallbackURL)?code=123",
232232
afterResponse: { res async throws in
233233
// The post request fails
234234
#expect(res.status == .internalServerError)
@@ -241,14 +241,14 @@ struct ImperialTests {
241241
func microsoftRoute() async throws {
242242
try await withApp(service: Microsoft.self) { app in
243243
try await app.test(
244-
.GET, "/service",
244+
.GET, apiAuthURL,
245245
afterResponse: { res async throws in
246246
#expect(res.status == .seeOther)
247247
}
248248
)
249249

250250
try await app.test(
251-
.GET, "/service-auth-complete?code=123",
251+
.GET, "\(apiCallbackURL)?code=123",
252252
afterResponse: { res async throws in
253253
// Microsoft returns a 400 Bad Request, signaling an error with `redirect_uri`
254254
#expect(res.status == .badRequest)
@@ -261,14 +261,14 @@ struct ImperialTests {
261261
func mixcloudRoute() async throws {
262262
try await withApp(service: Mixcloud.self) { app in
263263
try await app.test(
264-
.GET, "/service",
264+
.GET, apiAuthURL,
265265
afterResponse: { res async throws in
266266
#expect(res.status == .seeOther)
267267
}
268268
)
269269

270270
try await app.test(
271-
.GET, "/service-auth-complete?code=123",
271+
.GET, "\(apiCallbackURL)?code=123",
272272
afterResponse: { res async throws in
273273
// TODO: test this route
274274
#expect(res.status != .notFound)

Tests/ImperialTests/ShopifyTests.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ struct ShopifyTests {
99
@Test("Shopify Route") func shopifyRoute() async throws {
1010
try await withApp(service: Shopify.self) { app in
1111
try await app.test(
12-
.GET, "/service?shop=some-shop.myshopify.com",
12+
.GET, "\(apiAuthURL)?shop=some-shop.myshopify.com",
1313
afterResponse: { res async throws in
1414
#expect(res.status == .seeOther)
1515
}
1616
)
1717

1818
try await app.test(
1919
.GET,
20-
"/service-auth-complete?"
20+
"\(apiCallbackURL)?"
2121
+ "code=0907a61c0c8d55e99db179b68161bc00&"
2222
+ "hmac=700e2dadb827fcc8609e9d5ce208b2e9cdaab9df07390d2cbca10d7c328fc4bf&"
2323
+ "shop=some-shop.myshopify.com&"

Tests/ImperialTests/withApp.swift

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import ImperialCore
22
import Testing
33
import Vapor
44

5+
let authURL = "service"
6+
let callbackURL = "service-auth-complete"
7+
let apiGroup = "api"
8+
let apiAuthURL = "/\(apiGroup)/\(authURL)"
9+
let apiCallbackURL = "/\(apiGroup)/\(callbackURL)"
10+
511
func withApp<OAuthProvider>(
612
service: OAuthProvider.Type,
713
_ test: (Application) async throws -> Void
@@ -10,7 +16,9 @@ func withApp<OAuthProvider>(
1016
try #require(isLoggingConfigured)
1117
do {
1218
app.middleware.use(app.sessions.middleware)
13-
try app.oAuth(from: service.self, authenticate: "service", callback: "service-auth-complete", redirect: "/")
19+
// Test for https://github.com/vapor-community/Imperial/issues/43
20+
let grouped = app.grouped(PathComponent(stringLiteral: apiGroup))
21+
try grouped.oAuth(from: service.self, authenticate: authURL, callback: callbackURL, redirect: "/")
1422
try await test(app)
1523
} catch {
1624
try await app.asyncShutdown()

0 commit comments

Comments
 (0)