-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add admin guard, implemented ice cream creation with tests (#8)
Co-authored-by: Dominik Maćkiewicz <[email protected]>
- Loading branch information
Showing
12 changed files
with
118 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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 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,26 @@ | ||
import { | ||
Injectable, | ||
CanActivate, | ||
ExecutionContext, | ||
HttpException, | ||
HttpStatus, | ||
} from '@nestjs/common'; | ||
import { Role } from 'src/users/entities/user.entity'; | ||
import { UsersService } from 'src/users/users.service'; | ||
|
||
@Injectable() | ||
export class AdminGuard implements CanActivate { | ||
constructor(private readonly usersService: UsersService) {} | ||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const request = context.switchToHttp().getRequest(); | ||
|
||
const userId = request.user.id; | ||
const user = await this.usersService.getOneById(userId); | ||
|
||
if (user.role === Role.ADMIN) { | ||
return true; | ||
} else { | ||
throw new HttpException('Forbidden', HttpStatus.FORBIDDEN); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -10,6 +10,7 @@ import { | |
UserDocument, | ||
User, | ||
AccountType, | ||
Role, | ||
} from 'src/users/entities/user.entity'; | ||
import { | ||
IceCream, | ||
|
@@ -61,7 +62,11 @@ describe('reviews', () => { | |
await iceCreamModel.deleteMany(); | ||
}); | ||
|
||
const createAndLoginUser = async (email: string, password: string) => { | ||
const createAndLoginUser = async ( | ||
email: string, | ||
password: string, | ||
role: Role = Role.USER, | ||
) => { | ||
await usersService.createUnverified({ | ||
email, | ||
password, | ||
|
@@ -82,6 +87,22 @@ describe('reviews', () => { | |
return response.body.token; | ||
}; | ||
|
||
const createIceCreamDto: CreateIceCreamDto = { | ||
brand_pl: 'brand_pl', | ||
name_pl: 'name_pl', | ||
description_pl: 'description_pl', | ||
brand_en: 'brand_en', | ||
name_en: 'name_en', | ||
description_en: 'description_en', | ||
rating: 5, | ||
numberOfRatings: 0, | ||
image: 'https://image', | ||
vegan: false, | ||
type: IceCreamType.BAR, | ||
tags: [], | ||
barcode: '123', | ||
}; | ||
|
||
const createIceCream = async ({ | ||
brand_pl = 'brand_pl', | ||
name_pl = 'name_pl', | ||
|
@@ -139,10 +160,6 @@ describe('reviews', () => { | |
expect(response.body.iceCreams.length).toEqual(1); | ||
}); | ||
|
||
afterAll(async () => { | ||
await app.close(); | ||
}); | ||
|
||
it('should query only searchField matching ice cream', async () => { | ||
// given | ||
await createIceCream({ name_en: 'Ben Jerry' }); | ||
|
@@ -184,6 +201,35 @@ describe('reviews', () => { | |
expect(response.body.iceCreams[0].name_en).toEqual('most popular'); | ||
}); | ||
|
||
it('should add new ice cream by admin', async () => { | ||
// given | ||
const token = await createAndLoginUser('[email protected]', 'test'); | ||
const user = await usersService.getOneByEmail('[email protected]'); | ||
user.role = Role.ADMIN; | ||
user.save(); | ||
|
||
// when | ||
const response = await request(app.getHttpServer()) | ||
.post(`/ice-creams/add`) | ||
.set('Authorization', `Bearer ${token}`) | ||
.send(createIceCreamDto); | ||
// then | ||
expect(response.status).toBe(201); | ||
}); | ||
|
||
it('should block add new ice cream by plain user', async () => { | ||
// given | ||
const token = await createAndLoginUser('[email protected]', 'test'); | ||
|
||
// when | ||
const response = await request(app.getHttpServer()) | ||
.post(`/ice-creams/add`) | ||
.set('Authorization', `Bearer ${token}`) | ||
.send(createIceCreamDto); | ||
// then | ||
expect(response.status).toBe(403); | ||
}); | ||
|
||
afterAll(async () => { | ||
await app.close(); | ||
}); | ||
|
This file contains 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 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