|
| 1 | +/** |
| 2 | + * E2E tests for capiscio badge commands. |
| 3 | + * |
| 4 | + * Tests badge issuance and verification commands against the CLI. |
| 5 | + * These tests focus on the CLI interface itself, not the server. |
| 6 | + */ |
| 7 | + |
| 8 | +import { describe, it, expect } from 'vitest'; |
| 9 | +import { runCapiscio, extractToken } from './helpers'; |
| 10 | + |
| 11 | +describe('badge commands', () => { |
| 12 | + describe('badge issue', () => { |
| 13 | + it('should issue a self-signed badge', async () => { |
| 14 | + const result = await runCapiscio([ |
| 15 | + 'badge', 'issue', '--self-sign', '--domain', 'test.example.com' |
| 16 | + ]); |
| 17 | + |
| 18 | + // Self-signed badge issuance should succeed |
| 19 | + expect(result.exitCode).toBe(0); |
| 20 | + |
| 21 | + // Output should contain a JWT token (has dots for header.payload.signature) |
| 22 | + // Get last line in case there are download messages |
| 23 | + const token = extractToken(result.stdout); |
| 24 | + expect(token.split('.').length).toBe(3); // JWT format |
| 25 | + }, 15000); |
| 26 | + |
| 27 | + it('should issue badge with custom expiration', async () => { |
| 28 | + const result = await runCapiscio([ |
| 29 | + 'badge', 'issue', '--self-sign', '--exp', '10m' |
| 30 | + ]); |
| 31 | + |
| 32 | + expect(result.exitCode).toBe(0); |
| 33 | + const token = extractToken(result.stdout); |
| 34 | + expect(token.split('.').length).toBe(3); |
| 35 | + }, 15000); |
| 36 | + |
| 37 | + it('should issue badge with audience restriction', async () => { |
| 38 | + const result = await runCapiscio([ |
| 39 | + 'badge', 'issue', '--self-sign', '--aud', 'https://api.example.com' |
| 40 | + ]); |
| 41 | + |
| 42 | + expect(result.exitCode).toBe(0); |
| 43 | + const token = extractToken(result.stdout); |
| 44 | + expect(token.split('.').length).toBe(3); |
| 45 | + }, 15000); |
| 46 | + |
| 47 | + it('should display help for badge issue', async () => { |
| 48 | + const result = await runCapiscio(['badge', 'issue', '--help']); |
| 49 | + |
| 50 | + expect(result.exitCode).toBe(0); |
| 51 | + const helpText = result.stdout.toLowerCase(); |
| 52 | + expect(helpText.includes('issue')).toBe(true); |
| 53 | + expect(helpText.includes('self-sign') || helpText.includes('level')).toBe(true); |
| 54 | + }, 15000); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('badge verify', () => { |
| 58 | + it('should verify a self-signed badge', async () => { |
| 59 | + // First issue a badge |
| 60 | + const issueResult = await runCapiscio([ |
| 61 | + 'badge', 'issue', '--self-sign', '--domain', 'test.example.com' |
| 62 | + ]); |
| 63 | + expect(issueResult.exitCode).toBe(0); |
| 64 | + const token = extractToken(issueResult.stdout); |
| 65 | + |
| 66 | + // Then verify it with --accept-self-signed |
| 67 | + const verifyResult = await runCapiscio([ |
| 68 | + 'badge', 'verify', token, '--accept-self-signed', '--offline' |
| 69 | + ]); |
| 70 | + |
| 71 | + expect(verifyResult.exitCode).toBe(0); |
| 72 | + const output = verifyResult.stdout.toLowerCase(); |
| 73 | + expect( |
| 74 | + output.includes('valid') || output.includes('verified') || output.includes('ok') |
| 75 | + ).toBe(true); |
| 76 | + }, 15000); |
| 77 | + |
| 78 | + it('should fail for invalid token', async () => { |
| 79 | + const invalidToken = 'invalid.jwt.token'; |
| 80 | + const result = await runCapiscio(['badge', 'verify', invalidToken, '--accept-self-signed']); |
| 81 | + |
| 82 | + expect(result.exitCode).not.toBe(0); |
| 83 | + const errorOutput = (result.stderr + result.stdout).toLowerCase(); |
| 84 | + expect( |
| 85 | + errorOutput.includes('invalid') || |
| 86 | + errorOutput.includes('verify') || |
| 87 | + errorOutput.includes('failed') || |
| 88 | + errorOutput.includes('malformed') || |
| 89 | + errorOutput.includes('error') |
| 90 | + ).toBe(true); |
| 91 | + }, 15000); |
| 92 | + |
| 93 | + it('should display help for badge verify', async () => { |
| 94 | + const result = await runCapiscio(['badge', 'verify', '--help']); |
| 95 | + |
| 96 | + expect(result.exitCode).toBe(0); |
| 97 | + const helpText = result.stdout.toLowerCase(); |
| 98 | + expect(helpText.includes('verify') || helpText.includes('token')).toBe(true); |
| 99 | + }, 15000); |
| 100 | + }); |
| 101 | + |
| 102 | + describe('badge help', () => { |
| 103 | + it('should display help for badge command', async () => { |
| 104 | + const result = await runCapiscio(['badge', '--help']); |
| 105 | + |
| 106 | + expect(result.exitCode).toBe(0); |
| 107 | + const helpText = result.stdout.toLowerCase(); |
| 108 | + expect(helpText.includes('badge')).toBe(true); |
| 109 | + expect( |
| 110 | + helpText.includes('issue') || helpText.includes('verify') || helpText.includes('usage') |
| 111 | + ).toBe(true); |
| 112 | + }, 15000); |
| 113 | + }); |
| 114 | +}); |
0 commit comments