Explorar o código

add PageBulkExportJobCronService.deleteFailedExportJobs test

Futa Arai hai 1 ano
pai
achega
390a8dfa7c

+ 2 - 2
apps/app/src/features/page-bulk-export/server/service/page-bulk-export-job-cron.ts

@@ -1,4 +1,3 @@
-import { SupportedAction } from '~/interfaces/activity';
 import { configManager } from '~/server/service/config-manager';
 import { configManager } from '~/server/service/config-manager';
 import CronService from '~/server/service/cron';
 import CronService from '~/server/service/cron';
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
@@ -56,7 +55,8 @@ class PageBulkExportJobCronService extends CronService {
     });
     });
     for (const downloadExpiredExportJob of downloadExpiredExportJobs) {
     for (const downloadExpiredExportJob of downloadExpiredExportJobs) {
       try {
       try {
-        this.crowi.attachmentService?.removeAttachment(downloadExpiredExportJob.attachment);
+        // eslint-disable-next-line no-await-in-loop
+        await this.crowi.attachmentService?.removeAttachment(downloadExpiredExportJob.attachment);
       }
       }
       catch (err) {
       catch (err) {
         logger.error(err);
         logger.error(err);

+ 73 - 0
apps/app/test/integration/service/page-bulk-export-job-cron.test.ts

@@ -0,0 +1,73 @@
+// eslint-disable-next-line no-restricted-imports
+import axios from 'axios';
+
+import { PageBulkExportFormat, PageBulkExportJobStatus } from '../../../src/features/page-bulk-export/interfaces/page-bulk-export';
+import PageBulkExportJob from '../../../src/features/page-bulk-export/server/models/page-bulk-export-job';
+import { configManager } from '../../../src/server/service/config-manager';
+import { getInstance } from '../setup-crowi';
+
+const spyAxiosGet = jest.spyOn<typeof axios, 'get'>(
+  axios,
+  'get',
+);
+
+const spyAxiosPost = jest.spyOn<typeof axios, 'post'>(
+  axios,
+  'post',
+);
+
+describe('PageBulkExportJobCronService', () => {
+  let crowi;
+  let user;
+
+  beforeAll(async() => {
+    await configManager.loadConfigs();
+    await configManager.updateConfigsInTheSameNamespace('crowi', { 'app:fileUploadType': 'aws' });
+
+    crowi = await getInstance();
+    const User = crowi.model('User');
+    user = await User.create({
+      name: 'Example for PageBulkExportJobCronService Test',
+      username: 'page bulk export job cron test user',
+      email: 'bulkExportCronTestUser@example.com',
+      password: 'usertestpass',
+      createdAt: '2020-01-01',
+    });
+  });
+
+  beforeEach(async() => {
+    await PageBulkExportJob.deleteMany();
+  });
+
+  // describe('deleteDownloadExpiredExportJobs', () => {
+  //   beforeAll(async() => {
+  //     await configManager.updateConfigsInTheSameNamespace('crowi', { 'app:bulkExportDownloadExpirationSeconds': 86400 }); // 1 day
+  //   });
+  // });
+
+  describe('deleteFailedExportJobs', () => {
+    beforeAll(async() => {
+      await configManager.updateConfigsInTheSameNamespace('crowi', { 'app:bulkExportDownloadExpirationSeconds': 86400 }); // 1 day
+
+      await PageBulkExportJob.insertMany([
+        {
+          user, page: '/test-path1', format: PageBulkExportFormat.md, status: PageBulkExportJobStatus.failed,
+        },
+        {
+          user, page: '/test-path2', format: PageBulkExportFormat.md, status: PageBulkExportJobStatus.initializing,
+        },
+        {
+          user, page: '/test-path3', format: PageBulkExportFormat.md, status: PageBulkExportJobStatus.failed,
+        },
+      ]);
+    });
+
+    test('should delete failed export jobs', async() => {
+      expect(await PageBulkExportJob.find()).toHaveLength(2);
+      const pageBulkExportJobCronService = crowi.pageBulkExportJobCronService;
+      await pageBulkExportJobCronService.deleteFailedExportJobs();
+
+      expect(await PageBulkExportJob.find()).toHaveLength(1);
+    });
+  });
+});