-
Notifications
You must be signed in to change notification settings - Fork 276
Expand file tree
/
Copy pathupdate-vendor.dto.spec.ts
More file actions
113 lines (100 loc) · 4.83 KB
/
update-vendor.dto.spec.ts
File metadata and controls
113 lines (100 loc) · 4.83 KB
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
import { plainToInstance } from 'class-transformer';
import { validate } from 'class-validator';
import { UpdateVendorDto } from './update-vendor.dto';
/**
* Mirrors the global ValidationPipe config from main.ts:
* whitelist: true, transform: true, enableImplicitConversion: true
*/
function toDto(plain: Record<string, unknown>): UpdateVendorDto {
return plainToInstance(UpdateVendorDto, plain, {
enableImplicitConversion: true,
});
}
describe('UpdateVendorDto', () => {
it('should accept a valid full update payload', async () => {
const dto = toDto({
name: 'Acronis',
description: 'Backup solutions provider',
category: 'software_as_a_service',
status: 'assessed',
website: 'https://www.acronis.com',
isSubProcessor: false,
assigneeId: 'mem_abc123',
});
const errors = await validate(dto, { whitelist: true, forbidNonWhitelisted: true });
expect(errors).toHaveLength(0);
});
it('should accept a minimal update (single field)', async () => {
const dto = toDto({ website: 'https://www.acronis.com' });
const errors = await validate(dto, { whitelist: true, forbidNonWhitelisted: true });
expect(errors).toHaveLength(0);
});
it('should accept an empty body (no fields to update)', async () => {
const dto = toDto({});
const errors = await validate(dto, { whitelist: true, forbidNonWhitelisted: true });
expect(errors).toHaveLength(0);
});
// ── The bug this DTO fix addresses ────────────────────────────────
it('should accept empty description (vendors from onboarding)', async () => {
const dto = toDto({
name: 'Acronis',
description: '',
category: 'software_as_a_service',
status: 'assessed',
website: 'https://www.acronis.com',
isSubProcessor: false,
});
const errors = await validate(dto, { whitelist: true, forbidNonWhitelisted: true });
expect(errors).toHaveLength(0);
});
it('should still reject empty name', async () => {
const dto = toDto({ name: '' });
const errors = await validate(dto, { whitelist: true, forbidNonWhitelisted: true });
expect(errors.length).toBeGreaterThan(0);
expect(errors[0].property).toBe('name');
});
// ── assigneeId: null (unassigned vendor) ──────────────────────────
it('should accept assigneeId: null', async () => {
const dto = toDto({ assigneeId: null });
const errors = await validate(dto, { whitelist: true, forbidNonWhitelisted: true });
expect(errors).toHaveLength(0);
});
// ── website handling ──────────────────────────────────────────────
it('should transform empty website to undefined (skip validation)', async () => {
const dto = toDto({ website: '' });
const errors = await validate(dto, { whitelist: true, forbidNonWhitelisted: true });
expect(errors).toHaveLength(0);
expect(dto.website).toBeUndefined();
});
it('should accept a valid website URL', async () => {
const dto = toDto({ website: 'https://www.cloudflare.com' });
const errors = await validate(dto, { whitelist: true, forbidNonWhitelisted: true });
expect(errors).toHaveLength(0);
});
it('should reject an invalid website URL', async () => {
const dto = toDto({ website: 'not-a-url' });
const errors = await validate(dto, { whitelist: true, forbidNonWhitelisted: true });
expect(errors.length).toBeGreaterThan(0);
expect(errors[0].property).toBe('website');
});
// ── enum validation ───────────────────────────────────────────────
it('should reject invalid category enum', async () => {
const dto = toDto({ category: 'invalid_category' });
const errors = await validate(dto, { whitelist: true, forbidNonWhitelisted: true });
expect(errors.length).toBeGreaterThan(0);
expect(errors[0].property).toBe('category');
});
it('should reject invalid status enum', async () => {
const dto = toDto({ status: 'invalid_status' });
const errors = await validate(dto, { whitelist: true, forbidNonWhitelisted: true });
expect(errors.length).toBeGreaterThan(0);
expect(errors[0].property).toBe('status');
});
// ── forbidNonWhitelisted ──────────────────────────────────────────
it('should reject unknown properties', async () => {
const dto = toDto({ name: 'Acronis', unknownField: 'value' });
const errors = await validate(dto, { whitelist: true, forbidNonWhitelisted: true });
expect(errors.length).toBeGreaterThan(0);
expect(errors.some((e) => e.property === 'unknownField')).toBe(true);
});
});