|
| 1 | +import path from 'node:path' |
| 2 | +import { fileURLToPath } from 'node:url' |
| 3 | + |
| 4 | +import { beforeAll, describe, expect, it } from 'vitest' |
| 5 | + |
| 6 | +import ENV from '../../src/constants/env.mts' |
| 7 | +import { getDefaultApiToken } from '../../src/utils/socket/sdk.mts' |
| 8 | +import { executeCliCommand } from '../helpers/cli-execution.mts' |
| 9 | + |
| 10 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 11 | +const CLI_BIN_PATH = path.resolve(__dirname, '../../bin/cli.js') |
| 12 | + |
| 13 | +describe('Critical CLI Commands E2E', () => { |
| 14 | + let hasAuth = false |
| 15 | + |
| 16 | + beforeAll(async () => { |
| 17 | + // Check if running E2E tests and if Socket API token is available. |
| 18 | + if (ENV.RUN_E2E_TESTS) { |
| 19 | + const apiToken = await getDefaultApiToken() |
| 20 | + hasAuth = !!apiToken |
| 21 | + if (!apiToken) { |
| 22 | + console.log() |
| 23 | + console.warn('E2E tests require Socket authentication.') |
| 24 | + console.log('Please run one of the following:') |
| 25 | + console.log(' 1. socket login (to authenticate with Socket)') |
| 26 | + console.log(' 2. Set SOCKET_SECURITY_API_KEY environment variable') |
| 27 | + console.log(' 3. Skip E2E tests by not setting RUN_E2E_TESTS\n') |
| 28 | + console.log( |
| 29 | + 'E2E tests will be skipped due to missing authentication.\n', |
| 30 | + ) |
| 31 | + } |
| 32 | + } |
| 33 | + }) |
| 34 | + |
| 35 | + describe('Basic commands (no auth required)', () => { |
| 36 | + it.skipIf(!ENV.RUN_E2E_TESTS)('should display version', async () => { |
| 37 | + const result = await executeCliCommand(['--version'], { |
| 38 | + binPath: CLI_BIN_PATH, |
| 39 | + }) |
| 40 | + |
| 41 | + expect(result.exitCode).toBe(0) |
| 42 | + expect(result.stdout).toMatch(/^\d+\.\d+\.\d+/) |
| 43 | + }) |
| 44 | + |
| 45 | + it.skipIf(!ENV.RUN_E2E_TESTS)('should display help', async () => { |
| 46 | + const result = await executeCliCommand(['--help'], { |
| 47 | + binPath: CLI_BIN_PATH, |
| 48 | + }) |
| 49 | + |
| 50 | + expect(result.exitCode).toBe(0) |
| 51 | + expect(result.stdout).toContain('socket') |
| 52 | + expect(result.stdout).toContain('Commands') |
| 53 | + }) |
| 54 | + |
| 55 | + it.skipIf(!ENV.RUN_E2E_TESTS)( |
| 56 | + 'should display scan command help', |
| 57 | + async () => { |
| 58 | + const result = await executeCliCommand(['scan', '--help'], { |
| 59 | + binPath: CLI_BIN_PATH, |
| 60 | + }) |
| 61 | + |
| 62 | + expect(result.exitCode).toBe(0) |
| 63 | + expect(result.stdout).toContain('scan') |
| 64 | + }, |
| 65 | + ) |
| 66 | + }) |
| 67 | + |
| 68 | + describe('Auth-required commands', () => { |
| 69 | + it.skipIf(!ENV.RUN_E2E_TESTS || !hasAuth)( |
| 70 | + 'should list config settings', |
| 71 | + async () => { |
| 72 | + const result = await executeCliCommand(['config', 'list'], { |
| 73 | + binPath: CLI_BIN_PATH, |
| 74 | + }) |
| 75 | + |
| 76 | + expect(result.exitCode).toBe(0) |
| 77 | + }, |
| 78 | + ) |
| 79 | + |
| 80 | + it.skipIf(!ENV.RUN_E2E_TESTS || !hasAuth)( |
| 81 | + 'should display whoami information', |
| 82 | + async () => { |
| 83 | + const result = await executeCliCommand(['whoami'], { |
| 84 | + binPath: CLI_BIN_PATH, |
| 85 | + }) |
| 86 | + |
| 87 | + expect(result.exitCode).toBe(0) |
| 88 | + }, |
| 89 | + ) |
| 90 | + }) |
| 91 | +}) |
0 commit comments