test(auth): Add tests for AuthService and Auth validations
This commit is contained in:
50
frontend/src/lib/validations/auth.test.ts
Normal file
50
frontend/src/lib/validations/auth.test.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { describe, it, expect } from "vitest"
|
||||
import { loginSchema, registerSchema } from "./auth"
|
||||
|
||||
describe("Auth Validations", () => {
|
||||
describe("loginSchema", () => {
|
||||
it("validates correct input", () => {
|
||||
const result = loginSchema.safeParse({
|
||||
email: "test@example.com",
|
||||
password: "password123",
|
||||
})
|
||||
expect(result.success).toBe(true)
|
||||
})
|
||||
|
||||
it("fails on invalid email", () => {
|
||||
const result = loginSchema.safeParse({
|
||||
email: "not-an-email",
|
||||
password: "password123",
|
||||
})
|
||||
expect(result.success).toBe(false)
|
||||
})
|
||||
|
||||
it("fails on short password", () => {
|
||||
const result = loginSchema.safeParse({
|
||||
email: "test@example.com",
|
||||
password: "123",
|
||||
})
|
||||
expect(result.success).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("registerSchema", () => {
|
||||
it("validates correct input", () => {
|
||||
const result = registerSchema.safeParse({
|
||||
email: "test@example.com",
|
||||
password: "password123",
|
||||
confirmPassword: "password123",
|
||||
})
|
||||
expect(result.success).toBe(true)
|
||||
})
|
||||
|
||||
it("fails if passwords do not match", () => {
|
||||
const result = registerSchema.safeParse({
|
||||
email: "test@example.com",
|
||||
password: "password123",
|
||||
confirmPassword: "different",
|
||||
})
|
||||
expect(result.success).toBe(false)
|
||||
})
|
||||
})
|
||||
})
|
||||
60
frontend/src/services/auth.test.ts
Normal file
60
frontend/src/services/auth.test.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest"
|
||||
import axios from "axios"
|
||||
import { AuthService } from "./auth"
|
||||
|
||||
vi.mock("axios")
|
||||
const mockedAxios = axios as jest.Mocked<typeof axios>
|
||||
|
||||
describe("AuthService", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
localStorage.clear()
|
||||
})
|
||||
|
||||
it("successfully logs in and stores the token", async () => {
|
||||
const mockResponse = {
|
||||
data: {
|
||||
access_token: "fake-jwt-token",
|
||||
token_type: "bearer",
|
||||
},
|
||||
}
|
||||
mockedAxios.post.mockResolvedValueOnce(mockResponse)
|
||||
|
||||
const result = await AuthService.login("test@example.com", "password123")
|
||||
|
||||
expect(mockedAxios.post).toHaveBeenCalledWith("/api/auth/login", expect.any(FormData))
|
||||
expect(result.access_token).toBe("fake-jwt-token")
|
||||
expect(localStorage.getItem("token")).toBe("fake-jwt-token")
|
||||
})
|
||||
|
||||
it("handles login failure", async () => {
|
||||
mockedAxios.post.mockRejectedValueOnce(new Error("Invalid credentials"))
|
||||
|
||||
await expect(AuthService.login("test@example.com", "wrong")).rejects.toThrow("Invalid credentials")
|
||||
expect(localStorage.getItem("token")).toBeNull()
|
||||
})
|
||||
|
||||
it("successfully registers a user", async () => {
|
||||
const mockResponse = {
|
||||
data: {
|
||||
id: "user-123",
|
||||
email: "test@example.com",
|
||||
},
|
||||
}
|
||||
mockedAxios.post.mockResolvedValueOnce(mockResponse)
|
||||
|
||||
const result = await AuthService.register("test@example.com", "password123")
|
||||
|
||||
expect(mockedAxios.post).toHaveBeenCalledWith("/api/auth/register", {
|
||||
email: "test@example.com",
|
||||
password: "password123",
|
||||
})
|
||||
expect(result.email).toBe("test@example.com")
|
||||
})
|
||||
|
||||
it("logs out and clears the token", () => {
|
||||
localStorage.setItem("token", "some-token")
|
||||
AuthService.logout()
|
||||
expect(localStorage.getItem("token")).toBeNull()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user