2018-04-05 21:36:33 +00:00
import { moduleForComponent , test } from 'ember-qunit' ;
2018-07-05 18:28:12 +00:00
import { supportedAuthBackends } from 'vault/helpers/supported-auth-backends' ;
2018-04-05 21:36:33 +00:00
import Ember from 'ember' ;
import wait from 'ember-test-helpers/wait' ;
import hbs from 'htmlbars-inline-precompile' ;
2018-07-05 18:28:12 +00:00
import sinon from 'sinon' ;
2018-04-05 21:36:33 +00:00
import Pretender from 'pretender' ;
import { create } from 'ember-cli-page-object' ;
import authForm from '../../pages/components/auth-form' ;
const component = create ( authForm ) ;
2018-07-05 18:28:12 +00:00
const BACKENDS = supportedAuthBackends ( ) ;
2018-04-05 21:36:33 +00:00
const authService = Ember . Service . extend ( {
authenticate ( ) {
return Ember . $ . getJSON ( 'http://localhost:2000' ) ;
} ,
2018-08-16 17:48:24 +00:00
setLastFetch ( ) { } ,
2018-04-05 21:36:33 +00:00
} ) ;
2018-07-05 18:28:12 +00:00
const workingAuthService = Ember . Service . extend ( {
authenticate ( ) {
return Ember . RSVP . resolve ( { } ) ;
} ,
setLastFetch ( ) { } ,
} ) ;
const routerService = Ember . Service . extend ( {
transitionTo ( ) {
return Ember . RSVP . resolve ( ) ;
} ,
replaceWith ( ) {
return Ember . RSVP . resolve ( ) ;
} ,
} ) ;
2018-04-05 21:36:33 +00:00
moduleForComponent ( 'auth-form' , 'Integration | Component | auth form' , {
integration : true ,
beforeEach ( ) {
Ember . getOwner ( this ) . lookup ( 'service:csp-event' ) . attach ( ) ;
component . setContext ( this ) ;
2018-07-05 18:28:12 +00:00
this . register ( 'service:router' , routerService ) ;
this . inject . service ( 'router' ) ;
2018-04-05 21:36:33 +00:00
} ,
afterEach ( ) {
Ember . getOwner ( this ) . lookup ( 'service:csp-event' ) . remove ( ) ;
component . removeContext ( ) ;
} ,
} ) ;
const CSP _ERR _TEXT = ` Error This is a standby Vault node but can't communicate with the active node via request forwarding. Sign in at the active node to use the Vault UI. ` ;
test ( 'it renders error on CSP violation' , function ( assert ) {
this . register ( 'service:auth' , authService ) ;
this . inject . service ( 'auth' ) ;
this . set ( 'cluster' , Ember . Object . create ( { standby : true } ) ) ;
2018-07-05 18:28:12 +00:00
this . set ( 'selectedAuth' , 'token' ) ;
this . render ( hbs ` {{auth-form cluster=cluster selectedAuth=selectedAuth}} ` ) ;
2018-04-05 21:36:33 +00:00
assert . equal ( component . errorText , '' ) ;
2018-04-09 21:50:36 +00:00
component . login ( ) ;
// because this is an ember-concurrency backed service,
// we have to manually force settling the run queue
Ember . run . later ( ( ) => Ember . run . cancelTimers ( ) , 50 ) ;
return wait ( ) . then ( ( ) => {
2018-04-05 21:36:33 +00:00
assert . equal ( component . errorText , CSP _ERR _TEXT ) ;
} ) ;
} ) ;
test ( 'it renders with vault style errors' , function ( assert ) {
let server = new Pretender ( function ( ) {
this . get ( '/v1/auth/**' , ( ) => {
return [
400 ,
{ 'Content-Type' : 'application/json' } ,
JSON . stringify ( {
errors : [ 'Not allowed' ] ,
} ) ,
] ;
} ) ;
} ) ;
this . set ( 'cluster' , Ember . Object . create ( { } ) ) ;
2018-07-05 18:28:12 +00:00
this . set ( 'selectedAuth' , 'token' ) ;
this . render ( hbs ` {{auth-form cluster=cluster selectedAuth=selectedAuth}} ` ) ;
2018-04-05 21:36:33 +00:00
return component . login ( ) . then ( ( ) => {
assert . equal ( component . errorText , 'Error Authentication failed: Not allowed' ) ;
server . shutdown ( ) ;
} ) ;
} ) ;
test ( 'it renders AdapterError style errors' , function ( assert ) {
let server = new Pretender ( function ( ) {
this . get ( '/v1/auth/**' , ( ) => {
return [ 400 , { 'Content-Type' : 'application/json' } ] ;
} ) ;
} ) ;
this . set ( 'cluster' , Ember . Object . create ( { } ) ) ;
2018-07-05 18:28:12 +00:00
this . set ( 'selectedAuth' , 'token' ) ;
this . render ( hbs ` {{auth-form cluster=cluster selectedAuth=selectedAuth}} ` ) ;
2018-04-05 21:36:33 +00:00
return component . login ( ) . then ( ( ) => {
assert . equal ( component . errorText , 'Error Authentication failed: Bad Request' ) ;
server . shutdown ( ) ;
} ) ;
} ) ;
2018-07-05 18:28:12 +00:00
test ( 'it renders all the supported tabs when no methods are passed' , function ( assert ) {
this . render ( hbs ` {{auth-form cluster=cluster}} ` ) ;
assert . equal ( component . tabs . length , BACKENDS . length , 'renders a tab for every backend' ) ;
} ) ;
test ( 'it renders all the supported methods and Other tab when methods are present' , function ( assert ) {
2018-08-16 17:48:24 +00:00
let methods = {
'foo/' : {
2018-07-05 18:28:12 +00:00
type : 'userpass' ,
} ,
2018-08-16 17:48:24 +00:00
'approle/' : {
2018-07-05 18:28:12 +00:00
type : 'approle' ,
} ,
2018-08-16 17:48:24 +00:00
} ;
let server = new Pretender ( function ( ) {
this . get ( '/v1/sys/internal/ui/mounts' , ( ) => {
return [ 200 , { 'Content-Type' : 'application/json' } , JSON . stringify ( { data : { auth : methods } } ) ] ;
} ) ;
} ) ;
2018-07-05 18:28:12 +00:00
2018-08-16 17:48:24 +00:00
this . render ( hbs ` {{auth-form cluster=cluster }} ` ) ;
return wait ( ) . then ( ( ) => {
assert . equal ( component . tabs . length , 2 , 'renders a tab for userpass and Other' ) ;
assert . equal ( component . tabs . objectAt ( 0 ) . name , 'foo' , 'uses the path in the label' ) ;
assert . equal ( component . tabs . objectAt ( 1 ) . name , 'Other' , 'second tab is the Other tab' ) ;
server . shutdown ( ) ;
} ) ;
2018-07-05 18:28:12 +00:00
} ) ;
2018-07-20 21:48:25 +00:00
test ( 'it calls authorize with the correct path' , function ( assert ) {
this . register ( 'service:auth' , workingAuthService ) ;
this . inject . service ( 'auth' ) ;
let authSpy = sinon . spy ( this . get ( 'auth' ) , 'authenticate' ) ;
2018-08-16 17:48:24 +00:00
let methods = {
'foo/' : {
2018-07-20 21:48:25 +00:00
type : 'userpass' ,
} ,
2018-08-16 17:48:24 +00:00
} ;
let server = new Pretender ( function ( ) {
this . get ( '/v1/sys/internal/ui/mounts' , ( ) => {
return [ 200 , { 'Content-Type' : 'application/json' } , JSON . stringify ( { data : { auth : methods } } ) ] ;
} ) ;
} ) ;
2018-07-20 21:48:25 +00:00
2018-08-16 17:48:24 +00:00
this . set ( 'selectedAuth' , 'foo/' ) ;
this . render ( hbs ` {{auth-form cluster=cluster selectedAuth=selectedAuth}} ` ) ;
wait ( ) . then ( ( ) => {
return component . login ( ) ;
} ) ;
2018-07-20 21:48:25 +00:00
return wait ( ) . then ( ( ) => {
assert . ok ( authSpy . calledOnce , 'a call to authenticate was made' ) ;
let { data } = authSpy . getCall ( 0 ) . args [ 0 ] ;
2018-08-16 17:48:24 +00:00
assert . equal ( data . path , 'foo' , 'uses the id for the path' ) ;
2018-07-20 21:48:25 +00:00
authSpy . restore ( ) ;
2018-08-16 17:48:24 +00:00
server . shutdown ( ) ;
2018-07-20 21:48:25 +00:00
} ) ;
} ) ;
2018-07-05 18:28:12 +00:00
test ( 'it renders all the supported methods when no supported methods are present in passed methods' , function (
assert
) {
let methods = [
{
type : 'approle' ,
id : 'approle' ,
path : 'approle/' ,
} ,
] ;
this . set ( 'methods' , methods ) ;
this . render ( hbs ` {{auth-form cluster=cluster methods=methods}} ` ) ;
assert . equal ( component . tabs . length , BACKENDS . length , 'renders a tab for every backend' ) ;
} ) ;
test ( 'it makes a request to unwrap if passed a wrappedToken and logs in' , function ( assert ) {
this . register ( 'service:auth' , workingAuthService ) ;
this . inject . service ( 'auth' ) ;
let authSpy = sinon . spy ( this . get ( 'auth' ) , 'authenticate' ) ;
let server = new Pretender ( function ( ) {
this . post ( '/v1/sys/wrapping/unwrap' , ( ) => {
return [
200 ,
{ 'Content-Type' : 'application/json' } ,
JSON . stringify ( {
auth : {
client _token : '12345' ,
} ,
} ) ,
] ;
} ) ;
} ) ;
let wrappedToken = '54321' ;
this . set ( 'wrappedToken' , wrappedToken ) ;
this . render ( hbs ` {{auth-form cluster=cluster wrappedToken=wrappedToken}} ` ) ;
Ember . run . later ( ( ) => Ember . run . cancelTimers ( ) , 50 ) ;
return wait ( ) . then ( ( ) => {
assert . equal ( server . handledRequests [ 0 ] . url , '/v1/sys/wrapping/unwrap' , 'makes call to unwrap the token' ) ;
assert . equal (
server . handledRequests [ 0 ] . requestHeaders [ 'X-Vault-Token' ] ,
wrappedToken ,
'uses passed wrapped token for the unwrap'
) ;
assert . ok ( authSpy . calledOnce , 'a call to authenticate was made' ) ;
server . shutdown ( ) ;
authSpy . restore ( ) ;
} ) ;
} ) ;
test ( 'it shows an error if unwrap errors' , function ( assert ) {
let server = new Pretender ( function ( ) {
this . post ( '/v1/sys/wrapping/unwrap' , ( ) => {
return [
400 ,
{ 'Content-Type' : 'application/json' } ,
JSON . stringify ( {
errors : [ 'There was an error unwrapping!' ] ,
} ) ,
] ;
} ) ;
} ) ;
this . set ( 'wrappedToken' , '54321' ) ;
this . render ( hbs ` {{auth-form cluster=cluster wrappedToken=wrappedToken}} ` ) ;
Ember . run . later ( ( ) => Ember . run . cancelTimers ( ) , 50 ) ;
return wait ( ) . then ( ( ) => {
assert . equal (
component . errorText ,
'Error Token unwrap failed: There was an error unwrapping!' ,
'shows the error'
) ;
server . shutdown ( ) ;
} ) ;
} ) ;