questionnaire.test.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import { StatusType } from '../../../src/features/questionnaire/interfaces/questionnaire-answer-status';
  2. import QuestionnaireAnswerStatus from '../../../src/features/questionnaire/server/models/questionnaire-answer-status';
  3. import QuestionnaireOrder from '../../../src/features/questionnaire/server/models/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: '2000-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: 'closed',
  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('2000-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. daysSinceCreation: {
  94. moreThanOrEqualTo: 365,
  95. lessThanOrEqualTo: 365 * 1000,
  96. },
  97. },
  98. growi: {
  99. types: ['on-premise'],
  100. versionRegExps: [crowi.version],
  101. },
  102. },
  103. createdAt: '2023-01-01',
  104. updatedAt: '2023-01-01',
  105. };
  106. // insert initial db data
  107. await QuestionnaireOrder.insertMany([
  108. questionnaireToBeShown,
  109. // finished
  110. {
  111. ...questionnaireToBeShown,
  112. _id: '63b8354837e7aa378e16f0b2',
  113. showFrom: '2020-12-11',
  114. showUntil: '2021-12-12',
  115. },
  116. // for admin or guest
  117. {
  118. ...questionnaireToBeShown,
  119. _id: '63b8354837e7aa378e16f0b3',
  120. condition: {
  121. user: {
  122. types: ['admin', 'guest'],
  123. },
  124. growi: {
  125. types: ['on-premise'],
  126. versionRegExps: [crowi.version],
  127. },
  128. },
  129. },
  130. // answered
  131. {
  132. ...questionnaireToBeShown,
  133. _id: '63b8354837e7aa378e16f0b4',
  134. },
  135. // skipped
  136. {
  137. ...questionnaireToBeShown,
  138. _id: '63b8354837e7aa378e16f0b5',
  139. },
  140. // denied
  141. {
  142. ...questionnaireToBeShown,
  143. _id: '63b8354837e7aa378e16f0b6',
  144. },
  145. // for different growi type
  146. {
  147. ...questionnaireToBeShown,
  148. _id: '63b8354837e7aa378e16f0b7',
  149. condition: {
  150. user: {
  151. types: ['general'],
  152. },
  153. growi: {
  154. types: ['cloud'],
  155. versionRegExps: [crowi.version],
  156. },
  157. },
  158. },
  159. // for different growi version
  160. {
  161. ...questionnaireToBeShown,
  162. _id: '63b8354837e7aa378e16f0b8',
  163. condition: {
  164. user: {
  165. types: ['general'],
  166. },
  167. growi: {
  168. types: ['on-premise'],
  169. versionRegExps: ['1.0.0-alpha'],
  170. },
  171. },
  172. },
  173. // for users that used GROWI for less than or equal to a year
  174. {
  175. ...questionnaireToBeShown,
  176. _id: '63b8354837e7aa378e16f0b9',
  177. condition: {
  178. user: {
  179. types: ['general'],
  180. daysSinceCreation: {
  181. lessThanOrEqualTo: 365,
  182. },
  183. },
  184. growi: {
  185. types: ['on-premise'],
  186. versionRegExps: [crowi.version],
  187. },
  188. },
  189. },
  190. // for users that used GROWI for more than or equal to 1000 years
  191. {
  192. ...questionnaireToBeShown,
  193. _id: '63b8354837e7aa378e16f0c1',
  194. condition: {
  195. user: {
  196. types: ['general'],
  197. daysSinceCreation: {
  198. moreThanOrEqualTo: 365 * 1000,
  199. },
  200. },
  201. growi: {
  202. types: ['on-premise'],
  203. versionRegExps: [crowi.version],
  204. },
  205. },
  206. },
  207. // for users that used GROWI for more than a month and less than 6 months
  208. {
  209. ...questionnaireToBeShown,
  210. _id: '63b8354837e7aa378e16f0c2',
  211. condition: {
  212. user: {
  213. types: ['general'],
  214. daysSinceCreation: {
  215. moreThanOrEqualTo: 30,
  216. lessThanOrEqualTo: 30 * 6,
  217. },
  218. },
  219. growi: {
  220. types: ['on-premise'],
  221. versionRegExps: [crowi.version],
  222. },
  223. },
  224. },
  225. ]);
  226. await QuestionnaireAnswerStatus.insertMany([
  227. {
  228. user: user._id,
  229. questionnaireOrderId: '63b8354837e7aa378e16f0b4',
  230. status: StatusType.answered,
  231. },
  232. {
  233. user: user._id,
  234. questionnaireOrderId: '63b8354837e7aa378e16f0b5',
  235. status: StatusType.skipped,
  236. },
  237. {
  238. user: user._id,
  239. questionnaireOrderId: '63b8354837e7aa378e16f0b6',
  240. status: StatusType.skipped,
  241. },
  242. ]);
  243. });
  244. test('Should get questionnaire orders to show', async() => {
  245. const growiInfo = await crowi.questionnaireService.getGrowiInfo();
  246. const userInfo = crowi.questionnaireService.getUserInfo(user, growiInfo.appSiteUrlHashed);
  247. const questionnaireOrderDocuments = await crowi.questionnaireService.getQuestionnaireOrdersToShow(userInfo, growiInfo, user._id);
  248. const questionnaireOrderObjects = questionnaireOrderDocuments.map((document) => {
  249. const qo = document.toObject();
  250. delete qo.condition._id;
  251. return { ...qo, _id: qo._id.toString() };
  252. });
  253. expect(questionnaireOrderObjects).toEqual([
  254. {
  255. _id: '63b8354837e7aa378e16f0b1',
  256. __v: 0,
  257. shortTitle: {
  258. ja_JP: 'GROWI に関するアンケート',
  259. en_US: 'Questions about GROWI',
  260. },
  261. title: {
  262. ja_JP: 'GROWI に関するアンケート',
  263. en_US: 'Questions about GROWI',
  264. },
  265. showFrom: new Date('2022-12-11'),
  266. showUntil: new Date('2100-12-12'),
  267. questions: [],
  268. condition: {
  269. user: {
  270. types: ['general'],
  271. daysSinceCreation: {
  272. moreThanOrEqualTo: 365,
  273. lessThanOrEqualTo: 365 * 1000,
  274. },
  275. },
  276. growi: {
  277. types: ['on-premise'],
  278. versionRegExps: [crowi?.version],
  279. },
  280. },
  281. createdAt: new Date('2023-01-01'),
  282. updatedAt: new Date('2023-01-01'),
  283. },
  284. ]);
  285. });
  286. });
  287. });