Page MenuHomeSealhub

disallow-update.subtest.ts
No OneTemporary

disallow-update.subtest.ts

import { App, Field, Context, Collection } from "../../../main";
import assert from "assert";
import { with_running_app } from "../../../../test_utils/with-test-app.js";
import assert_throws_async from "../../../../test_utils/assert_throws_async.js";
const url = "/api/v1/collections/constseals";
describe("disallow-update", () => {
async function setup(app: App) {
class NullOrFive extends Field {
getTypeName = () => "null-or-five";
async isProperValue(_: Context, __: any, new_value: any) {
if (new_value === null || new_value === 5) {
return Field.valid();
}
return Field.invalid("Null or five, you got it?");
}
}
Collection.fromDefinition(app, {
name: "constseals",
fields: [
{
name: "age",
type: "disallow-update",
params: {
target_field: {
type: "int",
params: {
min: 0,
},
},
},
required: true,
},
{
name: "attribute",
type: "disallow-update",
params: {
target_field: {
type: NullOrFive,
},
},
required: true,
},
],
access_strategy: {
default: "public",
},
});
}
it("Respects target field type", () =>
with_running_app(async ({ app, rest_api }) => {
await setup(app);
await assert_throws_async(
() => rest_api.post(url, { age: "abc", attribute: 5 }),
(error) => {
assert.deepEqual(
error.response.data.data.age.message,
"Value 'abc' is not a int number format."
);
}
);
}));
it("Respects target field params", () =>
with_running_app(async ({ app, rest_api }) => {
await setup(app);
await assert_throws_async(
() => rest_api.post(url, { age: -2 }),
(error) =>
assert.deepEqual(
error.response.data.data.age.message,
"Value should be larger or equal to '0'."
)
);
}));
it("Initially allows to insert a value", () =>
with_running_app(async ({ app, rest_api }) => {
await setup(app);
await rest_api.post(url, { age: 2, attribute: 5 });
}));
it("Rejects a new value if there's an old value", () =>
with_running_app(async ({ app, rest_api }) => {
await setup(app);
const { id } = await rest_api.post(url, {
age: 18,
attribute: null,
});
await assert_throws_async(
() => rest_api.patch(`${url}/${id}`, { age: 21 }),
(error) =>
assert.deepEqual(
error.response.data.data.age.message,
"You cannot change previously set value."
)
);
}));
it("Rejects a new value if the old value is `null`", () =>
with_running_app(async ({ app, rest_api }) => {
await setup(app);
const { id } = await rest_api.post(url, {
age: 21,
attribute: null,
});
await assert_throws_async(
() => rest_api.patch(`${url}/${id}`, { attribute: 5 }),
(error) =>
assert.deepEqual(
error.response.data.data.attribute.message,
"You cannot change previously set value."
)
);
}));
});

File Metadata

Mime Type
text/x-java
Expires
Fri, Jan 24, 15:15 (1 d, 8 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
600982
Default Alt Text
disallow-update.subtest.ts (2 KB)

Event Timeline