@@ -3,13 +3,14 @@ import * as statusBar from "../../../gitguardian-interface/gitguardian-status-ba
3
3
import * as simple from "simple-mock" ;
4
4
import { diagnosticCollection , scanFile } from "../../../lib/ggshield-api" ;
5
5
import * as runGGShield from "../../../lib/run-ggshield" ;
6
- import path = require( "path" ) ;
7
6
import { Uri , window } from "vscode" ;
8
- import assert = require( "assert" ) ;
9
7
import {
10
8
scanResultsNoIncident ,
11
9
scanResultsWithIncident ,
12
10
} from "../../constants" ;
11
+ import * as assert from "assert" ;
12
+ import { ExtensionContext , Memento } from "vscode" ;
13
+ import { ggshieldAuthStatus } from "../../../lib/ggshield-api" ;
13
14
14
15
suite ( "scanFile" , ( ) => {
15
16
let updateStatusBarMock : simple . Stub < Function > ;
@@ -91,3 +92,65 @@ suite("scanFile", () => {
91
92
assert . strictEqual ( errorMessageMock . lastCall . args [ 0 ] , "ggshield: Error\n" ) ;
92
93
} ) ;
93
94
} ) ;
95
+
96
+ suite ( "ggshieldAuthStatus" , function ( ) {
97
+ let isAuthenticated : boolean ;
98
+ let mockGlobalState : Memento & {
99
+ setKeysForSync ( keys : readonly string [ ] ) : void ;
100
+ } ;
101
+ let mockContext : Partial < ExtensionContext > ;
102
+ let runGGShieldMock : simple . Stub < Function > ;
103
+
104
+ setup ( function ( ) {
105
+ isAuthenticated = false ;
106
+
107
+ mockGlobalState = {
108
+ get : ( key : string ) =>
109
+ key === "isAuthenticated" ? isAuthenticated : undefined ,
110
+ update : ( key : string , value : any ) => {
111
+ if ( key === "isAuthenticated" ) {
112
+ isAuthenticated = value ;
113
+ }
114
+ return Promise . resolve ( ) ;
115
+ } ,
116
+ keys : ( ) => [ ] ,
117
+ setKeysForSync : ( keys : readonly string [ ] ) => { } ,
118
+ } ;
119
+
120
+ mockContext = {
121
+ globalState : mockGlobalState ,
122
+ } ;
123
+ runGGShieldMock = simple . mock ( runGGShield , "runGGShieldCommand" ) ;
124
+ } ) ;
125
+ teardown ( function ( ) {
126
+ simple . restore ( ) ;
127
+ } ) ;
128
+
129
+ test ( "Valid authentication should update isAuthenticated to true" , async function ( ) {
130
+ runGGShieldMock . returnWith ( {
131
+ status : 0 ,
132
+ stdout : '{"detail": "Valid API key.", "status_code": 200}' ,
133
+ stderr : "" ,
134
+ } ) ;
135
+
136
+ await ggshieldAuthStatus (
137
+ { } as GGShieldConfiguration ,
138
+ mockContext as ExtensionContext
139
+ ) ;
140
+ assert . strictEqual ( isAuthenticated , true ) ;
141
+ } ) ;
142
+
143
+ test ( "Invalid authentication should keep isAuthenticated to false" , async function ( ) {
144
+ runGGShieldMock . returnWith ( {
145
+ status : 0 ,
146
+ stdout : '{"detail": "Invalid API key.", "status_code": 401}' ,
147
+ stderr : "" ,
148
+ } ) ;
149
+
150
+ await ggshieldAuthStatus (
151
+ { } as GGShieldConfiguration ,
152
+ mockContext as ExtensionContext
153
+ ) ;
154
+ assert . strictEqual ( isAuthenticated , false ) ;
155
+ } ) ;
156
+ } ) ;
0 commit comments