Merge pull request #7003 from keithjgrant/survey-launch-fix

Fix launch prompt errors when no survey present

Reviewed-by: https://github.com/apps/softwarefactory-project-zuul
This commit is contained in:
softwarefactory-project-zuul[bot] 2020-05-13 22:20:40 +00:00 committed by GitHub
commit 1b144470b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 95 additions and 6 deletions

View File

@ -8,10 +8,18 @@ import getSurveyValues from '../getSurveyValues';
function PreviewStep({ resource, config, survey, formErrors }) {
const { values } = useFormikContext();
const surveyValues = getSurveyValues(values);
const passwordFields = survey.spec
.filter(q => q.type === 'password')
.map(q => q.variable);
const masked = maskPasswords(surveyValues, passwordFields);
let extraVars;
if (survey && survey.spec) {
const passwordFields = survey.spec
.filter(q => q.type === 'password')
.map(q => q.variable);
const masked = maskPasswords(surveyValues, passwordFields);
extraVars = yaml.safeDump(
mergeExtraVars(values.extra_vars || '---', masked)
);
} else {
extraVars = values.extra_vars || '---';
}
return (
<>
<PromptDetail
@ -19,7 +27,7 @@ function PreviewStep({ resource, config, survey, formErrors }) {
launchConfig={config}
overrides={{
...values,
extra_vars: yaml.safeDump(mergeExtraVars(values.extra_vars, masked)),
extra_vars: extraVars,
}}
/>
{formErrors && (

View File

@ -0,0 +1,78 @@
import React from 'react';
import { act } from 'react-dom/test-utils';
import { Formik } from 'formik';
import { mountWithContexts } from '../../../../testUtils/enzymeHelpers';
import PreviewStep from './PreviewStep';
const resource = {
id: 1,
type: 'job_template',
summary_fields: {
inventory: { id: 12 },
recent_jobs: [],
},
related: {},
};
const survey = {
name: '',
spec: [
{
variable: 'foo',
type: 'text',
},
],
};
describe('PreviewStep', () => {
test('should render PromptDetail', async () => {
let wrapper;
await act(async () => {
wrapper = mountWithContexts(
<Formik initialValues={{ limit: '4', survey_foo: 'abc' }}>
<PreviewStep
resource={resource}
config={{
ask_limit_on_launch: true,
survey_enabled: true,
}}
survey={survey}
/>
</Formik>
);
});
const detail = wrapper.find('PromptDetail');
expect(detail).toHaveLength(1);
expect(detail.prop('resource')).toEqual(resource);
expect(detail.prop('overrides')).toEqual({
extra_vars: 'foo: abc\n',
limit: '4',
survey_foo: 'abc',
});
});
test('should render PromptDetail without survey', async () => {
let wrapper;
await act(async () => {
wrapper = mountWithContexts(
<Formik initialValues={{ limit: '4' }}>
<PreviewStep
resource={resource}
config={{
ask_limit_on_launch: true,
}}
/>
</Formik>
);
});
const detail = wrapper.find('PromptDetail');
expect(detail).toHaveLength(1);
expect(detail.prop('resource')).toEqual(resource);
expect(detail.prop('overrides')).toEqual({
extra_vars: '---',
limit: '4',
});
});
});

View File

@ -9,6 +9,9 @@ export default function useInventoryStep(config, resource, visitedSteps, i18n) {
const [stepErrors, setStepErrors] = useState({});
const validate = values => {
if (!config.ask_inventory_on_launch) {
return {};
}
const errors = {};
if (!values.inventory) {
errors.inventory = i18n._(t`An inventory must be selected`);

View File

@ -56,7 +56,7 @@ export default function useSurveyStep(config, resource, visitedSteps, i18n) {
isReady: !isLoading && !!survey,
error,
setTouched: setFieldsTouched => {
if (!survey) {
if (!survey || !survey.spec) {
return;
}
const fields = {};