@@ -3,13 +3,14 @@ import * as statusBar from "../../../gitguardian-interface/gitguardian-status-ba
33import * as simple from "simple-mock" ;
44import { diagnosticCollection , scanFile } from "../../../lib/ggshield-api" ;
55import * as runGGShield from "../../../lib/run-ggshield" ;
6- import path = require( "path" ) ;
76import { Uri , window } from "vscode" ;
8- import assert = require( "assert" ) ;
97import {
108 scanResultsNoIncident ,
119 scanResultsWithIncident ,
1210} from "../../constants" ;
11+ import * as assert from "assert" ;
12+ import { ExtensionContext , Memento } from "vscode" ;
13+ import { ggshieldAuthStatus } from "../../../lib/ggshield-api" ;
1314
1415suite ( "scanFile" , ( ) => {
1516 let updateStatusBarMock : simple . Stub < Function > ;
@@ -91,3 +92,65 @@ suite("scanFile", () => {
9192 assert . strictEqual ( errorMessageMock . lastCall . args [ 0 ] , "ggshield: Error\n" ) ;
9293 } ) ;
9394} ) ;
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