questionnaire.test.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import { StatusType } from '../../../src/interfaces/questionnaire/questionnaire-answer-status';
  2. import QuestionnaireAnswerStatus from '../../../src/server/models/questionnaire/questionnaire-answer-status';
  3. import QuestionnaireOrder from '../../../src/server/models/questionnaire/questionnaire-order';
  4. import { getInstance } from '../setup-crowi';
  5. describe('QuestionnaireService', () => {
  6. let crowi;
  7. let user;
  8. beforeAll(async() => {
  9. process.env.APP_SITE_URL = 'http://growi.test.jp';
  10. process.env.DEPLOYMENT_TYPE = 'growi-docker-compose';
  11. process.env.SAML_ENABLED = 'true';
  12. crowi = await getInstance();
  13. crowi.configManager.updateConfigsInTheSameNamespace('crowi', {
  14. 'security:passport-saml:isEnabled': true,
  15. 'security:passport-github:isEnabled': true,
  16. });
  17. crowi.setupQuestionnaireService();
  18. const User = crowi.model('User');
  19. user = await User.create({
  20. name: 'Example for Questionnaire Service Test',
  21. username: 'questionnaire test user',
  22. email: 'questionnaireTestUser@example.com',
  23. password: 'usertestpass',
  24. createdAt: '2023-01-01',
  25. });
  26. });
  27. describe('getGrowiInfo', () => {
  28. test('Should get correct GROWI info', async() => {
  29. const growiInfo = await crowi.questionnaireService.getGrowiInfo();
  30. expect(growiInfo.appSiteUrlHashed).toBeTruthy();
  31. expect(growiInfo.appSiteUrlHashed).not.toBe('http://growi.test.jp');
  32. expect(growiInfo.osInfo.type).toBeTruthy();
  33. expect(growiInfo.osInfo.platform).toBeTruthy();
  34. expect(growiInfo.osInfo.arch).toBeTruthy();
  35. expect(growiInfo.osInfo.totalmem).toBeTruthy();
  36. delete growiInfo.appSiteUrlHashed;
  37. delete growiInfo.currentActiveUsersCount;
  38. delete growiInfo.currentUsersCount;
  39. delete growiInfo.osInfo;
  40. expect(growiInfo).toEqual({
  41. activeExternalAccountTypes: ['saml', 'github'],
  42. appSiteUrl: 'http://growi.test.jp',
  43. attachmentType: 'aws',
  44. deploymentType: 'growi-docker-compose',
  45. type: 'on-premise',
  46. version: crowi.version,
  47. wikiType: 'open',
  48. });
  49. });
  50. describe('When url hash settings is on', () => {
  51. beforeEach(async() => {
  52. process.env.QUESTIONNAIRE_IS_APP_SITE_URL_HASHED = 'true';
  53. await crowi.setupConfigManager();
  54. });
  55. test('Should return app url string', async() => {
  56. const growiInfo = await crowi.questionnaireService.getGrowiInfo();
  57. expect(growiInfo.appSiteUrl).toBe(null);
  58. expect(growiInfo.appSiteUrlHashed).not.toBe('http://growi.test.jp');
  59. expect(growiInfo.appSiteUrlHashed).toBeTruthy();
  60. });
  61. });
  62. });
  63. describe('getUserInfo', () => {
  64. test('Should get correct user info when user given', () => {
  65. const userInfo = crowi.questionnaireService.getUserInfo(user, 'growiurlhashfortest');
  66. expect(userInfo.userIdHash).toBeTruthy();
  67. expect(userInfo.userIdHash).not.toBe(user._id);
  68. delete userInfo.userIdHash;
  69. expect(userInfo).toEqual({ type: 'general', userCreatedAt: new Date('2023-01-01') });
  70. });
  71. test('Should get correct user info when user is null', () => {
  72. const userInfo = crowi.questionnaireService.getUserInfo(null, '');
  73. expect(userInfo).toEqual({ type: 'guest' });
  74. });
  75. });
  76. describe('getQuestionnaireOrdersToShow', () => {
  77. beforeAll(async() => {
  78. const questionnaireToBeShown = {
  79. _id: '63b8354837e7aa378e16f0b1',
  80. shortTitle: {
  81. ja_JP: 'GROWI に関するアンケート',
  82. en_US: 'Questions about GROWI',
  83. },
  84. title: {
  85. ja_JP: 'GROWI に関するアンケート',
  86. en_US: 'Questions about GROWI',
  87. },
  88. showFrom: '2022-12-11',
  89. showUntil: '2100-12-12',
  90. condition: {
  91. user: {
  92. types: ['general'],
  93. },
  94. growi: {
  95. types: ['on-premise'],
  96. versionRegExps: [crowi.version],
  97. },
  98. },
  99. createdAt: '2023-01-01',
  100. updatedAt: '2023-01-01',
  101. };
  102. // insert initial db data
  103. await QuestionnaireOrder.insertMany([
  104. questionnaireToBeShown,
  105. // finished
  106. {
  107. ...questionnaireToBeShown,
  108. _id: '63b8354837e7aa378e16f0b2',
  109. showFrom: '2020-12-11',
  110. showUntil: '2021-12-12',
  111. },
  112. // for admin or guest
  113. {
  114. ...questionnaireToBeShown,
  115. _id: '63b8354837e7aa378e16f0b3',
  116. condition: {
  117. user: {
  118. types: ['admin', 'guest'],
  119. },
  120. growi: {
  121. types: ['on-premise'],
  122. versionRegExps: [crowi.version],
  123. },
  124. },
  125. },
  126. // answered
  127. {
  128. ...questionnaireToBeShown,
  129. _id: '63b8354837e7aa378e16f0b4',
  130. },
  131. // skipped
  132. {
  133. ...questionnaireToBeShown,
  134. _id: '63b8354837e7aa378e16f0b5',
  135. },
  136. // denied
  137. {
  138. ...questionnaireToBeShown,
  139. _id: '63b8354837e7aa378e16f0b6',
  140. },
  141. // for different growi type
  142. {
  143. ...questionnaireToBeShown,
  144. _id: '63b8354837e7aa378e16f0b7',
  145. condition: {
  146. user: {
  147. types: ['general'],
  148. },
  149. growi: {
  150. types: ['cloud'],
  151. versionRegExps: [crowi.version],
  152. },
  153. },
  154. },
  155. // for different growi version
  156. {
  157. ...questionnaireToBeShown,
  158. _id: '63b8354837e7aa378e16f0b8',
  159. condition: {
  160. user: {
  161. types: ['general'],
  162. },
  163. growi: {
  164. types: ['on-premise'],
  165. versionRegExps: ['1.0.0-alpha'],
  166. },
  167. },
  168. },
  169. ]);
  170. await QuestionnaireAnswerStatus.insertMany([
  171. {
  172. user: user._id,
  173. questionnaireOrderId: '63b8354837e7aa378e16f0b4',
  174. status: StatusType.answered,
  175. },
  176. {
  177. user: user._id,
  178. questionnaireOrderId: '63b8354837e7aa378e16f0b5',
  179. status: StatusType.skipped,
  180. },
  181. {
  182. user: user._id,
  183. questionnaireOrderId: '63b8354837e7aa378e16f0b6',
  184. status: StatusType.skipped,
  185. },
  186. ]);
  187. });
  188. test('Should get questionnaire orders to show', async() => {
  189. const growiInfo = await crowi.questionnaireService.getGrowiInfo();
  190. const userInfo = crowi.questionnaireService.getUserInfo(user, growiInfo.appSiteUrlHashed);
  191. const questionnaireOrderDocuments = await crowi.questionnaireService.getQuestionnaireOrdersToShow(userInfo, growiInfo, user._id);
  192. const questionnaireOrderObjects = questionnaireOrderDocuments.map((document) => {
  193. const qo = document.toObject();
  194. delete qo.condition._id;
  195. return { ...qo, _id: qo._id.toString() };
  196. });
  197. expect(questionnaireOrderObjects).toEqual([
  198. {
  199. _id: '63b8354837e7aa378e16f0b1',
  200. __v: 0,
  201. shortTitle: {
  202. ja_JP: 'GROWI に関するアンケート',
  203. en_US: 'Questions about GROWI',
  204. },
  205. title: {
  206. ja_JP: 'GROWI に関するアンケート',
  207. en_US: 'Questions about GROWI',
  208. },
  209. showFrom: new Date('2022-12-11'),
  210. showUntil: new Date('2100-12-12'),
  211. questions: [],
  212. condition: {
  213. user: {
  214. types: ['general'],
  215. },
  216. growi: {
  217. types: ['on-premise'],
  218. versionRegExps: [crowi?.version],
  219. },
  220. },
  221. createdAt: new Date('2023-01-01'),
  222. updatedAt: new Date('2023-01-01'),
  223. },
  224. ]);
  225. });
  226. });
  227. });