From e8d401f6b1f20496f925146585a1016d581cae2a Mon Sep 17 00:00:00 2001 From: Francesco Paolo Severino Date: Tue, 17 Dec 2024 22:38:40 +0100 Subject: [PATCH] Add testing for issue #43 --- Tests/ImperialTests/ImperialTests.swift | 52 ++++++++++++------------- Tests/ImperialTests/ShopifyTests.swift | 4 +- Tests/ImperialTests/withApp.swift | 10 ++++- 3 files changed, 37 insertions(+), 29 deletions(-) diff --git a/Tests/ImperialTests/ImperialTests.swift b/Tests/ImperialTests/ImperialTests.swift index e2fbe6c2..b77cb5fa 100644 --- a/Tests/ImperialTests/ImperialTests.swift +++ b/Tests/ImperialTests/ImperialTests.swift @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/Tests/ImperialTests/ShopifyTests.swift b/Tests/ImperialTests/ShopifyTests.swift index 8f76472f..dfbc3783 100644 --- a/Tests/ImperialTests/ShopifyTests.swift +++ b/Tests/ImperialTests/ShopifyTests.swift @@ -9,7 +9,7 @@ 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) } @@ -17,7 +17,7 @@ struct ShopifyTests { try await app.test( .GET, - "/service-auth-complete?" + "\(apiCallbackURL)?" + "code=0907a61c0c8d55e99db179b68161bc00&" + "hmac=700e2dadb827fcc8609e9d5ce208b2e9cdaab9df07390d2cbca10d7c328fc4bf&" + "shop=some-shop.myshopify.com&" diff --git a/Tests/ImperialTests/withApp.swift b/Tests/ImperialTests/withApp.swift index 5152e81b..48a48693 100644 --- a/Tests/ImperialTests/withApp.swift +++ b/Tests/ImperialTests/withApp.swift @@ -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( service: OAuthProvider.Type, _ test: (Application) async throws -> Void @@ -10,7 +16,9 @@ func withApp( 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()