|
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as simple from 'simple-mock'; |
| 3 | +import { ExtensionContext, Memento } from 'vscode'; |
| 4 | +import { ggshieldAuthStatus } from '../../lib/ggshield-api'; |
| 5 | +import * as runGGShield from '../../lib/run-ggshield'; |
| 6 | +import { GGShieldConfiguration } from '../../lib/ggshield-configuration'; |
| 7 | + |
| 8 | +suite('ggshieldAuthStatus', function () { |
| 9 | + let isAuthenticated: boolean; |
| 10 | + let mockGlobalState: Memento & { setKeysForSync(keys: readonly string[]): void; }; |
| 11 | + let mockContext: Partial<ExtensionContext>; |
| 12 | + |
| 13 | + setup(function () { |
| 14 | + isAuthenticated = false; |
| 15 | + |
| 16 | + mockGlobalState = { |
| 17 | + get: (key: string) => (key === 'isAuthenticated' ? isAuthenticated : undefined), |
| 18 | + update: (key: string, value: any) => { |
| 19 | + if (key === 'isAuthenticated') { |
| 20 | + isAuthenticated = value; |
| 21 | + } |
| 22 | + return Promise.resolve(); |
| 23 | + }, |
| 24 | + keys: () => [], |
| 25 | + setKeysForSync: (keys: readonly string[]) => {}, |
| 26 | + }; |
| 27 | + |
| 28 | + mockContext = { |
| 29 | + globalState: mockGlobalState, |
| 30 | + }; |
| 31 | + }); |
| 32 | + teardown(function () { |
| 33 | + simple.restore(); |
| 34 | + }); |
| 35 | + |
| 36 | + test('Valid authentication should update isAuthenticated to true', async function () { |
| 37 | + simple.mock(runGGShield, "runGGShieldCommand").returnWith({ |
| 38 | + status: 0, |
| 39 | + stdout: '{"detail": "Valid API key.", "status_code": 200, "app_version": "v2.111.0", "secrets_engine_version": "2.124.1"}', |
| 40 | + stderr: '' |
| 41 | + }); |
| 42 | + |
| 43 | + await ggshieldAuthStatus({} as GGShieldConfiguration, mockContext as ExtensionContext); |
| 44 | + assert.strictEqual(isAuthenticated, true); |
| 45 | + }); |
| 46 | + |
| 47 | + test('Invalid authentication should keep isAuthenticated to false', async function () { |
| 48 | + simple.mock(runGGShield, "runGGShieldCommand").returnWith({ |
| 49 | + status: 0, |
| 50 | + stdout: '{"detail": "Invalid API key.", "status_code": 401, "app_version": "v2.111.0", "secrets_engine_version": "2.124.1"}', |
| 51 | + stderr: '' |
| 52 | + }); |
| 53 | + |
| 54 | + await ggshieldAuthStatus({} as GGShieldConfiguration, mockContext as ExtensionContext); |
| 55 | + assert.strictEqual(isAuthenticated, false); |
| 56 | + }); |
| 57 | +}); |
0 commit comments