-
Notifications
You must be signed in to change notification settings - Fork 0
added unit test for the first part of the user module #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
425b8ff
added unit test for the first part of the user module
oyedeletemitope f0586ee
added the comments back
oyedeletemitope 6f5334a
removed unwanted comments
oyedeletemitope 162649f
CODE:Made chnages to how i tested the endpoints
oyedeletemitope e23f296
Merge branch 'main' into user-module-test1
oyedeletemitope File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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') | ||
|
|
@@ -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') | ||
|
|
@@ -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); | ||
|
|
||
|
||
| // Instead, we can verify the mock was called correctly | ||
| // expect(result).toEqual({ userId: '123', email: '[email protected]' }); | ||
| // expect(decryptMock).toHaveBeenCalledWith('encoded string'); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.