Commit 1803cbf3 authored by Kushal Pandya's avatar Kushal Pandya

Merge branch 'djadmin-fix-nested-auth-forms' into 'master'

Refactor DAST Site Profile form

See merge request gitlab-org/gitlab!60650
parents 023929fc 8290e793
<script> <script>
import { GlFormGroup, GlFormInput, GlFormCheckbox } from '@gitlab/ui'; import { GlFormGroup, GlFormInput, GlFormCheckbox } from '@gitlab/ui';
import { initFormField } from 'ee/security_configuration/utils'; import { initFormField } from 'ee/security_configuration/utils';
import { serializeFormObject } from '~/lib/utils/forms';
import validation from '~/vue_shared/directives/validation'; import validation from '~/vue_shared/directives/validation';
export default { export default {
...@@ -28,6 +29,11 @@ export default { ...@@ -28,6 +29,11 @@ export default {
required: false, required: false,
default: false, default: false,
}, },
isEditMode: {
type: Boolean,
required: false,
default: false,
},
}, },
data() { data() {
const { const {
...@@ -40,8 +46,6 @@ export default { ...@@ -40,8 +46,6 @@ export default {
passwordField = 'password', passwordField = 'password',
} = this.value.fields; } = this.value.fields;
const isEditMode = Object.keys(this.value.fields).length > 0;
return { return {
form: { form: {
state: false, state: false,
...@@ -49,23 +53,28 @@ export default { ...@@ -49,23 +53,28 @@ export default {
enabled: initFormField({ value: enabled, skipValidation: true }), enabled: initFormField({ value: enabled, skipValidation: true }),
url: initFormField({ value: url }), url: initFormField({ value: url }),
username: initFormField({ value: username }), username: initFormField({ value: username }),
password: isEditMode password: this.isEditMode
? initFormField({ value: password, required: false, skipValidation: true }) ? initFormField({ value: password, required: false, skipValidation: true })
: initFormField({ value: password }), : initFormField({ value: password }),
usernameField: initFormField({ value: usernameField }), usernameField: initFormField({ value: usernameField }),
passwordField: initFormField({ value: passwordField }), passwordField: initFormField({ value: passwordField }),
}, },
}, },
isEditMode, isSensitiveFieldRequired: !this.isEditMode,
isSensitiveFieldRequired: !isEditMode,
}; };
}, },
watch: { watch: {
form: { handler: 'emitUpdate', immediate: true, deep: true }, form: { handler: 'emitUpdate', immediate: true, deep: true },
}, },
created() {
this.emitUpdate();
},
methods: { methods: {
emitUpdate() { emitUpdate() {
this.$emit('input', this.form); this.$emit('input', {
fields: serializeFormObject(this.form.fields),
state: this.form.state,
});
}, },
}, },
}; };
......
...@@ -160,7 +160,7 @@ export default { ...@@ -160,7 +160,7 @@ export default {
.map((url) => url.trim()); .map((url) => url.trim());
}, },
serializedAuthFields() { serializedAuthFields() {
const authFields = serializeFormObject(this.authSection.fields); const authFields = this.authSection.fields;
// not to send password value if unchanged // not to send password value if unchanged
if (authFields.password === REDACTED_PASSWORD) { if (authFields.password === REDACTED_PASSWORD) {
delete authFields.password; delete authFields.password;
...@@ -178,7 +178,8 @@ export default { ...@@ -178,7 +178,8 @@ export default {
onSubmit() { onSubmit() {
const isAuthEnabled = const isAuthEnabled =
this.glFeatures.securityDastSiteProfilesAdditionalFields && this.glFeatures.securityDastSiteProfilesAdditionalFields &&
this.authSection.fields.enabled.value; this.authSection.fields.enabled &&
!this.isTargetAPI;
this.form.showValidation = true; this.form.showValidation = true;
...@@ -420,6 +421,7 @@ export default { ...@@ -420,6 +421,7 @@ export default {
v-model="authSection" v-model="authSection"
:disabled="isPolicyProfile" :disabled="isPolicyProfile"
:show-validation="form.showValidation" :show-validation="form.showValidation"
:is-edit-mode="isEdit"
/> />
<hr class="gl-border-gray-100" /> <hr class="gl-border-gray-100" />
......
...@@ -64,7 +64,7 @@ describe('DastSiteAuthSection', () => { ...@@ -64,7 +64,7 @@ describe('DastSiteAuthSection', () => {
'makes the component emit an "input" event when changed', 'makes the component emit an "input" event when changed',
async (enabled) => { async (enabled) => {
await setAuthentication({ enabled }); await setAuthentication({ enabled });
expect(getLatestInputEventPayload().fields.enabled.value).toBe(enabled); expect(getLatestInputEventPayload().fields.enabled).toBe(enabled);
}, },
); );
}); });
...@@ -89,13 +89,13 @@ describe('DastSiteAuthSection', () => { ...@@ -89,13 +89,13 @@ describe('DastSiteAuthSection', () => {
expect(findByNameAttribute(inputFieldName).exists()).toBe(true); expect(findByNameAttribute(inputFieldName).exists()).toBe(true);
}); });
it('makes the component emit an "input" event when its value changes', () => { it('makes the component emit an "input" event when its value changes', async () => {
const input = findByNameAttribute(inputFieldName); const input = findByNameAttribute(inputFieldName);
const newValue = 'foo'; const newValue = 'foo';
input.setValue(newValue); await input.setValue(newValue);
expect(getLatestInputEventPayload().fields[inputFieldName].value).toBe(newValue); expect(getLatestInputEventPayload().fields[inputFieldName]).toBe(newValue);
}); });
}); });
...@@ -112,13 +112,13 @@ describe('DastSiteAuthSection', () => { ...@@ -112,13 +112,13 @@ describe('DastSiteAuthSection', () => {
expect(getLatestInputEventPayload().state).toBe(true); expect(getLatestInputEventPayload().state).toBe(true);
}); });
it('is valid once all fields have been entered correctly', () => { it('is valid once all fields have been entered correctly', async () => {
Object.entries(inputFieldsWithValues).forEach(([inputFieldName, inputFieldValue]) => { Object.entries(inputFieldsWithValues).forEach(([inputFieldName, inputFieldValue]) => {
const input = findByNameAttribute(inputFieldName); const input = findByNameAttribute(inputFieldName);
input.setValue(inputFieldValue); input.setValue(inputFieldValue);
input.trigger('blur'); input.trigger('blur');
}); });
await wrapper.vm.$nextTick();
expect(getLatestInputEventPayload().state).toBe(true); expect(getLatestInputEventPayload().state).toBe(true);
}); });
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment