2018-09-25 16:28:26 +00:00
|
|
|
import { assign } from '@ember/polyfills';
|
|
|
|
import EmberObject from '@ember/object';
|
2018-04-03 14:16:57 +00:00
|
|
|
import ClusterRouteMixin from 'vault/mixins/cluster-route';
|
2019-08-01 23:50:43 +00:00
|
|
|
import {
|
|
|
|
INIT,
|
|
|
|
UNSEAL,
|
|
|
|
AUTH,
|
|
|
|
CLUSTER,
|
|
|
|
CLUSTER_INDEX,
|
|
|
|
DR_REPLICATION_SECONDARY,
|
2022-08-23 16:05:00 +00:00
|
|
|
REDIRECT,
|
|
|
|
} from 'vault/lib/route-paths';
|
2018-04-03 14:16:57 +00:00
|
|
|
import { module, test } from 'qunit';
|
2019-08-01 23:50:43 +00:00
|
|
|
import sinon from 'sinon';
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
module('Unit | Mixin | cluster route', function () {
|
2018-09-25 16:28:26 +00:00
|
|
|
function createClusterRoute(
|
|
|
|
clusterModel = {},
|
2019-08-01 23:50:43 +00:00
|
|
|
methods = { router: {}, hasKeyData: () => false, authToken: () => null, transitionTo: () => {} }
|
2018-09-25 16:28:26 +00:00
|
|
|
) {
|
|
|
|
let ClusterRouteObject = EmberObject.extend(
|
|
|
|
ClusterRouteMixin,
|
|
|
|
assign(methods, { clusterModel: () => clusterModel })
|
|
|
|
);
|
|
|
|
return ClusterRouteObject.create();
|
|
|
|
}
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('#targetRouteName init', function (assert) {
|
2018-09-25 16:28:26 +00:00
|
|
|
let subject = createClusterRoute({ needsInit: true });
|
|
|
|
subject.routeName = CLUSTER;
|
|
|
|
assert.equal(subject.targetRouteName(), INIT, 'forwards to INIT when cluster needs init');
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
subject = createClusterRoute({ needsInit: false, sealed: true });
|
|
|
|
subject.routeName = CLUSTER;
|
|
|
|
assert.equal(subject.targetRouteName(), UNSEAL, 'forwards to UNSEAL if sealed and initialized');
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
subject = createClusterRoute({ needsInit: false });
|
|
|
|
subject.routeName = CLUSTER;
|
|
|
|
assert.equal(subject.targetRouteName(), AUTH, 'forwards to AUTH if unsealed and initialized');
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
subject = createClusterRoute({ dr: { isSecondary: true } });
|
|
|
|
subject.routeName = CLUSTER;
|
|
|
|
assert.equal(
|
|
|
|
subject.targetRouteName(),
|
|
|
|
DR_REPLICATION_SECONDARY,
|
|
|
|
'forwards to DR_REPLICATION_SECONDARY if is a dr secondary'
|
|
|
|
);
|
|
|
|
});
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('#targetRouteName when #hasDataKey is true', function (assert) {
|
2018-09-25 16:28:26 +00:00
|
|
|
let subject = createClusterRoute(
|
|
|
|
{ needsInit: false, sealed: true },
|
|
|
|
{ hasKeyData: () => true, authToken: () => null }
|
|
|
|
);
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
subject.routeName = CLUSTER;
|
|
|
|
assert.equal(subject.targetRouteName(), INIT, 'still land on INIT if there are keys on the controller');
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
subject.routeName = UNSEAL;
|
|
|
|
assert.equal(subject.targetRouteName(), UNSEAL, 'allowed to proceed to unseal');
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
subject = createClusterRoute(
|
|
|
|
{ needsInit: false, sealed: false },
|
|
|
|
{ hasKeyData: () => true, authToken: () => null }
|
|
|
|
);
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
subject.routeName = AUTH;
|
|
|
|
assert.equal(subject.targetRouteName(), AUTH, 'allowed to proceed to auth');
|
|
|
|
});
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('#targetRouteName happy path forwards to CLUSTER route', function (assert) {
|
2018-09-25 16:28:26 +00:00
|
|
|
let subject = createClusterRoute(
|
|
|
|
{ needsInit: false, sealed: false, dr: { isSecondary: false } },
|
|
|
|
{ hasKeyData: () => false, authToken: () => 'a token' }
|
|
|
|
);
|
|
|
|
subject.routeName = INIT;
|
|
|
|
assert.equal(subject.targetRouteName(), CLUSTER, 'forwards when inited and navigating to INIT');
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
subject.routeName = UNSEAL;
|
|
|
|
assert.equal(subject.targetRouteName(), CLUSTER, 'forwards when unsealed and navigating to UNSEAL');
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
subject.routeName = AUTH;
|
2022-08-23 16:05:00 +00:00
|
|
|
assert.equal(subject.targetRouteName(), REDIRECT, 'forwards when authenticated and navigating to AUTH');
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
subject.routeName = DR_REPLICATION_SECONDARY;
|
|
|
|
assert.equal(
|
|
|
|
subject.targetRouteName(),
|
|
|
|
CLUSTER,
|
|
|
|
'forwards when not a DR secondary and navigating to DR_REPLICATION_SECONDARY'
|
|
|
|
);
|
|
|
|
});
|
2019-08-01 23:50:43 +00:00
|
|
|
|
2022-08-23 16:05:00 +00:00
|
|
|
test('#targetRouteName happy path when not authed forwards to AUTH', function (assert) {
|
|
|
|
let subject = createClusterRoute(
|
|
|
|
{ needsInit: false, sealed: false, dr: { isSecondary: false } },
|
|
|
|
{ hasKeyData: () => false, authToken: () => null }
|
|
|
|
);
|
|
|
|
subject.routeName = INIT;
|
|
|
|
assert.equal(subject.targetRouteName(), AUTH, 'forwards when inited and navigating to INIT');
|
|
|
|
|
|
|
|
subject.routeName = UNSEAL;
|
|
|
|
assert.equal(subject.targetRouteName(), AUTH, 'forwards when unsealed and navigating to UNSEAL');
|
|
|
|
|
|
|
|
subject.routeName = AUTH;
|
|
|
|
assert.equal(subject.targetRouteName(), AUTH, 'forwards when non-authenticated and navigating to AUTH');
|
|
|
|
|
|
|
|
subject.routeName = DR_REPLICATION_SECONDARY;
|
|
|
|
assert.equal(
|
|
|
|
subject.targetRouteName(),
|
|
|
|
AUTH,
|
|
|
|
'forwards when not a DR secondary and navigating to DR_REPLICATION_SECONDARY'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('#transitionToTargetRoute', function (assert) {
|
2019-08-01 23:50:43 +00:00
|
|
|
let redirectRouteURL = '/vault/secrets/secret/create';
|
|
|
|
let subject = createClusterRoute({ needsInit: false, sealed: false });
|
|
|
|
subject.router.currentURL = redirectRouteURL;
|
|
|
|
let spy = sinon.spy(subject, 'transitionTo');
|
|
|
|
subject.transitionToTargetRoute();
|
|
|
|
assert.ok(
|
|
|
|
spy.calledWithExactly(AUTH, { queryParams: { redirect_to: redirectRouteURL } }),
|
|
|
|
'calls transitionTo with the expected args'
|
|
|
|
);
|
|
|
|
|
|
|
|
spy.restore();
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('#transitionToTargetRoute with auth as a target', function (assert) {
|
2019-08-01 23:50:43 +00:00
|
|
|
let subject = createClusterRoute({ needsInit: false, sealed: false });
|
|
|
|
let spy = sinon.spy(subject, 'transitionTo');
|
|
|
|
// in this case it's already transitioning to the AUTH route so we don't need to call transitionTo again
|
|
|
|
subject.transitionToTargetRoute({ targetName: AUTH });
|
|
|
|
assert.ok(spy.notCalled, 'transitionTo is not called');
|
|
|
|
spy.restore();
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('#transitionToTargetRoute with auth target, coming from cluster route', function (assert) {
|
2019-08-01 23:50:43 +00:00
|
|
|
let subject = createClusterRoute({ needsInit: false, sealed: false });
|
|
|
|
let spy = sinon.spy(subject, 'transitionTo');
|
|
|
|
subject.transitionToTargetRoute({ targetName: CLUSTER_INDEX });
|
|
|
|
assert.ok(spy.calledWithExactly(AUTH), 'calls transitionTo without redirect_to');
|
|
|
|
spy.restore();
|
|
|
|
});
|
2018-04-03 14:16:57 +00:00
|
|
|
});
|