102 lines
2.8 KiB
TypeScript
102 lines
2.8 KiB
TypeScript
import { describe, it, expect, vi, beforeEach, type MockInstance } from "vitest"
|
|
import api from "./api"
|
|
import { AuthService } from "./auth"
|
|
|
|
vi.mock("./api")
|
|
const mockedApi = api as unknown as {
|
|
post: MockInstance<typeof api.post>
|
|
get: MockInstance<typeof api.get>
|
|
}
|
|
|
|
describe("AuthService", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
it("successfully logs in", async () => {
|
|
const mockResponse = {
|
|
data: {
|
|
access_token: "fake-jwt-token",
|
|
token_type: "bearer",
|
|
},
|
|
}
|
|
mockedApi.post.mockResolvedValueOnce(mockResponse)
|
|
|
|
const result = await AuthService.login("test@example.com", "password123")
|
|
|
|
expect(mockedApi.post).toHaveBeenCalledWith("/auth/login", expect.any(FormData))
|
|
|
|
// Validate FormData content
|
|
const formData = (mockedApi.post.mock.calls[0][1] as FormData)
|
|
expect(formData.get("username")).toBe("test@example.com")
|
|
expect(formData.get("password")).toBe("password123")
|
|
|
|
expect(result.access_token).toBe("fake-jwt-token")
|
|
})
|
|
|
|
it("successfully registers a user", async () => {
|
|
const mockResponse = {
|
|
data: {
|
|
id: "user-123",
|
|
email: "test@example.com",
|
|
},
|
|
}
|
|
mockedApi.post.mockResolvedValueOnce(mockResponse)
|
|
|
|
const result = await AuthService.register("test@example.com", "password123")
|
|
|
|
expect(mockedApi.post).toHaveBeenCalledWith("/auth/register", {
|
|
email: "test@example.com",
|
|
password: "password123",
|
|
})
|
|
expect(result.email).toBe("test@example.com")
|
|
})
|
|
|
|
it("successfully fetches current user", async () => {
|
|
const mockUser = {
|
|
id: "user-123",
|
|
email: "test@example.com",
|
|
display_name: "Test User"
|
|
}
|
|
mockedApi.get.mockResolvedValueOnce({
|
|
data: mockUser,
|
|
headers: { "content-type": "application/json" }
|
|
})
|
|
|
|
const result = await AuthService.getMe()
|
|
|
|
expect(mockedApi.get).toHaveBeenCalledWith("/auth/me")
|
|
expect(result).toEqual(mockUser)
|
|
})
|
|
|
|
it("throws error on invalid non-JSON response", async () => {
|
|
mockedApi.get.mockResolvedValueOnce({
|
|
data: "<html>Some fallback</html>",
|
|
headers: { "content-type": "text/html" }
|
|
})
|
|
|
|
await expect(AuthService.getMe()).rejects.toThrow("Invalid response from server")
|
|
})
|
|
|
|
it("logs out and calls backend", async () => {
|
|
mockedApi.post.mockResolvedValueOnce({ data: { detail: "success" } })
|
|
await AuthService.logout()
|
|
expect(mockedApi.post).toHaveBeenCalledWith("/auth/logout")
|
|
})
|
|
|
|
it("successfully refreshes session", async () => {
|
|
const mockResponse = {
|
|
data: {
|
|
access_token: "new-fake-token",
|
|
token_type: "bearer",
|
|
},
|
|
}
|
|
mockedApi.post.mockResolvedValueOnce(mockResponse)
|
|
|
|
const result = await AuthService.refreshSession()
|
|
|
|
expect(mockedApi.post).toHaveBeenCalledWith("/auth/refresh")
|
|
expect(result.access_token).toBe("new-fake-token")
|
|
})
|
|
})
|