Skip to content

Commit

Permalink
Add more basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino committed Nov 16, 2024
1 parent f3e2e60 commit c9199fc
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 61 deletions.
30 changes: 21 additions & 9 deletions .env.testing
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
AUTH0_DOMAIN=test
AUTH0_CLIENT_ID=test
AUTH0_CLIENT_SECRET=test

DISCORD_CLIENT_ID=test
DISCORD_CLIENT_SECRET=test

DROPBOX_CLIENT_ID=test
DROPBOX_CLIENT_SECRET=test

FACEBOOK_CLIENT_ID=test
FACEBOOK_CLIENT_SECRET=test

GITHUB_CLIENT_ID=test
GITHUB_CLIENT_SECRET=test

GITLAB_CLIENT_ID=test
GITLAB_CLIENT_SECRET=test

GOOGLE_CLIENT_ID=test
GOOGLE_CLIENT_SECRET=test

GOOGLEJWT_CLIENT_EMAIL=test
GOOGLEJWT_CLIENT_SECRET=test

SHOPIFY_CLIENT_ID=test
SHOPIFY_CLIENT_SECRET=test

FACEBOOK_CLIENT_ID=test
FACEBOOK_CLIENT_SECRET=test

KEYCLOAK_CLIENT_ID=test
KEYCLOAK_CLIENT_SECRET=test
KEYCLOAK_ACCESS_TOKEN_URL=test
KEYCLOAK_AUTH_URL=test

DISCORD_CLIENT_ID=test
DISCORD_CLIENT_SECRET=test

AUTH0_DOMAIN=test
AUTH0_CLIENT_ID=test
AUTH0_CLIENT_SECRET=test
MICROSOFT_CLIENT_ID=test
MICROSOFT_CLIENT_SECRET=test
11 changes: 7 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,16 @@ let package = Package(
name: "ImperialTests",
dependencies: [
.target(name: "ImperialCore"),
.target(name: "ImperialAuth0"),
.target(name: "ImperialDiscord"),
.target(name: "ImperialDropbox"),
.target(name: "ImperialFacebook"),
.target(name: "ImperialGitHub"),
.target(name: "ImperialGitlab"),
.target(name: "ImperialGoogle"),
.target(name: "ImperialShopify"),
.target(name: "ImperialFacebook"),
.target(name: "ImperialKeycloak"),
.target(name: "ImperialDiscord"),
.target(name: "ImperialAuth0"),
.target(name: "ImperialMicrosoft"),
.target(name: "ImperialShopify"),
.product(name: "XCTVapor", package: "vapor"),
],
swiftSettings: swiftSettings
Expand Down
15 changes: 0 additions & 15 deletions Sources/ImperialCore/Errors/ServiceError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
public struct ServiceError: Error, Sendable, Equatable {
public struct ErrorType: Sendable, Hashable, CustomStringConvertible, Equatable {
enum Base: String, Sendable, Equatable {
case noServiceFound
case noServiceEndpoint
}

Expand All @@ -12,7 +11,6 @@ public struct ServiceError: Error, Sendable, Equatable {
self.base = base
}

public static let noServiceFound = Self(.noServiceFound)
public static let noServiceEndpoint = Self(.noServiceEndpoint)

public var description: String {
Expand All @@ -22,16 +20,13 @@ public struct ServiceError: Error, Sendable, Equatable {

private struct Backing: Sendable, Equatable {
fileprivate let errorType: ErrorType
fileprivate let name: String?
fileprivate let endpoint: String?

init(
errorType: ErrorType,
name: String? = nil,
endpoint: String? = nil
) {
self.errorType = errorType
self.name = name
self.endpoint = endpoint
}

Expand All @@ -43,18 +38,12 @@ public struct ServiceError: Error, Sendable, Equatable {
private var backing: Backing

public var errorType: ErrorType { backing.errorType }
public var name: String? { backing.name }
public var endpoint: String? { backing.endpoint }

private init(backing: Backing) {
self.backing = backing
}

/// Thrown when no service is registered with a given name.
public static func noServiceFound(_ name: String) -> Self {
.init(backing: .init(errorType: .noServiceFound, name: name))
}

/// Thrown when a `FederatedCreatable` type has a `serviceKey` that does not match any available endpoints in the service.
public static func noServiceEndpoint(_ endpoint: String) -> Self {
.init(backing: .init(errorType: .noServiceEndpoint, endpoint: endpoint))
Expand All @@ -69,10 +58,6 @@ extension ServiceError: CustomStringConvertible {
public var description: String {
var result = #"ServiceError(errorType: \#(self.errorType)"#

if let name {
result.append(", no service was found with the name: \(name)")
}

if let endpoint {
result.append(", service does not have available endpoint for key: \(endpoint)")
}
Expand Down
128 changes: 95 additions & 33 deletions Tests/ImperialTests/ImperialTests.swift
Original file line number Diff line number Diff line change
@@ -1,24 +1,77 @@
import ImperialAuth0
import ImperialDiscord
import ImperialDropbox
import ImperialFacebook
import ImperialGitHub
import ImperialGitlab
import ImperialGoogle
import ImperialFacebook
import ImperialKeycloak
import ImperialDiscord
import ImperialAuth0
import ImperialMicrosoft
import Testing
import XCTVapor

@testable import ImperialCore

@Suite("ImperialCore Tests")
struct ImperialTests {
@Test("ImperialError & ServiceError")
func errors() {
#expect(ImperialError.missingEnvVar("test").description == "ImperialError(errorType: missingEnvVar, missing enviroment variable: test)")
#expect(ImperialError.missingEnvVar("foo") == ImperialError.missingEnvVar("bar"))
@Test("Auth0 Route")
func auth0Route() async throws {
try await withApp { app in
try await app.test(.GET, "/auth0", afterResponse: { res async throws in
#expect(res.status == .notFound)
})

#expect(ServiceError.noServiceFound("test").description == "ServiceError(errorType: noServiceFound, no service was found with the name: test)")
#expect(ServiceError.noServiceEndpoint("test").description == "ServiceError(errorType: noServiceEndpoint, service does not have available endpoint for key: test)")
#expect(ServiceError.noServiceFound("foo") == ServiceError.noServiceFound("bar"))
#expect(ServiceError.noServiceEndpoint("foo") == ServiceError.noServiceEndpoint("bar"))
try app.oAuth(from: Auth0.self, authenticate: "auth0", callback: "auth0-auth-complete", redirect: "/")

try await app.test(.GET, "/auth0", afterResponse: { res async throws in
#expect(res.status != .notFound)
})
}
}

@Test("Discord Route")
func discordRoute() async throws {
try await withApp { app in
try await app.test(.GET, "/discord", afterResponse: { res async throws in
#expect(res.status == .notFound)
})

try app.oAuth(from: Discord.self, authenticate: "discord", callback: "discord-auth-complete", redirect: "/")

try await app.test(.GET, "/discord", afterResponse: { res async throws in
#expect(res.status != .notFound)
})
}
}

@Test("Dropbox Route")
func dropboxRoute() async throws {
try await withApp { app in
try await app.test(.GET, "/dropbox", afterResponse: { res async throws in
#expect(res.status == .notFound)
})

try app.oAuth(from: Dropbox.self, authenticate: "dropbox", callback: "dropbox-auth-complete", redirect: "/")

try await app.test(.GET, "/dropbox", afterResponse: { res async throws in
#expect(res.status != .notFound)
})
}
}

@Test("Facebook Route")
func facebookRoute() async throws {
try await withApp { app in
try await app.test(.GET, "/facebook", afterResponse: { res async throws in
#expect(res.status == .notFound)
})

try app.oAuth(from: Facebook.self, authenticate: "facebook", callback: "facebook-auth-complete", redirect: "/")

try await app.test(.GET, "/facebook", afterResponse: { res async throws in
#expect(res.status != .notFound)
})
}
}

@Test("GitHub Route")
Expand All @@ -36,6 +89,21 @@ struct ImperialTests {
}
}

@Test("Gitlab Route")
func gitlabRoute() async throws {
try await withApp { app in
try await app.test(.GET, "/gitlab", afterResponse: { res async throws in
#expect(res.status == .notFound)
})

try app.oAuth(from: Gitlab.self, authenticate: "gitlab", callback: "gitlab-auth-complete", redirect: "/")

try await app.test(.GET, "/gitlab", afterResponse: { res async throws in
#expect(res.status != .notFound)
})
}
}

@Test("Google Route")
func googleRoute() async throws {
try await withApp { app in
Expand All @@ -51,16 +119,16 @@ struct ImperialTests {
}
}

@Test("Facebook Route")
func facebookRoute() async throws {
@Test("Google JWT Route")
func googleJWTRoute() async throws {
try await withApp { app in
try await app.test(.GET, "/facebook", afterResponse: { res async throws in
try await app.test(.GET, "/googleJWT", afterResponse: { res async throws in
#expect(res.status == .notFound)
})

try app.oAuth(from: Facebook.self, authenticate: "facebook", callback: "facebook-auth-complete", redirect: "/")
try app.oAuth(from: GoogleJWT.self, authenticate: "googleJWT", callback: "googleJWT-auth-complete", redirect: "/")

try await app.test(.GET, "/facebook", afterResponse: { res async throws in
try await app.test(.GET, "/googleJWT", afterResponse: { res async throws in
#expect(res.status != .notFound)
})
}
Expand All @@ -81,33 +149,27 @@ struct ImperialTests {
}
}

@Test("Discord Route")
func discordRoute() async throws {
@Test("Microsoft Route")
func microsoftRoute() async throws {
try await withApp { app in
try await app.test(.GET, "/discord", afterResponse: { res async throws in
try await app.test(.GET, "/microsoft", afterResponse: { res async throws in
#expect(res.status == .notFound)
})

try app.oAuth(from: Discord.self, authenticate: "discord", callback: "discord-auth-complete", redirect: "/")
try app.oAuth(from: Microsoft.self, authenticate: "microsoft", callback: "microsoft-auth-complete", redirect: "/")

try await app.test(.GET, "/discord", afterResponse: { res async throws in
try await app.test(.GET, "/microsoft", afterResponse: { res async throws in
#expect(res.status != .notFound)
})
}
}

@Test("Auth0 Route")
func auth0Route() async throws {
try await withApp { app in
try await app.test(.GET, "/auth0", afterResponse: { res async throws in
#expect(res.status == .notFound)
})

try app.oAuth(from: Auth0.self, authenticate: "auth0", callback: "auth0-auth-complete", redirect: "/")
@Test("ImperialError & ServiceError")
func errors() {
#expect(ImperialError.missingEnvVar("test").description == "ImperialError(errorType: missingEnvVar, missing enviroment variable: test)")
#expect(ImperialError.missingEnvVar("foo") == ImperialError.missingEnvVar("bar"))

try await app.test(.GET, "/auth0", afterResponse: { res async throws in
#expect(res.status != .notFound)
})
}
#expect(ServiceError.noServiceEndpoint("test").description == "ServiceError(errorType: noServiceEndpoint, service does not have available endpoint for key: test)")
#expect(ServiceError.noServiceEndpoint("foo") == ServiceError.noServiceEndpoint("bar"))
}
}

0 comments on commit c9199fc

Please sign in to comment.