-
-
Notifications
You must be signed in to change notification settings - Fork 520
/
a.params-options.spec.ts
168 lines (158 loc) · 5.01 KB
/
a.params-options.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import 'jest-extended';
import { Controller, INestApplication } from '@nestjs/common';
import { APP_FILTER } from '@nestjs/core';
import { Test } from '@nestjs/testing';
import { TypeOrmModule } from '@nestjs/typeorm';
import * as request from 'supertest';
import { Company } from '../../../integration/crud-typeorm/companies';
import { withCache } from '../../../integration/crud-typeorm/orm.config';
import { Project } from '../../../integration/crud-typeorm/projects';
import { User } from '../../../integration/crud-typeorm/users';
import { UserProfile } from '../../../integration/crud-typeorm/users-profiles';
import { HttpExceptionFilter } from '../../../integration/shared/https-exception.filter';
import { Crud } from '../../crud/src/decorators/crud.decorator';
import { UsersService } from './__fixture__/users.service';
// tslint:disable:max-classes-per-file
describe('#crud-typeorm', () => {
describe('#params options', () => {
let app: INestApplication;
let server: any;
@Crud({
model: { type: User },
params: {
companyId: {
field: 'companyId',
type: 'number',
},
id: {
field: 'id',
type: 'number',
primary: true,
},
},
routes: {
updateOneBase: {
allowParamsOverride: true,
returnShallow: true,
},
replaceOneBase: {
allowParamsOverride: true,
returnShallow: true,
},
},
})
@Controller('/companiesA/:companyId/users')
class UsersController1 {
constructor(public service: UsersService) {}
}
@Crud({
model: { type: User },
params: {
companyId: {
field: 'companyId',
type: 'number',
},
id: {
field: 'id',
type: 'number',
primary: true,
},
},
query: {
join: {
company: {
eager: true,
},
},
},
})
@Controller('/companiesB/:companyId/users')
class UsersController2 {
constructor(public service: UsersService) {}
}
beforeAll(async () => {
const fixture = await Test.createTestingModule({
imports: [
TypeOrmModule.forRoot({ ...withCache, logging: false }),
TypeOrmModule.forFeature([Company, Project, User, UserProfile]),
],
controllers: [UsersController1, UsersController2],
providers: [{ provide: APP_FILTER, useClass: HttpExceptionFilter }, UsersService],
}).compile();
app = fixture.createNestApplication();
await app.init();
server = app.getHttpServer();
});
afterAll(async () => {
await app.close();
});
describe('#updateOneBase', () => {
it('should override params', async () => {
const dto = { isActive: false, companyId: 2 };
const res = await request(server)
.patch('/companiesA/1/users/2')
.send(dto)
.expect(200);
expect(res.body.companyId).toBe(2);
});
it('should not override params', async () => {
const dto = { isActive: false, companyId: 2 };
const res = await request(server)
.patch('/companiesB/1/users/3')
.send(dto)
.expect(200);
expect(res.body.companyId).toBe(1);
});
it('should return full entity', async () => {
const dto = { isActive: false };
const res = await request(server)
.patch('/companiesB/2/users/2')
.send(dto)
.expect(200);
expect(res.body.company.id).toBe(2);
});
it('should return shallow entity', async () => {
const dto = { isActive: false };
const res = await request(server)
.patch('/companiesA/2/users/2')
.send(dto)
.expect(200);
expect(res.body.company).toBeUndefined();
});
});
describe('#replaceOneBase', () => {
it('should override params', async () => {
const dto = { isActive: false, companyId: 2, email: '4@email.com' };
const res = await request(server)
.put('/companiesA/1/users/4')
.send(dto)
.expect(200);
expect(res.body.companyId).toBe(2);
});
it('should not override params', async () => {
const dto = { isActive: false, companyId: 1 };
const res = await request(server)
.put('/companiesB/2/users/4')
.send(dto)
.expect(200);
expect(res.body.companyId).toBe(2);
});
it('should return full entity', async () => {
const dto = { isActive: false };
const res = await request(server)
.put('/companiesB/2/users/4')
.send(dto)
.expect(200);
expect(res.body.company.id).toBe(2);
});
it('should return shallow entity', async () => {
const dto = { isActive: false };
const res = await request(server)
.put('/companiesA/2/users/4')
.send(dto)
.expect(200);
expect(res.body.company).toBeUndefined();
});
});
});
});