- Refactored privilege keys in `api.md` to use a more structured naming convention, aligning with the new `Group.Parent.Module` format. - Updated various module configurations across the application to reflect the new privilege key structure, ensuring consistent access control. - Removed deprecated keys and streamlined the privilege management process, enhancing clarity and maintainability. - Added new tests for privilege key parsing and grouping functionalities to ensure reliability and correctness. These changes significantly improve the application's privilege management system, providing a clearer structure for access control and enhancing overall security.
133 lines
3.6 KiB
TypeScript
133 lines
3.6 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import type { AxiosInstance } from '@repo/core-api/http-client';
|
|
import { BaseDataTransformer } from '@repo/core-api/data-services';
|
|
import { TrackGoRemoteDataServices } from './trackgo-remote-data-services';
|
|
|
|
interface SampleEntity {
|
|
id?: string;
|
|
name: string;
|
|
code: string;
|
|
}
|
|
|
|
class SampleTransformer extends BaseDataTransformer<SampleEntity> {
|
|
transformToEntity(dto: SampleEntity): SampleEntity {
|
|
return { ...dto };
|
|
}
|
|
}
|
|
|
|
class SampleRemoteDataServices extends TrackGoRemoteDataServices<SampleEntity> {}
|
|
|
|
function createMockHttpClient(): AxiosInstance {
|
|
return {
|
|
request: vi.fn().mockResolvedValue({ data: {}, status: 200 }),
|
|
defaults: {} as AxiosInstance['defaults'],
|
|
interceptors: {
|
|
request: { use: vi.fn(), eject: vi.fn(), clear: vi.fn() },
|
|
response: { use: vi.fn(), eject: vi.fn(), clear: vi.fn() },
|
|
},
|
|
getUri: vi.fn(),
|
|
get: vi.fn(),
|
|
delete: vi.fn(),
|
|
head: vi.fn(),
|
|
options: vi.fn(),
|
|
post: vi.fn(),
|
|
put: vi.fn(),
|
|
patch: vi.fn(),
|
|
postForm: vi.fn(),
|
|
putForm: vi.fn(),
|
|
patchForm: vi.fn(),
|
|
} as unknown as AxiosInstance;
|
|
}
|
|
|
|
const unwrappedDetail = {
|
|
id: 'row-1',
|
|
name: 'Jakarta',
|
|
code: 'JKT',
|
|
};
|
|
|
|
describe('TrackGoRemoteDataServices', () => {
|
|
let httpClient: AxiosInstance;
|
|
let service: SampleRemoteDataServices;
|
|
|
|
beforeEach(() => {
|
|
httpClient = createMockHttpClient();
|
|
service = new SampleRemoteDataServices(httpClient, {
|
|
apiUrl: '/divisions',
|
|
moduleKey: 'ADMIN.SETTINGS.DATA.DIVISION',
|
|
transformer: new SampleTransformer(),
|
|
});
|
|
});
|
|
|
|
it('wraps an unwrapped getOne body as { data: entity }', async () => {
|
|
vi.mocked(httpClient.request).mockResolvedValueOnce({ data: unwrappedDetail, status: 200 });
|
|
|
|
const result = await service.getOne('row-1');
|
|
|
|
expect(result.data).toEqual({
|
|
data: expect.objectContaining({ id: 'row-1', code: 'JKT' }),
|
|
});
|
|
expect(httpClient.request).toHaveBeenCalledWith(
|
|
expect.objectContaining({ url: '/divisions/row-1', method: 'GET' }),
|
|
);
|
|
});
|
|
|
|
it('uses PATCH when editing', async () => {
|
|
await service.edit('row-1', { name: 'Jakarta', code: 'JKT' });
|
|
|
|
expect(httpClient.request).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
url: '/divisions/row-1',
|
|
method: 'PATCH',
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('activates via PATCH /:id/status with status active', async () => {
|
|
await service.activate('row-1');
|
|
|
|
expect(httpClient.request).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
url: '/divisions/row-1/status',
|
|
method: 'PATCH',
|
|
data: { status: 'active' },
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('deactivates via PATCH /:id/status with status archived', async () => {
|
|
await service.deactivate('row-1');
|
|
|
|
expect(httpClient.request).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
url: '/divisions/row-1/status',
|
|
method: 'PATCH',
|
|
data: { status: 'archived' },
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('bulk-deletes via POST /bulk-delete', async () => {
|
|
await service.batchDelete(['row-1', 'row-2']);
|
|
|
|
expect(httpClient.request).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
url: '/divisions/bulk-delete',
|
|
method: 'POST',
|
|
data: expect.objectContaining({ ids: ['row-1', 'row-2'] }),
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('bulk-activates via POST /bulk-status', async () => {
|
|
await service.batchActivate(['row-1']);
|
|
|
|
expect(httpClient.request).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
url: '/divisions/bulk-status',
|
|
method: 'POST',
|
|
data: { ids: ['row-1'], status: 'active' },
|
|
}),
|
|
);
|
|
});
|
|
});
|