-
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.
Merge pull request #2 from nhandt2021/nhandt/tutorial
Add e2e testing user APIs
- Loading branch information
Showing
12 changed files
with
212 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
DATABASE_URL="postgresql://postgres:123@localhost:5435/nest?schema=public" | ||
JWT_SECRET='super-secret' |
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,4 @@ | ||
import { Controller } from '@nestjs/common'; | ||
|
||
@Controller('bookmark') | ||
export class BookmarkController {} |
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 |
---|---|---|
@@ -1,4 +1,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { BookmarkController } from './bookmark.controller'; | ||
import { BookmarkService } from './bookmark.service'; | ||
|
||
@Module({}) | ||
@Module({ | ||
controllers: [BookmarkController], | ||
providers: [BookmarkService] | ||
}) | ||
export class BookmarkModule {} |
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,4 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class BookmarkService {} |
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,15 @@ | ||
import { IsEmail, IsOptional, IsString } from 'class-validator'; | ||
|
||
export class EditUserDto { | ||
@IsEmail() | ||
@IsOptional() | ||
email?: string; | ||
|
||
@IsString() | ||
@IsOptional() | ||
firstName?: string; | ||
|
||
@IsString() | ||
@IsOptional() | ||
lastName?: string; | ||
} |
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 @@ | ||
export * from './edit-user.dto'; |
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 |
---|---|---|
@@ -1,17 +1,22 @@ | ||
import { Controller, Get, Patch, UseGuards } from '@nestjs/common'; | ||
import { Body, Controller, Get, Patch, UseGuards } from '@nestjs/common'; | ||
import { User } from '@prisma/client'; | ||
import { JwtGuard } from '../auth/guard'; | ||
import { GetUser } from '../auth/decorator'; | ||
import { EditUserDto } from './dto'; | ||
import { UserService } from './user.service'; | ||
|
||
@UseGuards(JwtGuard) | ||
@Controller('users') | ||
export class UserController { | ||
constructor(private userService: UserService) {} | ||
|
||
@Get('me') | ||
getMe(@GetUser() user: User) { | ||
console.log('req gi day:', user); | ||
return user; | ||
} | ||
|
||
@Patch() | ||
editUser() {} | ||
editUser(@GetUser('id') userId: number, @Body() dto: EditUserDto) { | ||
return this.userService.editUser(userId, dto); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { UserController } from './user.controller'; | ||
import { UserService } from './user.service'; | ||
|
||
@Module({ | ||
controllers: [UserController], | ||
providers: [UserService], | ||
}) | ||
export class UserModule {} |
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,23 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { PrismaService } from '../prisma/prisma.service'; | ||
import { EditUserDto } from './dto'; | ||
|
||
@Injectable() | ||
export class UserService { | ||
constructor(private prisma: PrismaService) {} | ||
|
||
async editUser(userId: number, dto: EditUserDto) { | ||
const user = await this.prisma.user.update({ | ||
where: { | ||
id: userId, | ||
}, | ||
data: { | ||
...dto, | ||
}, | ||
}); | ||
|
||
delete user.hash; | ||
|
||
return user; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,9 +1,15 @@ | ||
import { INestApplication, ValidationPipe } from '@nestjs/common'; | ||
import { Test } from '@nestjs/testing'; | ||
import * as pactum from 'pactum'; | ||
import { AppModule } from '../src/app.module'; | ||
import { INestApplication, ValidationPipe } from '@nestjs/common'; | ||
import { PrismaService } from '../src/prisma/prisma.service'; | ||
import { AuthDto } from '../src/auth/dto'; | ||
import { EditUserDto } from '../src/user/dto'; | ||
|
||
describe('App (e2e)', () => { | ||
let app: INestApplication; | ||
let prisma: PrismaService; | ||
const HOST = 'http://localhost:3333'; | ||
|
||
beforeAll(async () => { | ||
const moduleRef = await Test.createTestingModule({ | ||
|
@@ -17,11 +23,139 @@ describe('App (e2e)', () => { | |
}), | ||
); | ||
await app.init(); | ||
await app.listen(3333); | ||
|
||
prisma = app.get(PrismaService); | ||
|
||
// Clean db before testing | ||
await prisma.cleanDb(); | ||
|
||
pactum.request.setBaseUrl(HOST); | ||
}); | ||
|
||
afterAll(() => { | ||
app.close(); | ||
}); | ||
|
||
it.todo('should pass'); | ||
describe('Auth', () => { | ||
const dto: AuthDto = { | ||
email: '[email protected]', | ||
password: '123', | ||
}; | ||
|
||
describe('Signup', () => { | ||
it('should throw if email is empty', () => { | ||
return pactum | ||
.spec() | ||
.post('/auth/signup') | ||
.withBody({ | ||
password: dto.password, | ||
}) | ||
.expectStatus(400); | ||
}); | ||
|
||
it('should throw if password is empty', () => { | ||
return pactum | ||
.spec() | ||
.post('/auth/signup') | ||
.withBody({ | ||
password: dto.password, | ||
}) | ||
.expectStatus(400); | ||
}); | ||
|
||
it('should throw if no body provided', () => { | ||
return pactum.spec().post('/auth/signup').expectStatus(400); | ||
}); | ||
|
||
it('should signup', () => { | ||
return pactum | ||
.spec() | ||
.post('/auth/signup') | ||
.withBody(dto) | ||
.expectStatus(201); | ||
}); | ||
}); | ||
|
||
describe('Signin', () => { | ||
it('should throw if email is empty', () => { | ||
return pactum | ||
.spec() | ||
.post('/auth/signin') | ||
.withBody({ | ||
password: dto.password, | ||
}) | ||
.expectStatus(400); | ||
}); | ||
|
||
it('should throw if password is empty', () => { | ||
return pactum | ||
.spec() | ||
.post('/auth/signin') | ||
.withBody({ | ||
password: dto.password, | ||
}) | ||
.expectStatus(400); | ||
}); | ||
|
||
it('should throw if no body provided', () => { | ||
return pactum.spec().post('/auth/signin').expectStatus(400); | ||
}); | ||
|
||
it('should signin', () => { | ||
return pactum | ||
.spec() | ||
.post('/auth/signin') | ||
.withBody(dto) | ||
.expectStatus(200) | ||
.stores('userAt', 'access_token'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('User', () => { | ||
describe('Get me', () => { | ||
it('should get current user', () => { | ||
return pactum | ||
.spec() | ||
.get('/users/me') | ||
.withHeaders({ | ||
Authorization: 'Bearer $S{userAt}', | ||
}) | ||
.expectStatus(200); | ||
}); | ||
}); | ||
|
||
describe('Edit user', () => { | ||
const dto: EditUserDto = { | ||
firstName: 'Nhan', | ||
email: '[email protected]', | ||
}; | ||
|
||
it('should edit user', () => { | ||
return pactum | ||
.spec() | ||
.patch('/users') | ||
.withHeaders({ | ||
Authorization: 'Bearer $S{userAt}', | ||
}) | ||
.withBody(dto) | ||
.expectStatus(200) | ||
.expectBodyContains(dto.firstName) | ||
.expectBodyContains(dto.email); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Bookmark', () => { | ||
describe('Create bookmark', () => {}); | ||
|
||
describe('Get bookmark', () => {}); | ||
|
||
describe('Get bookmark by id', () => {}); | ||
|
||
describe('Edit bookmark by id', () => {}); | ||
|
||
describe('Delete bookmark by id', () => {}); | ||
}); | ||
}); |