Skip to content

Commit

Permalink
Add testing for issue #43 (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino authored Dec 18, 2024
1 parent ac84c96 commit 81e0b74
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
52 changes: 26 additions & 26 deletions Tests/ImperialTests/ImperialTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ struct ImperialTests {
func auth0Route() async throws {
try await withApp(service: Auth0.self) { app in
try await app.test(
.GET, "/service",
.GET, apiAuthURL,
afterResponse: { res async throws in
#expect(res.status == .seeOther)
}
)

try await app.test(
.GET, "/service-auth-complete?code=123",
.GET, "\(apiCallbackURL)?code=123",
afterResponse: { res async throws in
// A real Auth0 domain is needed to test this route
#expect(res.status == .internalServerError)
Expand All @@ -41,14 +41,14 @@ struct ImperialTests {
func deviantArtRoute() async throws {
try await withApp(service: DeviantArt.self) { app in
try await app.test(
.GET, "/service",
.GET, apiAuthURL,
afterResponse: { res async throws in
#expect(res.status == .seeOther)
}
)

try await app.test(
.GET, "/service-auth-complete?code=123",
.GET, "\(apiCallbackURL)?code=123",
afterResponse: { res async throws in
// TODO: test this route
#expect(res.status != .notFound)
Expand All @@ -61,14 +61,14 @@ struct ImperialTests {
func discordRoute() async throws {
try await withApp(service: Discord.self) { app in
try await app.test(
.GET, "/service",
.GET, apiAuthURL,
afterResponse: { res async throws in
#expect(res.status == .seeOther)
}
)

try await app.test(
.GET, "/service-auth-complete?code=123",
.GET, "\(apiCallbackURL)?code=123",
afterResponse: { res async throws in
// Discord returns a 400 Bad Request error when the code is invalid with a JSON error message
#expect(res.status == .badRequest)
Expand All @@ -81,14 +81,14 @@ struct ImperialTests {
func dropboxRoute() async throws {
try await withApp(service: Dropbox.self) { app in
try await app.test(
.GET, "/service",
.GET, apiAuthURL,
afterResponse: { res async throws in
#expect(res.status == .seeOther)
}
)

try await app.test(
.GET, "/service-auth-complete?code=123",
.GET, "\(apiCallbackURL)?code=123",
afterResponse: { res async throws in
// Dropbox returns a 400 Bad Request error when the code is invalid with a JSON error message
#expect(res.status == .badRequest)
Expand All @@ -101,14 +101,14 @@ struct ImperialTests {
func facebookRoute() async throws {
try await withApp(service: Facebook.self) { app in
try await app.test(
.GET, "/service",
.GET, apiAuthURL,
afterResponse: { res async throws in
#expect(res.status == .seeOther)
}
)

try await app.test(
.GET, "/service-auth-complete?code=123",
.GET, "\(apiCallbackURL)?code=123",
afterResponse: { res async throws in
// The response is an JS, signaling an error with `redirect_uri`
#expect(res.status == .unsupportedMediaType)
Expand All @@ -121,14 +121,14 @@ struct ImperialTests {
func githubRoute() async throws {
try await withApp(service: GitHub.self) { app in
try await app.test(
.GET, "/service",
.GET, apiAuthURL,
afterResponse: { res async throws in
#expect(res.status == .seeOther)
}
)

try await app.test(
.GET, "/service-auth-complete?code=123",
.GET, "\(apiCallbackURL)?code=123",
afterResponse: { res async throws in
// The response is an HTML page likely signaling an error
#expect(res.status == .unsupportedMediaType)
Expand All @@ -141,14 +141,14 @@ struct ImperialTests {
func gitlabRoute() async throws {
try await withApp(service: Gitlab.self) { app in
try await app.test(
.GET, "/service",
.GET, apiAuthURL,
afterResponse: { res async throws in
#expect(res.status == .seeOther)
}
)

try await app.test(
.GET, "/service-auth-complete?code=123",
.GET, "\(apiCallbackURL)?code=123",
afterResponse: { res async throws in
// Gitlab returns a 400 Bad Request error when the code is invalid with a JSON error message
#expect(res.status == .badRequest)
Expand All @@ -161,14 +161,14 @@ struct ImperialTests {
func googleRoute() async throws {
try await withApp(service: Google.self) { app in
try await app.test(
.GET, "/service",
.GET, apiAuthURL,
afterResponse: { res async throws in
#expect(res.status == .seeOther)
}
)

try await app.test(
.GET, "/service-auth-complete?code=123",
.GET, "\(apiCallbackURL)?code=123",
afterResponse: { res async throws in
// Google returns a 400 Bad Request error when the code is invalid with a JSON error message
#expect(res.status == .badRequest)
Expand All @@ -181,14 +181,14 @@ struct ImperialTests {
func googleJWTRoute() async throws {
try await withApp(service: GoogleJWT.self) { app in
try await app.test(
.GET, "/service",
.GET, apiAuthURL,
afterResponse: { res async throws in
#expect(res.status == .seeOther)
}
)

try await app.test(
.GET, "/service-auth-complete",
.GET, apiCallbackURL,
afterResponse: { res async throws in
// We don't have a valid key to sign the JWT
#expect(res.status == .internalServerError)
Expand All @@ -201,14 +201,14 @@ struct ImperialTests {
func imgurRoute() async throws {
try await withApp(service: Imgur.self) { app in
try await app.test(
.GET, "/service",
.GET, apiAuthURL,
afterResponse: { res async throws in
#expect(res.status == .seeOther)
}
)

try await app.test(
.GET, "/service-auth-complete?code=123",
.GET, "\(apiCallbackURL)?code=123",
afterResponse: { res async throws in
// TODO: test this route
#expect(res.status != .notFound)
Expand All @@ -221,14 +221,14 @@ struct ImperialTests {
func keycloakRoute() async throws {
try await withApp(service: Keycloak.self) { app in
try await app.test(
.GET, "/service",
.GET, apiAuthURL,
afterResponse: { res async throws in
#expect(res.status == .seeOther)
}
)

try await app.test(
.GET, "/service-auth-complete?code=123",
.GET, "\(apiCallbackURL)?code=123",
afterResponse: { res async throws in
// The post request fails
#expect(res.status == .internalServerError)
Expand All @@ -241,14 +241,14 @@ struct ImperialTests {
func microsoftRoute() async throws {
try await withApp(service: Microsoft.self) { app in
try await app.test(
.GET, "/service",
.GET, apiAuthURL,
afterResponse: { res async throws in
#expect(res.status == .seeOther)
}
)

try await app.test(
.GET, "/service-auth-complete?code=123",
.GET, "\(apiCallbackURL)?code=123",
afterResponse: { res async throws in
// Microsoft returns a 400 Bad Request, signaling an error with `redirect_uri`
#expect(res.status == .badRequest)
Expand All @@ -261,14 +261,14 @@ struct ImperialTests {
func mixcloudRoute() async throws {
try await withApp(service: Mixcloud.self) { app in
try await app.test(
.GET, "/service",
.GET, apiAuthURL,
afterResponse: { res async throws in
#expect(res.status == .seeOther)
}
)

try await app.test(
.GET, "/service-auth-complete?code=123",
.GET, "\(apiCallbackURL)?code=123",
afterResponse: { res async throws in
// TODO: test this route
#expect(res.status != .notFound)
Expand Down
4 changes: 2 additions & 2 deletions Tests/ImperialTests/ShopifyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ struct ShopifyTests {
@Test("Shopify Route") func shopifyRoute() async throws {
try await withApp(service: Shopify.self) { app in
try await app.test(
.GET, "/service?shop=some-shop.myshopify.com",
.GET, "\(apiAuthURL)?shop=some-shop.myshopify.com",
afterResponse: { res async throws in
#expect(res.status == .seeOther)
}
)

try await app.test(
.GET,
"/service-auth-complete?"
"\(apiCallbackURL)?"
+ "code=0907a61c0c8d55e99db179b68161bc00&"
+ "hmac=700e2dadb827fcc8609e9d5ce208b2e9cdaab9df07390d2cbca10d7c328fc4bf&"
+ "shop=some-shop.myshopify.com&"
Expand Down
10 changes: 9 additions & 1 deletion Tests/ImperialTests/withApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import ImperialCore
import Testing
import Vapor

let authURL = "service"
let callbackURL = "service-auth-complete"
let apiGroup = "api"
let apiAuthURL = "/\(apiGroup)/\(authURL)"
let apiCallbackURL = "/\(apiGroup)/\(callbackURL)"

func withApp<OAuthProvider>(
service: OAuthProvider.Type,
_ test: (Application) async throws -> Void
Expand All @@ -10,7 +16,9 @@ func withApp<OAuthProvider>(
try #require(isLoggingConfigured)
do {
app.middleware.use(app.sessions.middleware)
try app.oAuth(from: service.self, authenticate: "service", callback: "service-auth-complete", redirect: "/")
// Test for https://github.com/vapor-community/Imperial/issues/43
let grouped = app.grouped(PathComponent(stringLiteral: apiGroup))
try grouped.oAuth(from: service.self, authenticate: authURL, callback: callbackURL, redirect: "/")
try await test(app)
} catch {
try await app.asyncShutdown()
Expand Down

0 comments on commit 81e0b74

Please sign in to comment.