questionnaire.test.ts 7.6 KB

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