Skip to content
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

feat: coverage #86

Merged
merged 1 commit into from
Dec 4, 2023
Merged
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
2 changes: 0 additions & 2 deletions src/dish/dish.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Module } from '@nestjs/common';
import { KnexModule } from '../global/modules/knex.module';
import { DishController } from './dish.controller';
import { DishService } from './dish.service';

@Module({
imports: [KnexModule],
controllers: [DishController],
providers: [DishService],
})
Expand Down
2 changes: 0 additions & 2 deletions src/district/district.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Module } from '@nestjs/common';
import { KnexModule } from '../global/modules/knex.module';
import { DistrictController } from './district.controller';
import { DistrictService } from './district.service';

@Module({
imports: [KnexModule],
controllers: [DistrictController],
providers: [DistrictService],
})
Expand Down
6 changes: 0 additions & 6 deletions src/global/guards/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ export class AuthGuard implements CanActivate {
context: ExecutionContext,
): boolean | Promise<boolean> | Observable<boolean> {
const request = context.switchToHttp().getRequest();

if (request.headers.authorization?.split(' ')[0] !== 'Bearer') {
console.log({ message: 'invalid token' });
return false;
}

const token = request.headers.authorization?.split(' ')[1];

if (token) {
Expand Down
12 changes: 0 additions & 12 deletions src/global/guards/specs/auth.guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,6 @@ describe('AuthGuard', () => {
});

describe('validation', () => {
it('cannot process to next step if the header does not start with Bearer', async () => {
const context = createMock<ExecutionContext>();
context.switchToHttp().getRequest.mockReturnValue({
headers: {
authorization: '123',
},
});
console.log = jest.fn();
expect(authGuard.canActivate(context)).toBeFalsy();
expect(console.log).toBeCalledTimes(1);
});

it('cannot process to next step if the token is null', async () => {
const context = createMock<ExecutionContext>();
context.switchToHttp().getRequest.mockReturnValue({
Expand Down
2 changes: 0 additions & 2 deletions src/paymentMethod/paymentMethod.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Module } from '@nestjs/common';
import { KnexModule } from '../global/modules/knex.module';
import { PaymentMethodController } from './paymentMethod.controller';
import { PaymentMethodService } from './paymentMethod.service';

@Module({
imports: [KnexModule],
controllers: [PaymentMethodController],
providers: [PaymentMethodService],
})
Expand Down
2 changes: 0 additions & 2 deletions src/photo/photo.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Module } from '@nestjs/common';
import { KnexModule } from '../global/modules/knex.module';
import { PhotoController } from './photo.controller';
import { PhotoService } from './photo.service';

@Module({
imports: [KnexModule],
controllers: [PhotoController],
providers: [PhotoService],
})
Expand Down
2 changes: 0 additions & 2 deletions src/photoCategory/photoCategory.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Module } from '@nestjs/common';
import { KnexModule } from '../global/modules/knex.module';
import { PhotoCategoryController } from './photoCategory.controller';
import { PhotoCategoryService } from './photoCategory.service';

@Module({
imports: [KnexModule],
controllers: [PhotoCategoryController],
providers: [PhotoCategoryService],
})
Expand Down
2 changes: 0 additions & 2 deletions src/restaurant/restaurant.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Module } from '@nestjs/common';
import { KnexModule } from '../global/modules/knex.module';
import { RestaurantController } from './restaurant.controller';
import { RestaurantService } from './restaurant.service';

@Module({
imports: [KnexModule],
controllers: [RestaurantController],
providers: [RestaurantService],
})
Expand Down
2 changes: 0 additions & 2 deletions src/restaurantDish/restaurantDish.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Module } from '@nestjs/common';
import { KnexModule } from '../global/modules/knex.module';
import { RestaurantDishController } from './restaurantDish.controller';
import { RestaurantDishService } from './restaurantDish.service';

@Module({
imports: [KnexModule],
controllers: [RestaurantDishController],
providers: [RestaurantDishService],
})
Expand Down
2 changes: 0 additions & 2 deletions src/restaurantOwner/restaurantOwner.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Module } from '@nestjs/common';
import { KnexModule } from '../global/modules/knex.module';
import { RestaurantOwnerController } from './restaurantOwner.controller';
import { RestaurantOwnerService } from './restaurantOwner.service';

@Module({
imports: [KnexModule],
controllers: [RestaurantOwnerController],
providers: [RestaurantOwnerService],
})
Expand Down
2 changes: 0 additions & 2 deletions src/restaurantPaymentMethod/restaurantPaymentMethod.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Module } from '@nestjs/common';
import { KnexModule } from '../global/modules/knex.module';
import { RestaurantPaymentMethodController } from './restaurantPaymentMethod.controller';
import { RestaurantPaymentMethodService } from './restaurantPaymentMethod.service';

@Module({
imports: [KnexModule],
controllers: [RestaurantPaymentMethodController],
providers: [RestaurantPaymentMethodService],
})
Expand Down
2 changes: 0 additions & 2 deletions src/review/review.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Module } from '@nestjs/common';
import { KnexModule } from '../global/modules/knex.module';
import { ReviewController } from './review.controller';
import { ReviewService } from './review.service';

@Module({
imports: [KnexModule],
controllers: [ReviewController],
providers: [ReviewService],
})
Expand Down
2 changes: 0 additions & 2 deletions src/userRelated/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { Module } from '@nestjs/common';
import { KnexModule } from '../../global/modules/knex.module';
import { UserService } from '../user/user.service';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';

@Module({
imports: [KnexModule],
controllers: [AuthController],
providers: [AuthService, UserService],
})
Expand Down
6 changes: 2 additions & 4 deletions src/userRelated/auth/specs/auth.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ describe('AuthController', () => {
profile_picture_url: expectedUsers[0].profile_picture_url,
},
} as any as Request;

jest
.spyOn(userService, 'getUsers')
.mockImplementation(() => Promise.resolve(expectedUsers));
jest.spyOn(userService, 'getUsers').mockResolvedValue(expectedUsers);
jest.spyOn(userService, 'createUser').mockResolvedValue(expectedUsers);
});

describe('register', () => {
Expand Down
61 changes: 52 additions & 9 deletions src/userRelated/user/specs/user.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,20 @@ describe('UserController', () => {
});

beforeEach(() => {
req = {
user: {
user_id: expectedUsers[0].user_id,
username: expectedUsers[0].username,
email: expectedUsers[0].email,
role: expectedUsers[0].role,
profile_picture_url: expectedUsers[0].profile_picture_url,
},
} as any as Request;
jest.spyOn(userService, 'getUsers').mockResolvedValue(expectedUsers);
jest.spyOn(userService, 'getUserByID').mockResolvedValue(expectedUsers);
jest
.spyOn(userService, 'updateUserProfile')
.mockResolvedValue(expectedUsers);
jest.spyOn(userService, 'deleteUser').mockResolvedValue(expectedUsers);
});

Expand All @@ -52,15 +64,6 @@ describe('UserController', () => {

describe('updateUserProfile', () => {
it('should return that user after updating a user profile', async () => {
req = {
user: {
user_id: expectedUsers[0].user_id,
username: expectedUsers[0].username,
email: expectedUsers[0].email,
role: expectedUsers[0].role,
profile_picture_url: expectedUsers[0].profile_picture_url,
},
} as any as Request;
const expectedUsersHashPasswordSync = await expectedUsersHashPassword();
jest
.spyOn(userService, 'updateUserProfile')
Expand Down Expand Up @@ -92,6 +95,46 @@ describe('UserController', () => {
});
});

it('cannot update the user profile if the new username already exists or it is different from the original username', async () => {
jest
.spyOn(userService, 'getUsers')
.mockResolvedValue([{ ...expectedUsers[0], username: 'ttiimmothy' }]);
const result = await userController.updateUserProfile(
{ user_id: expectedUsers[0].user_id },
{
updateUserDto: { username: 'ttiimmothy' },
fileExtension: '',
},
req,
);

expect(result).toEqual({
message:
'The username cannot be updated because this username is already used',
});
});

it('cannot update the user profile if the new email already exists or it is different from the original email', async () => {
jest
.spyOn(userService, 'getUsers')
.mockResolvedValue([
{ ...expectedUsers[0], email: '[email protected]' },
]);
const result = await userController.updateUserProfile(
{ user_id: expectedUsers[0].user_id },
{
updateUserDto: { email: '[email protected]' },
fileExtension: '',
},
req,
);

expect(result).toEqual({
message:
'The email cannot be updated because this email is already used',
});
});

it('should return user cannot be found message if the user cannot be found', async () => {
jest.spyOn(userService, 'getUserByID').mockResolvedValue(null);
const result = await userController.updateUserProfile(
Expand Down
45 changes: 44 additions & 1 deletion src/userRelated/user/specs/user.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,60 @@ describe('UserService', () => {
},
]);
});

it('should return that user with profile picture url after creating a user', async () => {
const result = await userService.createUser(
{
username: expectedUsers[0].username,
email: expectedUsers[0].email,
password: expectedUsers[0].password,
role: expectedUsers[0].role,
},
'png',
);

userIDs.push({ user_id: result[0].user_id });

expect(result).toMatchObject([
{
username: expectedUsers[0].username,
email: expectedUsers[0].email,
password: expectedUsers[0].password,
role: expectedUsers[0].role,
profile_picture_url: `${process.env.IMAGE_PREFIX}/user/${result[0].user_id}/profile_picture_url.png`,
},
]);
});
});

describe('updateUserProfile', () => {
it('should return that user after updating a user profile', async () => {
it('should return that user with profile picture url after updating a user profile', async () => {
const result = await userService.updateUserProfile(
userIDs[0].user_id,
{
username: 'ttiimmothy',
},
'png',
);
expect(result).toMatchObject([
{
username: 'ttiimmothy',
email: expectedUsers[0].email,
password: expectedUsers[0].password,
role: expectedUsers[0].role,
profile_picture_url: `${process.env.IMAGE_PREFIX}/user/${result[0].user_id}/profile_picture_url.png`,
},
]);
});

it('should return that user after updating a user profile', async () => {
const result = await userService.updateUserProfile(
userIDs[0].user_id,
{
username: 'ttiimmothy',
},
'',
);
expect(result).toMatchObject([
{
username: 'ttiimmothy',
Expand Down
2 changes: 0 additions & 2 deletions src/userRelated/user/user.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { Module } from '@nestjs/common';
import { KnexModule } from '../../global/modules/knex.module';
import { AuthService } from '../auth/auth.service';
import { UserController } from './user.controller';
import { UserService } from './user.service';

@Module({
imports: [KnexModule],
controllers: [UserController],
providers: [AuthService, UserService],
})
Expand Down