1- const { describe, it } = require ( 'mocha' ) ;
2- const { cliux } = require ( '@contentstack/cli-utilities' ) ;
3- const sinon = require ( 'sinon' ) ;
4- const { config } = require ( 'dotenv' ) ;
51const { expect } = require ( 'chai' ) ;
2+ const sinon = require ( 'sinon' ) ;
3+ const { describe, it, beforeEach, afterEach } = require ( 'mocha' ) ;
4+ const UnpublishCommand = require ( '../../../../src/commands/cm/assets/unpublish' ) ;
5+ const AddTokenCommand = require ( '@contentstack/cli-auth/lib/commands/auth/tokens/add' ) . default ;
6+ const helper = require ( '../../../helpers/helper' ) ;
7+ const { cliux } = require ( '@contentstack/cli-utilities' ) ;
68
7- const AssetsUnpublish = require ( '../../../../src/commands/cm/assets/unpublish' ) ;
9+ describe ( 'AssetsUnpublish Command' , ( ) => {
10+ let sandbox ;
11+ let stackDetails ;
812
9- const { stub } = sinon ;
13+ beforeEach ( async ( ) => {
14+ sandbox = sinon . createSandbox ( ) ;
1015
11- config ( ) ;
16+ stackDetails = {
17+ api_key : 'asdf' ,
18+ environment : 'env' ,
19+ delivery_token : 'asdf' ,
20+ management_token : 'asdf' ,
21+ alias : 'm_alias' ,
22+ } ;
23+ } ) ;
1224
13- const environments = process . env . ENVIRONMENTS . split ( ',' ) ;
14- const locales = process . env . LOCALES . split ( ',' ) ;
25+ afterEach ( ( ) => {
26+ sandbox . restore ( ) ;
27+ } ) ;
28+
29+ it ( 'executes successfully with required parameters' , async ( ) => {
30+ const runStub = sandbox . stub ( UnpublishCommand . prototype , 'run' ) . resolves ( ) ;
1531
16- describe ( 'AssetsUnpublish' , ( ) => {
17- it ( 'Should run successfully when all the flags are passed' , async ( ) => {
18- const args = [ '--environment' , environments [ 0 ] , '--locale' , locales [ 0 ] , '--alias' , process . env . MANAGEMENT_ALIAS , '--delivery-token' , process . env . DELIVERY_TOKEN , '--yes' ] ;
19- const inquireStub = stub ( cliux , 'prompt' ) ;
20- await AssetsUnpublish . run ( args ) ;
21- sinon . assert . notCalled ( inquireStub ) ;
22- inquireStub . restore ( ) ;
32+ const result = await UnpublishCommand . run ( [
33+ '--alias' ,
34+ 'm_alias' ,
35+ '--environment' ,
36+ 'env' ,
37+ '--locale' ,
38+ 'en-us' ,
39+ '--delivery-token' ,
40+ 'test-delivery-token' ,
41+ '--yes' ,
42+ ] ) ;
43+
44+ expect ( runStub . calledOnce ) . to . be . true ;
45+ expect ( result ) . to . be . undefined ;
2346 } ) ;
2447
25- it ( 'Should ask for delivery token when the flag is not passed' , async ( ) => {
26- const args = [ '--environment' , environments [ 0 ] , '--locale' , locales [ 0 ] , '--alias' , process . env . MANAGEMENT_ALIAS , '--yes' ] ;
27- const inquireStub = stub ( cliux , 'prompt' ) . resolves ( process . env . DELIVERY_TOKEN ) ;
28- await AssetsUnpublish . run ( args ) ;
29- sinon . assert . calledOnce ( inquireStub ) ;
30- inquireStub . restore ( ) ;
48+ it ( 'executes successfully with stack identifier' , async ( ) => {
49+ const runStub = sandbox . stub ( UnpublishCommand . prototype , 'run' ) . resolves ( ) ;
50+
51+ const result = await UnpublishCommand . run ( [
52+ '--stack-api-key' ,
53+ stackDetails . api_key ,
54+ '--environment' ,
55+ 'env' ,
56+ '--locale' ,
57+ 'en-us' ,
58+ '--delivery-token' ,
59+ 'test-delivery-token' ,
60+ '--yes' ,
61+ ] ) ;
62+
63+ expect ( runStub . calledOnce ) . to . be . true ;
64+ expect ( result ) . to . be . undefined ;
65+ } ) ;
66+
67+ it ( 'prompts for missing delivery token' , async ( ) => {
68+ sandbox . stub ( helper , 'getStack' ) . resolves ( { ...stackDetails , delivery_token : undefined } ) ;
69+ sandbox . stub ( cliux , 'prompt' ) . resolves ( 'prompted-token' ) ;
70+ sandbox . stub ( AddTokenCommand . prototype , 'run' ) . resolves ( ) ;
71+ const runStub = sandbox . stub ( UnpublishCommand . prototype , 'run' ) . resolves ( 'Success' ) ;
72+
73+ const result = await UnpublishCommand . run ( [
74+ '--alias' ,
75+ 'm_alias' ,
76+ '--environment' ,
77+ 'env' ,
78+ '--locale' ,
79+ 'en-us' ,
80+ '--yes' ,
81+ ] ) ;
82+
83+ expect ( result ) . to . equal ( 'Success' ) ;
84+ sinon . assert . calledOnce ( runStub ) ;
3185 } ) ;
3286
33- it ( 'Should fail when alias and stack api key flags are not passed' , async ( ) => {
34- const args = [ '--environment' , environments [ 0 ] , '--locale' , locales [ 0 ] , '--yes' ] ;
35- const inquireStub = stub ( cliux , 'prompt' ) ;
36- const assetUnpublishSpy = sinon . spy ( AssetsUnpublish . prototype , 'run' ) ;
37- const expectedError = 'Please use `--alias` or `--stack-api-key` to proceed.' ;
87+ it ( 'throws error for missing authentication parameters' , async ( ) => {
88+ const runStub = sandbox . stub ( UnpublishCommand . prototype , 'run' ) ;
89+
3890 try {
39- await AssetsUnpublish . run ( args ) ;
91+ await UnpublishCommand . run ( [ '--environment' , 'env' , '--locale' , 'en-us' , '--yes' ] ) ;
4092 } catch ( error ) {
41- expect ( error ) . to . be . an . instanceOf ( Error ) ;
42- expect ( error . message ) . to . equal ( expectedError ) ;
43- expect ( assetUnpublishSpy . calledOnce ) . to . be . true ;
93+ expect ( error . message ) . to . equal ( 'Please use `--alias` or `--stack-api-key` to proceed.' ) ;
94+ expect ( runStub . called ) . to . be . false ;
4495 }
45- sinon . assert . notCalled ( inquireStub ) ;
46- inquireStub . restore ( ) ;
47- assetUnpublishSpy . restore ( ) ;
48- } ) ;
49-
50- it ( 'Should run successfully when user is logged in and stack api key is passed' , async ( ) => {
51- const args = [ '--environment' , environments [ 0 ] , '--locale' , locales [ 0 ] , '--stack-api-key' , process . env . STACK_API_KEY , '--delivery-token' , process . env . DELIVERY_TOKEN , '--yes' ] ;
52- const inquireStub = stub ( cliux , 'prompt' ) ;
53- await AssetsUnpublish . run ( args ) ;
54- sinon . assert . notCalled ( inquireStub ) ;
55- inquireStub . restore ( ) ;
5696 } ) ;
57- } ) ;
97+ } ) ;
0 commit comments