Skip to content

Commit

Permalink
Add schema validation for an upgrade if the schema exists (#1216)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andres Martinez Gotor authored Oct 14, 2019
1 parent fa77a4e commit e6247b1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
24 changes: 24 additions & 0 deletions dashboard/src/actions/apps.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,30 @@ describe("upgradeApp", () => {
await store.dispatch(provisionCMD);
expect(store.getActions()).toEqual(expectedActions);
});

it("returns false and dispatches UnprocessableEntity if the given values don't satisfy the schema ", async () => {
const res = await store.dispatch(
actions.apps.upgradeApp(
"my-version" as any,
"my-release",
definedNamespaces.default,
"foo: 1",
{ properties: { foo: { type: "string" } } },
),
);

expect(res).toBe(false);
const expectedActions = [
{ type: getType(actions.apps.requestUpgradeApp) },
{
type: getType(actions.apps.errorApps),
payload: new UnprocessableEntity(
"The given values don't match the required format. The following errors were found:\n - .foo: should be string",
),
},
];
expect(store.getActions()).toEqual(expectedActions);
});
});

describe("rollbackApp", () => {
Expand Down
12 changes: 12 additions & 0 deletions dashboard/src/actions/apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,25 @@ export function upgradeApp(
releaseName: string,
namespace: string,
values?: string,
schema?: JSONSchema4,
): ThunkAction<Promise<boolean>, IStoreState, null, AppsAction> {
return async (dispatch, getState) => {
dispatch(requestUpgradeApp());
try {
const {
config: { namespace: kubeappsNamespace },
} = getState();
if (values && schema) {
const validation = validate(values, schema);
if (!validation.valid) {
const errorText =
validation.errors &&
validation.errors.map(e => ` - ${e.dataPath}: ${e.message}`).join("\n");
throw new UnprocessableEntity(
`The given values don't match the required format. The following errors were found:\n${errorText}`,
);
}
}
await App.upgrade(releaseName, namespace, kubeappsNamespace, chartVersion, values);
dispatch(receiveUpgradeApp());
return true;
Expand Down

0 comments on commit e6247b1

Please sign in to comment.