@@ -8,10 +8,12 @@ const inquirer = require('inquirer');
88
99// our packages
1010const { handler : updateEndpoint } = require ( '../src/commands/endpoint' ) ;
11+ const { handler : removeEndpoint } = require ( '../src/commands/endpoint-rm' ) ;
1112
1213module . exports = ( ) => {
1314 const configPath = path . join ( __dirname , 'fixtures' , 'cli.config.yml' ) ;
1415 const mockEndpoint = 'http://test.endpoint' ;
16+ const mockEndpoint2 = 'http://test' ;
1517
1618 // load original config
1719 const origCfg = yaml . safeLoad ( fs . readFileSync ( configPath , 'utf8' ) ) ;
@@ -43,6 +45,36 @@ module.exports = () => {
4345 } ) ;
4446 } ) ;
4547
48+ // test config generation
49+ tap . test ( 'Should add second new endpoint' , t => {
50+ // spy on console
51+ const consoleSpy = sinon . spy ( console , 'log' ) ;
52+ // execute login
53+ updateEndpoint ( { url : mockEndpoint2 } ) . then ( ( ) => {
54+ // first check console output
55+ t . deepEqual (
56+ consoleSpy . args ,
57+ [ [ 'Updating endpoint URL to:' , mockEndpoint2 ] , [ 'Endpoint URL updated!' ] ] ,
58+ 'Correct log output'
59+ ) ;
60+ // then check config changes
61+ const cfg = yaml . safeLoad ( fs . readFileSync ( configPath , 'utf8' ) ) ;
62+ t . equal ( cfg . endpoint , mockEndpoint2 , 'Correct endpoint' ) ;
63+ t . equal ( cfg . user , null , 'Correct new user' ) ;
64+ t . equal ( cfg . token , null , 'Correct token' ) ;
65+ t . equal ( cfg . endpoints . length , 2 , 'Correct new endpoints list' ) ;
66+ t . equal ( cfg . endpoints [ 0 ] . endpoint , origCfg . endpoint , 'Correct endpoint in list' ) ;
67+ t . equal ( cfg . endpoints [ 0 ] . user . username , origCfg . user . username , 'Correct user in list' ) ;
68+ t . equal ( cfg . endpoints [ 0 ] . token , origCfg . token , 'Correct token in list' ) ;
69+ t . equal ( cfg . endpoints [ 1 ] . endpoint , mockEndpoint , 'Correct endpoint in list' ) ;
70+ t . notOk ( cfg . endpoints [ 1 ] . user , 'Correct user in list' ) ;
71+ t . notOk ( cfg . endpoints [ 1 ] . token , 'Correct token in list' ) ;
72+ // restore console
73+ console . log . restore ( ) ;
74+ t . end ( ) ;
75+ } ) ;
76+ } ) ;
77+
4678 // test config generation
4779 tap . test ( 'Should select old endpoint' , t => {
4880 // spy on console
@@ -62,17 +94,158 @@ module.exports = () => {
6294 t . equal ( cfg . endpoint , origCfg . endpoint , 'Correct endpoint' ) ;
6395 t . equal ( cfg . user . username , origCfg . user . username , 'Correct new user' ) ;
6496 t . equal ( cfg . token , origCfg . token , 'Correct token' ) ;
65- t . equal ( cfg . endpoints . length , 1 , 'Correct new endpoints list' ) ;
97+ t . equal ( cfg . endpoints . length , 2 , 'Correct new endpoints list' ) ;
6698 t . equal ( cfg . endpoints [ 0 ] . endpoint , mockEndpoint , 'Correct endpoint in list' ) ;
6799 t . notOk ( cfg . endpoints [ 0 ] . user , 'Correct user in list' ) ;
68100 t . notOk ( cfg . endpoints [ 0 ] . token , 'Correct token in list' ) ;
101+ t . equal ( cfg . endpoints [ 1 ] . endpoint , mockEndpoint2 , 'Correct endpoint in list' ) ;
102+ t . notOk ( cfg . endpoints [ 1 ] . user , 'Correct user in list' ) ;
103+ t . notOk ( cfg . endpoints [ 1 ] . token , 'Correct token in list' ) ;
104+ // restore console
105+ console . log . restore ( ) ;
106+ // restore inquirer
107+ inquirer . prompt . restore ( ) ;
108+ t . end ( ) ;
109+ } ) ;
110+ } ) ;
111+
112+ // test config generation
113+ tap . test ( 'Should select old endpoint using URL param' , t => {
114+ // spy on console
115+ const consoleSpy = sinon . spy ( console , 'log' ) ;
116+ // execute login
117+ updateEndpoint ( { url : mockEndpoint } ) . then ( ( ) => {
118+ // first check console output
119+ t . deepEqual (
120+ consoleSpy . args ,
121+ [ [ 'Updating endpoint URL to:' , mockEndpoint ] , [ 'Endpoint URL updated!' ] ] ,
122+ 'Correct log output'
123+ ) ;
124+ // then check config changes
125+ const cfg = yaml . safeLoad ( fs . readFileSync ( configPath , 'utf8' ) ) ;
126+ t . equal ( cfg . endpoint , mockEndpoint , 'Correct endpoint' ) ;
127+ t . notOk ( cfg . user , 'Correct new user' ) ;
128+ t . notOk ( cfg . token , 'Correct token' ) ;
129+ t . equal ( cfg . endpoints . length , 2 , 'Correct new endpoints list' ) ;
130+ t . equal ( cfg . endpoints [ 0 ] . endpoint , mockEndpoint2 , 'Correct endpoint in list' ) ;
131+ t . notOk ( cfg . endpoints [ 0 ] . user , 'Correct user in list' ) ;
132+ t . notOk ( cfg . endpoints [ 0 ] . token , 'Correct token in list' ) ;
133+ t . equal ( cfg . endpoints [ 1 ] . endpoint , origCfg . endpoint , 'Correct endpoint in list' ) ;
134+ t . equal ( cfg . endpoints [ 1 ] . user . username , origCfg . user . username , 'Correct user in list' ) ;
135+ t . equal ( cfg . endpoints [ 1 ] . token , origCfg . token , 'Correct token in list' ) ;
136+ // restore console
137+ console . log . restore ( ) ;
138+ t . end ( ) ;
139+ } ) ;
140+ } ) ;
141+
142+ tap . test ( 'Should show error on remove of non-existent endpoint' , t => {
143+ // spy on console
144+ const consoleSpy = sinon . spy ( console , 'log' ) ;
145+ // stup inquirer answers
146+ sinon . stub ( inquirer , 'prompt' ) . callsFake ( ( ) => Promise . resolve ( { delEndpoint : 'do-not-exist' } ) ) ;
147+ // execute login
148+ removeEndpoint ( { } ) . then ( ( ) => {
149+ // first check console output
150+ t . deepEqual (
151+ consoleSpy . args ,
152+ [ [ 'Error!' , "Couldn't find endpoint with URL:" , 'do-not-exist' ] ] ,
153+ 'Correct log output'
154+ ) ;
155+ // restore console
156+ console . log . restore ( ) ;
157+ // restore inquirer
158+ inquirer . prompt . restore ( ) ;
159+ t . end ( ) ;
160+ } ) ;
161+ } ) ;
162+
163+ tap . test ( 'Should remove current endpoint using inquirer' , t => {
164+ // spy on console
165+ const consoleSpy = sinon . spy ( console , 'log' ) ;
166+ // stup inquirer answers
167+ sinon . stub ( inquirer , 'prompt' ) . callsFake ( ( ) => Promise . resolve ( { delEndpoint : mockEndpoint } ) ) ;
168+ // execute login
169+ removeEndpoint ( { } ) . then ( ( ) => {
170+ // first check console output
171+ t . deepEqual ( consoleSpy . args , [ [ 'Removing endpoint:' , mockEndpoint ] , [ 'Endpoint removed!' ] ] , 'Correct log output' ) ;
172+ // then check config changes
173+ const cfg = yaml . safeLoad ( fs . readFileSync ( configPath , 'utf8' ) ) ;
174+ t . equal ( cfg . endpoint , mockEndpoint2 , 'Correct endpoint' ) ;
175+ t . notOk ( cfg . user , 'Correct new user' ) ;
176+ t . notOk ( cfg . token , 'Correct token' ) ;
177+ t . equal ( cfg . endpoints . length , 1 , 'Correct new endpoints list' ) ;
178+ t . equal ( cfg . endpoints [ 0 ] . endpoint , origCfg . endpoint , 'Correct endpoint in list' ) ;
179+ t . equal ( cfg . endpoints [ 0 ] . user . username , origCfg . user . username , 'Correct user in list' ) ;
180+ t . equal ( cfg . endpoints [ 0 ] . token , origCfg . token , 'Correct token in list' ) ;
69181 // restore console
70182 console . log . restore ( ) ;
71183 // restore inquirer
72184 inquirer . prompt . restore ( ) ;
73- // restore original config
74- fs . writeFileSync ( configPath , yaml . safeDump ( origCfg ) , 'utf8' ) ;
75185 t . end ( ) ;
76186 } ) ;
77187 } ) ;
188+
189+ tap . test ( 'Should remove existing endpoint using param' , t => {
190+ let consoleSpy ;
191+ // select original endpoint to test removal from list
192+ updateEndpoint ( { url : origCfg . endpoint } )
193+ . then ( ( ) => {
194+ // spy on console
195+ consoleSpy = sinon . spy ( console , 'log' ) ;
196+ // execute login
197+ return removeEndpoint ( { url : mockEndpoint2 } ) ;
198+ } )
199+ . then ( ( ) => {
200+ // first check console output
201+ t . deepEqual (
202+ consoleSpy . args ,
203+ [ [ 'Removing endpoint:' , mockEndpoint2 ] , [ 'Endpoint removed!' ] ] ,
204+ 'Correct log output'
205+ ) ;
206+ // then check config changes
207+ const cfg = yaml . safeLoad ( fs . readFileSync ( configPath , 'utf8' ) ) ;
208+ console . log ( cfg ) ;
209+ t . equal ( cfg . endpoint , origCfg . endpoint , 'Correct endpoint' ) ;
210+ t . equal ( cfg . user . username , origCfg . user . username , 'Correct new user' ) ;
211+ t . equal ( cfg . token , origCfg . token , 'Correct token' ) ;
212+ t . equal ( cfg . endpoints . length , 0 , 'Correct new endpoints list' ) ;
213+ // restore console
214+ console . log . restore ( ) ;
215+ t . end ( ) ;
216+ } ) ;
217+ } ) ;
218+
219+ tap . test ( 'Should not remove only endpoint' , t => {
220+ // spy on console
221+ const consoleSpy = sinon . spy ( console , 'log' ) ;
222+ // stup inquirer answers
223+ sinon . stub ( inquirer , 'prompt' ) . callsFake ( ( ) => Promise . resolve ( { delEndpoint : origCfg . endpoint } ) ) ;
224+ // execute login
225+ removeEndpoint ( { } ) . then ( ( ) => {
226+ // first check console output
227+ t . deepEqual (
228+ consoleSpy . args ,
229+ [ [ 'Error!' , 'Cannot remove the only endpoint URL:' , origCfg . endpoint ] ] ,
230+ 'Correct log output'
231+ ) ;
232+ // then check config changes
233+ const cfg = yaml . safeLoad ( fs . readFileSync ( configPath , 'utf8' ) ) ;
234+ t . equal ( cfg . endpoint , origCfg . endpoint , 'Correct endpoint' ) ;
235+ t . equal ( cfg . user . username , origCfg . user . username , 'Correct new user' ) ;
236+ t . equal ( cfg . token , origCfg . token , 'Correct token' ) ;
237+ t . equal ( cfg . endpoints . length , 0 , 'Correct new endpoints list' ) ;
238+ // restore console
239+ console . log . restore ( ) ;
240+ // restore inquirer
241+ inquirer . prompt . restore ( ) ;
242+ t . end ( ) ;
243+ } ) ;
244+ } ) ;
245+
246+ tap . test ( 'Restore original config' , t => {
247+ // restore original config
248+ fs . writeFileSync ( configPath , yaml . safeDump ( origCfg ) , 'utf8' ) ;
249+ t . end ( ) ;
250+ } ) ;
78251} ;
0 commit comments