forked from Code-4-Community/scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 0
added create user endpoint and unit tests for users #49
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 all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
176da5c
added create user endpoint and unit tests for users
bhuvanh66 d25cb37
added 2nd user to user unit tests + installed jest
bhuvanh66 a3d8a52
refactored tests to use mock
bhuvanh66 298d8fa
user tests fixes
bhuvanh66 0223a85
dto minor fixes + removing role optional
bhuvanh66 56220d9
reverted dependencies
bhuvanh66 68e9ea3
Merge branch 'main' of https://github.com/Code-4-Community/ssf into B…
bhuvanh66 89e4753
changed test description and reverted yarn lock file
bhuvanh66 74b93b1
removed optional role in service and updated tests
bhuvanh66 a46b272
reverted yarn lock
bhuvanh66 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
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
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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { | ||
| IsEmail, | ||
| IsEnum, | ||
| IsNotEmpty, | ||
| IsString, | ||
| IsOptional, | ||
| IsPhoneNumber, | ||
| } from 'class-validator'; | ||
| import { Role } from '../types'; | ||
|
|
||
| export class userSchemaDto { | ||
bhuvanh66 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @IsEmail() | ||
| @IsNotEmpty() | ||
| email: string; | ||
|
|
||
| @IsString() | ||
| @IsNotEmpty() | ||
| firstName: string; | ||
|
|
||
| @IsString() | ||
| @IsNotEmpty() | ||
| lastName: string; | ||
|
|
||
| @IsString() | ||
| @IsNotEmpty() | ||
| @IsPhoneNumber('US', { | ||
| message: | ||
| 'phone must be a valid phone number (make sure all the digits are correct)', | ||
| }) | ||
| phone: string; | ||
|
|
||
| @IsEnum(Role) | ||
| role: Role; | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| import { BadRequestException } from '@nestjs/common'; | ||
| import { UsersController } from './users.controller'; | ||
| import { UsersService } from './users.service'; | ||
| import { User } from './user.entity'; | ||
| import { Role } from './types'; | ||
| import { userSchemaDto } from './dtos/userSchema.dto'; | ||
|
|
||
| import { Test, TestingModule } from '@nestjs/testing'; | ||
| import { mock } from 'jest-mock-extended'; | ||
|
|
||
| const mockUserService = mock<UsersService>(); | ||
|
|
||
| const mockUser1: User = { | ||
| id: 1, | ||
| email: '[email protected]', | ||
| firstName: 'John', | ||
| lastName: 'Doe', | ||
| phone: '1234567890', | ||
| role: Role.STANDARD_VOLUNTEER, | ||
| }; | ||
|
|
||
| const mockUser2: User = { | ||
| id: 2543210, | ||
| email: '[email protected]', | ||
| firstName: 'Bob', | ||
| lastName: 'Smith', | ||
| phone: '9876', | ||
| role: Role.LEAD_VOLUNTEER, | ||
| }; | ||
|
|
||
Juwang110 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| describe('UsersController', () => { | ||
| let controller: UsersController; | ||
|
|
||
| beforeEach(async () => { | ||
| mockUserService.findUsersByRoles.mockReset(); | ||
| mockUserService.findOne.mockReset(); | ||
| mockUserService.remove.mockReset(); | ||
| mockUserService.update.mockReset(); | ||
| mockUserService.create.mockReset(); | ||
|
|
||
| const module: TestingModule = await Test.createTestingModule({ | ||
| controllers: [UsersController], | ||
| providers: [ | ||
| { | ||
| provide: UsersService, | ||
| useValue: mockUserService, | ||
| }, | ||
| ], | ||
| }).compile(); | ||
|
|
||
| controller = module.get<UsersController>(UsersController); | ||
| }); | ||
|
|
||
| it('should be defined', () => { | ||
| expect(controller).toBeDefined(); | ||
| }); | ||
|
|
||
| describe('GET /volunteers', () => { | ||
| it('should return all volunteers', async () => { | ||
| const volunteers = [mockUser1, mockUser2]; | ||
| mockUserService.findUsersByRoles.mockResolvedValue(volunteers); | ||
|
|
||
| const result = await controller.getAllVolunteers(); | ||
|
|
||
| const hasAdmin = result.some((user) => user.role === Role.ADMIN); | ||
| expect(hasAdmin).toBe(false); | ||
|
|
||
| expect(result).toEqual(volunteers); | ||
| expect(mockUserService.findUsersByRoles).toHaveBeenCalledWith([ | ||
| Role.LEAD_VOLUNTEER, | ||
| Role.STANDARD_VOLUNTEER, | ||
| ]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('GET /:id', () => { | ||
| it('should return a user by id', async () => { | ||
| mockUserService.findOne.mockResolvedValue(mockUser1); | ||
|
|
||
| const result = await controller.getUser(1); | ||
|
|
||
| expect(result).toEqual(mockUser1); | ||
| expect(mockUserService.findOne).toHaveBeenCalledWith(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe('DELETE /:id', () => { | ||
| it('should remove a user by id', async () => { | ||
| mockUserService.remove.mockResolvedValue(mockUser1); | ||
|
|
||
| const result = await controller.removeUser(1); | ||
|
|
||
| expect(result).toEqual(mockUser1); | ||
| expect(mockUserService.remove).toHaveBeenCalledWith(1); | ||
| }); | ||
| }); | ||
|
|
||
| describe('PUT :id/role', () => { | ||
| it('should update user role with valid role', async () => { | ||
| const updatedUser = { ...mockUser1, role: Role.ADMIN }; | ||
| mockUserService.update.mockResolvedValue(updatedUser); | ||
|
|
||
| const result = await controller.updateRole(1, Role.ADMIN); | ||
|
|
||
| expect(result).toEqual(updatedUser); | ||
| expect(mockUserService.update).toHaveBeenCalledWith(1, { | ||
| role: Role.ADMIN, | ||
| }); | ||
| }); | ||
|
|
||
| it('should throw BadRequestException for invalid role', async () => { | ||
| await expect(controller.updateRole(1, 'invalid_role')).rejects.toThrow( | ||
| BadRequestException, | ||
| ); | ||
| expect(mockUserService.update).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('POST /api/users', () => { | ||
| it('should create a new user with all required fields', async () => { | ||
| const createUserSchema: userSchemaDto = { | ||
| email: '[email protected]', | ||
| firstName: 'Jane', | ||
| lastName: 'Smith', | ||
| phone: '9876543210', | ||
| role: Role.ADMIN, | ||
| }; | ||
|
|
||
| const createdUser = { ...createUserSchema, id: 2 }; | ||
| mockUserService.create.mockResolvedValue(createdUser); | ||
|
|
||
| const result = await controller.createUser(createUserSchema); | ||
|
|
||
| expect(result).toEqual(createdUser); | ||
| expect(mockUserService.create).toHaveBeenCalledWith( | ||
| createUserSchema.email, | ||
| createUserSchema.firstName, | ||
| createUserSchema.lastName, | ||
| createUserSchema.phone, | ||
| createUserSchema.role, | ||
| ); | ||
| }); | ||
|
|
||
| it('should handle service errors', async () => { | ||
| const createUserSchema: userSchemaDto = { | ||
| email: '[email protected]', | ||
| firstName: 'Jane', | ||
| lastName: 'Smith', | ||
| phone: '9876543210', | ||
| role: Role.STANDARD_VOLUNTEER, | ||
| }; | ||
|
|
||
| const error = new Error('Database error'); | ||
| mockUserService.create.mockRejectedValue(error); | ||
|
|
||
| await expect(controller.createUser(createUserSchema)).rejects.toThrow( | ||
| error, | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
Juwang110 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
Oops, something went wrong.
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.