Yuki Takei 2 лет назад
Родитель
Сommit
66d38eb6dd

+ 17 - 0
packages/remark-lsx/src/server/routes/list-pages/index.spec.ts

@@ -2,10 +2,27 @@ import type { IUser } from '@growi/core';
 import type { Request, Response } from 'express';
 import { mock } from 'vitest-mock-extended';
 
+import type { PageQueryBuilder } from './generate-base-query';
+
 import { listPages } from '.';
 
 describe('listPages', () => {
 
+  it("returns 400 HTTP response when the query 'pagePath' is undefined", async() => {
+    // setup
+    const reqMock = mock<Request & { user: IUser }>();
+    const resMock = mock<Response>();
+    const resStatusMock = mock<Response>();
+    resMock.status.calledWith(400).mockReturnValue(resStatusMock);
+
+    // when
+    await listPages(reqMock, resMock);
+
+    // then
+    expect(resMock.status).toBeCalledWith(400);
+    expect(resStatusMock.send).toHaveBeenCalledOnce();
+  });
+
   describe('with num option', () => {
 
     it('returns 500 HTTP response when an unexpected error occured', async() => {

+ 8 - 3
packages/remark-lsx/src/server/routes/list-pages/index.ts

@@ -144,11 +144,16 @@ function addSortCondition(query, pagePath, optionsSortArg, optionsReverse) {
 export const listPages = async(req: Request & { user: IUser }, res: Response): Promise<Response> => {
   const user = req.user;
 
-  let pagePath;
-  let options;
+  let pagePath: string;
+  let options: string | undefined;
 
   try {
-    pagePath = req.query.pagePath;
+    // TODO: use express-validator
+    if (req.query.pagePath == null) {
+      throw new Error("The 'pagePath' query must not be null.");
+    }
+
+    pagePath = req.query.pagePath?.toString();
     if (req.query.options != null) {
       options = JSON.parse(req.query.options.toString());
     }