Skip to content

Commit

Permalink
[TM-1531] more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
egrojMonroy committed Dec 11, 2024
1 parent f898f22 commit 0b83383
Showing 1 changed file with 57 additions and 32 deletions.
89 changes: 57 additions & 32 deletions apps/job-service/src/jobs/delayed-jobs.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DelayedJobsController } from './delayed-jobs.controller';
import { DelayedJob } from '@terramatch-microservices/database/entities';
import { DelayedJobBulkUpdateBodyDto } from './dto/delayed-job-update.dto';
import { v4 as uuidv4 } from 'uuid';
import { BadRequestException, NotFoundException, UnauthorizedException } from '@nestjs/common';
import { UnauthorizedException, NotFoundException, BadRequestException } from '@nestjs/common';

describe('DelayedJobsController', () => {
let controller: DelayedJobsController;
Expand All @@ -25,10 +25,61 @@ describe('DelayedJobsController', () => {
jest.restoreAllMocks();
});

describe('bulkClearJobs', () => {
it('should successfully clear multiple jobs', async () => {
describe('getRunningJobs', () => {
it('should return a list of running jobs for the authenticated user', async () => {
const authenticatedUserId = 130999;

const job = await DelayedJob.create({
uuid: uuidv4(),
createdBy: authenticatedUserId,
isAcknowledged: false,
status: 'completed',
});
const request = {
authenticatedUserId,
};

const result = await controller.getRunningJobs(request);

const data = Array.isArray(result.data) ? result.data : [result.data];

expect(data).toHaveLength(1);
expect(data[0].id).toBe(job.uuid);
});
it('should return an empty list when there are no running jobs', async () => {
const authenticatedUserId = 130999;
const request = { authenticatedUserId };

const result = await controller.getRunningJobs(request);
expect(result.data).toHaveLength(0);
});
});

describe('findOne', () => {
it('should return a job by UUID', async () => {
const authenticatedUserId = 130999;
const job = await DelayedJob.create({
uuid: uuidv4(),
createdBy: authenticatedUserId,
isAcknowledged: false,
status: 'completed'
});

const result = await controller.findOne(job.uuid);
const jobData = Array.isArray(result.data) ? result.data[0] : result.data;
expect(jobData.id).toBe(job.uuid);
});
it('should throw NotFoundException when the job does not exist', async () => {
const nonExistentUuid = uuidv4();

await expect(controller.findOne(nonExistentUuid)).rejects.toThrow(NotFoundException);
});

});

describe('bulkClearJobs', () => {
it('should successfully bulk update jobs to acknowledged', async () => {
const authenticatedUserId = 130999;
const job1 = await DelayedJob.create({
uuid: uuidv4(),
createdBy: authenticatedUserId,
Expand Down Expand Up @@ -60,7 +111,7 @@ describe('DelayedJobsController', () => {
const request = { authenticatedUserId };

const result = await controller.bulkClearJobs(payload, request);

expect(result.data).toHaveLength(2);
expect(result.data[0].id).toBe(job1.uuid);
expect(result.data[1].id).toBe(job2.uuid);
Expand Down Expand Up @@ -95,33 +146,7 @@ describe('DelayedJobsController', () => {
.rejects.toThrow(NotFoundException);
});

it('should not update jobs created by other users', async () => {
const authenticatedUserId = 130999;
const otherUserId = 999999;

const otherUserJob = await DelayedJob.create({
uuid: uuidv4(),
createdBy: otherUserId,
isAcknowledged: false,
status: 'completed'
});

const payload: DelayedJobBulkUpdateBodyDto = {
data: [
{
type: 'delayedJobs',
uuid: otherUserJob.uuid,
attributes: { isAcknowledged: true }
}
]
};
const request = { authenticatedUserId };

await expect(controller.bulkClearJobs(payload, request))
.rejects.toThrow(NotFoundException);
});

it('should throw UnauthorizedException for missing authenticated id', async () => {
it('should throw UnauthorizedException if no authenticated user id', async () => {
const payload: DelayedJobBulkUpdateBodyDto = {
data: [
{ type: 'delayedJobs', uuid: uuidv4(), attributes: { isAcknowledged: true } }
Expand All @@ -134,7 +159,6 @@ describe('DelayedJobsController', () => {

it('should not update jobs with status "pending"', async () => {
const authenticatedUserId = 130999;

const pendingJob = await DelayedJob.create({
uuid: uuidv4(),
createdBy: authenticatedUserId,
Expand All @@ -156,5 +180,6 @@ describe('DelayedJobsController', () => {
await expect(controller.bulkClearJobs(payload, request))
.rejects.toThrow(NotFoundException);
});

});
});

0 comments on commit 0b83383

Please sign in to comment.