■ ■ ■ ■ ■ ■
packages/public-api/src/router.test.ts
packages/public-api/src/website/router.test.ts
1 | | - | import { TRPC_ERROR_CODES_BY_KEY } from '@trpc/server/rpc'; |
2 | 1 | | import { |
3 | 2 | | internalApi, |
4 | 3 | | PackageMetadata, |
| skipped 7 lines |
12 | 11 | | useTransactionalTesting, |
13 | 12 | | } from '@gradejs-public/test-utils'; |
14 | 13 | | import { getRepository } from 'typeorm'; |
15 | | - | import { createApp } from './app'; |
| 14 | + | import { createApp } from '../app'; |
16 | 15 | | |
17 | 16 | | useDatabaseConnection(); |
18 | 17 | | useTransactionalTesting(); |
19 | 18 | | |
20 | 19 | | const api = createSupertestApi(createApp); |
21 | 20 | | |
22 | | - | describe('routes / heathCheck', () => { |
23 | | - | it('should return valid response', async () => { |
24 | | - | await api.get('/healthcheck').set('Origin', 'http://localhost:3000').send().expect(200); |
25 | | - | }); |
26 | | - | |
27 | | - | it('should return not found error', async () => { |
| 21 | + | describe('POST /website', () => { |
| 22 | + | it('should validate body', async () => { |
28 | 23 | | const response = await api |
29 | | - | .get('/any-invalid-route') |
30 | | - | .set('Origin', 'http://localhost:3000') |
31 | | - | .send() |
32 | | - | .expect(404); |
| 24 | + | .post('/webpage') |
| 25 | + | .set('Origin', 'http://test') |
| 26 | + | .send({ url: 'invalid url' }) |
| 27 | + | .expect(400); |
33 | 28 | | |
34 | 29 | | expect(response.body).toMatchObject({ |
35 | 30 | | error: { |
36 | | - | code: TRPC_ERROR_CODES_BY_KEY.NOT_FOUND, |
| 31 | + | code: 400, |
| 32 | + | message: 'Invalid url', |
| 33 | + | type: 'invalid_string', |
| 34 | + | param: 'url', |
37 | 35 | | }, |
38 | 36 | | }); |
39 | 37 | | }); |
40 | | - | }); |
41 | 38 | | |
42 | | - | describe('routes / website', () => { |
43 | 39 | | it('should initiate webpage parsing', async () => { |
44 | 40 | | const siteUrl = 'https://example.com/' + Math.random().toString(); |
45 | 41 | | |
| skipped 7 lines |
53 | 49 | | ); |
54 | 50 | | |
55 | 51 | | const response = await api |
56 | | - | .post('/requestParseWebsite') |
57 | | - | .set('Origin', 'http://localhost:3000') |
58 | | - | .send(JSON.stringify(siteUrl)) |
| 52 | + | .post('/webpage') |
| 53 | + | .set('Origin', 'http://test') |
| 54 | + | .send({ url: siteUrl }) |
59 | 55 | | .expect(200); |
60 | 56 | | const webpage = await getRepository(WebPage).findOne({ url: siteUrl }); |
61 | 57 | | |
62 | 58 | | expect(initiateUrlProcessingInternalMock).toHaveBeenCalledTimes(1); |
63 | 59 | | expect(initiateUrlProcessingInternalMock).toHaveBeenCalledWith(siteUrl); |
64 | 60 | | expect(response.body).toMatchObject({ |
65 | | - | result: { |
66 | | - | data: { |
67 | | - | id: expect.anything(), |
68 | | - | url: siteUrl, |
69 | | - | hostname: 'example.com', |
70 | | - | status: 'pending', |
71 | | - | }, |
| 61 | + | data: { |
| 62 | + | id: expect.anything(), |
| 63 | + | url: siteUrl, |
| 64 | + | hostname: 'example.com', |
| 65 | + | status: 'pending', |
72 | 66 | | }, |
73 | 67 | | }); |
74 | 68 | | |
| skipped 1 lines |
76 | 70 | | url: siteUrl, |
77 | 71 | | hostname: 'example.com', |
78 | 72 | | status: 'pending', |
| 73 | + | }); |
| 74 | + | }); |
| 75 | + | }); |
| 76 | + | |
| 77 | + | describe('GET /website/:hostname', () => { |
| 78 | + | it('should validate params', async () => { |
| 79 | + | const response = await api |
| 80 | + | .get('/website/any:invalid') |
| 81 | + | .set('Origin', 'http://test') |
| 82 | + | .send() |
| 83 | + | .expect(400); |
| 84 | + | |
| 85 | + | expect(response.body).toMatchObject({ |
| 86 | + | error: { |
| 87 | + | code: 400, |
| 88 | + | message: 'Invalid', |
| 89 | + | type: 'invalid_string', |
| 90 | + | param: 'hostname', |
| 91 | + | }, |
79 | 92 | | }); |
80 | 93 | | }); |
81 | 94 | | |
| skipped 40 lines |
122 | 135 | | }, |
123 | 136 | | }); |
124 | 137 | | |
125 | | - | const response = await api |
126 | | - | .post('/syncWebsite') |
127 | | - | .set('Origin', 'http://localhost:3000') |
128 | | - | .send(JSON.stringify(hostname)) |
129 | | - | .expect(200); |
| 138 | + | const response = await api.get(`/website/${hostname}`).set('Origin', 'http://test').expect(200); |
130 | 139 | | |
131 | 140 | | expect(fetchUrlPackagesMock).toHaveBeenCalledTimes(0); |
132 | 141 | | expect(response.body).toMatchObject({ |
133 | | - | result: { |
134 | | - | data: { |
135 | | - | webpages: webpageInsert.generatedMaps, |
136 | | - | packages: [ |
| 142 | + | data: { |
| 143 | + | webpages: webpageInsert.generatedMaps, |
| 144 | + | packages: [ |
| 145 | + | { |
| 146 | + | ...packageInsert.generatedMaps[0], |
| 147 | + | registryMetadata: packageMetadataInsert.generatedMaps[0], |
| 148 | + | }, |
| 149 | + | ], |
| 150 | + | vulnerabilities: { |
| 151 | + | react: [ |
137 | 152 | | { |
138 | | - | ...packageInsert.generatedMaps[0], |
139 | | - | registryMetadata: packageMetadataInsert.generatedMaps[0], |
| 153 | + | affectedPackageName: 'react', |
| 154 | + | affectedVersionRange: '>=17.0.0 <18.0.0', |
| 155 | + | osvId: 'GRJS-test-id', |
| 156 | + | detailsUrl: `https://github.com/advisories/GRJS-test-id`, |
| 157 | + | summary: 'Test summary', |
| 158 | + | severity: 'HIGH', |
140 | 159 | | }, |
141 | 160 | | ], |
142 | | - | vulnerabilities: { |
143 | | - | react: [ |
144 | | - | { |
145 | | - | affectedPackageName: 'react', |
146 | | - | affectedVersionRange: '>=17.0.0 <18.0.0', |
147 | | - | osvId: 'GRJS-test-id', |
148 | | - | detailsUrl: `https://github.com/advisories/GRJS-test-id`, |
149 | | - | summary: 'Test summary', |
150 | | - | severity: 'HIGH', |
151 | | - | }, |
152 | | - | ], |
153 | | - | }, |
154 | 161 | | }, |
155 | 162 | | }, |
156 | 163 | | }); |
| skipped 36 lines |
193 | 200 | | } as internalApi.Website) |
194 | 201 | | ); |
195 | 202 | | |
196 | | - | const response = await api |
197 | | - | .post('/syncWebsite') |
198 | | - | .set('Origin', 'http://localhost:3000') |
199 | | - | .send(JSON.stringify(hostname)) |
200 | | - | .expect(200); |
| 203 | + | const response = await api.get(`/website/${hostname}`).set('Origin', 'http://test').expect(200); |
201 | 204 | | expect(fetchUrlPackagesMock).toHaveBeenCalledTimes(1); |
202 | 205 | | expect(fetchUrlPackagesMock).toHaveBeenCalledWith(siteUrl); |
203 | 206 | | |
204 | 207 | | expect(response.body).toMatchObject({ |
205 | | - | result: { |
206 | | - | data: { |
207 | | - | webpages: [ |
208 | | - | { |
209 | | - | ...webpageInsert.generatedMaps.at(0), |
210 | | - | status: WebPage.Status.Processed, |
211 | | - | updatedAt: expect.anything(), |
212 | | - | }, |
213 | | - | ], |
214 | | - | packages: [ |
215 | | - | { |
216 | | - | latestUrl: siteUrl, |
217 | | - | hostname, |
218 | | - | packageName: 'react', |
219 | | - | possiblePackageVersions: ['17.0.2'], |
220 | | - | packageVersionRange: '17.0.2', |
221 | | - | packageMetadata: { |
222 | | - | approximateByteSize: 1337, |
223 | | - | }, |
| 208 | + | data: { |
| 209 | + | webpages: [ |
| 210 | + | { |
| 211 | + | ...webpageInsert.generatedMaps.at(0), |
| 212 | + | status: WebPage.Status.Processed, |
| 213 | + | updatedAt: expect.anything(), |
| 214 | + | }, |
| 215 | + | ], |
| 216 | + | packages: [ |
| 217 | + | { |
| 218 | + | latestUrl: siteUrl, |
| 219 | + | hostname, |
| 220 | + | packageName: 'react', |
| 221 | + | possiblePackageVersions: ['17.0.2'], |
| 222 | + | packageVersionRange: '17.0.2', |
| 223 | + | packageMetadata: { |
| 224 | + | approximateByteSize: 1337, |
224 | 225 | | }, |
225 | | - | { |
226 | | - | latestUrl: siteUrl, |
227 | | - | hostname, |
228 | | - | packageName: 'object-assign', |
229 | | - | possiblePackageVersions: ['4.1.0', '4.1.1'], |
230 | | - | packageVersionRange: '4.1.0 - 4.1.1', |
231 | | - | packageMetadata: { |
232 | | - | approximateByteSize: 42, |
233 | | - | }, |
| 226 | + | }, |
| 227 | + | { |
| 228 | + | latestUrl: siteUrl, |
| 229 | + | hostname, |
| 230 | + | packageName: 'object-assign', |
| 231 | + | possiblePackageVersions: ['4.1.0', '4.1.1'], |
| 232 | + | packageVersionRange: '4.1.0 - 4.1.1', |
| 233 | + | packageMetadata: { |
| 234 | + | approximateByteSize: 42, |
234 | 235 | | }, |
235 | | - | ], |
236 | | - | vulnerabilities: {}, |
237 | | - | }, |
| 236 | + | }, |
| 237 | + | ], |
| 238 | + | vulnerabilities: {}, |
238 | 239 | | }, |
239 | 240 | | }); |
240 | 241 | | }); |
| skipped 2 lines |