Fix SAML variables default values

Fix SAML variables default values

See: https://github.com/ansible/tower/issues/5372
This commit is contained in:
nixocio 2021-10-20 09:59:57 -04:00 committed by Shane McDonald
parent 0f77ca605d
commit 8e9fc14b0e
No known key found for this signature in database
GPG Key ID: 6F374AF6E9EB9374
20 changed files with 44 additions and 49 deletions

View File

@ -94,10 +94,9 @@ function AzureADEdit() {
const initialValues = (fields) =>
Object.keys(fields).reduce((acc, key) => {
if (fields[key].type === 'list' || fields[key].type === 'nested object') {
const emptyDefault = fields[key].type === 'list' ? '[]' : '{}';
acc[key] = fields[key].value
? JSON.stringify(fields[key].value, null, 2)
: emptyDefault;
: null;
} else {
acc[key] = fields[key].value ?? '';
}

View File

@ -147,8 +147,8 @@ describe('<GitHubDetail />', () => {
);
assertDetail(wrapper, 'GitHub OAuth2 Key', 'mock github key');
assertDetail(wrapper, 'GitHub OAuth2 Secret', 'Encrypted');
assertVariableDetail(wrapper, 'GitHub OAuth2 Organization Map', '{}');
assertVariableDetail(wrapper, 'GitHub OAuth2 Team Map', '{}');
assertVariableDetail(wrapper, 'GitHub OAuth2 Organization Map', 'null');
assertVariableDetail(wrapper, 'GitHub OAuth2 Team Map', 'null');
});
test('should hide edit button from non-superusers', async () => {
@ -226,12 +226,12 @@ describe('<GitHubDetail />', () => {
assertVariableDetail(
wrapper,
'GitHub Organization OAuth2 Organization Map',
'{}'
'null'
);
assertVariableDetail(
wrapper,
'GitHub Organization OAuth2 Team Map',
'{}'
'null'
);
});
});
@ -333,9 +333,13 @@ describe('<GitHubDetail />', () => {
assertVariableDetail(
wrapper,
'GitHub Enterprise OAuth2 Organization Map',
'{}'
'null'
);
assertVariableDetail(
wrapper,
'GitHub Enterprise OAuth2 Team Map',
'null'
);
assertVariableDetail(wrapper, 'GitHub Enterprise OAuth2 Team Map', '{}');
});
});
@ -398,12 +402,12 @@ describe('<GitHubDetail />', () => {
assertVariableDetail(
wrapper,
'GitHub Enterprise Organization OAuth2 Organization Map',
'{}'
'null'
);
assertVariableDetail(
wrapper,
'GitHub Enterprise Organization OAuth2 Team Map',
'{}'
'null'
);
});
});
@ -463,12 +467,12 @@ describe('<GitHubDetail />', () => {
assertVariableDetail(
wrapper,
'GitHub Enterprise Team OAuth2 Organization Map',
'{}'
'null'
);
assertVariableDetail(
wrapper,
'GitHub Enterprise Team OAuth2 Team Map',
'{}'
'null'
);
});
});

View File

@ -92,10 +92,9 @@ function GitHubEdit() {
const initialValues = (fields) =>
Object.keys(fields).reduce((acc, key) => {
if (fields[key].type === 'list' || fields[key].type === 'nested object') {
const emptyDefault = fields[key].type === 'list' ? '[]' : '{}';
acc[key] = fields[key].value
? JSON.stringify(fields[key].value, null, 2)
: emptyDefault;
: null;
} else {
acc[key] = fields[key].value ?? '';
}

View File

@ -94,10 +94,9 @@ function GitHubEnterpriseEdit() {
const initialValues = (fields) =>
Object.keys(fields).reduce((acc, key) => {
if (fields[key].type === 'list' || fields[key].type === 'nested object') {
const emptyDefault = fields[key].type === 'list' ? '[]' : '{}';
acc[key] = fields[key].value
? JSON.stringify(fields[key].value, null, 2)
: emptyDefault;
: null;
} else {
acc[key] = fields[key].value ?? '';
}

View File

@ -133,7 +133,7 @@ describe('<GitHubEnterpriseEdit />', () => {
SOCIAL_AUTH_GITHUB_ENTERPRISE_API_URL: '',
SOCIAL_AUTH_GITHUB_ENTERPRISE_KEY: '',
SOCIAL_AUTH_GITHUB_ENTERPRISE_SECRET: '',
SOCIAL_AUTH_GITHUB_ENTERPRISE_TEAM_MAP: {},
SOCIAL_AUTH_GITHUB_ENTERPRISE_TEAM_MAP: null,
SOCIAL_AUTH_GITHUB_ENTERPRISE_ORGANIZATION_MAP: {
Default: {
users: false,

View File

@ -94,10 +94,9 @@ function GitHubEnterpriseOrgEdit() {
const initialValues = (fields) =>
Object.keys(fields).reduce((acc, key) => {
if (fields[key].type === 'list' || fields[key].type === 'nested object') {
const emptyDefault = fields[key].type === 'list' ? '[]' : '{}';
acc[key] = fields[key].value
? JSON.stringify(fields[key].value, null, 2)
: emptyDefault;
: null;
} else {
acc[key] = fields[key].value ?? '';
}

View File

@ -146,7 +146,7 @@ describe('<GitHubEnterpriseOrgEdit />', () => {
SOCIAL_AUTH_GITHUB_ENTERPRISE_ORG_KEY: '',
SOCIAL_AUTH_GITHUB_ENTERPRISE_ORG_SECRET: '',
SOCIAL_AUTH_GITHUB_ENTERPRISE_ORG_NAME: '',
SOCIAL_AUTH_GITHUB_ENTERPRISE_ORG_TEAM_MAP: {},
SOCIAL_AUTH_GITHUB_ENTERPRISE_ORG_TEAM_MAP: null,
SOCIAL_AUTH_GITHUB_ENTERPRISE_ORG_ORGANIZATION_MAP: {
Default: {
users: false,

View File

@ -94,10 +94,9 @@ function GitHubEnterpriseTeamEdit() {
const initialValues = (fields) =>
Object.keys(fields).reduce((acc, key) => {
if (fields[key].type === 'list' || fields[key].type === 'nested object') {
const emptyDefault = fields[key].type === 'list' ? '[]' : '{}';
acc[key] = fields[key].value
? JSON.stringify(fields[key].value, null, 2)
: emptyDefault;
: null;
} else {
acc[key] = fields[key].value ?? '';
}

View File

@ -140,7 +140,7 @@ describe('<GitHubEnterpriseTeamEdit />', () => {
SOCIAL_AUTH_GITHUB_ENTERPRISE_TEAM_KEY: '',
SOCIAL_AUTH_GITHUB_ENTERPRISE_TEAM_SECRET: '',
SOCIAL_AUTH_GITHUB_ENTERPRISE_TEAM_ID: '',
SOCIAL_AUTH_GITHUB_ENTERPRISE_TEAM_TEAM_MAP: {},
SOCIAL_AUTH_GITHUB_ENTERPRISE_TEAM_TEAM_MAP: null,
SOCIAL_AUTH_GITHUB_ENTERPRISE_TEAM_ORGANIZATION_MAP: {
Default: {
users: false,

View File

@ -94,10 +94,9 @@ function GitHubOrgEdit() {
const initialValues = (fields) =>
Object.keys(fields).reduce((acc, key) => {
if (fields[key].type === 'list' || fields[key].type === 'nested object') {
const emptyDefault = fields[key].type === 'list' ? '[]' : '{}';
acc[key] = fields[key].value
? JSON.stringify(fields[key].value, null, 2)
: emptyDefault;
: null;
} else {
acc[key] = fields[key].value ?? '';
}

View File

@ -122,7 +122,7 @@ describe('<GitHubOrgEdit />', () => {
SOCIAL_AUTH_GITHUB_ORG_KEY: '',
SOCIAL_AUTH_GITHUB_ORG_SECRET: '',
SOCIAL_AUTH_GITHUB_ORG_NAME: 'new org',
SOCIAL_AUTH_GITHUB_ORG_TEAM_MAP: {},
SOCIAL_AUTH_GITHUB_ORG_TEAM_MAP: null,
SOCIAL_AUTH_GITHUB_ORG_ORGANIZATION_MAP: {
Default: {
users: false,

View File

@ -94,10 +94,9 @@ function GitHubTeamEdit() {
const initialValues = (fields) =>
Object.keys(fields).reduce((acc, key) => {
if (fields[key].type === 'list' || fields[key].type === 'nested object') {
const emptyDefault = fields[key].type === 'list' ? '[]' : '{}';
acc[key] = fields[key].value
? JSON.stringify(fields[key].value, null, 2)
: emptyDefault;
: null;
} else {
acc[key] = fields[key].value ?? '';
}

View File

@ -100,10 +100,9 @@ function GoogleOAuth2Edit() {
const initialValues = (fields) =>
Object.keys(fields).reduce((acc, key) => {
if (fields[key].type === 'list' || fields[key].type === 'nested object') {
const emptyDefault = fields[key].type === 'list' ? '[]' : '{}';
acc[key] = fields[key].value
? JSON.stringify(fields[key].value, null, 2)
: emptyDefault;
: null;
} else {
acc[key] = fields[key].value ?? '';
}

View File

@ -103,10 +103,9 @@ function JobsEdit() {
const initialValues = (fields) =>
Object.keys(fields).reduce((acc, key) => {
if (fields[key].type === 'list' || fields[key].type === 'nested object') {
const emptyDefault = fields[key].type === 'list' ? '[]' : '{}';
acc[key] = fields[key].value
? JSON.stringify(fields[key].value, null, 2)
: emptyDefault;
: null;
} else {
acc[key] = fields[key].value ?? '';
}

View File

@ -146,10 +146,9 @@ function LDAPEdit() {
const initialValues = (fields) =>
Object.keys(fields).reduce((acc, key) => {
if (fields[key].type === 'list' || fields[key].type === 'nested object') {
const emptyDefault = fields[key].type === 'list' ? '[]' : '{}';
acc[key] = fields[key].value
? JSON.stringify(fields[key].value, null, 2)
: emptyDefault;
: null;
} else {
acc[key] = fields[key].value ?? '';
}

View File

@ -164,10 +164,9 @@ function MiscAuthenticationEdit() {
const initialValues = (fields) =>
Object.keys(fields).reduce((acc, key) => {
if (fields[key].type === 'list' || fields[key].type === 'nested object') {
const emptyDefault = fields[key].type === 'list' ? '[]' : '{}';
acc[key] = fields[key].value
? JSON.stringify(fields[key].value, null, 2)
: emptyDefault;
: null;
} else {
acc[key] = fields[key].value ?? '';
}

View File

@ -29,9 +29,9 @@ const authenticationData = {
'awx.sso.backends.TACACSPlusBackend',
'awx.main.backends.AWXModelBackend',
],
SOCIAL_AUTH_ORGANIZATION_MAP: {},
SOCIAL_AUTH_TEAM_MAP: {},
SOCIAL_AUTH_USER_FIELDS: [],
SOCIAL_AUTH_ORGANIZATION_MAP: null,
SOCIAL_AUTH_TEAM_MAP: null,
SOCIAL_AUTH_USER_FIELDS: null,
};
describe('<MiscAuthenticationEdit />', () => {

View File

@ -112,10 +112,9 @@ function SAMLEdit() {
const initialValues = (fields) =>
Object.keys(fields).reduce((acc, key) => {
if (fields[key].type === 'list' || fields[key].type === 'nested object') {
const emptyDefault = fields[key].type === 'list' ? '[]' : '{}';
acc[key] = fields[key].value
? JSON.stringify(fields[key].value, null, 2)
: emptyDefault;
: null;
} else {
acc[key] = fields[key].value ?? '';
}

View File

@ -5,7 +5,7 @@ import { Detail } from 'components/DetailList';
import CodeDetail from 'components/DetailList/CodeDetail';
function sortObj(obj) {
if (typeof obj !== 'object' || Array.isArray(obj)) {
if (typeof obj !== 'object' || Array.isArray(obj) || obj === null) {
return obj;
}
const sorted = {};
@ -30,7 +30,7 @@ export default ({ helpText, id, label, type, unit = '', value }) => {
label={label}
mode="javascript"
rows={4}
value={JSON.stringify(sortObj(value || {}), undefined, 2)}
value={JSON.stringify(sortObj(value), undefined, 2)}
/>
);
break;
@ -42,7 +42,7 @@ export default ({ helpText, id, label, type, unit = '', value }) => {
label={label}
mode="javascript"
rows={4}
value={JSON.stringify(value || [], undefined, 2)}
value={JSON.stringify(value, undefined, 2)}
/>
);
break;

View File

@ -440,10 +440,8 @@ const ObjectField = ({ name, config, isRequired = false }) => {
const [field, meta, helpers] = useField({ name, validate });
const isValid = !(meta.touched && meta.error);
const emptyDefault = config?.type === 'list' ? '[]' : '{}';
const defaultRevertValue = config?.default
? JSON.stringify(config.default, null, 2)
: emptyDefault;
const defaultRevertValue =
config?.default !== null ? JSON.stringify(config.default, null, 2) : null;
return config ? (
<FormFullWidthLayout>
@ -458,7 +456,12 @@ const ObjectField = ({ name, config, isRequired = false }) => {
>
<CodeEditor
{...field}
rows="auto"
value={
field.value === null
? JSON.stringify(field.value, null, 2)
: field.value
}
rows={field.value !== null ? 'auto' : 1}
id={name}
mode="javascript"
onChange={(value) => {