Explorar el Código

Modified the test code to prevent the errors.

Shunm634-source hace 3 años
padre
commit
2512247322

+ 2 - 7
packages/app/src/server/routes/apiv3/pages.js

@@ -504,13 +504,8 @@ module.exports = (crowi) => {
     const nOptions = {
       ip: req.ip,
       endpoint: req.originalUrl,
-      action: options.isRecursively ? SupportedAction.ACTION_PAGE_RECURSIVELY_RENAME : SupportedAction.ACTION_PAGE_RENAME,
-      user: req.user?._id,
-      snapshot: {
-        username: req.user?.username,
-      },
+      activityId: res.locals.activity._id,
     };
-    const activityId = res.locals.activity._id;
 
     if (!isCreatablePage(newPagePath)) {
       return res.apiv3Err(new ErrorV3(`Could not use the path '${newPagePath}'`, 'invalid_path'), 409);
@@ -544,7 +539,7 @@ module.exports = (crowi) => {
       if (!page.isEmpty && !page.isUpdatable(revisionId)) {
         return res.apiv3Err(new ErrorV3('Someone could update this page, so couldn\'t delete.', 'notfound_or_forbidden'), 409);
       }
-      renamedPage = await crowi.pageService.renamePage(page, newPagePath, req.user, options, nOptions, activityId);
+      renamedPage = await crowi.pageService.renamePage(page, newPagePath, req.user, options, nOptions);
     }
     catch (err) {
       logger.error(err);

+ 2 - 2
packages/app/src/server/service/activity.ts

@@ -98,11 +98,11 @@ class ActivityService {
     }
 
     return Array.from(availableActionsSet);
-  }
+  };
 
   shoudUpdateActivity = function(action: SupportedActionType): boolean {
     return this.getAvailableActions().includes(action);
-  }
+  };
 
   // for GET request
   createActivity = async function(parameters): Promise<void> {

+ 26 - 11
packages/app/src/server/service/page.ts

@@ -350,19 +350,24 @@ class PageService {
       .cursor({ batchSize: BULK_REINDEX_SIZE });
   }
 
-  async renamePage(page: IPage, newPagePath, user, options, nOptions, activityId): Promise<PageDocument | null> {
+  async renamePage(page: IPage, newPagePath, user, options, nOptions): Promise<PageDocument | null> {
     /*
      * Common Operation
      */
     const Page = mongoose.model('Page') as unknown as PageModel;
-    this.crowi.activityService.createActivity(nOptions);
 
     const parameters = {
-      targetModel: SupportedTargetModel.MODEL_PAGE,
-      target: page,
-      action: nOptions.action,
+      ip: nOptions.ip,
+      endpoint: nOptions.endpoint,
+      action: page.descendantCount > 0 ? SupportedAction.ACTION_PAGE_RECURSIVELY_RENAME : SupportedAction.ACTION_PAGE_RENAME,
+      user,
+      snapshot: {
+        username: user.username,
+      },
     };
 
+    this.crowi.activityService.createActivity(parameters);
+
     const isExist = await Page.exists({ path: newPagePath });
     if (isExist) {
       throw Error(`Page already exists at ${newPagePath}`);
@@ -412,15 +417,20 @@ class PageService {
       logger.error('Failed to create PageOperation document.', err);
       throw err;
     }
-    const renamedPage = await this.renameMainOperation(page, newPagePath, user, options, pageOp._id, parameters, activityId);
+    const renamedPage = await this.renameMainOperation(page, newPagePath, user, options, pageOp._id, nOptions);
     if (!options.isRecursively) {
-      this.activityEvent.emit('update', activityId, parameters, page);
+      const parameters = {
+        targetModel: SupportedTargetModel.MODEL_PAGE,
+        target: page,
+        action: SupportedAction.ACTION_PAGE_RENAME,
+      };
+      this.activityEvent.emit('update', nOptions.activityId, parameters, page);
     }
 
     return renamedPage;
   }
 
-  async renameMainOperation(page, newPagePath: string, user, options, pageOpId: ObjectIdLike, parameters, activityId): Promise<PageDocument | null> {
+  async renameMainOperation(page, newPagePath: string, user, options, pageOpId: ObjectIdLike, nOptions): Promise<PageDocument | null> {
     const Page = mongoose.model('Page') as unknown as PageModel;
 
     const updateMetadata = options.updateMetadata || false;
@@ -505,21 +515,26 @@ class PageService {
     /*
      * Sub Operation
      */
-    this.renameSubOperation(page, newPagePath, user, options, renamedPage, pageOp._id, parameters, activityId);
+    this.renameSubOperation(page, newPagePath, user, options, renamedPage, pageOp._id, nOptions);
 
     return renamedPage;
   }
 
-  async renameSubOperation(page, newPagePath: string, user, options, renamedPage, pageOpId: ObjectIdLike, parameters?, activityId?): Promise<void> {
+  async renameSubOperation(page, newPagePath: string, user, options, renamedPage, pageOpId: ObjectIdLike, nOptions?): Promise<void> {
     const Page = mongoose.model('Page') as unknown as PageModel;
 
     const exParentId = page.parent;
 
     const timerObj = this.crowi.pageOperationService.autoUpdateExpiryDate(pageOpId);
+    const parameters = {
+      targetModel: SupportedTargetModel.MODEL_PAGE,
+      target: page,
+      action: SupportedAction.ACTION_PAGE_RECURSIVELY_RENAME,
+    };
     try {
     // update descendants first
       const descendantPages = await this.renameDescendantsWithStream(page, newPagePath, user, options, false);
-      this.activityEvent.emit('update', activityId, parameters, page, descendantPages);
+      this.activityEvent.emit('update', nOptions.activityId, parameters, page, descendantPages);
     }
     catch (err) {
       logger.warn(err);

+ 11 - 6
packages/app/test/integration/service/page.test.js

@@ -422,7 +422,7 @@ describe('PageService', () => {
     });
   });
 
-  describe('rename page', () => {
+  describe.only('rename page', () => {
     let pageEventSpy;
     let renameDescendantsWithStreamSpy;
     // mock new Date() and Date.now()
@@ -438,7 +438,8 @@ describe('PageService', () => {
 
       test('rename page without options', async() => {
 
-        const resultPage = await crowi.pageService.renamePage(parentForRename1, '/renamed1', testUser2, {});
+        const resultPage = await crowi.pageService.renamePage(parentForRename1,
+          '/renamed1', testUser2, {}, { ip: '::ffff:127.0.0.1', endpoint: '/_api/v3/pages/rename' });
 
         expect(xssSpy).toHaveBeenCalled();
 
@@ -451,7 +452,8 @@ describe('PageService', () => {
 
       test('rename page with updateMetadata option', async() => {
 
-        const resultPage = await crowi.pageService.renamePage(parentForRename2, '/renamed2', testUser2, { updateMetadata: true });
+        const resultPage = await crowi.pageService.renamePage(parentForRename2, '/renamed2', testUser2, { updateMetadata: true },
+          { ip: '::ffff:127.0.0.1', endpoint: '/_api/v3/pages/rename' });
 
         expect(xssSpy).toHaveBeenCalled();
 
@@ -464,7 +466,8 @@ describe('PageService', () => {
 
       test('rename page with createRedirectPage option', async() => {
 
-        const resultPage = await crowi.pageService.renamePage(parentForRename3, '/renamed3', testUser2, { createRedirectPage: true });
+        const resultPage = await crowi.pageService.renamePage(parentForRename3, '/renamed3', testUser2, { createRedirectPage: true },
+          { ip: '::ffff:127.0.0.1', endpoint: '/_api/v3/pages/rename' });
 
         expect(xssSpy).toHaveBeenCalled();
         expect(pageEventSpy).toHaveBeenCalledWith('rename');
@@ -476,7 +479,8 @@ describe('PageService', () => {
 
       test('rename page with isRecursively', async() => {
 
-        const resultPage = await crowi.pageService.renamePage(parentForRename4, '/renamed4', testUser2, { isRecursively: true });
+        const resultPage = await crowi.pageService.renamePage(parentForRename4, '/renamed4', testUser2, { isRecursively: true },
+          { ip: '::ffff:127.0.0.1', endpoint: '/_api/v3/pages/rename' });
 
         expect(xssSpy).toHaveBeenCalled();
         expect(renameDescendantsWithStreamSpy).toHaveBeenCalled();
@@ -489,7 +493,8 @@ describe('PageService', () => {
 
       test('rename page with different tree with isRecursively', async() => {
 
-        const resultPage = await crowi.pageService.renamePage(parentForRename5, '/parentForRename5/renamedChild', testUser1, { isRecursively: true });
+        const resultPage = await crowi.pageService.renamePage(parentForRename5, '/parentForRename5/renamedChild', testUser1, { isRecursively: true },
+          { ip: '::ffff:127.0.0.1', endpoint: '/_api/v3/pages/rename' });
         const wrongPage = await Page.findOne({ path: '/parentForRename5/renamedChild/renamedChild' });
         const expectPage = await Page.findOne({ path: '/parentForRename5/renamedChild' });