Skip to content

Commit

Permalink
UI: Fix updating static roles via role edit page on UI (#29498)
Browse files Browse the repository at this point in the history
* added check for updating static roles, appending full payload data

* pulling specific properties into payload obj to fix popups

* adding changelog

* add else to keep previous imp for dynamic roles

* removing separate request, utilizing snapshot

* renamed serialized data var, added comment for required username line

* adding test for editing static role

* updated test for edit payload

* Update changelog/29498.txt

Co-authored-by: claire bontempo <[email protected]>

---------

Co-authored-by: claire bontempo <[email protected]>
  • Loading branch information
drivera258 and hellobontempo authored Feb 10, 2025
1 parent 9e6b5ce commit 7fb0db7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
3 changes: 3 additions & 0 deletions changelog/29498.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
ui/database: Fixes 'cannot update static username' error when updating static role's rotation period
```
11 changes: 10 additions & 1 deletion ui/app/adapters/database/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,19 @@ export default ApplicationAdapter.extend({

async updateRecord(store, type, snapshot) {
const serializer = store.serializerFor(type.modelName);
const data = serializer.serialize(snapshot);
const serializedData = serializer.serialize(snapshot);
const roleType = snapshot.attr('type');
const backend = snapshot.attr('backend');
const id = snapshot.attr('name');
let data = {};
if (roleType === 'static') {
data = {
...serializedData,
username: snapshot.attr('username'), // username is required for updating a static role
};
} else {
data = serializedData;
}

return this.ajax(this.urlFor(backend, id, roleType), 'POST', { data }).then(() => data);
},
Expand Down
22 changes: 22 additions & 0 deletions ui/tests/integration/components/database-role-edit-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { render } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
import { setupMirage } from 'ember-cli-mirage/test-support';
import { capabilitiesStub } from 'vault/tests/helpers/stubs';
import { click, fillIn } from '@ember/test-helpers';

module('Integration | Component | database-role-edit', function (hooks) {
setupRenderingTest(hooks);
Expand All @@ -20,6 +21,7 @@ module('Integration | Component | database-role-edit', function (hooks) {
modelName: 'database/role',
database: ['my-mongodb-database'],
backend: 'database',
username: 'staticTestUser',
type: 'static',
name: 'my-static-role',
id: 'my-static-role',
Expand All @@ -36,6 +38,26 @@ module('Integration | Component | database-role-edit', function (hooks) {
this.modelDynamic = this.store.peekRecord('database/role', 'my-dynamic-role');
});

test('it should let user edit a static role when given update capability', async function (assert) {
this.server.post('/sys/capabilities-self', capabilitiesStub('database/static-creds/my-role', ['update']));

this.server.post(`/database/static-roles/my-static-role`, (schema, req) => {
assert.true(true, 'request made to update static role');
assert.propEqual(
JSON.parse(req.requestBody),
{
username: 'staticTestUser',
rotation_period: '1728000s', // 20 days in seconds
},
'it updates static role with correct payload'
);
});

await render(hbs`<DatabaseRoleEdit @model={{this.modelStatic}} @mode="edit"/>`);
await fillIn('[data-test-ttl-value="Rotation period"]', '20');
await click('[data-test-secret-save]');
});

test('it should show Get credentials button when a user has the correct policy', async function (assert) {
this.server.post('/sys/capabilities-self', capabilitiesStub('database/static-creds/my-role', ['read']));
await render(hbs`<DatabaseRoleEdit @model={{this.modelStatic}} @mode="show"/>`);
Expand Down

0 comments on commit 7fb0db7

Please sign in to comment.