Explorar el Código

Merge pull request #7420 from arafubeatbox/imprv/116146-116148-questionnaire-service-test

refs 116148: questionnaire service test
Ryoji Shimizu hace 3 años
padre
commit
12655228f2

+ 4 - 4
packages/app/src/interfaces/questionnaire/growi-info.ts

@@ -7,7 +7,7 @@ export const GrowiServiceType = {
   others: 'others',
   others: 'others',
 } as const;
 } as const;
 export const GrowiWikiType = { open: 'open', closed: 'closed' } as const;
 export const GrowiWikiType = { open: 'open', closed: 'closed' } as const;
-const GrowiAttachmentType = {
+export const GrowiAttachmentType = {
   aws: 'aws',
   aws: 'aws',
   gcs: 'gcs',
   gcs: 'gcs',
   gcp: 'gcp',
   gcp: 'gcp',
@@ -17,7 +17,7 @@ const GrowiAttachmentType = {
   local: 'local',
   local: 'local',
   none: 'none',
   none: 'none',
 } as const;
 } as const;
-const GrowiDeploymentType = {
+export const GrowiDeploymentType = {
   officialHelmChart: 'official-helm-chart',
   officialHelmChart: 'official-helm-chart',
   growiDockerCompose: 'growi-docker-compose',
   growiDockerCompose: 'growi-docker-compose',
   node: 'node',
   node: 'node',
@@ -33,8 +33,8 @@ export const GrowiExternalAuthProviderType = {
 
 
 export type GrowiServiceType = typeof GrowiServiceType[keyof typeof GrowiServiceType]
 export type GrowiServiceType = typeof GrowiServiceType[keyof typeof GrowiServiceType]
 type GrowiWikiType = typeof GrowiWikiType[keyof typeof GrowiWikiType]
 type GrowiWikiType = typeof GrowiWikiType[keyof typeof GrowiWikiType]
-type GrowiAttachmentType = typeof GrowiAttachmentType[keyof typeof GrowiAttachmentType]
-type GrowiDeploymentType = typeof GrowiDeploymentType[keyof typeof GrowiDeploymentType]
+export type GrowiAttachmentType = typeof GrowiAttachmentType[keyof typeof GrowiAttachmentType]
+export type GrowiDeploymentType = typeof GrowiDeploymentType[keyof typeof GrowiDeploymentType]
 export type GrowiExternalAuthProviderType = typeof GrowiExternalAuthProviderType[keyof typeof GrowiExternalAuthProviderType]
 export type GrowiExternalAuthProviderType = typeof GrowiExternalAuthProviderType[keyof typeof GrowiExternalAuthProviderType]
 
 
 interface IGrowiOSInfo {
 interface IGrowiOSInfo {

+ 15 - 4
packages/app/src/server/service/questionnaire.ts

@@ -1,7 +1,9 @@
 import crypto from 'crypto';
 import crypto from 'crypto';
 import * as os from 'node:os';
 import * as os from 'node:os';
 
 
-import { GrowiWikiType, GrowiExternalAuthProviderType, IGrowiInfo } from '~/interfaces/questionnaire/growi-info';
+import {
+  GrowiWikiType, GrowiExternalAuthProviderType, IGrowiInfo, GrowiServiceType, GrowiAttachmentType, GrowiDeploymentType,
+} from '~/interfaces/questionnaire/growi-info';
 import { StatusType } from '~/interfaces/questionnaire/questionnaire-answer-status';
 import { StatusType } from '~/interfaces/questionnaire/questionnaire-answer-status';
 import { IUserInfo, UserType } from '~/interfaces/questionnaire/user-info';
 import { IUserInfo, UserType } from '~/interfaces/questionnaire/user-info';
 import { IUserHasId } from '~/interfaces/user';
 import { IUserHasId } from '~/interfaces/user';
@@ -38,6 +40,15 @@ class QuestionnaireService {
       return this.crowi.configManager.getConfig('crowi', `security:passport-${type}:isEnabled`);
       return this.crowi.configManager.getConfig('crowi', `security:passport-${type}:isEnabled`);
     });
     });
 
 
+    const typeStr = this.crowi.configManager.getConfig('crowi', 'app:serviceType');
+    const type = Object.values(GrowiServiceType).includes(typeStr) ? typeStr : null;
+
+    const attachmentTypeStr = this.crowi.configManager.getConfig('crowi', 'app:fileUploadType');
+    const attachmentType = Object.values(GrowiAttachmentType).includes(attachmentTypeStr) ? attachmentTypeStr : null;
+
+    const deploymentTypeStr = this.crowi.configManager.getConfig('crowi', 'app:deploymentType');
+    const deploymentType = Object.values(GrowiDeploymentType).includes(deploymentTypeStr) ? deploymentTypeStr : null;
+
     return {
     return {
       version: this.crowi.version,
       version: this.crowi.version,
       osInfo: {
       osInfo: {
@@ -48,13 +59,13 @@ class QuestionnaireService {
       },
       },
       appSiteUrl: this.crowi.configManager.getConfig('crowi', 'questionnaire:isAppSiteUrlHashed') ? null : appSiteUrl,
       appSiteUrl: this.crowi.configManager.getConfig('crowi', 'questionnaire:isAppSiteUrlHashed') ? null : appSiteUrl,
       appSiteUrlHashed,
       appSiteUrlHashed,
-      type: this.crowi.configManager.getConfig('crowi', 'app:serviceType'),
+      type,
       currentUsersCount,
       currentUsersCount,
       currentActiveUsersCount,
       currentActiveUsersCount,
       wikiType,
       wikiType,
-      attachmentType: this.crowi.configManager.getConfig('crowi', 'app:fileUploadType'),
+      attachmentType,
       activeExternalAccountTypes,
       activeExternalAccountTypes,
-      deploymentType: this.crowi.configManager.getConfig('crowi', 'app:deploymentType'),
+      deploymentType,
     };
     };
   }
   }
 
 

+ 0 - 2
packages/app/test/integration/service/questionnaire-cron.test.ts

@@ -129,8 +129,6 @@ describe('QuestionnaireCronService', () => {
 
 
   beforeAll(async() => {
   beforeAll(async() => {
     crowi = await getInstance();
     crowi = await getInstance();
-    // reload
-    await crowi.setupConfigManager();
   });
   });
 
 
   beforeEach(async() => {
   beforeEach(async() => {

+ 248 - 0
packages/app/test/integration/service/questionnaire.test.ts

@@ -0,0 +1,248 @@
+import mongoose from 'mongoose';
+
+import { StatusType } from '../../../src/interfaces/questionnaire/questionnaire-answer-status';
+import QuestionnaireAnswerStatus from '../../../src/server/models/questionnaire/questionnaire-answer-status';
+import QuestionnaireOrder from '../../../src/server/models/questionnaire/questionnaire-order';
+import { getInstance } from '../setup-crowi';
+
+describe('QuestionnaireService', () => {
+  let crowi;
+  let user;
+
+  beforeAll(async() => {
+    process.env.APP_SITE_URL = 'http://growi.test.jp';
+    process.env.DEPLOYMENT_TYPE = 'growi-docker-compose';
+    process.env.SAML_ENABLED = 'true';
+    crowi = await getInstance();
+
+    crowi.configManager.updateConfigsInTheSameNamespace('crowi', {
+      'security:passport-saml:isEnabled': true,
+      'security:passport-github:isEnabled': true,
+    });
+
+    crowi.setupQuestionnaireService();
+
+    const User = crowi.model('User');
+    user = await User.create({
+      name: 'Example for Questionnaire Service Test',
+      username: 'questionnaire test user',
+      email: 'questionnaireTestUser@example.com',
+      password: 'usertestpass',
+      createdAt: '2023-01-01',
+    });
+  });
+
+  describe('getGrowiInfo', () => {
+    test('Should get correct GROWI info', async() => {
+      const growiInfo = await crowi.questionnaireService.getGrowiInfo();
+
+      expect(growiInfo.appSiteUrlHashed).toBeTruthy();
+      expect(growiInfo.appSiteUrlHashed).not.toBe('http://growi.test.jp');
+      expect(growiInfo.osInfo.type).toBeTruthy();
+      expect(growiInfo.osInfo.platform).toBeTruthy();
+      expect(growiInfo.osInfo.arch).toBeTruthy();
+      expect(growiInfo.osInfo.totalmem).toBeTruthy();
+
+      delete growiInfo.appSiteUrlHashed;
+      delete growiInfo.currentActiveUsersCount;
+      delete growiInfo.currentUsersCount;
+      delete growiInfo.osInfo;
+
+      expect(growiInfo).toEqual({
+        activeExternalAccountTypes: ['saml', 'github'],
+        appSiteUrl: null,
+        attachmentType: 'aws',
+        deploymentType: 'growi-docker-compose',
+        type: 'on-premise',
+        version: crowi.version,
+        wikiType: 'open',
+      });
+    });
+
+    describe('When url hash settings is off', () => {
+      beforeEach(async() => {
+        process.env.QUESTIONNAIRE_IS_APP_SITE_URL_HASHED = 'false';
+        await crowi.setupConfigManager();
+      });
+
+      test('Should return app url string', async() => {
+        const growiInfo = await crowi.questionnaireService.getGrowiInfo();
+        expect(growiInfo.appSiteUrl).toBe('http://growi.test.jp');
+        expect(growiInfo.appSiteUrlHashed).not.toBe('http://growi.test.jp');
+        expect(growiInfo.appSiteUrlHashed).toBeTruthy();
+      });
+    });
+  });
+
+  describe('getUserInfo', () => {
+    test('Should get correct user info when user given', () => {
+      const userInfo = crowi.questionnaireService.getUserInfo(user, 'growiurlhashfortest');
+      expect(userInfo.userIdHash).toBeTruthy();
+      expect(userInfo.userIdHash).not.toBe(user._id);
+
+      delete userInfo.userIdHash;
+
+      expect(userInfo).toEqual({ type: 'general', userCreatedAt: new Date('2023-01-01') });
+    });
+
+    test('Should get correct user info when user is null', () => {
+      const userInfo = crowi.questionnaireService.getUserInfo(null, '');
+      expect(userInfo).toEqual({ type: 'guest' });
+    });
+  });
+
+  describe('getQuestionnaireOrdersToShow', () => {
+    beforeAll(async() => {
+      const questionnaireToBeShown = {
+        _id: '63b8354837e7aa378e16f0b1',
+        shortTitle: {
+          ja_JP: 'GROWI に関するアンケート',
+          en_US: 'Questions about GROWI',
+        },
+        title: {
+          ja_JP: 'GROWI に関するアンケート',
+          en_US: 'Questions about GROWI',
+        },
+        showFrom: '2022-12-11',
+        showUntil: '2100-12-12',
+        condition: {
+          user: {
+            types: ['general'],
+          },
+          growi: {
+            types: ['on-premise'],
+            versionRegExps: [crowi.version],
+          },
+        },
+        createdAt: '2023-01-01',
+        updatedAt: '2023-01-01',
+      };
+
+      // insert initial db data
+      await QuestionnaireOrder.insertMany([
+        questionnaireToBeShown,
+        // finished
+        {
+          ...questionnaireToBeShown,
+          _id: '63b8354837e7aa378e16f0b2',
+          showFrom: '2020-12-11',
+          showUntil: '2021-12-12',
+        },
+        // for admin or guest
+        {
+          ...questionnaireToBeShown,
+          _id: '63b8354837e7aa378e16f0b3',
+          condition: {
+            user: {
+              types: ['admin', 'guest'],
+            },
+            growi: {
+              types: ['on-premise'],
+              versionRegExps: [crowi.version],
+            },
+          },
+        },
+        // answered
+        {
+          ...questionnaireToBeShown,
+          _id: '63b8354837e7aa378e16f0b4',
+        },
+        // skipped
+        {
+          ...questionnaireToBeShown,
+          _id: '63b8354837e7aa378e16f0b5',
+        },
+        // denied
+        {
+          ...questionnaireToBeShown,
+          _id: '63b8354837e7aa378e16f0b6',
+        },
+        // for different growi type
+        {
+          ...questionnaireToBeShown,
+          _id: '63b8354837e7aa378e16f0b7',
+          condition: {
+            user: {
+              types: ['general'],
+            },
+            growi: {
+              types: ['cloud'],
+              versionRegExps: [crowi.version],
+            },
+          },
+        },
+        // for different growi version
+        {
+          ...questionnaireToBeShown,
+          _id: '63b8354837e7aa378e16f0b8',
+          condition: {
+            user: {
+              types: ['general'],
+            },
+            growi: {
+              types: ['on-premise'],
+              versionRegExps: ['1.0.0-alpha'],
+            },
+          },
+        },
+      ]);
+
+      await QuestionnaireAnswerStatus.insertMany([
+        {
+          user: user._id,
+          questionnaireOrderId: '63b8354837e7aa378e16f0b4',
+          status: StatusType.answered,
+        },
+        {
+          user: user._id,
+          questionnaireOrderId: '63b8354837e7aa378e16f0b5',
+          status: StatusType.skipped,
+        },
+        {
+          user: user._id,
+          questionnaireOrderId: '63b8354837e7aa378e16f0b6',
+          status: StatusType.skipped,
+        },
+      ]);
+    });
+
+    test('Should get questionnaire orders to show', async() => {
+      const growiInfo = await crowi.questionnaireService.getGrowiInfo();
+      const userInfo = crowi.questionnaireService.getUserInfo(user, growiInfo.appSiteUrlHashed);
+      const questionnaireOrderDocuments = await crowi.questionnaireService.getQuestionnaireOrdersToShow(userInfo, growiInfo, user._id);
+      const questionnaireOrderObjects = questionnaireOrderDocuments.map((document) => {
+        const qo = document.toObject();
+        delete qo.condition._id;
+        return { ...qo, _id: qo._id.toString() };
+      });
+      expect(questionnaireOrderObjects).toEqual([
+        {
+          _id: '63b8354837e7aa378e16f0b1',
+          __v: 0,
+          shortTitle: {
+            ja_JP: 'GROWI に関するアンケート',
+            en_US: 'Questions about GROWI',
+          },
+          title: {
+            ja_JP: 'GROWI に関するアンケート',
+            en_US: 'Questions about GROWI',
+          },
+          showFrom: new Date('2022-12-11'),
+          showUntil: new Date('2100-12-12'),
+          questions: [],
+          condition: {
+            user: {
+              types: ['general'],
+            },
+            growi: {
+              types: ['on-premise'],
+              versionRegExps: [crowi?.version],
+            },
+          },
+          createdAt: new Date('2023-01-01'),
+          updatedAt: new Date('2023-01-01'),
+        },
+      ]);
+    });
+  });
+});