11import { expect } from 'chai' ;
22import * as sinon from 'sinon' ;
3- import { authHandler , interactive } from '../../src/utils' ;
4- import { configHandler , cliux } from '@contentstack/cli-utilities' ;
3+ import { authHandler , interactive , totpHandler } from '../../src/utils' ;
4+ import {
5+ configHandler ,
6+ cliux ,
7+ messageHandler ,
8+ authHandler as oauthHandler ,
9+ CLIError
10+ } from '@contentstack/cli-utilities' ;
11+ import * as managementSDK from '@contentstack/cli-utilities' ;
512import { Helper } from './helper' ;
613
714const config = configHandler ;
@@ -11,24 +18,48 @@ const TFATestToken = '24563992';
1118
1219describe ( 'contentstack-auth plugin test' , ( ) => {
1320 let sandbox : sinon . SinonSandbox ;
21+ let mockClient : {
22+ login : sinon . SinonStub ;
23+ logout : sinon . SinonStub ;
24+ getUser : sinon . SinonStub ;
25+ } ;
1426
1527 beforeEach ( ( ) => {
1628 sandbox = sinon . createSandbox ( ) ;
1729
18- // Stub interactive prompts
30+ // Interactive prompts
1931 sandbox . stub ( interactive , 'askUsername' ) . resolves ( credentials . email ) ;
2032 sandbox . stub ( interactive , 'askPassword' ) . resolves ( credentials . password ) ;
21- sandbox . stub ( interactive , 'askOTPChannel' ) . resolves ( 'authy ' ) ;
33+ sandbox . stub ( interactive , 'askOTPChannel' ) . resolves ( 'authenticator_app ' ) ;
2234 sandbox . stub ( interactive , 'askOTP' ) . resolves ( TFATestToken ) ;
2335
24- // Stub cliux
36+ // CLI UI
2537 sandbox . stub ( cliux , 'success' ) ;
2638 sandbox . stub ( cliux , 'error' ) ;
2739 sandbox . stub ( cliux , 'inquire' ) . resolves ( credentials . email ) ;
2840
29- // Stub config
41+ // Config
3042 sandbox . stub ( config , 'set' ) ;
3143 sandbox . stub ( config , 'get' ) . returns ( credentials . email ) ;
44+
45+ // Management SDK Client
46+ mockClient = {
47+ login : sandbox . stub ( ) . resolves ( { user : { email : credentials . email , authtoken : 'test-token' } } ) ,
48+ logout : sandbox . stub ( ) . resolves ( { } ) ,
49+ getUser : sandbox . stub ( ) . resolves ( { email : credentials . email } )
50+ } ;
51+ sandbox . stub ( managementSDK , 'managementSDKClient' ) . resolves ( mockClient ) ;
52+ authHandler . client = mockClient ;
53+
54+ // TOTP Handler
55+ sandbox . stub ( totpHandler , 'getTOTPCode' ) . resolves ( TFATestToken ) ;
56+
57+ // OAuth Handler
58+ sandbox . stub ( oauthHandler , 'setConfigData' ) . resolves ( ) ;
59+ sandbox . stub ( oauthHandler , 'host' ) . value ( 'https://api.contentstack.io' ) ;
60+
61+ // Message Handler
62+ sandbox . stub ( messageHandler , 'parse' ) . returns ( 'Successfully logged in!!' ) ;
3263 } ) ;
3364
3465 afterEach ( ( ) => {
@@ -64,33 +95,37 @@ describe('contentstack-auth plugin test', () => {
6495 } ) ;
6596
6697 describe ( 'Check auth:login command with 2FA' , function ( ) {
67- this . timeout ( 10000 ) ; // Increase timeout to 10s
98+ this . timeout ( 10000 ) ;
6899
69100 it ( 'Login should succeed with 2FA' , async ( ) => {
70- const loginStub = sandbox . stub ( authHandler , 'login' ) ;
71- loginStub . onFirstCall ( ) . rejects ( { error_code : 294 } ) ;
72- loginStub . onSecondCall ( ) . resolves ( {
73- email : credentials . email ,
74- authtoken : 'test-token'
101+ mockClient . login . resetBehavior ( ) ;
102+ mockClient . login . resetHistory ( ) ;
103+
104+ mockClient . login
105+ . onFirstCall ( ) . resolves ( { error_code : 294 } )
106+ . onSecondCall ( ) . resolves ( { user : { email : credentials . email , authtoken : 'test-token' } } ) ;
107+
108+ await authHandler . login ( credentials . email , credentials . password ) ;
109+ expect ( mockClient . login . callCount ) . to . equal ( 2 ) ;
75110 } ) ;
76-
77- await Helper . run ( [ 'auth:login' , `--username=${ credentials . email } ` , `--password=${ credentials . password } ` ] ) ;
78- expect ( loginStub . calledTwice ) . to . be . true ;
79- } ) ;
80111
81- it ( 'Login should fail with invalid 2FA code' , async ( ) => {
82- const loginStub = sandbox . stub ( authHandler , 'login' ) ;
83- loginStub . onFirstCall ( ) . rejects ( { error_code : 294 } ) ;
84- loginStub . onSecondCall ( ) . rejects ( new Error ( 'Invalid 2FA code' ) ) ;
85-
86- try {
87- await Helper . run ( [ 'auth:login' , `--username=${ credentials . email } ` , `--password=${ credentials . password } ` ] ) ;
88- } catch ( error ) {
89- expect ( ( error as Error ) . message ) . to . include ( 'Invalid 2FA code' ) ;
90- }
91-
92- expect ( loginStub . calledTwice ) . to . be . true ;
93- } ) ;
112+ it ( 'Login should fail with invalid 2FA code' , async ( ) => {
113+ mockClient . login . resetBehavior ( ) ;
114+ mockClient . login . resetHistory ( ) ;
115+
116+ mockClient . login
117+ . onFirstCall ( ) . resolves ( { error_code : 294 } )
118+ . onSecondCall ( ) . rejects ( new Error ( 'Invalid 2FA code' ) ) ;
119+
120+ try {
121+ await authHandler . login ( credentials . email , credentials . password ) ;
122+ throw new Error ( 'Should have failed' ) ;
123+ } catch ( error ) {
124+ expect ( error ) . to . be . instanceOf ( CLIError ) ;
125+ }
126+
127+ expect ( mockClient . login . callCount ) . to . equal ( 2 ) ;
128+ } ) ;
94129 } ) ;
95130
96131 describe ( 'Check auth:login command with OAuth' , function ( ) {
0 commit comments