Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino committed Nov 23, 2024
1 parent 27611f7 commit cb6e3e5
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 33 deletions.
3 changes: 2 additions & 1 deletion Sources/ImperialAuth0/Auth0Router.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ final public class Auth0Router: FederatedServiceRouter {
clientId: self.tokens.clientID,
clientSecret: self.tokens.clientSecret,
code: code,
redirectURI: self.callbackURL)
redirectURI: self.callbackURL
)
}
}
1 change: 0 additions & 1 deletion Sources/ImperialDiscord/DiscordRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ final public class DiscordRouter: FederatedServiceRouter {
}

public func authURL(_ request: Request) throws -> String {

var components = URLComponents()
components.scheme = "https"
components.host = "discord.com"
Expand Down
3 changes: 2 additions & 1 deletion Sources/ImperialDropbox/DropboxRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ final public class DropboxRouter: FederatedServiceRouter {
public func callbackBody(with code: String) -> any AsyncResponseEncodable {
DropboxCallbackBody(
code: code,
redirectURI: callbackURL)
redirectURI: callbackURL
)
}

}
3 changes: 2 additions & 1 deletion Sources/ImperialFacebook/FacebookRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ final public class FacebookRouter: FederatedServiceRouter {
code: code,
clientId: tokens.clientID,
clientSecret: tokens.clientSecret,
redirectURI: callbackURL)
redirectURI: callbackURL
)
}

}
3 changes: 2 additions & 1 deletion Sources/ImperialGoogle/Standard/GoogleRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ final public class GoogleRouter: FederatedServiceRouter {
code: code,
clientId: tokens.clientID,
clientSecret: tokens.clientSecret,
redirectURI: callbackURL)
redirectURI: callbackURL
)
}

}
3 changes: 2 additions & 1 deletion Sources/ImperialKeycloak/KeycloakRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ final public class KeycloakRouter: FederatedServiceRouter {
code: code,
clientId: tokens.clientID,
clientSecret: tokens.clientSecret,
redirectURI: callbackURL)
redirectURI: callbackURL
)
}
}
3 changes: 2 additions & 1 deletion Sources/ImperialMicrosoft/MicrosoftRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ final public class MicrosoftRouter: FederatedServiceRouter {
clientId: tokens.clientID,
clientSecret: tokens.clientSecret,
redirectURI: callbackURL,
scope: scope.joined(separator: " "))
scope: scope.joined(separator: " ")
)
}

}
48 changes: 29 additions & 19 deletions Tests/ImperialTests/ImperialTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ struct ImperialTests {
)

try await app.test(
.GET, "/service-auth-complete",
.GET, "/service-auth-complete?code=123",
afterResponse: { res async throws in
#expect(res.status != .notFound)
// A real Auth0 domain is needed to test this route
#expect(res.status == .internalServerError)
}
)
}
Expand All @@ -44,9 +45,10 @@ struct ImperialTests {
)

try await app.test(
.GET, "/service-auth-complete",
.GET, "/service-auth-complete?code=123",
afterResponse: { res async throws in
#expect(res.status != .notFound)
// Discord returns a 400 Bad Request error when the code is invalid with a JSON error message
#expect(res.status == .badRequest)
}
)
}
Expand All @@ -63,9 +65,10 @@ struct ImperialTests {
)

try await app.test(
.GET, "/service-auth-complete",
.GET, "/service-auth-complete?code=123",
afterResponse: { res async throws in
#expect(res.status != .notFound)
// Dropbox returns a 400 Bad Request error when the code is invalid with a JSON error message
#expect(res.status == .badRequest)
}
)
}
Expand All @@ -82,9 +85,10 @@ struct ImperialTests {
)

try await app.test(
.GET, "/service-auth-complete",
.GET, "/service-auth-complete?code=123",
afterResponse: { res async throws in
#expect(res.status != .notFound)
// The response is an JS, signaling an error with `redirect_uri`
#expect(res.status == .unsupportedMediaType)
}
)
}
Expand All @@ -101,9 +105,10 @@ struct ImperialTests {
)

try await app.test(
.GET, "/service-auth-complete",
.GET, "/service-auth-complete?code=123",
afterResponse: { res async throws in
#expect(res.status != .notFound)
// The response is an HTML page likely signaling an error
#expect(res.status == .unsupportedMediaType)
}
)
}
Expand All @@ -120,9 +125,10 @@ struct ImperialTests {
)

try await app.test(
.GET, "/service-auth-complete",
.GET, "/service-auth-complete?code=123",
afterResponse: { res async throws in
#expect(res.status != .notFound)
// Gitlab returns a 400 Bad Request error when the code is invalid with a JSON error message
#expect(res.status == .badRequest)
}
)
}
Expand All @@ -139,9 +145,10 @@ struct ImperialTests {
)

try await app.test(
.GET, "/service-auth-complete",
.GET, "/service-auth-complete?code=123",
afterResponse: { res async throws in
#expect(res.status != .notFound)
// Google returns a 400 Bad Request error when the code is invalid with a JSON error message
#expect(res.status == .badRequest)
}
)
}
Expand All @@ -160,7 +167,8 @@ struct ImperialTests {
try await app.test(
.GET, "/service-auth-complete",
afterResponse: { res async throws in
#expect(res.status != .notFound)
// We don't have a valid key to sign the JWT
#expect(res.status == .internalServerError)
}
)
}
Expand All @@ -177,9 +185,10 @@ struct ImperialTests {
)

try await app.test(
.GET, "/service-auth-complete",
.GET, "/service-auth-complete?code=123",
afterResponse: { res async throws in
#expect(res.status != .notFound)
// The post request fails
#expect(res.status == .internalServerError)
}
)
}
Expand All @@ -196,9 +205,10 @@ struct ImperialTests {
)

try await app.test(
.GET, "/service-auth-complete",
.GET, "/service-auth-complete?code=123",
afterResponse: { res async throws in
#expect(res.status != .notFound)
// Microsoft returns a 400 Bad Request, signaling an error with `redirect_uri`
#expect(res.status == .badRequest)
}
)
}
Expand Down
23 changes: 16 additions & 7 deletions Tests/ImperialTests/ShopifyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@ struct ShopifyTests {
@Test("Shopify Route") func shopifyRoute() async throws {
try await withApp(service: Shopify.self) { app in
try await app.test(
.GET, "/service",
.GET, "/service?shop=some-shop.myshopify.com",
afterResponse: { res async throws in
#expect(res.status != .notFound)
#expect(res.status == .seeOther)
}
)

try await app.test(
.GET, "/service-auth-complete",
.GET, "/service-auth-complete?"
+ "code=0907a61c0c8d55e99db179b68161bc00&"
+ "hmac=700e2dadb827fcc8609e9d5ce208b2e9cdaab9df07390d2cbca10d7c328fc4bf&"
+ "shop=some-shop.myshopify.com&"
+ "state=0.6784241404160823&"
+ "timestamp=1337178173",
afterResponse: { res async throws in
#expect(res.status != .notFound)
// The session should have the `nonce` property set
#expect(res.status == .badRequest)
}
)
}
Expand Down Expand Up @@ -51,9 +57,12 @@ struct ShopifyTests {
}

@Test("HMAC Validation") func hmacValidation() throws {
let url = URL(
string:
"https://domain.com/?code=0907a61c0c8d55e99db179b68161bc00&hmac=700e2dadb827fcc8609e9d5ce208b2e9cdaab9df07390d2cbca10d7c328fc4bf&shop=some-shop.myshopify.com&state=0.6784241404160823&timestamp=1337178173"
let url = URL(string: "https://domain.com/?"
+ "code=0907a61c0c8d55e99db179b68161bc00&"
+ "hmac=700e2dadb827fcc8609e9d5ce208b2e9cdaab9df07390d2cbca10d7c328fc4bf&"
+ "shop=some-shop.myshopify.com&"
+ "state=0.6784241404160823&"
+ "timestamp=1337178173"
)!

let hmac = url.generateHMAC(key: "hush")
Expand Down

0 comments on commit cb6e3e5

Please sign in to comment.