|
| 1 | +import { mkdtempSync, rmSync, symlinkSync } from 'node:fs' |
| 2 | +import { tmpdir } from 'node:os' |
| 3 | +import path from 'node:path' |
| 4 | +import { fileURLToPath } from 'node:url' |
| 5 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 6 | +import { main, runSmartSig, shouldRunCli } from '../../src/cli.ts' |
| 7 | +import { Transloadit } from '../../src/Transloadit.ts' |
| 8 | + |
| 9 | +const mockedExpiresDate = '2025-01-01T00:00:00.000Z' |
| 10 | +const mockExpires = () => |
| 11 | + vi |
| 12 | + .spyOn(Transloadit.prototype as unknown as { _getExpiresDate: () => string }, '_getExpiresDate') |
| 13 | + .mockReturnValue(mockedExpiresDate) |
| 14 | + |
| 15 | +const resetExitCode = () => { |
| 16 | + process.exitCode = undefined |
| 17 | +} |
| 18 | + |
| 19 | +afterEach(() => { |
| 20 | + vi.restoreAllMocks() |
| 21 | + vi.unstubAllEnvs() |
| 22 | + resetExitCode() |
| 23 | +}) |
| 24 | + |
| 25 | +describe('cli smart_sig', () => { |
| 26 | + it('recognizes symlinked invocation paths', () => { |
| 27 | + const tmpDir = mkdtempSync(path.join(tmpdir(), 'transloadit-cli-')) |
| 28 | + const symlinkTarget = fileURLToPath(new URL('../../src/cli.ts', import.meta.url)) |
| 29 | + const symlinkPath = path.join(tmpDir, 'transloadit') |
| 30 | + |
| 31 | + symlinkSync(symlinkTarget, symlinkPath) |
| 32 | + try { |
| 33 | + expect(shouldRunCli(symlinkPath)).toBe(true) |
| 34 | + } finally { |
| 35 | + rmSync(symlinkPath) |
| 36 | + rmSync(tmpDir, { recursive: true, force: true }) |
| 37 | + } |
| 38 | + }) |
| 39 | + |
| 40 | + it('overwrites auth key with env credentials', async () => { |
| 41 | + mockExpires() |
| 42 | + vi.stubEnv('TRANSLOADIT_KEY', 'key') |
| 43 | + vi.stubEnv('TRANSLOADIT_SECRET', 'secret') |
| 44 | + |
| 45 | + const stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true) |
| 46 | + const stderrSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 47 | + |
| 48 | + const expires = '2025-01-03T00:00:00.000Z' |
| 49 | + await runSmartSig(JSON.stringify({ auth: { key: 'other', expires } })) |
| 50 | + |
| 51 | + expect(stderrSpy).not.toHaveBeenCalled() |
| 52 | + expect(stdoutSpy).toHaveBeenCalledTimes(1) |
| 53 | + const output = JSON.parse(`${stdoutSpy.mock.calls[0]?.[0]}`.trim()) |
| 54 | + const params = JSON.parse(output.params) |
| 55 | + |
| 56 | + expect(params.auth?.key).toBe('key') |
| 57 | + expect(params.auth?.expires).toBe(expires) |
| 58 | + }) |
| 59 | + |
| 60 | + it('prints signature JSON built from stdin params', async () => { |
| 61 | + mockExpires() |
| 62 | + vi.stubEnv('TRANSLOADIT_KEY', 'key') |
| 63 | + vi.stubEnv('TRANSLOADIT_SECRET', 'secret') |
| 64 | + |
| 65 | + const stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true) |
| 66 | + const stderrSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 67 | + |
| 68 | + const params = { auth: { expires: '2025-01-02T00:00:00.000Z' } } |
| 69 | + await runSmartSig(JSON.stringify(params)) |
| 70 | + |
| 71 | + expect(stderrSpy).not.toHaveBeenCalled() |
| 72 | + expect(stdoutSpy).toHaveBeenCalledTimes(1) |
| 73 | + const output = stdoutSpy.mock.calls[0]?.[0] |
| 74 | + const parsed = JSON.parse(`${output}`.trim()) |
| 75 | + |
| 76 | + const client = new Transloadit({ authKey: 'key', authSecret: 'secret' }) |
| 77 | + const expected = client.calcSignature({ auth: { expires: '2025-01-02T00:00:00.000Z' } }) |
| 78 | + expect(parsed).toEqual(expected) |
| 79 | + expect(process.exitCode).toBeUndefined() |
| 80 | + }) |
| 81 | + |
| 82 | + it('fails when credentials are missing', async () => { |
| 83 | + const stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true) |
| 84 | + const stderrSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 85 | + |
| 86 | + await runSmartSig('{}') |
| 87 | + |
| 88 | + expect(stdoutSpy).not.toHaveBeenCalled() |
| 89 | + expect(stderrSpy).toHaveBeenCalledWith( |
| 90 | + 'Missing credentials. Please set TRANSLOADIT_KEY and TRANSLOADIT_SECRET environment variables.', |
| 91 | + ) |
| 92 | + expect(process.exitCode).toBe(1) |
| 93 | + }) |
| 94 | + |
| 95 | + it('fails when stdin is not valid JSON', async () => { |
| 96 | + mockExpires() |
| 97 | + vi.stubEnv('TRANSLOADIT_KEY', 'key') |
| 98 | + vi.stubEnv('TRANSLOADIT_SECRET', 'secret') |
| 99 | + |
| 100 | + const stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true) |
| 101 | + const stderrSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 102 | + |
| 103 | + await runSmartSig('this is not json') |
| 104 | + |
| 105 | + expect(stdoutSpy).not.toHaveBeenCalled() |
| 106 | + expect(stderrSpy).toHaveBeenCalled() |
| 107 | + expect(stderrSpy.mock.calls[0]?.[0]).toContain('Failed to parse JSON from stdin') |
| 108 | + expect(process.exitCode).toBe(1) |
| 109 | + }) |
| 110 | + |
| 111 | + it('fails when params are not an object', async () => { |
| 112 | + mockExpires() |
| 113 | + vi.stubEnv('TRANSLOADIT_KEY', 'key') |
| 114 | + vi.stubEnv('TRANSLOADIT_SECRET', 'secret') |
| 115 | + |
| 116 | + const stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true) |
| 117 | + const stderrSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 118 | + |
| 119 | + await runSmartSig('[]') |
| 120 | + |
| 121 | + expect(stdoutSpy).not.toHaveBeenCalled() |
| 122 | + expect(stderrSpy).toHaveBeenCalledWith( |
| 123 | + 'Invalid params provided via stdin. Expected a JSON object.', |
| 124 | + ) |
| 125 | + expect(process.exitCode).toBe(1) |
| 126 | + }) |
| 127 | + |
| 128 | + it('prints usage when no command is provided', async () => { |
| 129 | + const stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true) |
| 130 | + const stderrSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 131 | + |
| 132 | + await main([]) |
| 133 | + |
| 134 | + expect(stderrSpy).not.toHaveBeenCalled() |
| 135 | + expect(stdoutSpy).toHaveBeenCalled() |
| 136 | + const message = `${stdoutSpy.mock.calls[0]?.[0]}` |
| 137 | + expect(message).toContain('Usage:') |
| 138 | + expect(message).toContain('npx transloadit smart_sig') |
| 139 | + expect(process.exitCode).toBe(1) |
| 140 | + }) |
| 141 | +}) |
0 commit comments