@@ -16,7 +16,7 @@ import { CliExitData } from '../../../src/cli';
16
16
import { TestItem } from './testOSItem' ;
17
17
import { OpenShiftItem } from '../../../src/openshift/openshiftItem' ;
18
18
import pq = require( 'proxyquire' ) ;
19
- import { getVscodeModule } from '../../../src/util/credentialManager' ;
19
+ import { getVscodeModule , TokenStore } from '../../../src/util/credentialManager' ;
20
20
21
21
const expect = chai . expect ;
22
22
chai . use ( sinonChai ) ;
@@ -44,6 +44,27 @@ suite('Openshift/Cluster', () => {
44
44
stderr : error ,
45
45
stdout : 'output'
46
46
} ;
47
+ const routeObj = `{
48
+ "apiVersion": "v1",
49
+ "items": [
50
+ {
51
+ "apiVersion": "route.openshift.io/v1",
52
+ "kind": "Route",
53
+ "spec": {
54
+ "host": "console-openshift-console.apps-crc.testing",
55
+ "port": {
56
+ "targetPort": "https"
57
+ },
58
+ "wildcardPolicy": "None"
59
+ }
60
+ }
61
+ ],
62
+ "kind": "List",
63
+ "metadata": {
64
+ "resourceVersion": "",
65
+ "selfLink": ""
66
+ }
67
+ }` ;
47
68
const testUrl = 'https://162.165.64.43:8443' ;
48
69
const testUser = 'user' ;
49
70
const password = 'password' ;
@@ -83,13 +104,21 @@ suite('Openshift/Cluster', () => {
83
104
expect ( status ) . null ;
84
105
} ) ;
85
106
107
+ test ( 'returns null if loginActions is not selected' , async ( ) => {
108
+ infoStub . resolves ( 'Yes' ) ;
109
+ quickPickStub . onFirstCall ( ) . resolves ( null ) ;
110
+ const status = await Cluster . login ( ) ;
111
+
112
+ expect ( status ) . null ;
113
+ } ) ;
114
+
86
115
test ( 'wraps incoming errors' , async ( ) => {
87
116
quickPickStub . resolves ( 'Credentials' ) ;
88
117
quickPickStub . onSecondCall ( ) . resolves ( { description : "Current Context" , label : testUrl } ) ;
89
118
quickPickStub . onThirdCall ( ) . resolves ( { description : "Current Context" , label : testUser } ) ;
90
119
inputStub . resolves ( password ) ;
91
120
commandStub . rejects ( err ) ;
92
- let expectedErr ;
121
+ let expectedErr : { message : any ; } ;
93
122
try {
94
123
await Cluster . login ( ) ;
95
124
} catch ( error ) {
@@ -125,6 +154,27 @@ suite('Openshift/Cluster', () => {
125
154
expect ( result ) . equals ( `Successfully logged in to '${ testUrl } '` ) ;
126
155
} ) ;
127
156
157
+ test ( 'returns null if cluster url is not provided' , async ( ) => {
158
+ infoStub . resolves ( 'Yes' ) ;
159
+ quickPickStub . onFirstCall ( ) . resolves ( null ) ;
160
+ const result = await Cluster . credentialsLogin ( ) ;
161
+ expect ( result ) . null ;
162
+ } ) ;
163
+
164
+ test ( 'returns null if username is not provided' , async ( ) => {
165
+ infoStub . resolves ( 'Yes' ) ;
166
+ quickPickStub . onSecondCall ( ) . resolves ( null ) ;
167
+ const result = await Cluster . credentialsLogin ( ) ;
168
+ expect ( result ) . null ;
169
+ } ) ;
170
+
171
+ test ( "doesn't ask to save password if old and new passwords are the same" , async ( ) => {
172
+ infoStub . resolves ( 'Yes' ) ;
173
+ sandbox . stub ( TokenStore , 'getItem' ) . resolves ( password ) ;
174
+ const result = await Cluster . credentialsLogin ( ) ;
175
+ expect ( result ) . equals ( `Successfully logged in to '${ testUrl } '` ) ;
176
+ } ) ;
177
+
128
178
test ( 'exits if the user cancels url input box' , async ( ) => {
129
179
loginStub . resolves ( false ) ;
130
180
inputStub . onFirstCall ( ) . resolves ( null ) ;
@@ -163,7 +213,7 @@ suite('Openshift/Cluster', () => {
163
213
164
214
test ( 'errors if there is output on odo stderr' , async ( ) => {
165
215
execStub . resolves ( errorData ) ;
166
- let expectedErr ;
216
+ let expectedErr : { message : any ; } ;
167
217
try {
168
218
await Cluster . credentialsLogin ( ) ;
169
219
} catch ( err ) {
@@ -248,13 +298,12 @@ suite('Openshift/Cluster', () => {
248
298
249
299
test ( 'handles incoming errors the same way as credentials login' , async ( ) => {
250
300
execStub . rejects ( err ) ;
251
- let expectedErr ;
301
+ let expectedErr : { message : any ; } ;
252
302
try {
253
303
await Cluster . tokenLogin ( ) ;
254
304
} catch ( error ) {
255
305
expectedErr = error ;
256
306
}
257
- console . log ( expectedErr . message ) ;
258
307
expect ( expectedErr . message ) . equals ( `Failed to login to cluster '${ testUrl } ' with '${ err . message } '!` ) ;
259
308
} ) ;
260
309
} ) ;
@@ -294,7 +343,7 @@ suite('Openshift/Cluster', () => {
294
343
295
344
test ( 'handles errors from odo' , async ( ) => {
296
345
execStub . rejects ( error ) ;
297
- let expectedErr ;
346
+ let expectedErr : any ;
298
347
try {
299
348
await Cluster . logout ( ) ;
300
349
@@ -306,7 +355,7 @@ suite('Openshift/Cluster', () => {
306
355
307
356
test ( 'handles errors from odo stderr' , async ( ) => {
308
357
execStub . resolves ( errorData ) ;
309
- let expectedErr ;
358
+ let expectedErr : any ;
310
359
try {
311
360
await Cluster . logout ( ) ;
312
361
} catch ( err ) {
@@ -322,7 +371,7 @@ suite('Openshift/Cluster', () => {
322
371
quickPickStub . onSecondCall ( ) . resolves ( { description : "Current Context" , label : testUrl } ) ;
323
372
quickPickStub . onThirdCall ( ) . resolves ( { description : "Current Context" , label : testUrl } ) ;
324
373
inputStub . resolves ( password ) ;
325
- let expectedErr ;
374
+ let expectedErr : { message : any ; } ;
326
375
try {
327
376
await Cluster . logout ( ) ;
328
377
} catch ( err ) {
@@ -350,35 +399,70 @@ suite('Openshift/Cluster', () => {
350
399
} ) ;
351
400
} ) ;
352
401
402
+ suite ( 'switchContext' , ( ) => {
403
+ const choice = {
404
+ label : 'minishift'
405
+ } ;
406
+ setup ( ( ) => {
407
+ execStub . onFirstCall ( ) . resolves ( ) ;
408
+ } ) ;
409
+
410
+ test ( 'changes cluster\'s context' , async ( ) => {
411
+ quickPickStub . onFirstCall ( ) . resolves ( choice ) ;
412
+ const result = await Cluster . switchContext ( ) ;
413
+ expect ( result ) . equals ( `Cluster context is changed to: ${ choice . label } ` ) ;
414
+ } ) ;
415
+
416
+ test ( 'returns null if OpenShift context is not selected' , async ( ) => {
417
+ quickPickStub . onFirstCall ( ) . resolves ( null ) ;
418
+ const result = await Cluster . switchContext ( ) ;
419
+ expect ( result ) . null ;
420
+ } ) ;
421
+ } ) ;
422
+
353
423
suite ( 'open console' , ( ) => {
354
424
const openStub : sinon . SinonStub = sinon . stub ( ) ;
355
- let clusterMock ;
425
+ let clusterMock : { openshiftConsole : { ( arg0 : TestItem ) : void ; ( ) : void ; ( ) : void ; } ; } ;
426
+ let cluster : TestItem ;
427
+
356
428
setup ( ( ) => {
357
429
clusterMock = pq ( '../../../src/openshift/cluster' , {
358
430
open : openStub
359
431
} ) . Cluster ;
432
+ cluster = new TestItem ( null , 'http://localhost' , ContextType . CLUSTER ) ;
360
433
} ) ;
361
434
362
435
test ( 'opens URL from cluster\'s tree item label if called from cluster\'s context menu' , ( ) => {
363
- const cluster = new TestItem ( null , 'http://localhost' , ContextType . CLUSTER ) ;
364
436
clusterMock . openshiftConsole ( cluster ) ;
365
437
openStub . calledOnceWith ( 'http://localhost' ) ;
366
438
} ) ;
367
439
368
440
test ( 'opens URL from first cluster label' , ( ) => {
369
- const cluster = new TestItem ( null , 'http://localhost' , ContextType . CLUSTER ) ;
370
441
sandbox . stub ( OdoImpl . prototype , 'getClusters' ) . resolves ( [ cluster ] ) ;
371
442
clusterMock . openshiftConsole ( ) ;
372
443
openStub . calledOnceWith ( 'http://localhost' ) ;
373
444
} ) ;
374
445
375
446
test ( 'shows error message if node label is not URL' , ( ) => {
376
- const cluster = new TestItem ( null , 'localhost' , ContextType . CLUSTER ) ;
377
447
sandbox . stub ( OdoImpl . prototype , 'getClusters' ) . resolves ( [ cluster ] ) ;
378
448
const errMsgStub = sandbox . stub ( vscode . window , 'showErrorMessage' ) ;
379
449
clusterMock . openshiftConsole ( ) ;
380
450
errMsgStub . calledOnceWith ( 'localhost' , undefined ) ;
381
451
} ) ;
382
452
453
+ test ( 'opens cluster\'s URL from context menu' , ( ) => {
454
+ execStub . onFirstCall ( ) . resolves ( {
455
+ error : null ,
456
+ stderr : error ,
457
+ stdout : 'output'
458
+ } ) ;
459
+ execStub . onSecondCall ( ) . resolves ( {
460
+ error : null ,
461
+ stderr : error ,
462
+ stdout : routeObj
463
+ } ) ;
464
+ clusterMock . openshiftConsole ( cluster ) ;
465
+ openStub . calledOnceWith ( 'http://localhost' ) ;
466
+ } ) ;
383
467
} ) ;
384
468
} ) ;
0 commit comments