|
| 1 | +import { fakeServer } from '../../acceptance/fake-server'; |
| 2 | +import { runSnykCLI } from '../util/runSnykCLI'; |
| 3 | + |
| 4 | +describe('analytics module', () => { |
| 5 | + let server; |
| 6 | + let env: Record<string, string>; |
| 7 | + |
| 8 | + beforeAll((done) => { |
| 9 | + const port = process.env.PORT || process.env.SNYK_PORT || '12345'; |
| 10 | + const baseApi = '/api/v1'; |
| 11 | + env = { |
| 12 | + ...process.env, |
| 13 | + SNYK_API: 'http://localhost:' + port + baseApi, |
| 14 | + SNYK_HOST: 'http://localhost:' + port, |
| 15 | + SNYK_TOKEN: '123456789', |
| 16 | + }; |
| 17 | + |
| 18 | + server = fakeServer(baseApi, env.SNYK_TOKEN); |
| 19 | + server.listen(port, () => { |
| 20 | + done(); |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + afterAll((done) => { |
| 25 | + server.close(() => { |
| 26 | + done(); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + test('validate actual analytics call', async () => { |
| 31 | + server.setNextResponse({}); |
| 32 | + const { code } = await runSnykCLI(`version --org=fooOrg --all-projects`, { |
| 33 | + env, |
| 34 | + }); |
| 35 | + expect(code).toBe(0); |
| 36 | + |
| 37 | + const lastRequest = server.popRequest(); |
| 38 | + expect(lastRequest).toMatchObject({ |
| 39 | + headers: { |
| 40 | + host: 'localhost:12345', |
| 41 | + accept: 'application/json', |
| 42 | + authorization: 'token 123456789', |
| 43 | + 'content-type': 'application/json; charset=utf-8', |
| 44 | + 'x-snyk-cli-version': '1.0.0-monorepo', |
| 45 | + }, |
| 46 | + query: { |
| 47 | + org: 'fooOrg', |
| 48 | + }, |
| 49 | + body: { |
| 50 | + data: { |
| 51 | + args: [ |
| 52 | + { |
| 53 | + org: 'fooOrg', |
| 54 | + allProjects: true, |
| 55 | + }, |
| 56 | + ], |
| 57 | + ci: expect.any(Boolean), |
| 58 | + command: 'version', |
| 59 | + durationMs: expect.any(Number), |
| 60 | + environment: { |
| 61 | + npmVersion: expect.any(String), |
| 62 | + }, |
| 63 | + id: expect.any(String), |
| 64 | + integrationEnvironment: '', |
| 65 | + integrationEnvironmentVersion: '', |
| 66 | + integrationName: '', |
| 67 | + integrationVersion: '', |
| 68 | + // prettier-ignore |
| 69 | + metrics: { |
| 70 | + 'network_time': { |
| 71 | + type: 'timer', |
| 72 | + values: [], |
| 73 | + total: expect.any(Number), |
| 74 | + }, |
| 75 | + 'cpu_time': { |
| 76 | + type: 'synthetic', |
| 77 | + values: [expect.any(Number)], |
| 78 | + total: expect.any(Number), |
| 79 | + }, |
| 80 | + }, |
| 81 | + nodeVersion: process.version, |
| 82 | + org: 'fooOrg', |
| 83 | + os: expect.any(String), |
| 84 | + standalone: false, |
| 85 | + version: '1.0.0-monorepo', |
| 86 | + }, |
| 87 | + }, |
| 88 | + }); |
| 89 | + }); |
| 90 | +}); |
0 commit comments