-
Notifications
You must be signed in to change notification settings - Fork 13
/
report.spec.ts
205 lines (175 loc) · 6.17 KB
/
report.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { baseURL, sessionToken, source } from './constants/base';
import { reportBundle, reportScm } from '../src/report';
import * as http from '../src/http';
import * as needle from '../src/needle';
import { initReportReturn, getReportReturn } from './constants/sample';
const mockInitReport = jest.spyOn(http, 'initReport');
const mockGetReport = jest.spyOn(http, 'getReport');
const mockInitScmReport = jest.spyOn(http, 'initScmReport');
const mockGetScmReport = jest.spyOn(http, 'getScmReport');
describe('Functional test for report', () => {
const baseConfig = {
baseURL,
sessionToken,
source,
};
describe('reportBundle - File-based (bundle) report', () => {
beforeAll(() => {
mockInitReport.mockResolvedValue({ type: 'success', value: initReportReturn });
mockGetReport.mockResolvedValue({ type: 'success', value: getReportReturn });
});
afterAll(() => {
mockInitReport.mockRestore();
mockGetReport.mockRestore();
});
it('should complete report with correct parameters', async () => {
const reportConfig = {
enabled: true,
projectName: 'test-project',
targetName: 'test-target',
targetRef: 'test-ref',
remoteRepoUrl: 'https://github.com/owner/repo',
};
const result = await reportBundle({
bundleHash: 'dummy-bundle',
...baseConfig,
report: reportConfig,
});
expect(mockInitReport).toHaveBeenCalledWith(
expect.objectContaining({
report: reportConfig,
}),
);
expect(result).not.toBeNull();
expect(result.status).toBe('COMPLETE');
expect(result).toHaveProperty('uploadResult');
expect(result).toHaveProperty('analysisResult');
});
it('should fail report if no project name was given', async () => {
const reportConfig = {
enabled: true,
projectName: undefined,
};
await expect(
reportBundle({
bundleHash: 'dummy-bundle',
...baseConfig,
report: reportConfig,
}),
).rejects.toHaveProperty('message', '"project-name" must be provided for "report"');
});
it('should fail report if the given project name includes invalid characters', async () => {
const invalidProjectName = '*&^%$';
const reportConfig = {
enabled: true,
projectName: invalidProjectName,
};
await expect(
reportBundle({
bundleHash: 'dummy-bundle',
...baseConfig,
report: reportConfig,
}),
).rejects.toHaveProperty('message', `"project-name" must not contain spaces or special characters except [/-_]`);
});
it('getReport should return error with received error message rather than generic error message', async () => {
const statusCode = 400;
const expectedErrorMessage = 'Dummy received error message';
mockGetReport.mockRestore();
jest
.spyOn(needle, 'makeRequest')
.mockResolvedValueOnce({ success: false, errorCode: statusCode, error: new Error(expectedErrorMessage) });
const reportConfig = {
enabled: true,
projectName: 'test-project',
targetName: 'test-target',
targetRef: 'test-ref',
remoteRepoUrl: 'https://github.com/owner/repo',
};
await expect(
reportBundle({
bundleHash: 'dummy-bundle',
...baseConfig,
report: reportConfig,
}),
).rejects.toMatchObject({
apiName: 'getReport',
statusCode: statusCode,
statusText: expectedErrorMessage,
});
});
});
describe('reportScm - SCM-based (Git) report', () => {
beforeAll(() => {
mockInitScmReport.mockResolvedValue({ type: 'success', value: initReportReturn });
mockGetScmReport.mockResolvedValue({ type: 'success', value: getReportReturn });
});
afterAll(() => {
mockInitScmReport.mockRestore();
mockGetScmReport.mockRestore();
});
it('should complete report with correct parameters', async () => {
const reportConfig = {
projectId: '00000000-0000-0000-0000-000000000000',
commitId: '0000000',
};
const result = await reportScm({
...baseConfig,
...reportConfig,
});
expect(mockInitScmReport).toHaveBeenCalledWith(expect.objectContaining(reportConfig));
expect(result).not.toBeNull();
expect(result.status).toBe('COMPLETE');
expect(result).toHaveProperty('uploadResult');
expect(result).toHaveProperty('analysisResult');
});
it('should fail report if no project ID was given', async () => {
await expect(
reportScm({
...baseConfig,
}),
).rejects.toHaveProperty('message', '"project-id" must be provided for "report"');
});
it('should fail report if the given project ID is not a valid UUID', async () => {
await expect(
reportScm({
...baseConfig,
projectId: 'invalid-id',
}),
).rejects.toHaveProperty('message', `"project-id" must be a valid UUID`);
});
it('should fail report if no commit ID was given', async () => {
const reportConfig = {
projectId: '00000000-0000-0000-0000-000000000000',
};
await expect(
reportScm({
...baseConfig,
...reportConfig,
}),
).rejects.toHaveProperty('message', '"commit-id" must be provided for "report"');
});
it('getScmReport should return error with received error message rather than generic error message', async () => {
const statusCode = 400;
const expectedErrorMessage = 'Dummy received error message';
mockGetScmReport.mockRestore();
jest
.spyOn(needle, 'makeRequest')
.mockResolvedValueOnce({ success: false, errorCode: statusCode, error: new Error(expectedErrorMessage) });
const reportConfig = {
projectId: '00000000-0000-0000-0000-000000000000',
commitId: '0000000',
};
await expect(
reportScm({
...baseConfig,
...reportConfig,
}),
).rejects.toMatchObject({
apiName: 'getReport',
statusCode: statusCode,
statusText: expectedErrorMessage,
});
});
});
});