Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions package-lock.json
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no new change in package.json; There is no need to push your local installation changes. (package-lock.json)

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@
"@faker-js/faker": "^8.4.1",
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^11.0.15",


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DennisTemoye an extra space added

"@types/bcryptjs": "^2.4.4",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
Expand Down
2 changes: 2 additions & 0 deletions src/shared/auth/guards/jwt.users.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import {
import { JwtService } from '@nestjs/jwt';
import { AuthGuard } from '@nestjs/passport';
import { hasRequiredRoles } from 'src/shared/utils';

import { UserRole } from '../../interfaces';


@Injectable()
export class JwtUsersGuard
extends AuthGuard('jwt-user')
Expand Down
92 changes: 78 additions & 14 deletions src/shared/datalogs/data.logs.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,94 @@
import { Test, TestingModule } from '@nestjs/testing';
import { JwtService } from '@nestjs/jwt';
import { TestModule } from '../testkits';
import { DataLogsController } from './data.logs.controller';
import { DataLogsService } from './data.logs.service';
import { JwtAdminsGuard } from '../auth/guards/jwt.admins.guard';
import { JwtUsersGuard } from '../auth/guards/jwt.users.guard';
import { NotFoundException } from '@nestjs/common';

describe('DataLogsController', () => {
let dataLogsController: DataLogsController;
let controller: DataLogsController;
let logsService: DataLogsService;

const mockLogsService = {
findAll: jest.fn(),
publicLog: jest.fn(),
remove: jest.fn(),
};

beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
imports: [TestModule],
const module: TestingModule = await Test.createTestingModule({
controllers: [DataLogsController],
providers: [DataLogsService],
providers: [
{
provide: DataLogsService,
useValue: mockLogsService,
},
],
}).compile();

dataLogsController = app.get<DataLogsController>(
DataLogsController,
) as DataLogsController;
controller = module.get<DataLogsController>(DataLogsController);
logsService = module.get<DataLogsService>(DataLogsService);
});

describe('findAll', () => {
it('should return all logs for admins', async () => {
const mockResponse = [{ id: '1', message: 'Log entry' }];
mockLogsService.findAll.mockResolvedValue(mockResponse);

const result = await controller.findAll({ query: {} } as any);
expect(result).toEqual(mockResponse);
expect(logsService.findAll).toHaveBeenCalledWith({ query: {} });
});
});

describe('logUser', () => {
it('should log a user activity', async () => {
const mockRequest = { body: { message: 'User action' } };
const mockSource = 'user-app';
mockLogsService.publicLog.mockResolvedValue({ success: true });

const result = await controller.logUser(mockRequest as any, mockSource);
expect(result).toEqual({ success: true });
expect(logsService.publicLog).toHaveBeenCalledWith(
mockRequest,
mockSource,
);
});
});

global.dataLogsService = app.get<DataLogsService>(DataLogsService);
global.jwtService = app.get<JwtService>(JwtService);
describe('logAdmin', () => {
it('should log an admin activity', async () => {
const mockRequest = { body: { message: 'Admin action' } };
const mockSource = 'admin-dashboard';
mockLogsService.publicLog.mockResolvedValue({ success: true });

const result = await controller.logAdmin(mockRequest as any, mockSource);
expect(result).toEqual({ success: true });
expect(logsService.publicLog).toHaveBeenCalledWith(
mockRequest,
mockSource,
);
});
});

describe('dataLogsController', () => {
it('should return true for dataLogsController"', () => {
expect(!!dataLogsController).toBe(true);
describe('remove', () => {
it('should remove a data log by ID', async () => {
const mockLogId = 'log123';
mockLogsService.remove.mockResolvedValue({ success: true });

const result = await controller.remove(mockLogId);
expect(result).toEqual({ success: true });
expect(logsService.remove).toHaveBeenCalledWith(mockLogId);
});

it('should throw NotFoundException if log is not found', async () => {
mockLogsService.remove.mockImplementationOnce(() => {
throw new NotFoundException('Log not found');
});

await expect(controller.remove('invalid-log-id')).rejects.toThrow(
NotFoundException,
);
});
});
});
Loading