16
16
* SPDX-License-Identifier: Apache-2.0
17
17
***********************************************************************/
18
18
19
- import { beforeEach , expect , suite , test , vi } from 'vitest' ;
20
- import { activate } from './extension' ;
19
+ import { beforeEach , afterEach , expect , suite , test , vi } from 'vitest' ;
20
+ import * as extension from './extension' ;
21
21
import {
22
22
AuthenticationGetSessionOptions ,
23
23
TelemetryLogger ,
@@ -28,6 +28,7 @@ import {
28
28
import { authentication , commands } from '@podman-desktop/api' ;
29
29
import * as podmanCli from './podman-cli' ;
30
30
import { ExtensionTelemetryLogger } from './telemetry' ;
31
+ import { OrganizationService } from '@redhat-developer/rhsm-client' ;
31
32
32
33
vi . mock ( '@podman-desktop/api' , async ( ) => {
33
34
return {
@@ -104,11 +105,15 @@ function createExtContext(): ExtensionContext {
104
105
105
106
beforeEach ( ( ) => {
106
107
vi . resetAllMocks ( ) ;
107
- activate ( createExtContext ( ) ) ;
108
+ } ) ;
109
+
110
+ afterEach ( ( ) => {
111
+ extension . deactivate ( ) ;
108
112
} ) ;
109
113
110
114
suite ( 'extension activation' , ( ) => {
111
115
test ( 'register commands declared in package.json' , async ( ) => {
116
+ await extension . activate ( createExtContext ( ) ) ;
112
117
expect ( commands . registerCommand ) . toBeCalledTimes ( 4 ) ;
113
118
expect ( commands . registerCommand ) . toBeCalledWith ( 'redhat.authentication.signin' , expect . anything ( ) ) ;
114
119
expect ( commands . registerCommand ) . toBeCalledWith ( 'redhat.authentication.navigate.settings' , expect . anything ( ) ) ;
@@ -171,7 +176,7 @@ suite('signin command telemetry reports', () => {
171
176
} ,
172
177
) ;
173
178
vi . spyOn ( podmanCli , 'isPodmanMachineRunning' ) . mockReturnValue ( false ) ;
174
- await activate ( createExtContext ( ) ) ;
179
+ await extension . activate ( createExtContext ( ) ) ;
175
180
expect ( commandFunctionCopy ! ) . toBeDefined ( ) ;
176
181
await commandFunctionCopy ! ( ) ;
177
182
expect ( authentication . onDidChangeSessions ) . toBeCalled ( ) ;
@@ -181,4 +186,68 @@ suite('signin command telemetry reports', () => {
181
186
errorIn : 'subscription-activation' ,
182
187
} ) ;
183
188
} ) ;
189
+
190
+ test ( 'unsuccessful login when simple content access is not enabled for account' , async ( ) => {
191
+ const logSpy = vi . spyOn ( ExtensionTelemetryLogger , 'logUsage' ) . mockImplementation ( ( ) => {
192
+ return ;
193
+ } ) ;
194
+ let notificationCallback : ( event : any ) => Promise < any > ;
195
+ let session : AuthenticationSession ;
196
+ vi . mocked ( authentication . onDidChangeSessions ) . mockImplementation ( ( callback : ( event : any ) => Promise < any > ) => {
197
+ notificationCallback = callback ;
198
+ return {
199
+ dispose : vi . fn ( ) ,
200
+ } ;
201
+ } ) ;
202
+ vi . mocked ( authentication . getSession ) . mockImplementation (
203
+ async (
204
+ _p1 : string ,
205
+ _p2 : string [ ] ,
206
+ options : AuthenticationGetSessionOptions | undefined ,
207
+ ) : Promise < AuthenticationSession | undefined > => {
208
+ if ( session ) {
209
+ return Promise . resolve ( session ) ;
210
+ }
211
+ if ( options ?. createIfNone ) {
212
+ session = {
213
+ id : '1' ,
214
+ accessToken : 'token' ,
215
+ scopes : [ 'scope1' , 'scope2' ] ,
216
+ account : {
217
+ id : 'accountId' ,
218
+ label : 'label' ,
219
+ } ,
220
+ } ;
221
+ await notificationCallback ( {
222
+ provider : {
223
+ id : 'redhat.authentication-provider' ,
224
+ } ,
225
+ } ) ;
226
+ }
227
+ return ;
228
+ } ,
229
+ ) ;
230
+ let commandFunctionCopy : ( ) => Promise < void > ;
231
+ vi . mocked ( commands . registerCommand ) . mockImplementation (
232
+ ( commandId : string , commandFunction : ( ) => Promise < void > ) => {
233
+ if ( commandId === 'redhat.authentication.signin' ) {
234
+ commandFunctionCopy = commandFunction ;
235
+ }
236
+ return {
237
+ dispose : vi . fn ( ) ,
238
+ } ;
239
+ } ,
240
+ ) ;
241
+ vi . spyOn ( podmanCli , 'isPodmanMachineRunning' ) . mockReturnValue ( true ) ;
242
+ vi . spyOn ( OrganizationService . prototype , 'checkOrgScaCapability' ) . mockResolvedValue ( { body : { } } ) ;
243
+ await extension . activate ( createExtContext ( ) ) ;
244
+ expect ( commandFunctionCopy ! ) . toBeDefined ( ) ;
245
+ await commandFunctionCopy ! ( ) ;
246
+ expect ( authentication . onDidChangeSessions ) . toBeCalled ( ) ;
247
+ expect ( logSpy ) . toBeCalledWith ( 'signin' , {
248
+ successful : false ,
249
+ error : 'Error: SCA is not enabled and message closed' ,
250
+ errorIn : 'subscription-activation' ,
251
+ } ) ;
252
+ } ) ;
184
253
} ) ;
0 commit comments