Request validation for /api/subscriptions.* (#5476)
* chore: req validation for subscriptions.list * chore: req validation for subscriptions.info * chore: req validation for subscriptions.create * chore: req validation for subscriptions.delete * fix: reuse validations
This commit is contained in:
1
server/routes/api/subscriptions/index.ts
Normal file
1
server/routes/api/subscriptions/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export { default } from "./subscriptions";
|
||||||
44
server/routes/api/subscriptions/schema.ts
Normal file
44
server/routes/api/subscriptions/schema.ts
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
import { z } from "zod";
|
||||||
|
import { ValidateDocumentId } from "@server/validation";
|
||||||
|
import BaseSchema from "../BaseSchema";
|
||||||
|
|
||||||
|
export const SubscriptionsListSchema = BaseSchema.extend({
|
||||||
|
body: z.object({
|
||||||
|
documentId: z.string().refine(ValidateDocumentId.isValid, {
|
||||||
|
message: ValidateDocumentId.message,
|
||||||
|
}),
|
||||||
|
event: z.literal("documents.update"),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type SubscriptionsListReq = z.infer<typeof SubscriptionsListSchema>;
|
||||||
|
|
||||||
|
export const SubscriptionsInfoSchema = BaseSchema.extend({
|
||||||
|
body: z.object({
|
||||||
|
documentId: z.string().refine(ValidateDocumentId.isValid, {
|
||||||
|
message: ValidateDocumentId.message,
|
||||||
|
}),
|
||||||
|
event: z.literal("documents.update"),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type SubscriptionsInfoReq = z.infer<typeof SubscriptionsInfoSchema>;
|
||||||
|
|
||||||
|
export const SubscriptionsCreateSchema = BaseSchema.extend({
|
||||||
|
body: z.object({
|
||||||
|
documentId: z.string().refine(ValidateDocumentId.isValid, {
|
||||||
|
message: ValidateDocumentId.message,
|
||||||
|
}),
|
||||||
|
event: z.literal("documents.update"),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type SubscriptionsCreateReq = z.infer<typeof SubscriptionsCreateSchema>;
|
||||||
|
|
||||||
|
export const SubscriptionsDeleteSchema = BaseSchema.extend({
|
||||||
|
body: z.object({
|
||||||
|
id: z.string().uuid(),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type SubscriptionsDeleteReq = z.infer<typeof SubscriptionsDeleteSchema>;
|
||||||
@@ -140,7 +140,7 @@ describe("#subscriptions.create", () => {
|
|||||||
expect(body.ok).toEqual(false);
|
expect(body.ok).toEqual(false);
|
||||||
expect(body.error).toEqual("validation_error");
|
expect(body.error).toEqual("validation_error");
|
||||||
expect(body.message).toEqual(
|
expect(body.message).toEqual(
|
||||||
"Not a valid subscription event for documents"
|
`event: Invalid literal value, expected "documents.update"`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -323,7 +323,7 @@ describe("#subscriptions.info", () => {
|
|||||||
expect(response0.ok).toEqual(false);
|
expect(response0.ok).toEqual(false);
|
||||||
expect(response0.error).toEqual("validation_error");
|
expect(response0.error).toEqual("validation_error");
|
||||||
expect(response0.message).toEqual(
|
expect(response0.message).toEqual(
|
||||||
"Not a valid subscription event for documents"
|
`event: Invalid literal value, expected "documents.update"`
|
||||||
);
|
);
|
||||||
|
|
||||||
// `viewer` wants info about `subscriber`'s
|
// `viewer` wants info about `subscriber`'s
|
||||||
@@ -343,7 +343,7 @@ describe("#subscriptions.info", () => {
|
|||||||
expect(response1.ok).toEqual(false);
|
expect(response1.ok).toEqual(false);
|
||||||
expect(response1.error).toEqual("validation_error");
|
expect(response1.error).toEqual("validation_error");
|
||||||
expect(response1.message).toEqual(
|
expect(response1.message).toEqual(
|
||||||
"Not a valid subscription event for documents"
|
`event: Invalid literal value, expected "documents.update"`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -515,7 +515,7 @@ describe("#subscriptions.list", () => {
|
|||||||
expect(body.ok).toEqual(false);
|
expect(body.ok).toEqual(false);
|
||||||
expect(body.error).toEqual("validation_error");
|
expect(body.error).toEqual("validation_error");
|
||||||
expect(body.message).toEqual(
|
expect(body.message).toEqual(
|
||||||
"Not a valid subscription event for documents"
|
`event: Invalid literal value, expected "documents.update"`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -3,12 +3,13 @@ import subscriptionCreator from "@server/commands/subscriptionCreator";
|
|||||||
import subscriptionDestroyer from "@server/commands/subscriptionDestroyer";
|
import subscriptionDestroyer from "@server/commands/subscriptionDestroyer";
|
||||||
import auth from "@server/middlewares/authentication";
|
import auth from "@server/middlewares/authentication";
|
||||||
import { transaction } from "@server/middlewares/transaction";
|
import { transaction } from "@server/middlewares/transaction";
|
||||||
|
import validate from "@server/middlewares/validate";
|
||||||
import { Subscription, Document } from "@server/models";
|
import { Subscription, Document } from "@server/models";
|
||||||
import { authorize } from "@server/policies";
|
import { authorize } from "@server/policies";
|
||||||
import { presentSubscription } from "@server/presenters";
|
import { presentSubscription } from "@server/presenters";
|
||||||
import { APIContext } from "@server/types";
|
import { APIContext } from "@server/types";
|
||||||
import { assertIn, assertUuid } from "@server/validation";
|
import pagination from "../middlewares/pagination";
|
||||||
import pagination from "./middlewares/pagination";
|
import * as T from "./schema";
|
||||||
|
|
||||||
const router = new Router();
|
const router = new Router();
|
||||||
|
|
||||||
@@ -16,17 +17,10 @@ router.post(
|
|||||||
"subscriptions.list",
|
"subscriptions.list",
|
||||||
auth(),
|
auth(),
|
||||||
pagination(),
|
pagination(),
|
||||||
async (ctx: APIContext) => {
|
validate(T.SubscriptionsListSchema),
|
||||||
|
async (ctx: APIContext<T.SubscriptionsListReq>) => {
|
||||||
const { user } = ctx.state.auth;
|
const { user } = ctx.state.auth;
|
||||||
const { documentId, event } = ctx.request.body;
|
const { documentId, event } = ctx.input.body;
|
||||||
|
|
||||||
assertUuid(documentId, "documentId is required");
|
|
||||||
|
|
||||||
assertIn(
|
|
||||||
event,
|
|
||||||
["documents.update"],
|
|
||||||
`Not a valid subscription event for documents`
|
|
||||||
);
|
|
||||||
|
|
||||||
const document = await Document.findByPk(documentId, { userId: user.id });
|
const document = await Document.findByPk(documentId, { userId: user.id });
|
||||||
|
|
||||||
@@ -50,53 +44,43 @@ router.post(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
router.post("subscriptions.info", auth(), async (ctx: APIContext) => {
|
router.post(
|
||||||
const { user } = ctx.state.auth;
|
"subscriptions.info",
|
||||||
const { documentId, event } = ctx.request.body;
|
auth(),
|
||||||
|
validate(T.SubscriptionsInfoSchema),
|
||||||
|
async (ctx: APIContext<T.SubscriptionsInfoReq>) => {
|
||||||
|
const { user } = ctx.state.auth;
|
||||||
|
const { documentId, event } = ctx.input.body;
|
||||||
|
|
||||||
assertUuid(documentId, "documentId is required");
|
const document = await Document.findByPk(documentId, { userId: user.id });
|
||||||
|
|
||||||
assertIn(
|
authorize(user, "read", document);
|
||||||
event,
|
|
||||||
["documents.update"],
|
|
||||||
"Not a valid subscription event for documents"
|
|
||||||
);
|
|
||||||
|
|
||||||
const document = await Document.findByPk(documentId, { userId: user.id });
|
// There can be only one subscription with these props.
|
||||||
|
const subscription = await Subscription.findOne({
|
||||||
|
where: {
|
||||||
|
userId: user.id,
|
||||||
|
documentId: document.id,
|
||||||
|
event,
|
||||||
|
},
|
||||||
|
rejectOnEmpty: true,
|
||||||
|
});
|
||||||
|
|
||||||
authorize(user, "read", document);
|
ctx.body = {
|
||||||
|
data: presentSubscription(subscription),
|
||||||
// There can be only one subscription with these props.
|
};
|
||||||
const subscription = await Subscription.findOne({
|
}
|
||||||
where: {
|
);
|
||||||
userId: user.id,
|
|
||||||
documentId: document.id,
|
|
||||||
event,
|
|
||||||
},
|
|
||||||
rejectOnEmpty: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
ctx.body = {
|
|
||||||
data: presentSubscription(subscription),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
router.post(
|
router.post(
|
||||||
"subscriptions.create",
|
"subscriptions.create",
|
||||||
auth(),
|
auth(),
|
||||||
|
validate(T.SubscriptionsCreateSchema),
|
||||||
transaction(),
|
transaction(),
|
||||||
async (ctx: APIContext) => {
|
async (ctx: APIContext<T.SubscriptionsCreateReq>) => {
|
||||||
const { auth, transaction } = ctx.state;
|
const { auth, transaction } = ctx.state;
|
||||||
const { user } = auth;
|
const { user } = auth;
|
||||||
const { documentId, event } = ctx.request.body;
|
const { documentId, event } = ctx.input.body;
|
||||||
|
|
||||||
assertUuid(documentId, "documentId is required");
|
|
||||||
|
|
||||||
assertIn(
|
|
||||||
event,
|
|
||||||
["documents.update"],
|
|
||||||
"Not a valid subscription event for documents"
|
|
||||||
);
|
|
||||||
|
|
||||||
const document = await Document.findByPk(documentId, {
|
const document = await Document.findByPk(documentId, {
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
@@ -122,13 +106,12 @@ router.post(
|
|||||||
router.post(
|
router.post(
|
||||||
"subscriptions.delete",
|
"subscriptions.delete",
|
||||||
auth(),
|
auth(),
|
||||||
|
validate(T.SubscriptionsDeleteSchema),
|
||||||
transaction(),
|
transaction(),
|
||||||
async (ctx: APIContext) => {
|
async (ctx: APIContext<T.SubscriptionsDeleteReq>) => {
|
||||||
const { auth, transaction } = ctx.state;
|
const { auth, transaction } = ctx.state;
|
||||||
const { user } = auth;
|
const { user } = auth;
|
||||||
const { id } = ctx.request.body;
|
const { id } = ctx.input.body;
|
||||||
|
|
||||||
assertUuid(id, "id is required");
|
|
||||||
|
|
||||||
const subscription = await Subscription.findByPk(id, {
|
const subscription = await Subscription.findByPk(id, {
|
||||||
rejectOnEmpty: true,
|
rejectOnEmpty: true,
|
||||||
Reference in New Issue
Block a user