Skip to content

Commit

Permalink
fix by returning apiPath from the model (#10122)
Browse files Browse the repository at this point in the history
* fix by returning apiPath from the model

* remove unused service

* be more specific of when setting dynamicApiPath

* new acceptance test for auth list

* remove unused policy

* udpate comment
  • Loading branch information
Monkeychip committed Oct 19, 2020
1 parent 0668169 commit e371ded
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 8 deletions.
18 changes: 13 additions & 5 deletions ui/app/adapters/generated-item-list.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
import { assign } from '@ember/polyfills';
import ApplicationAdapter from './application';
import { task } from 'ember-concurrency';

export default ApplicationAdapter.extend({
namespace: 'v1',
urlForItem() {},
dynamicApiPath: '',
getDynamicApiPath: task(function*(id) {
let result = yield this.store.peekRecord('auth-method', id);
this.dynamicApiPath = result.apiPath;
return;
}),

fetchByQuery(store, query, isList) {
fetchByQuery: task(function*(store, query, isList) {
const { id } = query;
let data = {};
if (isList) {
data.list = true;
yield this.getDynamicApiPath.perform(id);
}

return this.ajax(this.urlForItem(id, isList), 'GET', { data }).then(resp => {
return this.ajax(this.urlForItem(id, isList, this.dynamicApiPath), 'GET', { data }).then(resp => {
const data = {
id,
method: id,
};
return assign({}, resp, data);
});
},
}),

query(store, type, query) {
return this.fetchByQuery(store, query, true);
return this.fetchByQuery.perform(store, query, true);
},

queryRecord(store, type, query) {
return this.fetchByQuery(store, query);
return this.fetchByQuery.perform(store, query);
},
});
10 changes: 8 additions & 2 deletions ui/app/services/path-help.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function sanitizePath(path) {

export default Service.extend({
attrs: null,
dynamicApiPath: '',
ajax(url, options = {}) {
let appAdapter = getOwner(this).lookup(`adapter:application`);
let { data } = options;
Expand Down Expand Up @@ -223,11 +224,16 @@ export default Service.extend({
const deletePath = paths.find(path => path.operations.includes('delete'));

return generatedItemAdapter.extend({
urlForItem(id, isList) {
urlForItem(id, isList, dynamicApiPath) {
const itemType = getPath.path.slice(1);
let url;
id = encodePath(id);

// the apiPath changes when you switch between routes but the apiPath variable does not unless the model is reloaded
// overwrite apiPath if dynamicApiPath exist.
// dynamicApiPath comes from the model->adapter
if (dynamicApiPath) {
apiPath = dynamicApiPath;
}
// isList indicates whether we are viewing the list page
// of a top-level item such as userpass
if (isList) {
Expand Down
1 change: 1 addition & 0 deletions ui/app/templates/components/generated-item-list.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<Toolbar>
<ToolbarActions>
<ToolbarLink
data-test-create={{itemType}}
@type="add"
@params={{array
"vault.cluster.access.method.item.create"
Expand Down
2 changes: 1 addition & 1 deletion ui/app/templates/vault/cluster/access/methods.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<Toolbar>
<ToolbarActions>
<ToolbarLink @type="add" @params={{array 'vault.cluster.settings.auth.enable'}}>
<ToolbarLink @type="add" @params={{array 'vault.cluster.settings.auth.enable'}} data-test-auth-enable>
Enable new method
</ToolbarLink>
</ToolbarActions>
Expand Down
59 changes: 59 additions & 0 deletions ui/tests/acceptance/auth-list-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { click, fillIn, settled, visit } from '@ember/test-helpers';
import { module, test } from 'qunit';
import { setupApplicationTest } from 'ember-qunit';
import authPage from 'vault/tests/pages/auth';
import logout from 'vault/tests/pages/logout';
import enablePage from 'vault/tests/pages/settings/auth/enable';

module('Acceptance | userpass secret backend', function(hooks) {
setupApplicationTest(hooks);

hooks.beforeEach(function() {
return authPage.login();
});

hooks.afterEach(function() {
return logout.visit();
});

test('userpass backend', async function(assert) {
let n = Math.random();
const path1 = `userpass-${++n}`;
const path2 = `userpass-${++n}`;
const user1 = 'user1';
const user2 = 'user2';

// enable the first userpass method with one username
await enablePage.enable('userpass', path1);
await click('[data-test-save-config="true"]');
await visit(`/vault/access/${path1}/item/user/create`);
await fillIn('[data-test-input="username"]', user1);
await fillIn('[data-test-textarea]', user1);
await click('[data-test-save-config="true"]');

// enable the first userpass method with one username
await visit(`/vault/settings/auth/enable`);
await click('[data-test-mount-type="userpass"]');
await click('[data-test-mount-next]');
await fillIn('[data-test-input="path"]', path2);
await click('[data-test-mount-submit="true"]');
await click('[data-test-save-config="true"]');
await settled();
await click(`[data-test-auth-backend-link="${path2}"]`);
await click('[data-test-create="user"]');
await fillIn('[data-test-input="username"]', user2);
await fillIn('[data-test-textarea]', user2);
await click('[data-test-save-config="true"]');

//confirming that the user was created. There was a bug where the apiPath was not being updated when toggling between auth routes
assert
.dom('[data-test-list-item-content]')
.hasText(user2, 'user just created shows in current auth list');

//confirm that the auth method 1 shows the user1. There was a bug where it was not updated the list when toggling between auth routes
await visit(`/vault/access/${path1}/item/user`);
assert
.dom('[data-test-list-item-content]')
.hasText(user1, 'first user created shows in current auth list');
});
});

0 comments on commit e371ded

Please sign in to comment.