Skip to content

Commit 1ddc48b

Browse files
committed
write tests for login and register
1 parent 5596314 commit 1ddc48b

File tree

4 files changed

+187
-9
lines changed

4 files changed

+187
-9
lines changed

src/users/dto/update-user.dto.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ export class UpdateUserDto {
66
@ApiModelProperty()
77
@IsOptional()
88
@IsString()
9-
readonly firstName: string;
9+
readonly firstName?: string;
1010

1111
@ApiModelProperty()
1212
@IsOptional()
1313
@IsString()
14-
readonly lastName: string;
14+
readonly lastName?: string;
1515

1616
@ApiModelProperty()
1717
@IsOptional()
1818
@IsEnum(Gender)
19-
readonly gender: Gender;
19+
readonly gender?: Gender;
2020

2121
@ApiModelProperty()
2222
@IsOptional()
2323
@IsISO8601()
24-
readonly birthday: string;
24+
readonly birthday?: string;
2525
}

src/users/dto/user.dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ApiModelProperty } from '@nestjs/swagger';
44

55
export class UserDto {
66
@ApiModelProperty()
7-
readonly id: string;
7+
id: string;
88

99
@ApiModelProperty()
1010
readonly email: string;

test/app.e2e-spec.ts

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1-
import { Test, TestingModule } from '@nestjs/testing';
2-
import * as request from 'supertest';
1+
import { Test } from '@nestjs/testing';
2+
import request from 'supertest';
33
import { AppModule } from './../src/app.module';
4-
import { INestApplication, ValidationPipe } from '@nestjs/common';
4+
import { INestApplication, ValidationPipe, HttpStatus } from '@nestjs/common';
55
import { Sequelize } from 'sequelize-typescript';
6-
import { ConfigService } from 'src/shared/config/config.service';
6+
import { ConfigService } from './../src/shared/config/config.service';
77
import { Post } from './../src/posts/post.entity';
88
import { User } from './../src/users/user.entity';
9+
import {
10+
createUserDto1,
11+
createUserDto2,
12+
userLoginResponseDto1,
13+
createUserDto3,
14+
createUserDto4,
15+
createUserDto5,
16+
userLoginRequestDto1,
17+
userLoginRequestDto2,
18+
userLoginRequestDto3,
19+
} from './test-data';
920

1021
describe('/', () => {
1122
let app: INestApplication;
1223
let sequelize: Sequelize;
24+
let userId: string;
25+
let token: string;
1326

1427
beforeAll(async () => {
1528
const module = await Test.createTestingModule({
@@ -36,6 +49,93 @@ describe('/', () => {
3649
await app.init();
3750
});
3851

52+
describe('/users', () => {
53+
describe('POST /register', () => {
54+
it('should return 400 if email is not valid', () => {
55+
return request(app.getHttpServer())
56+
.post('/users/register')
57+
.send(createUserDto3)
58+
.expect(HttpStatus.BAD_REQUEST);
59+
});
60+
61+
it('should return 400 if birthday is not ISO 8601 date string', () => {
62+
return request(app.getHttpServer())
63+
.post('/users/register')
64+
.send(createUserDto4)
65+
.expect(HttpStatus.BAD_REQUEST);
66+
});
67+
68+
it('should return 400 if gender is not a valid enum value', () => {
69+
return request(app.getHttpServer())
70+
.post('/users/register')
71+
.send(createUserDto5)
72+
.expect(HttpStatus.BAD_REQUEST);
73+
});
74+
75+
it('should return 400 if any of the required fields is missing', () => {
76+
return request(app.getHttpServer())
77+
.post('/users/register')
78+
.send(createUserDto2)
79+
.expect(HttpStatus.BAD_REQUEST);
80+
});
81+
82+
it('should return 201 if user is created', () => {
83+
return request(app.getHttpServer())
84+
.post('/users/register')
85+
.send(createUserDto1)
86+
.expect(HttpStatus.CREATED)
87+
.expect(res => {
88+
userId = res.body.id;
89+
userLoginResponseDto1.id = res.body.id;
90+
userLoginResponseDto1.token = res.body.token;
91+
expect(res.body).toEqual(userLoginResponseDto1);
92+
});
93+
});
94+
95+
it('should return 409 if user with given email already exists', () => {
96+
return request(app.getHttpServer())
97+
.post('/users/register')
98+
.send(createUserDto1)
99+
.expect(HttpStatus.CONFLICT);
100+
});
101+
});
102+
103+
describe('POST /login', () => {
104+
it('should return 200 and jwt token', () => {
105+
return request(app.getHttpServer())
106+
.post('/users/login')
107+
.send(userLoginRequestDto1)
108+
.expect(HttpStatus.OK)
109+
.expect(res => {
110+
token = res.body.token;
111+
userLoginResponseDto1.id = res.body.id;
112+
userLoginResponseDto1.token = token;
113+
expect(res.body).toEqual(userLoginResponseDto1);
114+
});
115+
});
116+
117+
it('should return 400 when user with given email not found', () => {
118+
return request(app.getHttpServer())
119+
.post('/users/login')
120+
.send(userLoginRequestDto2)
121+
.expect(HttpStatus.BAD_REQUEST);
122+
});
123+
124+
it('should return 400 when wrong password inserted', () => {
125+
return request(app.getHttpServer())
126+
.post('/users/login')
127+
.send(userLoginRequestDto3)
128+
.expect(HttpStatus.BAD_REQUEST);
129+
});
130+
it('should return 400 when no data sent', () => {
131+
return request(app.getHttpServer())
132+
.post('/users/login')
133+
.send({})
134+
.expect(HttpStatus.BAD_REQUEST);
135+
});
136+
});
137+
});
138+
39139
afterAll(async done => {
40140
await app.close();
41141
await sequelize.drop();

test/test-data.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { UserLoginRequestDto } from './../src/users/dto/user-login-request.dto';
2+
import { UpdateUserDto } from './../src/users/dto/update-user.dto';
3+
import { UserLoginResponseDto } from './../src/users/dto/user-login-response.dto';
4+
import { UserDto } from './../src/users/dto/user.dto';
5+
import { Gender } from './../src/shared/enum/gender';
6+
import { CreateUserDto } from './../src/users/dto/create-user.dto';
7+
8+
export const createUserDto1: CreateUserDto = {
9+
10+
password: 'password123',
11+
firstName: 'John',
12+
lastName: 'Smith',
13+
gender: Gender.male,
14+
birthday: '1986-07-17',
15+
};
16+
17+
export const createUserDto2 = {
18+
19+
password: 'password123',
20+
lastName: 'Smith',
21+
gender: Gender.male,
22+
birthday: '1986-07-17',
23+
};
24+
25+
export const createUserDto3 = {
26+
...createUserDto1,
27+
email: 'not-email',
28+
};
29+
30+
export const createUserDto4 = {
31+
...createUserDto1,
32+
birthday: 'not-valid-date',
33+
};
34+
35+
export const createUserDto5 = {
36+
...createUserDto1,
37+
gender: 'not-valid-gender',
38+
};
39+
40+
export const userLoginRequestDto1: UserLoginRequestDto = {
41+
email: createUserDto1.email,
42+
password: createUserDto1.password,
43+
};
44+
45+
export const userLoginRequestDto2: UserLoginRequestDto = {
46+
email: 'wrong-email',
47+
password: createUserDto1.password,
48+
};
49+
50+
export const userLoginRequestDto3: UserLoginRequestDto = {
51+
email: 'wrong-email',
52+
password: createUserDto1.password,
53+
};
54+
55+
export const userDto1: UserDto = {
56+
id: 'uuid/v4',
57+
58+
firstName: 'John',
59+
lastName: 'Smith',
60+
gender: Gender.male,
61+
birthday: '1986-07-17',
62+
};
63+
64+
export const userLoginResponseDto1: UserLoginResponseDto = {
65+
...userDto1,
66+
token: 'token',
67+
};
68+
69+
export const updateUserDto1: UpdateUserDto = {
70+
gender: Gender.female,
71+
birthday: '1996-07-17',
72+
};
73+
74+
export const userDto2: UserDto = {
75+
...userDto1,
76+
gender: Gender.female,
77+
birthday: '1996-07-17',
78+
};

0 commit comments

Comments
 (0)