Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport: ui/fixes validation bug in pki role sign certificate form #19280 #19293

Merged
merged 2 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -21,11 +20,6 @@ const generateFromRole = [
],
},
];
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-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 {}