tududi/backend/tests/unit/services/rolesService.test.js
Chris 3486541272
Add comprehensive LLM development documentation (#939)
* Increase coverage

* Add comprehensive LLM development documentation

- Add CLAUDE.md as main documentation index
- Create 8 detailed documentation files in docs/:
  - architecture.md: Tech stack, data models, auth system
  - directory-structure.md: Complete file tree with paths
  - backend-patterns.md: Module architecture and patterns
  - database.md: Models, migrations, and workflows
  - development-workflow.md: Setup and daily development
  - code-conventions.md: Style guide and best practices
  - testing.md: Test organization and patterns
  - common-tasks.md: How-to guides for frequent tasks
- Update .gitignore to allow project-level CLAUDE.md
- 4,285 lines of comprehensive documentation
- Organized for easy navigation with cross-links
- LLM-optimized with absolute paths and code examples

* fixup! Add comprehensive LLM development documentation
2026-03-14 02:54:59 +02:00

84 lines
3.1 KiB
JavaScript

const { isAdmin } = require('../../../services/rolesService');
const { User, Role, sequelize } = require('../../../models');
const bcrypt = require('bcrypt');
describe('rolesService', () => {
beforeEach(async () => {
await sequelize.query('DELETE FROM roles');
});
describe('isAdmin', () => {
it('should return false for null uid', async () => {
expect(await isAdmin(null)).toBe(false);
});
it('should return false for undefined uid', async () => {
expect(await isAdmin(undefined)).toBe(false);
});
it('should return false for empty string uid', async () => {
expect(await isAdmin('')).toBe(false);
});
it('should return false when user does not exist', async () => {
expect(await isAdmin('nonexistent-uid')).toBe(false);
});
it('should return true for first user (auto-admin via afterCreate hook)', async () => {
const hash = await bcrypt.hash('pass', 10);
// First user created when no admin exists becomes admin automatically
const user = await User.create({
email: 'first@example.com',
password_digest: hash,
});
expect(await isAdmin(user.uid)).toBe(true);
});
it('should return false for non-first user (non-admin)', async () => {
const hash = await bcrypt.hash('pass', 10);
// First user becomes admin
await User.create({
email: 'first@example.com',
password_digest: hash,
});
// Second user is not admin
const second = await User.create({
email: 'second@example.com',
password_digest: hash,
});
expect(await isAdmin(second.uid)).toBe(false);
});
it('should return false when user role has is_admin=false', async () => {
const hash = await bcrypt.hash('pass', 10);
const user = await User.create({
email: 'demoted@example.com',
password_digest: hash,
});
// The hook created an admin role; update it to non-admin
await Role.update(
{ is_admin: false },
{ where: { user_id: user.id } }
);
expect(await isAdmin(user.uid)).toBe(false);
});
it('should return true when user role has is_admin=true', async () => {
const hash = await bcrypt.hash('pass', 10);
await User.create({
email: 'first@example.com',
password_digest: hash,
});
const user = await User.create({
email: 'promoted@example.com',
password_digest: hash,
});
// The hook created a non-admin role; update it to admin
await Role.update(
{ is_admin: true },
{ where: { user_id: user.id } }
);
expect(await isAdmin(user.uid)).toBe(true);
});
});
});