Skip to content

Commit

Permalink
UI: fixes validation bug in sign certificate form (hashicorp#19280)
Browse files Browse the repository at this point in the history
* move validations to base certificate

* add test
  • Loading branch information
hellobontempo authored Feb 22, 2023
1 parent fe7eeda commit 85e8519
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 45 deletions.
6 changes: 0 additions & 6 deletions ui/app/models/pki/certificate/generate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { attr } from '@ember-data/model';
import { withFormFields } from 'vault/decorators/model-form-fields';
import { withModelValidations } from 'vault/decorators/model-validations';
import PkiCertificateBaseModel from './base';

const generateFromRole = [
Expand All @@ -20,11 +19,6 @@ const generateFromRole = [
'More Options': ['format', 'privateKeyFormat'],
},
];
const validations = {
commonName: [{ type: 'presence', message: 'Common name is required.' }],
};

@withModelValidations(validations)
@withFormFields(null, generateFromRole)
export default class PkiCertificateGenerateModel extends PkiCertificateBaseModel {
getHelpUrl(backend) {
Expand Down
12 changes: 3 additions & 9 deletions ui/lib/pki/addon/components/pki-role-generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import errorMessage from 'vault/utils/error-message';
import FlashMessageService from 'vault/services/flash-messages';
import DownloadService from 'vault/services/download';
import PkiCertificateGenerateModel from 'vault/models/pki/certificate/generate';
import PkiCertificateSignModel from 'vault/models/pki/certificate/sign';

interface Args {
onSuccess: CallableFunction;
model: PkiCertificateGenerateModel;
model: PkiCertificateGenerateModel | PkiCertificateSignModel;
type: string;
}

Expand All @@ -24,7 +25,6 @@ export default class PkiRoleGenerate extends Component<Args> {

@tracked errorBanner = '';
@tracked invalidFormAlert = '';
@tracked modelValidations = null;

get verb() {
return this.args.type === 'sign' ? 'sign' : 'generate';
Expand All @@ -35,18 +35,12 @@ export default class PkiRoleGenerate extends Component<Args> {
evt.preventDefault();
this.errorBanner = '';
const { model, onSuccess } = this.args;
const { isValid, state, invalidFormMessage } = model.validate();

this.modelValidations = isValid ? null : state;
this.invalidFormAlert = invalidFormMessage;

if (!isValid) return;

try {
yield model.save();
onSuccess();
} catch (err) {
this.errorBanner = errorMessage(err, `Could not ${this.verb} certificate. See Vault logs for details.`);
this.invalidFormAlert = 'There was an error submitting this form.';
}
}

Expand Down
25 changes: 1 addition & 24 deletions ui/tests/integration/components/pki/pki-role-generate-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render, fillIn, click } from '@ember/test-helpers';
import { render, fillIn } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
import { setupMirage } from 'ember-cli-mirage/test-support';
import { setupEngine } from 'ember-engines/test-support';
Expand Down Expand Up @@ -69,27 +69,4 @@ module('Integration | Component | pki-role-generate', function (hooks) {
assert.dom(SELECTORS.certificate).exists({ count: 1 }, 'shows certificate info row');
assert.dom(SELECTORS.serialNumber).hasText('abcd-efgh-ijkl', 'shows serial number info row');
});

test('it should display validation errors', async function (assert) {
assert.expect(3);

await render(
hbs`
<div class="has-top-margin-xxl">
<PkiRoleGenerate
@model={{this.model}}
@onSuccess={{this.onSuccess}}
/>
</div>
`,
{ owner: this.engine }
);
await click(SELECTORS.generateButton);

assert
.dom(SELECTORS.commonNameInlineError)
.hasText('Common name is required.', 'Common name validation error renders');
assert.dom(SELECTORS.inlineAlert).hasText('There is an error with this form.', 'Alert renders');
assert.dom(SELECTORS.commonNameErrorBorder).hasClass('has-error-border');
});
});
8 changes: 2 additions & 6 deletions ui/types/vault/models/pki/certificate/generate.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import Model from '@ember-data/model';
import { FormField } from 'vault/app-types';
import PkiCertificateBaseModel from './base';

export default class PkiCertificateGenerateModel extends Model {
export default class PkiCertificateGenerateModel extends PkiCertificateBaseModel {
name: string;
backend: string;
serialNumber: string;
certificate: string;
formFields: FormField[];
formFieldsGroup: {
[k: string]: FormField[];
}[];
validate(): ModelValidations;
}
3 changes: 3 additions & 0 deletions ui/types/vault/models/pki/certificate/sign.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import PkiCertificateBaseModel from './base';

export default class PkiCertificateGenerateModel extends PkiCertificateBaseModel {}

0 comments on commit 85e8519

Please sign in to comment.