2015-07-20 00:09:47 -07:00
'use strict' ;
/* global it:false */
/* global describe:false */
/* global before:false */
/* global after:false */
var async = require ( 'async' ) ,
config = require ( '../../config.js' ) ,
database = require ( '../../database.js' ) ,
expect = require ( 'expect.js' ) ,
2017-08-08 20:40:18 +02:00
http = require ( 'http' ) ,
2016-06-27 22:24:30 -05:00
locker = require ( '../../locker.js' ) ,
2015-07-20 00:09:47 -07:00
nock = require ( 'nock' ) ,
2015-12-31 11:55:01 +01:00
os = require ( 'os' ) ,
2015-12-15 09:12:52 -08:00
superagent = require ( 'superagent' ) ,
2015-07-20 00:09:47 -07:00
server = require ( '../../server.js' ) ,
2016-10-13 14:55:14 -07:00
settings = require ( '../../settings.js' ) ,
2017-08-26 13:59:45 -07:00
shell = require ( '../../shell.js' ) ,
tokendb = require ( '../../tokendb.js' ) ;
2015-07-20 00:09:47 -07:00
var SERVER _URL = 'http://localhost:' + config . get ( 'port' ) ;
2016-04-13 16:50:20 -07:00
var USERNAME = 'superadmin' , PASSWORD = 'Foobar?1337' , EMAIL = 'silly@me.com' ;
2015-07-20 00:09:47 -07:00
var token = null ; // authentication token
2017-08-26 13:59:45 -07:00
var USERNAME _1 = 'userTheFirst' , EMAIL _1 = 'taO@zen.mac' , userId _1 , token _1 ;
2015-07-20 00:09:47 -07:00
function setup ( done ) {
2015-08-04 16:59:35 +02:00
nock . cleanAll ( ) ;
2016-01-29 14:17:31 +01:00
config . _reset ( ) ;
2017-11-27 15:30:55 -08:00
config . setFqdn ( 'example-cloudron-test.com' ) ;
2018-01-10 20:40:15 -08:00
config . setAdminFqdn ( 'my.example-cloudron-test.com' ) ;
2016-10-13 14:55:14 -07:00
2017-11-27 15:30:55 -08:00
async . series ( [
server . start . bind ( server ) ,
database . _clear ,
settings . setBackupConfig . bind ( null , { provider : 'filesystem' , backupFolder : '/tmp' , format : 'tgz' } )
] , done ) ;
2015-07-20 00:09:47 -07:00
}
function cleanup ( done ) {
database . _clear ( function ( error ) {
expect ( error ) . to . not . be . ok ( ) ;
2016-01-29 14:17:31 +01:00
config . _reset ( ) ;
2015-07-20 00:09:47 -07:00
server . stop ( done ) ;
} ) ;
}
var gSudoOriginal = null ;
function injectShellMock ( ) {
gSudoOriginal = shell . sudo ;
shell . sudo = function ( tag , options , callback ) { callback ( null ) ; } ;
}
function restoreShellMock ( ) {
shell . sudo = gSudoOriginal ;
}
describe ( 'Cloudron' , function ( ) {
describe ( 'activate' , function ( ) {
before ( setup ) ;
after ( cleanup ) ;
it ( 'fails due to missing setupToken' , function ( done ) {
2015-12-15 09:12:52 -08:00
superagent . post ( SERVER _URL + '/api/v1/cloudron/activate' )
2017-09-17 18:50:26 -07:00
. send ( { username : '' , password : 'somepassword' , email : 'admin@foo.bar' } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 400 ) ;
done ( ) ;
} ) ;
2015-07-20 00:09:47 -07:00
} ) ;
2015-12-29 16:07:04 +01:00
it ( 'fails due to internal server error on appstore side' , function ( done ) {
var scope = nock ( config . apiServerOrigin ( ) ) . get ( '/api/v1/boxes/' + config . fqdn ( ) + '/setup/verify?setupToken=somesetuptoken' ) . reply ( 500 , { message : 'this is wrong' } ) ;
superagent . post ( SERVER _URL + '/api/v1/cloudron/activate' )
2017-09-17 18:50:26 -07:00
. query ( { setupToken : 'somesetuptoken' } )
. send ( { username : 'someuser' , password : 'strong#A3asdf' , email : 'admin@foo.bar' } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 500 ) ;
expect ( scope . isDone ( ) ) . to . be . ok ( ) ;
done ( ) ;
} ) ;
2015-12-29 16:07:04 +01:00
} ) ;
2015-07-20 00:09:47 -07:00
it ( 'fails due to empty username' , function ( done ) {
var scope = nock ( config . apiServerOrigin ( ) ) . get ( '/api/v1/boxes/' + config . fqdn ( ) + '/setup/verify?setupToken=somesetuptoken' ) . reply ( 200 , { } ) ;
2015-12-15 09:12:52 -08:00
superagent . post ( SERVER _URL + '/api/v1/cloudron/activate' )
2017-09-17 18:50:26 -07:00
. query ( { setupToken : 'somesetuptoken' } )
. send ( { username : '' , password : 'ADSFsdf$%436' , email : 'admin@foo.bar' } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 400 ) ;
expect ( scope . isDone ( ) ) . to . be . ok ( ) ;
done ( ) ;
} ) ;
2015-07-20 00:09:47 -07:00
} ) ;
it ( 'fails due to empty password' , function ( done ) {
var scope = nock ( config . apiServerOrigin ( ) ) . get ( '/api/v1/boxes/' + config . fqdn ( ) + '/setup/verify?setupToken=somesetuptoken' ) . reply ( 200 , { } ) ;
2015-12-15 09:12:52 -08:00
superagent . post ( SERVER _URL + '/api/v1/cloudron/activate' )
2017-09-17 18:50:26 -07:00
. query ( { setupToken : 'somesetuptoken' } )
. send ( { username : 'someuser' , password : '' , email : 'admin@foo.bar' } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 400 ) ;
expect ( scope . isDone ( ) ) . to . be . ok ( ) ;
done ( ) ;
} ) ;
2015-07-20 00:09:47 -07:00
} ) ;
it ( 'fails due to empty email' , function ( done ) {
var scope = nock ( config . apiServerOrigin ( ) ) . get ( '/api/v1/boxes/' + config . fqdn ( ) + '/setup/verify?setupToken=somesetuptoken' ) . reply ( 200 , { } ) ;
2015-12-15 09:12:52 -08:00
superagent . post ( SERVER _URL + '/api/v1/cloudron/activate' )
2017-09-17 18:50:26 -07:00
. query ( { setupToken : 'somesetuptoken' } )
. send ( { username : 'someuser' , password : 'ADSF#asd546' , email : '' } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 400 ) ;
expect ( scope . isDone ( ) ) . to . be . ok ( ) ;
done ( ) ;
} ) ;
2015-07-20 00:09:47 -07:00
} ) ;
2016-01-20 16:14:44 +01:00
it ( 'fails due to wrong displayName type' , function ( done ) {
2015-07-20 00:09:47 -07:00
var scope = nock ( config . apiServerOrigin ( ) ) . get ( '/api/v1/boxes/' + config . fqdn ( ) + '/setup/verify?setupToken=somesetuptoken' ) . reply ( 200 , { } ) ;
2015-12-15 09:12:52 -08:00
superagent . post ( SERVER _URL + '/api/v1/cloudron/activate' )
2017-09-17 18:50:26 -07:00
. query ( { setupToken : 'somesetuptoken' } )
. send ( { username : 'someuser' , password : 'ADSF?#asd546' , email : 'admin@foo.bar' , displayName : 1234 } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 400 ) ;
expect ( scope . isDone ( ) ) . to . be . ok ( ) ;
done ( ) ;
} ) ;
2015-07-20 00:09:47 -07:00
} ) ;
it ( 'fails due to invalid email' , function ( done ) {
var scope = nock ( config . apiServerOrigin ( ) ) . get ( '/api/v1/boxes/' + config . fqdn ( ) + '/setup/verify?setupToken=somesetuptoken' ) . reply ( 200 , { } ) ;
2015-12-15 09:12:52 -08:00
superagent . post ( SERVER _URL + '/api/v1/cloudron/activate' )
2017-09-17 18:50:26 -07:00
. query ( { setupToken : 'somesetuptoken' } )
. send ( { username : 'someuser' , password : 'ADSF#asd546' , email : 'invalidemail' } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 400 ) ;
expect ( scope . isDone ( ) ) . to . be . ok ( ) ;
done ( ) ;
} ) ;
2015-07-20 00:09:47 -07:00
} ) ;
it ( 'succeeds' , function ( done ) {
var scope1 = nock ( config . apiServerOrigin ( ) ) . get ( '/api/v1/boxes/' + config . fqdn ( ) + '/setup/verify?setupToken=somesetuptoken' ) . reply ( 200 , { } ) ;
var scope2 = nock ( config . apiServerOrigin ( ) ) . post ( '/api/v1/boxes/' + config . fqdn ( ) + '/setup/done?setupToken=somesetuptoken' ) . reply ( 201 , { } ) ;
2015-12-15 09:12:52 -08:00
superagent . post ( SERVER _URL + '/api/v1/cloudron/activate' )
2017-09-17 18:50:26 -07:00
. query ( { setupToken : 'somesetuptoken' } )
. send ( { username : 'someuser' , password : 'ADSF#asd546' , email : 'admin@foo.bar' , displayName : 'tester' } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 201 ) ;
expect ( scope1 . isDone ( ) ) . to . be . ok ( ) ;
expect ( scope2 . isDone ( ) ) . to . be . ok ( ) ;
done ( ) ;
} ) ;
2015-07-20 00:09:47 -07:00
} ) ;
it ( 'fails the second time' , function ( done ) {
var scope = nock ( config . apiServerOrigin ( ) ) . get ( '/api/v1/boxes/' + config . fqdn ( ) + '/setup/verify?setupToken=somesetuptoken' ) . reply ( 200 , { } ) ;
2015-12-15 09:12:52 -08:00
superagent . post ( SERVER _URL + '/api/v1/cloudron/activate' )
2017-09-17 18:50:26 -07:00
. query ( { setupToken : 'somesetuptoken' } )
. send ( { username : 'someuser' , password : 'ADSF#asd546' , email : 'admin@foo.bar' } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 409 ) ;
expect ( scope . isDone ( ) ) . to . be . ok ( ) ;
done ( ) ;
} ) ;
2015-07-20 00:09:47 -07:00
} ) ;
} ) ;
describe ( 'get config' , function ( ) {
before ( function ( done ) {
async . series ( [
setup ,
function ( callback ) {
var scope1 = nock ( config . apiServerOrigin ( ) ) . get ( '/api/v1/boxes/' + config . fqdn ( ) + '/setup/verify?setupToken=somesetuptoken' ) . reply ( 200 , { } ) ;
var scope2 = nock ( config . apiServerOrigin ( ) ) . post ( '/api/v1/boxes/' + config . fqdn ( ) + '/setup/done?setupToken=somesetuptoken' ) . reply ( 201 , { } ) ;
2015-12-15 09:12:52 -08:00
superagent . post ( SERVER _URL + '/api/v1/cloudron/activate' )
2017-09-17 18:50:26 -07:00
. query ( { setupToken : 'somesetuptoken' } )
. send ( { username : USERNAME , password : PASSWORD , email : EMAIL } )
. end ( function ( error , result ) {
expect ( result ) . to . be . ok ( ) ;
expect ( scope1 . isDone ( ) ) . to . be . ok ( ) ;
expect ( scope2 . isDone ( ) ) . to . be . ok ( ) ;
// stash token for further use
token = result . body . token ;
callback ( ) ;
} ) ;
2015-07-20 00:09:47 -07:00
} ,
2017-08-26 13:59:45 -07:00
function ( callback ) {
superagent . post ( SERVER _URL + '/api/v1/users' )
2017-09-17 18:50:26 -07:00
. query ( { access _token : token } )
. send ( { username : USERNAME _1 , email : EMAIL _1 , invite : false } )
. end ( function ( error , result ) {
expect ( result ) . to . be . ok ( ) ;
expect ( result . statusCode ) . to . eql ( 201 ) ;
token _1 = tokendb . generateToken ( ) ;
userId _1 = result . body . id ;
// HACK to get a token for second user (passwords are generated and the user should have gotten a password setup link...)
tokendb . add ( token _1 , userId _1 , 'test-client-id' , Date . now ( ) + 100000 , '*' , callback ) ;
} ) ;
2017-08-26 13:59:45 -07:00
}
2015-07-20 00:09:47 -07:00
] , done ) ;
} ) ;
after ( cleanup ) ;
it ( 'cannot get without token' , function ( done ) {
2015-12-15 09:12:52 -08:00
superagent . get ( SERVER _URL + '/api/v1/cloudron/config' )
2017-09-17 18:50:26 -07:00
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 401 ) ;
done ( ) ;
} ) ;
2015-07-20 00:09:47 -07:00
} ) ;
it ( 'succeeds without appstore' , function ( done ) {
2015-12-15 09:12:52 -08:00
superagent . get ( SERVER _URL + '/api/v1/cloudron/config' )
2017-09-17 18:50:26 -07:00
. query ( { access _token : token } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 200 ) ;
expect ( result . body . apiServerOrigin ) . to . eql ( 'http://localhost:6060' ) ;
expect ( result . body . webServerOrigin ) . to . eql ( null ) ;
expect ( result . body . fqdn ) . to . eql ( config . fqdn ( ) ) ;
2018-01-10 20:40:15 -08:00
expect ( result . body . adminFqdn ) . to . eql ( config . adminFqdn ( ) ) ;
2017-09-17 18:50:26 -07:00
expect ( result . body . progress ) . to . be . an ( 'object' ) ;
expect ( result . body . update ) . to . be . an ( 'object' ) ;
expect ( result . body . version ) . to . eql ( config . version ( ) ) ;
expect ( result . body . size ) . to . eql ( null ) ;
expect ( result . body . region ) . to . eql ( null ) ;
expect ( result . body . memory ) . to . eql ( os . totalmem ( ) ) ;
expect ( result . body . cloudronName ) . to . be . a ( 'string' ) ;
done ( ) ;
} ) ;
2015-07-20 00:09:47 -07:00
} ) ;
2017-08-26 13:59:45 -07:00
it ( 'succeeds (admin)' , function ( done ) {
2016-04-10 17:15:19 -07:00
var scope = nock ( config . apiServerOrigin ( ) )
2017-11-27 15:30:55 -08:00
. get ( ` /api/v1/boxes/ ${ config . fqdn ( ) } ?token= ${ config . token ( ) } ` )
2017-09-17 18:50:26 -07:00
. reply ( 200 , { box : { region : 'sfo' , size : '1gb' } , user : { } } ) ;
2015-07-20 00:09:47 -07:00
2015-12-15 09:12:52 -08:00
superagent . get ( SERVER _URL + '/api/v1/cloudron/config' )
2017-09-17 18:50:26 -07:00
. query ( { access _token : token } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 200 ) ;
expect ( result . body . apiServerOrigin ) . to . eql ( 'http://localhost:6060' ) ;
expect ( result . body . webServerOrigin ) . to . eql ( null ) ;
expect ( result . body . fqdn ) . to . eql ( config . fqdn ( ) ) ;
2018-01-10 20:40:15 -08:00
expect ( result . body . adminFqdn ) . to . eql ( config . adminFqdn ( ) ) ;
2017-09-17 18:50:26 -07:00
expect ( result . body . progress ) . to . be . an ( 'object' ) ;
expect ( result . body . update ) . to . be . an ( 'object' ) ;
expect ( result . body . version ) . to . eql ( config . version ( ) ) ;
expect ( result . body . size ) . to . eql ( '1gb' ) ;
expect ( result . body . region ) . to . eql ( 'sfo' ) ;
expect ( result . body . memory ) . to . eql ( os . totalmem ( ) ) ;
expect ( result . body . cloudronName ) . to . be . a ( 'string' ) ;
expect ( result . body . provider ) . to . be . a ( 'string' ) ;
expect ( scope . isDone ( ) ) . to . be . ok ( ) ;
done ( ) ;
} ) ;
2015-07-20 00:09:47 -07:00
} ) ;
2017-08-26 13:59:45 -07:00
it ( 'succeeds (non-admin)' , function ( done ) {
superagent . get ( SERVER _URL + '/api/v1/cloudron/config' )
2017-09-17 18:50:26 -07:00
. query ( { access _token : token _1 } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 200 ) ;
2017-08-26 13:59:45 -07:00
2017-09-17 18:50:26 -07:00
expect ( result . body . apiServerOrigin ) . to . eql ( 'http://localhost:6060' ) ;
expect ( result . body . webServerOrigin ) . to . eql ( null ) ;
expect ( result . body . fqdn ) . to . eql ( config . fqdn ( ) ) ;
2018-01-10 20:40:15 -08:00
expect ( result . body . adminFqdn ) . to . eql ( config . adminFqdn ( ) ) ;
2017-09-17 18:50:26 -07:00
expect ( result . body . progress ) . to . be . an ( 'object' ) ;
expect ( result . body . version ) . to . eql ( config . version ( ) ) ;
expect ( result . body . cloudronName ) . to . be . a ( 'string' ) ;
expect ( result . body . provider ) . to . be . a ( 'string' ) ;
2017-08-26 13:59:45 -07:00
2017-09-17 18:50:26 -07:00
expect ( result . body . update ) . to . be ( undefined ) ;
expect ( result . body . size ) . to . be ( undefined ) ;
expect ( result . body . region ) . to . be ( undefined ) ;
expect ( result . body . memory ) . to . be ( undefined ) ;
2017-08-26 13:59:45 -07:00
2017-09-17 18:50:26 -07:00
done ( ) ;
} ) ;
2017-08-26 13:59:45 -07:00
} ) ;
2015-07-20 00:09:47 -07:00
} ) ;
2017-04-24 15:37:03 -07:00
xdescribe ( 'migrate' , function ( ) {
2016-06-27 22:24:30 -05:00
before ( function ( done ) {
async . series ( [
setup ,
function ( callback ) {
var scope1 = nock ( config . apiServerOrigin ( ) ) . get ( '/api/v1/boxes/' + config . fqdn ( ) + '/setup/verify?setupToken=somesetuptoken' ) . reply ( 200 , { } ) ;
var scope2 = nock ( config . apiServerOrigin ( ) ) . post ( '/api/v1/boxes/' + config . fqdn ( ) + '/setup/done?setupToken=somesetuptoken' ) . reply ( 201 , { } ) ;
superagent . post ( SERVER _URL + '/api/v1/cloudron/activate' )
2017-09-17 18:50:26 -07:00
. query ( { setupToken : 'somesetuptoken' } )
. send ( { username : USERNAME , password : PASSWORD , email : EMAIL } )
. end ( function ( error , result ) {
expect ( result ) . to . be . ok ( ) ;
expect ( scope1 . isDone ( ) ) . to . be . ok ( ) ;
expect ( scope2 . isDone ( ) ) . to . be . ok ( ) ;
// stash token for further use
token = result . body . token ;
callback ( ) ;
} ) ;
2016-06-27 22:24:30 -05:00
}
] , done ) ;
} ) ;
after ( function ( done ) {
locker . unlock ( locker . _operation ) ; // migrate never unlocks
cleanup ( done ) ;
} ) ;
it ( 'fails without token' , function ( done ) {
superagent . post ( SERVER _URL + '/api/v1/cloudron/migrate' )
2017-09-17 18:50:26 -07:00
. send ( { size : 'small' , region : 'sfo' } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 401 ) ;
done ( ) ;
} ) ;
2016-06-27 22:24:30 -05:00
} ) ;
it ( 'fails without password' , function ( done ) {
superagent . post ( SERVER _URL + '/api/v1/cloudron/migrate' )
2017-09-17 18:50:26 -07:00
. send ( { size : 'small' , region : 'sfo' } )
. query ( { access _token : token } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 400 ) ;
done ( ) ;
} ) ;
2016-06-27 22:24:30 -05:00
} ) ;
2016-07-14 16:17:09 +02:00
it ( 'succeeds without size' , function ( done ) {
2016-06-27 22:24:30 -05:00
superagent . post ( SERVER _URL + '/api/v1/cloudron/migrate' )
2017-09-17 18:50:26 -07:00
. send ( { region : 'sfo' , password : PASSWORD } )
. query ( { access _token : token } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 202 ) ;
done ( ) ;
} ) ;
2016-06-27 22:24:30 -05:00
} ) ;
it ( 'fails with wrong size type' , function ( done ) {
superagent . post ( SERVER _URL + '/api/v1/cloudron/migrate' )
2017-09-17 18:50:26 -07:00
. send ( { size : 4 , region : 'sfo' , password : PASSWORD } )
. query ( { access _token : token } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 400 ) ;
done ( ) ;
} ) ;
2016-06-27 22:24:30 -05:00
} ) ;
2016-07-14 16:17:09 +02:00
it ( 'succeeds without region' , function ( done ) {
2016-06-27 22:24:30 -05:00
superagent . post ( SERVER _URL + '/api/v1/cloudron/migrate' )
2017-09-17 18:50:26 -07:00
. send ( { size : 'small' , password : PASSWORD } )
. query ( { access _token : token } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 202 ) ;
done ( ) ;
} ) ;
2016-06-27 22:24:30 -05:00
} ) ;
it ( 'fails with wrong region type' , function ( done ) {
superagent . post ( SERVER _URL + '/api/v1/cloudron/migrate' )
2017-09-17 18:50:26 -07:00
. send ( { size : 'small' , region : 4 , password : PASSWORD } )
. query ( { access _token : token } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 400 ) ;
done ( ) ;
} ) ;
2016-06-27 22:24:30 -05:00
} ) ;
it ( 'fails when in wrong state' , function ( done ) {
var scope2 = nock ( config . apiServerOrigin ( ) )
2017-09-17 18:50:26 -07:00
. post ( '/api/v1/boxes/' + config . fqdn ( ) + '/awscredentials?token=BACKUP_TOKEN' )
. reply ( 201 , { credentials : { AccessKeyId : 'accessKeyId' , SecretAccessKey : 'secretAccessKey' , SessionToken : 'sessionToken' } } ) ;
2016-06-27 22:24:30 -05:00
var scope3 = nock ( config . apiServerOrigin ( ) )
2017-09-17 18:50:26 -07:00
. post ( '/api/v1/boxes/' + config . fqdn ( ) + '/backupDone?token=APPSTORE_TOKEN' , function ( body ) {
return body . boxVersion && body . restoreKey && ! body . appId && ! body . appVersion && body . appBackupIds . length === 0 ;
} )
. reply ( 200 , { id : 'someid' } ) ;
2016-06-27 22:24:30 -05:00
var scope1 = nock ( config . apiServerOrigin ( ) )
. post ( '/api/v1/boxes/' + config . fqdn ( ) + '/migrate?token=APPSTORE_TOKEN' , function ( body ) {
return body . size && body . region && body . restoreKey ;
} ) . reply ( 409 , { } ) ;
injectShellMock ( ) ;
superagent . post ( SERVER _URL + '/api/v1/cloudron/migrate' )
2017-09-17 18:50:26 -07:00
. send ( { size : 'small' , region : 'sfo' , password : PASSWORD } )
. query ( { access _token : token } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 202 ) ;
function checkAppstoreServerCalled ( ) {
if ( scope1 . isDone ( ) && scope2 . isDone ( ) && scope3 . isDone ( ) ) {
restoreShellMock ( ) ;
return done ( ) ;
}
setTimeout ( checkAppstoreServerCalled , 100 ) ;
2016-06-27 22:24:30 -05:00
}
2017-09-17 18:50:26 -07:00
checkAppstoreServerCalled ( ) ;
} ) ;
2016-06-27 22:24:30 -05:00
} ) ;
it ( 'succeeds' , function ( done ) {
var scope1 = nock ( config . apiServerOrigin ( ) ) . post ( '/api/v1/boxes/' + config . fqdn ( ) + '/migrate?token=APPSTORE_TOKEN' , function ( body ) {
return body . size && body . region && body . restoreKey ;
} ) . reply ( 202 , { } ) ;
var scope2 = nock ( config . apiServerOrigin ( ) )
2017-09-17 18:50:26 -07:00
. post ( '/api/v1/boxes/' + config . fqdn ( ) + '/backupDone?token=APPSTORE_TOKEN' , function ( body ) {
return body . boxVersion && body . restoreKey && ! body . appId && ! body . appVersion && body . appBackupIds . length === 0 ;
} )
. reply ( 200 , { id : 'someid' } ) ;
2016-06-27 22:24:30 -05:00
var scope3 = nock ( config . apiServerOrigin ( ) )
2017-09-17 18:50:26 -07:00
. post ( '/api/v1/boxes/' + config . fqdn ( ) + '/awscredentials?token=BACKUP_TOKEN' )
. reply ( 201 , { credentials : { AccessKeyId : 'accessKeyId' , SecretAccessKey : 'secretAccessKey' , SessionToken : 'sessionToken' } } ) ;
2016-06-27 22:24:30 -05:00
injectShellMock ( ) ;
superagent . post ( SERVER _URL + '/api/v1/cloudron/migrate' )
2017-09-17 18:50:26 -07:00
. send ( { size : 'small' , region : 'sfo' , password : PASSWORD } )
. query ( { access _token : token } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 202 ) ;
function checkAppstoreServerCalled ( ) {
if ( scope1 . isDone ( ) && scope2 . isDone ( ) && scope3 . isDone ( ) ) {
restoreShellMock ( ) ;
return done ( ) ;
}
setTimeout ( checkAppstoreServerCalled , 100 ) ;
2016-06-27 22:24:30 -05:00
}
2017-09-17 18:50:26 -07:00
checkAppstoreServerCalled ( ) ;
} ) ;
2016-06-27 22:24:30 -05:00
} ) ;
} ) ;
2015-08-04 16:59:35 +02:00
describe ( 'feedback' , function ( ) {
before ( function ( done ) {
async . series ( [
setup ,
function ( callback ) {
var scope1 = nock ( config . apiServerOrigin ( ) ) . get ( '/api/v1/boxes/' + config . fqdn ( ) + '/setup/verify?setupToken=somesetuptoken' ) . reply ( 200 , { } ) ;
var scope2 = nock ( config . apiServerOrigin ( ) ) . post ( '/api/v1/boxes/' + config . fqdn ( ) + '/setup/done?setupToken=somesetuptoken' ) . reply ( 201 , { } ) ;
2015-12-15 09:12:52 -08:00
superagent . post ( SERVER _URL + '/api/v1/cloudron/activate' )
2017-09-17 18:50:26 -07:00
. query ( { setupToken : 'somesetuptoken' } )
. send ( { username : USERNAME , password : PASSWORD , email : EMAIL } )
. end ( function ( error , result ) {
expect ( result ) . to . be . ok ( ) ;
expect ( scope1 . isDone ( ) ) . to . be . ok ( ) ;
expect ( scope2 . isDone ( ) ) . to . be . ok ( ) ;
// stash token for further use
token = result . body . token ;
callback ( ) ;
} ) ;
2015-08-04 16:59:35 +02:00
} ,
] , done ) ;
} ) ;
after ( cleanup ) ;
it ( 'fails without token' , function ( done ) {
2017-04-18 14:49:28 -07:00
superagent . post ( SERVER _URL + '/api/v1/feedback' )
2017-09-17 18:50:26 -07:00
. send ( { type : 'ticket' , subject : 'some subject' , description : 'some description' } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 401 ) ;
done ( ) ;
} ) ;
2015-08-04 16:59:35 +02:00
} ) ;
it ( 'fails without type' , function ( done ) {
2017-04-18 14:49:28 -07:00
superagent . post ( SERVER _URL + '/api/v1/feedback' )
2017-09-17 18:50:26 -07:00
. send ( { subject : 'some subject' , description : 'some description' } )
. query ( { access _token : token } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 400 ) ;
done ( ) ;
} ) ;
2015-08-04 16:59:35 +02:00
} ) ;
it ( 'fails with empty type' , function ( done ) {
2017-04-18 14:49:28 -07:00
superagent . post ( SERVER _URL + '/api/v1/feedback' )
2017-09-17 18:50:26 -07:00
. send ( { type : '' , subject : 'some subject' , description : 'some description' } )
. query ( { access _token : token } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 400 ) ;
done ( ) ;
} ) ;
2015-08-04 16:59:35 +02:00
} ) ;
it ( 'fails with unknown type' , function ( done ) {
2017-04-18 14:49:28 -07:00
superagent . post ( SERVER _URL + '/api/v1/feedback' )
2017-09-17 18:50:26 -07:00
. send ( { type : 'foobar' , subject : 'some subject' , description : 'some description' } )
. query ( { access _token : token } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 400 ) ;
done ( ) ;
} ) ;
2015-08-04 16:59:35 +02:00
} ) ;
2017-11-14 20:34:25 -08:00
it ( 'fails without description' , function ( done ) {
2017-04-18 14:49:28 -07:00
superagent . post ( SERVER _URL + '/api/v1/feedback' )
2017-11-14 20:34:25 -08:00
. send ( { type : 'ticket' , subject : 'some subject' } )
2017-09-17 18:50:26 -07:00
. query ( { access _token : token } )
. end ( function ( error , result ) {
2017-11-14 20:34:25 -08:00
expect ( result . statusCode ) . to . equal ( 400 ) ;
2017-09-17 18:50:26 -07:00
done ( ) ;
} ) ;
2015-08-06 17:34:40 +02:00
} ) ;
2017-11-14 20:34:25 -08:00
it ( 'fails with empty subject' , function ( done ) {
2017-04-18 14:49:28 -07:00
superagent . post ( SERVER _URL + '/api/v1/feedback' )
2017-11-14 20:34:25 -08:00
. send ( { type : 'ticket' , subject : '' , description : 'some description' } )
2017-09-17 18:50:26 -07:00
. query ( { access _token : token } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 400 ) ;
done ( ) ;
} ) ;
2015-08-04 16:59:35 +02:00
} ) ;
2017-11-14 20:34:25 -08:00
it ( 'fails with empty description' , function ( done ) {
2017-04-18 14:49:28 -07:00
superagent . post ( SERVER _URL + '/api/v1/feedback' )
2017-11-14 20:34:25 -08:00
. send ( { type : 'ticket' , subject : 'some subject' , description : '' } )
2017-09-17 18:50:26 -07:00
. query ( { access _token : token } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 400 ) ;
done ( ) ;
} ) ;
2015-08-04 16:59:35 +02:00
} ) ;
2017-11-14 20:34:25 -08:00
it ( 'fails without subject' , function ( done ) {
2017-04-18 14:49:28 -07:00
superagent . post ( SERVER _URL + '/api/v1/feedback' )
2017-11-14 20:34:25 -08:00
. send ( { type : 'ticket' , description : 'some description' } )
2017-09-17 18:50:26 -07:00
. query ( { access _token : token } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 400 ) ;
done ( ) ;
} ) ;
2015-08-04 16:59:35 +02:00
} ) ;
2017-11-14 20:34:25 -08:00
it ( 'succeeds with ticket type' , function ( done ) {
var scope1 = nock ( config . apiServerOrigin ( ) ) . post ( '/api/v1/exchangeBoxTokenWithUserToken?token=APPSTORE_TOKEN' ) . reply ( 201 , { userId : 'USER_ID' , cloudronId : 'CLOUDRON_ID' , token : 'ACCESS_TOKEN' } ) ;
var scope2 = nock ( config . apiServerOrigin ( ) )
. filteringRequestBody ( function ( /* unusedBody */ ) { return '' ; } ) // strip out body
. post ( '/api/v1/users/USER_ID/cloudrons/CLOUDRON_ID/feedback?accessToken=ACCESS_TOKEN' )
. reply ( 201 , { } ) ;
2017-04-18 14:49:28 -07:00
superagent . post ( SERVER _URL + '/api/v1/feedback' )
2017-11-14 20:34:25 -08:00
. send ( { type : 'ticket' , subject : 'some subject' , description : 'some description' } )
2017-09-17 18:50:26 -07:00
. query ( { access _token : token } )
. end ( function ( error , result ) {
expect ( result . statusCode ) . to . equal ( 201 ) ;
2017-11-14 20:34:25 -08:00
expect ( scope1 . isDone ( ) ) . to . be . ok ( ) ;
expect ( scope2 . isDone ( ) ) . to . be . ok ( ) ;
2017-09-17 18:50:26 -07:00
done ( ) ;
} ) ;
2015-08-04 16:59:35 +02:00
} ) ;
2017-11-14 20:34:25 -08:00
it ( 'succeeds with app type' , function ( done ) {
var scope1 = nock ( config . apiServerOrigin ( ) ) . post ( '/api/v1/exchangeBoxTokenWithUserToken?token=APPSTORE_TOKEN' ) . reply ( 201 , { userId : 'USER_ID' , cloudronId : 'CLOUDRON_ID' , token : 'ACCESS_TOKEN' } ) ;
var scope2 = nock ( config . apiServerOrigin ( ) )
. filteringRequestBody ( function ( /* unusedBody */ ) { return '' ; } ) // strip out body
. post ( '/api/v1/users/USER_ID/cloudrons/CLOUDRON_ID/feedback?accessToken=ACCESS_TOKEN' )
. reply ( 201 , { } ) ;
2017-04-18 14:49:28 -07:00
superagent . post ( SERVER _URL + '/api/v1/feedback' )
2017-11-14 20:34:25 -08:00
. send ( { type : 'app_missing' , subject : 'some subject' , description : 'some description' } )
2017-09-17 18:50:26 -07:00
. query ( { access _token : token } )
. end ( function ( error , result ) {
2017-11-14 20:34:25 -08:00
expect ( result . statusCode ) . to . equal ( 201 ) ;
expect ( scope1 . isDone ( ) ) . to . be . ok ( ) ;
expect ( scope2 . isDone ( ) ) . to . be . ok ( ) ;
2017-09-17 18:50:26 -07:00
done ( ) ;
} ) ;
2015-08-04 16:59:35 +02:00
} ) ;
} ) ;
2017-08-08 20:40:18 +02:00
describe ( 'logs' , function ( ) {
before ( function ( done ) {
async . series ( [
setup ,
function ( callback ) {
var scope1 = nock ( config . apiServerOrigin ( ) ) . get ( '/api/v1/boxes/' + config . fqdn ( ) + '/setup/verify?setupToken=somesetuptoken' ) . reply ( 200 , { } ) ;
var scope2 = nock ( config . apiServerOrigin ( ) ) . post ( '/api/v1/boxes/' + config . fqdn ( ) + '/setup/done?setupToken=somesetuptoken' ) . reply ( 201 , { } ) ;
superagent . post ( SERVER _URL + '/api/v1/cloudron/activate' )
2017-09-17 18:50:26 -07:00
. query ( { setupToken : 'somesetuptoken' } )
. send ( { username : USERNAME , password : PASSWORD , email : EMAIL } )
. end ( function ( error , result ) {
expect ( result ) . to . be . ok ( ) ;
expect ( scope1 . isDone ( ) ) . to . be . ok ( ) ;
expect ( scope2 . isDone ( ) ) . to . be . ok ( ) ;
// stash token for further use
token = result . body . token ;
callback ( ) ;
} ) ;
2017-08-08 20:40:18 +02:00
} ,
] , done ) ;
} ) ;
after ( cleanup ) ;
it ( 'logStream - requires event-stream accept header' , function ( done ) {
superagent . get ( SERVER _URL + '/api/v1/cloudron/logstream' )
. query ( { access _token : token , fromLine : 0 } )
. end ( function ( err , res ) {
2017-09-17 18:50:26 -07:00
expect ( res . statusCode ) . to . be ( 400 ) ;
done ( ) ;
} ) ;
2017-08-08 20:40:18 +02:00
} ) ;
it ( 'logStream - stream logs' , function ( done ) {
var options = {
port : config . get ( 'port' ) , host : 'localhost' , path : '/api/v1/cloudron/logstream?units=all&lines=10&access_token=' + token ,
headers : { 'Accept' : 'text/event-stream' , 'Connection' : 'keep-alive' }
} ;
// superagent doesn't work. maybe https://github.com/visionmedia/superagent/issues/420
var req = http . get ( options , function ( res ) {
var data = '' ;
res . on ( 'data' , function ( d ) { data += d . toString ( 'utf8' ) ; } ) ;
setTimeout ( function checkData ( ) {
var dataMessageFound = false ;
expect ( data . length ) . to . not . be ( 0 ) ;
data . split ( '\n' ) . forEach ( function ( line ) {
if ( line . indexOf ( 'id: ' ) === 0 ) {
expect ( parseInt ( line . substr ( 'id: ' . length ) , 10 ) ) . to . be . a ( 'number' ) ;
} else if ( line . indexOf ( 'data: ' ) === 0 ) {
2017-09-13 23:01:04 -07:00
var message = JSON . parse ( line . slice ( 'data: ' . length ) ) . message ;
if ( Array . isArray ( message ) || typeof message === 'string' ) dataMessageFound = true ;
2017-08-08 20:40:18 +02:00
}
} ) ;
expect ( dataMessageFound ) . to . be . ok ( ) ;
req . abort ( ) ;
done ( ) ;
} , 1000 ) ;
res . on ( 'error' , done ) ;
} ) ;
req . on ( 'error' , done ) ;
} ) ;
} ) ;
2015-07-20 00:09:47 -07:00
} ) ;