import { describe, expect, it } from 'vitest'; import { createAdminClient, createRandomString } from './config'; import { IamClient } from '../../core/iam-client'; describe('UserService', () => { async function registerTestUser(client: IamClient) { const salt = createRandomString(4).toLowerCase(); const email = `test+${salt}@applica.guru`; const password = createRandomString(10); const name = `Test User (${salt})`; const registerResponse = await client.auth.register({ name, email, password }); const activationCode = registerResponse.userId.substring(0, 6); await client.auth.activate(activationCode); return { userId: registerResponse.userId, email, name, password }; } it('should search users', async () => { const client = await createAdminClient(); const result = await client.users.search({}); expect(result).toBeDefined(); expect(result.responseCode).toBe('ok'); }); it('should get user by id', async () => { const client = await createAdminClient(); const { userId } = await registerTestUser(client); const result = await client.users.getById(userId); expect(result).toBeDefined(); expect(result.responseCode).toBe('ok'); expect(result.user).toBeDefined(); expect(result.user.id).toBe(userId); }); it('should get user by email', async () => { const client = await createAdminClient(); const { email } = await registerTestUser(client); const result = await client.users.getByEmail(email); expect(result).toBeDefined(); expect(result.responseCode).toBe('ok'); expect(result.user).toBeDefined(); expect(result.user.email).toBe(email); }); it('should update a user', async () => { const client = await createAdminClient(); const { userId } = await registerTestUser(client); const result = await client.users.update({ userId, profile: { name: 'Updated Name' } }); expect(result).toBeDefined(); expect(result.responseCode).toBe('ok'); }); it('should update user roles', async () => { const client = await createAdminClient(); const { userId } = await registerTestUser(client); const result = await client.users.updateRoles({ userId, roles: ['ROLE_USER', 'ROLE_ADMIN'] }); expect(result).toBeDefined(); expect(result.responseCode).toBe('ok'); }); it('should enable and disable a user', async () => { const client = await createAdminClient(); const { userId } = await registerTestUser(client); const disableResult = await client.users.disable(userId); expect(disableResult).toBeDefined(); expect(disableResult.responseCode).toBe('ok'); const enableResult = await client.users.enable(userId); expect(enableResult).toBeDefined(); expect(enableResult.responseCode).toBe('ok'); }); it('should search users with keyword filter', async () => { const client = await createAdminClient(); const { email } = await registerTestUser(client); const result = await client.users.search({ keyword: email }); expect(result).toBeDefined(); expect(result.responseCode).toBe('ok'); }); it('should delete a user', async () => { const client = await createAdminClient(); const { userId } = await registerTestUser(client); const result = await client.users.delete(userId); expect(result).toBeDefined(); expect(result.responseCode).toBe('ok'); }); it('should add user to tenant and remove from tenant', async () => { const client = await createAdminClient(); const { userId } = await registerTestUser(client); const tenantCode = `test-tenant-${createRandomString(4).toLowerCase()}`; const tenantResult = await client.tenants.register({ code: tenantCode, name: `Tenant ${tenantCode}` }); const tenantId = tenantResult.id; const addResult = await client.users.addToTenant({ userId, tenantId }); expect(addResult).toBeDefined(); expect(addResult.responseCode).toBe('ok'); const removeResult = await client.users.removeFromTenant({ userId, tenantId }); expect(removeResult).toBeDefined(); expect(removeResult.responseCode).toBe('ok'); }); it('should set user tenants', async () => { const client = await createAdminClient(); const { userId } = await registerTestUser(client); const code1 = `test-tenant-${createRandomString(4).toLowerCase()}`; const code2 = `test-tenant-${createRandomString(4).toLowerCase()}`; const tenant1 = await client.tenants.register({ code: code1, name: `Tenant ${code1}` }); const tenant2 = await client.tenants.register({ code: code2, name: `Tenant ${code2}` }); const result = await client.users.setTenants({ userId, tenantIds: [tenant1.id, tenant2.id] }); expect(result).toBeDefined(); expect(result.responseCode).toBe('ok'); }); });