Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add more CrossmintAuth tests, add isEmailValid tests #989

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 161 additions & 0 deletions packages/common/auth/src/CrossmintAuth.test.ts
Original file line number Diff line number Diff line change
@@ -35,4 +35,165 @@ describe("CrossmintAuth", () => {
expect(crossmintAuth.getJwksUri()).toBe("https://api.crossmint.com/.well-known/jwks.json");
});
});

describe("refreshAuthMaterial", () => {
it("should throw an error when refresh token is missing and no custom route is set", async () => {
await expect(
(crossmintAuth as any).refreshAuthMaterial()
).rejects.toThrow("Refresh token missing from parameters");
});

it("should call refresh method when refresh token is provided", async () => {
const refreshSpy = vi.spyOn(crossmintAuth as any, "refresh").mockResolvedValue({
jwt: "test-jwt",
refreshToken: "test-refresh-token",
user: { id: "user-id" }
});

await (crossmintAuth as any).refreshAuthMaterial("test-refresh-token");

expect(refreshSpy).toHaveBeenCalledWith("test-refresh-token");
});

it("should call refreshFromCustomRoute when custom route is set", async () => {
const customRouteAuth = CrossmintAuth.from(
mockCrossmint as unknown as Crossmint,
{ refreshRoute: "https://custom-route.com/refresh" }
);

const refreshCustomSpy = vi.spyOn(customRouteAuth as any, "refreshFromCustomRoute").mockResolvedValue({
jwt: "test-jwt",
refreshToken: "test-refresh-token",
user: { id: "user-id" }
});

await (customRouteAuth as any).refreshAuthMaterial("test-refresh-token");

expect(refreshCustomSpy).toHaveBeenCalledWith("test-refresh-token");
});
});

describe("refresh", () => {
it("should call the API client with correct parameters", async () => {
mockApiClient.post.mockResolvedValue({
ok: true,
json: async () => ({
jwt: "test-jwt",
refresh: "test-refresh-token",
user: { id: "user-id" }
})
});

const result = await (crossmintAuth as any).refresh("test-refresh-token");

expect(mockApiClient.post).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
body: JSON.stringify({ refresh: "test-refresh-token" }),
headers: { "Content-Type": "application/json" }
})
);
expect(result).toEqual({
jwt: "test-jwt",
refreshToken: "test-refresh-token",
user: { id: "user-id" }
});
});

it("should throw an error when the API response is not ok", async () => {
mockApiClient.post.mockResolvedValue({
ok: false,
statusText: "Unauthorized"
});

await expect(
(crossmintAuth as any).refresh("test-refresh-token")
).rejects.toThrow("Unauthorized");
});
});

describe("refreshFromCustomRoute", () => {
it("should throw an error when custom refresh route is not set", async () => {
await expect(
(crossmintAuth as any).refreshFromCustomRoute("test-refresh-token")
).rejects.toThrow("Custom refresh route is not set");
});

it("should fetch from the custom route when set", async () => {
const customRouteAuth = CrossmintAuth.from(
mockCrossmint as unknown as Crossmint,
{ refreshRoute: "https://custom-route.com/refresh" }
);

global.fetch = vi.fn().mockResolvedValue({
ok: true,
json: async () => ({
jwt: "test-jwt",
refreshToken: "test-refresh-token",
user: { id: "user-id" }
})
});

const result = await (customRouteAuth as any).refreshFromCustomRoute("test-refresh-token");

expect(global.fetch).toHaveBeenCalledWith(
"https://custom-route.com/refresh",
expect.objectContaining({
method: "POST",
body: JSON.stringify({ refresh: "test-refresh-token" }),
headers: { "Content-Type": "application/json" }
})
);
expect(result).toEqual({
jwt: "test-jwt",
refreshToken: "test-refresh-token",
user: { id: "user-id" }
});
});

it("should throw an error when the custom route response is not ok", async () => {
const customRouteAuth = CrossmintAuth.from(
mockCrossmint as unknown as Crossmint,
{ refreshRoute: "https://custom-route.com/refresh" }
);

global.fetch = vi.fn().mockResolvedValue({
ok: false,
json: async () => ({ message: "Authentication failed" })
});

await expect(
(customRouteAuth as any).refreshFromCustomRoute("test-refresh-token")
).rejects.toThrow("Authentication failed");
});
});

describe("logoutFromDefaultRoute", () => {
it("should call the API client with correct parameters", async () => {
mockApiClient.post.mockResolvedValue({ ok: true });

await (crossmintAuth as any).logoutFromDefaultRoute("test-refresh-token");

expect(mockApiClient.post).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
body: JSON.stringify({ refresh: "test-refresh-token" }),
headers: { "Content-Type": "application/json" }
})
);
});
});

describe("defaultApiClient", () => {
it("should create a new CrossmintApiClient with the correct parameters", () => {
CrossmintAuth.defaultApiClient(mockCrossmint as unknown as Crossmint);

expect(CrossmintApiClient).toHaveBeenCalledWith(
mockCrossmint,
expect.objectContaining({
internalConfig: expect.anything()
})
);
});
});
});
33 changes: 33 additions & 0 deletions packages/common/auth/src/utils/isEmailValid.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { describe, expect, it } from "vitest";
import { isEmailValid } from "./isEmailValid";

describe("isEmailValid", () => {
it("should return true for valid email addresses", () => {
expect(isEmailValid("[email protected]")).toBe(true);
expect(isEmailValid("[email protected]")).toBe(true);
expect(isEmailValid("[email protected]")).toBe(true);
expect(isEmailValid("[email protected]")).toBe(true);
expect(isEmailValid("[email protected]")).toBe(true);
expect(isEmailValid("[email protected]")).toBe(true);
});

it("should return false for invalid email addresses", () => {
expect(isEmailValid("")).toBe(false);
expect(isEmailValid("test")).toBe(false);
expect(isEmailValid("test@")).toBe(false);
expect(isEmailValid("@example.com")).toBe(false);
expect(isEmailValid("test@example")).toBe(false);
expect(isEmailValid("[email protected]")).toBe(false);
expect(isEmailValid("test@example.")).toBe(false);
expect(isEmailValid("test@exam ple.com")).toBe(false);
expect(isEmailValid(" [email protected]")).toBe(false);
expect(isEmailValid("[email protected] ")).toBe(false);
});

it("should handle edge cases correctly", () => {
expect(isEmailValid("[email protected]")).toBe(true); // Minimal valid email
expect(isEmailValid("test@localhost")).toBe(false); // Missing TLD
expect(isEmailValid("test.example.com")).toBe(false); // Missing @ symbol
expect(isEmailValid("test@@example.com")).toBe(false); // Double @ symbol
});
});