Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
288 changes: 286 additions & 2 deletions src/users/users.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,6 @@ describe('UsersAdminController', () => {

it('should request new verification token', async () => {
const mockReq = { user: createUserMock() };

// Mocking the requestVerification method to resolve successfully
jest
.spyOn(usersService, 'requestVerification')
Expand Down Expand Up @@ -652,7 +651,6 @@ describe('UsersAdminController', () => {
it('should throw error when updating non-existent user', async () => {
const statusDto = { status: UserStatus.ACTIVE };
const mockReq = { user: createUserMock() };

// Mocking the service to throw an error when the user is not found
jest
.spyOn(usersService, 'updateStatus')
Expand Down Expand Up @@ -785,4 +783,290 @@ describe('UsersAdminController', () => {
});
});
});

describe('Deactivate User Account', () => {
const userId = new Types.ObjectId().toString();
const deactivateDto = { reason: 'Taking a break' };

it('should deactivate a user account', async () => {
const mockReq = { user: createUserMock() };
const expectedResult = createUserMock({ status: UserStatus.DEACTIVATED });

jest
.spyOn(usersService, 'deactivateAccount')
.mockResolvedValue(expectedResult);

const result = await controller.deactivateAccount(
mockReq,
userId,
deactivateDto,
);

expect(result).toEqual(expectedResult);
expect(usersService.deactivateAccount).toHaveBeenCalledWith(userId);
});

it('should throw NotFoundException when user not found', async () => {
const mockReq = { user: createUserMock() };

jest
.spyOn(usersService, 'deactivateAccount')
.mockRejectedValue(
new NotFoundException(`User with ID ${userId} not found`),
);

await expect(
controller.deactivateAccount(mockReq, userId, deactivateDto),
).rejects.toThrow(NotFoundException);
});

it('should throw BadRequestException when user cannot be deactivated', async () => {
const mockReq = { user: createUserMock() };

jest
.spyOn(usersService, 'deactivateAccount')
.mockRejectedValue(
new BadRequestException(
'User account cannot be deactivated at this time',
),
);

await expect(
controller.deactivateAccount(mockReq, userId, deactivateDto),
).rejects.toThrow(BadRequestException);
});
});

describe('Request Reactivation', () => {
const userId = new Types.ObjectId().toString();
const reactivationDto = { message: 'Ready to come back' };

it('should request reactivation of a user account', async () => {
const expectedResult = createUserMock({ status: UserStatus.ACTIVE });

jest
.spyOn(usersService, 'requestReactivation')
.mockResolvedValue(expectedResult);

const result = await controller.requestReactivation(
userId,
reactivationDto,
);

expect(result).toEqual(expectedResult);
expect(usersService.requestReactivation).toHaveBeenCalledWith(userId);
});

it('should throw NotFoundException when user not found', async () => {
jest
.spyOn(usersService, 'requestReactivation')
.mockRejectedValue(
new NotFoundException(`User with ID ${userId} not found`),
);

await expect(
controller.requestReactivation(userId, reactivationDto),
).rejects.toThrow(NotFoundException);
});

it('should throw BadRequestException when user cannot be reactivated', async () => {
jest
.spyOn(usersService, 'requestReactivation')
.mockRejectedValue(
new BadRequestException(
'User account cannot be reactivated at this time',
),
);

await expect(
controller.requestReactivation(userId, reactivationDto),
).rejects.toThrow(BadRequestException);
});
});

describe('Lead Registration', () => {
const tempLeadDto = {
email: '[email protected]',
leadPosition: 'Senior Developer',
firstName: 'John',
lastName: 'Doe',
createdAt: new Date(),
};

it('should successfully register a temporary lead', async () => {
const expectedResult = 'Application sent';

jest
.spyOn(usersService, 'createTempRegistration')
.mockResolvedValue(expectedResult);

const result = await controller.createLead(tempLeadDto);

expect(result).toEqual(expectedResult);
expect(usersService.createTempRegistration).toHaveBeenCalledWith(
tempLeadDto.email,
tempLeadDto.leadPosition,
);
});

it('should throw BadRequestException when lead registration is not allowed yet', async () => {
const errorMessage =
'The next time you can apply as a lead is Monday, January 1st, 10:00 am';

jest
.spyOn(usersService, 'createTempRegistration')
.mockRejectedValue(new BadRequestException(errorMessage));

await expect(controller.createLead(tempLeadDto)).rejects.toThrow(
new BadRequestException(errorMessage),
);
});

it('should handle errors during lead registration', async () => {
jest
.spyOn(usersService, 'createTempRegistration')
.mockRejectedValue(new Error('Error updating user'));

await expect(controller.createLead(tempLeadDto)).rejects.toThrow(Error);
});
});

describe('Register New User Form', () => {
const encryptedData = 'encryptedString';
const userId = new Types.ObjectId().toString();
const email = '[email protected]';

beforeEach(() => {
jest.clearAllMocks();
});

it('should redirect to new user form when userId is missing', async () => {
jest.spyOn(usersService, 'paraseEncryptedParams').mockReturnValue({
userId: '',
email,
});

const result = await controller.register(encryptedData);

expect(result).toEqual({
url: `/leads/new-user-form?email=${encodeURIComponent(email)}`,
});
});

it('should redirect to create lead page when user exists', async () => {
const mockUser = createUserMock({
_id: new Types.ObjectId(userId),
email,
});

jest.spyOn(usersService, 'paraseEncryptedParams').mockReturnValue({
userId,
email,
});

jest.spyOn(usersService, 'findById').mockResolvedValue(mockUser);

const result = await controller.register(encryptedData);

expect(result).toEqual({
url: `/leads/createLead?email=${email}`,
});
expect(usersService.findById).toHaveBeenCalledWith(userId);
});

it('should throw NotFoundException when user does not exist', async () => {
jest.spyOn(usersService, 'paraseEncryptedParams').mockReturnValue({
userId,
email,
});

jest
.spyOn(usersService, 'findById')
.mockRejectedValue(
new NotFoundException(`User with ID ${userId} not found`),
);

await expect(controller.register(encryptedData)).rejects.toThrow(
new NotFoundException('Invalid link'),
);
});

it('should throw NotFoundException when decryption fails', async () => {
jest
.spyOn(usersService, 'paraseEncryptedParams')
.mockImplementation(() => {
throw new Error('Decryption failed');
});

await expect(controller.register(encryptedData)).rejects.toThrow(
new NotFoundException('Invalid link'),
);
});
});

describe('User Invite Link', () => {
const encryptedData = 'encryptedInviteString';
const userId = new Types.ObjectId().toString();
const email = '[email protected]';

beforeEach(() => {
jest.clearAllMocks();
});

it('should process an invite link and redirect to the appropriate page', async () => {
const mockUser = createUserMock({
_id: new Types.ObjectId(userId),
email,
});

jest.spyOn(usersService, 'paraseEncryptedParams').mockReturnValue({
userId,
email,
});

jest.spyOn(usersService, 'findById').mockResolvedValue(mockUser);

const result = await controller.register(encryptedData);

expect(result).toEqual({
url: `/leads/createLead?email=${email}`,
});
expect(usersService.paraseEncryptedParams).toHaveBeenCalledWith(
encryptedData,
);
});

it('should handle invite link with missing parameters correctly', async () => {
jest.spyOn(usersService, 'paraseEncryptedParams').mockReturnValue({
userId: '',
email,
});

const result = await controller.register(encryptedData);

expect(result).toEqual({
url: `/leads/new-user-form?email=${encodeURIComponent(email)}`,
});
});

it('should test the parameter parsing function', () => {
// Since this is a specific function used by the endpoint, we should test it
// This would typically be in a separate test for the service, but including here for completeness

// Mock the decrypt function since it's an external dependency
const decryptMock = jest
.fn()
.mockReturnValue('userId=123&[email protected]');
global.decrypt = decryptMock;

const encryptedParams = 'encoded%20string';

// We would need to actually inject the real service to test this
// const result = usersService.paraseEncryptedParams(encryptedParams);

Copy link
Member

Choose a reason for hiding this comment

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

@oyedeletemitope why do we have alot of comment codes? can we remove them if not used.

// Instead, we can verify the mock was called correctly
// expect(result).toEqual({ userId: '123', email: '[email protected]' });
// expect(decryptMock).toHaveBeenCalledWith('encoded string');
});
});
});
7 changes: 2 additions & 5 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,6 @@ export class UsersService {
async createUser(userData: CreateUserDto) {
return (this.userModel as any).signUp(userData);
}

async requestVerification(req: ApiReq, userId: string) {
const user = await this.userModel.findById(userId);
if (!user) {
Expand All @@ -490,12 +489,10 @@ export class UsersService {
throw new BadRequestException('You are already verified.');
}

// Update user state
user.applicationStatus = ApplicationStatus.PENDING;
user.nextVerificationRequestDate = new Date(Date.now() + 30 * 24 * 60 * 60 * 1000); // 30 days later (this was initially 3 months)
user.nextVerificationRequestDate = new Date(Date.now() + 30 * 24 * 60 * 60 * 1000); // 30 days later
await user.save();

// Send confirmation to user
await sendMail({
to: user.email,
from: EmailFromType.HELLO,
Expand All @@ -506,7 +503,7 @@ export class UsersService {
nextTryDate: format(user.nextVerificationRequestDate, 'PPPP'),
},
});

return {
message: 'Verification request sent.',
nextAllowedRequest: user.nextVerificationRequestDate,
Expand Down