@@ -16,18 +16,36 @@ module.exports = () => {
1616 const loginRequest = { phrase : 'test' , uid : '123' } ;
1717 const privateKeyName = path . join ( __dirname , 'fixtures' , 'id_rsa' ) ;
1818 const privateKeyNameBroken = path . join ( __dirname , 'fixtures' , 'id_rsa_b' ) ;
19+ const privateKeyWithPassphrase = path . join ( __dirname , 'fixtures' , 'id_rsa_keyphrase' ) ;
1920 const cert = fs . readFileSync ( privateKeyName ) ;
21+ const certKey = fs . readFileSync ( privateKeyWithPassphrase ) ;
2022 const certBroken = fs . readFileSync ( privateKeyNameBroken ) ;
2123 const reqToken = jwt . sign ( loginRequest . phrase , cert , { algorithm : 'RS256' } ) ;
24+ const reqTokenKey = jwt . sign ( loginRequest . phrase , { key : certKey , passphrase : 'test123' } , { algorithm : 'RS256' } ) ;
2225 const reqTokenBroken = jwt . sign ( loginRequest . phrase , certBroken , { algorithm : 'RS256' } ) ;
2326 const correctLogin = { user : { username : 'admin' } , token : reqToken , requestId : loginRequest . uid } ;
27+ const correctLoginWithPassphrase = {
28+ user : { username : 'admin' } ,
29+ token : reqTokenKey ,
30+ requestId : loginRequest . uid ,
31+ } ;
2432 const failedLogin = { user : { username : 'broken' } , token : reqTokenBroken , requestId : loginRequest . uid } ;
2533 const wrongUser = { username : 'wrong' , privateKeyName : 'i am broken' , password : '' } ;
2634
2735 // handle correct request
28- nock ( 'http://localhost:8080' ) . get ( '/login' ) . times ( 3 ) . reply ( 200 , loginRequest ) ;
29- const correctLoginSrv = nock ( 'http://localhost:8080' ) . post ( '/login' , correctLogin ) . reply ( 200 , { token} ) ;
30- const failedLoginSrv = nock ( 'http://localhost:8080' ) . post ( '/login' , failedLogin ) . reply ( 401 ) ;
36+ nock ( 'http://localhost:8080' )
37+ . get ( '/login' )
38+ . times ( 4 )
39+ . reply ( 200 , loginRequest ) ;
40+ const correctLoginSrv = nock ( 'http://localhost:8080' )
41+ . post ( '/login' , correctLogin )
42+ . reply ( 200 , { token} ) ;
43+ const correctLoginPassSrv = nock ( 'http://localhost:8080' )
44+ . post ( '/login' , correctLoginWithPassphrase )
45+ . reply ( 200 , { token} ) ;
46+ const failedLoginSrv = nock ( 'http://localhost:8080' )
47+ . post ( '/login' , failedLogin )
48+ . reply ( 401 ) ;
3149
3250 // test login
3351 tap . test ( 'Should login' , t => {
@@ -59,6 +77,38 @@ module.exports = () => {
5977 } ) ;
6078 } ) ;
6179
80+ // test login
81+ tap . test ( 'Should login using key with passphrase' , t => {
82+ // stup inquirer answers
83+ sinon
84+ . stub ( inquirer , 'prompt' )
85+ . callsFake ( ( ) => Promise . resolve ( Object . assign ( { } , correctLoginWithPassphrase . user , { password : 'test123' } ) ) ) ;
86+ // spy on console
87+ const consoleSpy = sinon . spy ( console , 'log' ) ;
88+ // execute login
89+ login ( { key : privateKeyWithPassphrase } ) . then ( ( ) => {
90+ // make sure log in was successful
91+ // check that server was called
92+ t . ok ( correctLoginPassSrv . isDone ( ) ) ;
93+ // first check console output
94+ t . deepEqual (
95+ consoleSpy . args ,
96+ [ [ 'Logging in to:' , 'http://localhost:8080' ] , [ 'Successfully logged in!' ] ] ,
97+ 'Correct log output'
98+ ) ;
99+ // then check config changes
100+ const configPath = path . join ( __dirname , 'fixtures' , 'cli.config.yml' ) ;
101+ const cfg = yaml . safeLoad ( fs . readFileSync ( configPath , 'utf8' ) ) ;
102+ t . equal ( cfg . token , token , 'Correct token' ) ;
103+ t . equal ( cfg . user . username , correctLoginWithPassphrase . user . username , 'Correct username' ) ;
104+ // restore inquirer
105+ inquirer . prompt . restore ( ) ;
106+ // restore console
107+ console . log . restore ( ) ;
108+ t . end ( ) ;
109+ } ) ;
110+ } ) ;
111+
62112 // test wrong credentials
63113 tap . test ( 'Should fail to login with broken private key' , t => {
64114 // stup inquirer answers
0 commit comments