Skip to content

Commit da467b8

Browse files
committed
fix(tests): Update tests for ggshieldAuthStatus
1 parent 70c0deb commit da467b8

File tree

5 files changed

+339
-306
lines changed

5 files changed

+339
-306
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,15 @@
135135
"@types/glob": "^8.1.0",
136136
"@types/mocha": "^10.0.1",
137137
"@types/node": "20.2.5",
138+
"@types/simple-mock": "^0.8.6",
138139
"@types/vscode": "^1.81.0",
139140
"@typescript-eslint/eslint-plugin": "^5.59.8",
140141
"@typescript-eslint/parser": "^5.59.8",
141142
"@vscode/test-electron": "^2.3.2",
142143
"eslint": "^8.41.0",
143144
"glob": "^8.1.0",
144145
"mocha": "^10.2.0",
146+
"simple-mock": "^0.8.0",
145147
"typescript": "^5.1.3"
146148
},
147149
"dependencies": {

src/lib/ggshield-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
SpawnOptionsWithoutStdio,
44
spawn,
55
} from "child_process";
6-
import { window, WebviewView } from "vscode";
6+
import { window, WebviewView, ExtensionContext, commands } from "vscode";
77
import axios from 'axios';
88
import { GGShieldConfiguration } from "./ggshield-configuration";
99
import { GGShieldScanResults } from "./api-types";

src/test/suite/authentication.test.ts

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/test/suite/ggshield-api.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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

Comments
 (0)