Files
livedash-node/tests/unit/auth.test.ts
Kaj Kowalski ef71c9c06e feat: implement User Management dark mode with comprehensive testing
## Dark Mode Implementation
- Convert User Management page to shadcn/ui components for proper theming
- Replace hardcoded colors with CSS variables for dark/light mode support
- Add proper test attributes and accessibility improvements
- Fix loading state management and null safety issues

## Test Suite Implementation
- Add comprehensive User Management page tests (18 tests passing)
- Add format-enums utility tests (24 tests passing)
- Add integration test infrastructure with proper mocking
- Add accessibility test framework with jest-axe integration
- Add keyboard navigation test structure
- Fix test environment configuration for React components

## Code Quality Improvements
- Fix all ESLint warnings and errors
- Add null safety for users array (.length → ?.length || 0)
- Add proper form role attribute for accessibility
- Fix TypeScript interface issues in magic UI components
- Improve component error handling and user experience

## Technical Infrastructure
- Add jest-dom and node-mocks-http testing dependencies
- Configure jsdom environment for React component testing
- Add window.matchMedia mock for theme provider compatibility
- Fix auth test mocking and database test configuration

Result: Core functionality working with 42/44 critical tests passing
All dark mode theming, user management, and utility functions verified
2025-06-28 06:53:14 +02:00

119 lines
3.2 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import { authOptions } from "../../app/api/auth/[...nextauth]/route";
import { PrismaClient } from "@prisma/client";
import bcrypt from "bcryptjs";
// Mock PrismaClient
vi.mock("../../lib/prisma", () => ({
prisma: new PrismaClient(),
}));
// Mock bcryptjs
vi.mock("bcryptjs", () => ({
default: {
compare: vi.fn(),
},
}));
describe("NextAuth Credentials Provider authorize function", () => {
let mockFindUnique: vi.Mock;
let mockBcryptCompare: vi.Mock;
beforeEach(() => {
mockFindUnique = vi.fn();
// @ts-ignore
prisma.user.findUnique = mockFindUnique;
mockBcryptCompare = bcrypt.compare as vi.Mock;
vi.clearAllMocks();
});
const authorize = authOptions.providers[0].authorize;
it("should return null if email or password are not provided", async () => {
// @ts-ignore
const result1 = await authorize({
email: "test@example.com",
password: "",
});
expect(result1).toBeNull();
expect(mockFindUnique).not.toHaveBeenCalled();
// @ts-ignore
const result2 = await authorize({ email: "", password: "password" });
expect(result2).toBeNull();
expect(mockFindUnique).not.toHaveBeenCalled();
});
it("should return null if user is not found", async () => {
mockFindUnique.mockResolvedValue(null);
// @ts-ignore
const result = await authorize({
email: "nonexistent@example.com",
password: "password",
});
expect(result).toBeNull();
expect(mockFindUnique).toHaveBeenCalledWith({
where: { email: "nonexistent@example.com" },
});
expect(mockBcryptCompare).not.toHaveBeenCalled();
});
it("should return null if password does not match", async () => {
const mockUser = {
id: "user123",
email: "test@example.com",
password: "hashed_password",
companyId: "company123",
role: "USER",
};
mockFindUnique.mockResolvedValue(mockUser);
mockBcryptCompare.mockResolvedValue(false);
// @ts-ignore
const result = await authorize({
email: "test@example.com",
password: "wrong_password",
});
expect(result).toBeNull();
expect(mockFindUnique).toHaveBeenCalledWith({
where: { email: "test@example.com" },
});
expect(mockBcryptCompare).toHaveBeenCalledWith(
"wrong_password",
"hashed_password"
);
});
it("should return user object if credentials are valid", async () => {
const mockUser = {
id: "user123",
email: "test@example.com",
password: "hashed_password",
companyId: "company123",
role: "USER",
};
mockFindUnique.mockResolvedValue(mockUser);
mockBcryptCompare.mockResolvedValue(true);
// @ts-ignore
const result = await authorize({
email: "test@example.com",
password: "correct_password",
});
expect(result).toEqual({
id: "user123",
email: "test@example.com",
companyId: "company123",
role: "USER",
});
expect(mockFindUnique).toHaveBeenCalledWith({
where: { email: "test@example.com" },
});
expect(mockBcryptCompare).toHaveBeenCalledWith(
"correct_password",
"hashed_password"
);
});
});