questionnaire-cron.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. import { GrowiDeploymentType, GrowiServiceType, GrowiWikiType } from '@growi/core';
  2. // eslint-disable-next-line no-restricted-imports
  3. import axios from 'axios';
  4. import mongoose from 'mongoose';
  5. import type {
  6. IProactiveQuestionnaireAnswer, IProactiveQuestionnaireAnswerLegacy,
  7. } from '../../../src/features/questionnaire/interfaces/proactive-questionnaire-answer';
  8. import type { IQuestionnaireAnswer, IQuestionnaireAnswerLegacy } from '../../../src/features/questionnaire/interfaces/questionnaire-answer';
  9. import { StatusType } from '../../../src/features/questionnaire/interfaces/questionnaire-answer-status';
  10. import ProactiveQuestionnaireAnswer from '../../../src/features/questionnaire/server/models/proactive-questionnaire-answer';
  11. import QuestionnaireAnswer from '../../../src/features/questionnaire/server/models/questionnaire-answer';
  12. import QuestionnaireAnswerStatus from '../../../src/features/questionnaire/server/models/questionnaire-answer-status';
  13. import QuestionnaireOrder from '../../../src/features/questionnaire/server/models/questionnaire-order';
  14. import { AttachmentMethodType } from '../../../src/interfaces/attachment';
  15. import { getInstance } from '../setup-crowi';
  16. const spyAxiosGet = jest.spyOn<typeof axios, 'get'>(
  17. axios,
  18. 'get',
  19. );
  20. const spyAxiosPost = jest.spyOn<typeof axios, 'post'>(
  21. axios,
  22. 'post',
  23. );
  24. describe('QuestionnaireCronService', () => {
  25. let crowi;
  26. const mockResponse = {
  27. data: {
  28. questionnaireOrders: [
  29. // saved in db、not finished (user.types is updated from the time it was saved)
  30. {
  31. _id: '63a8354837e7aa378e16f0b1',
  32. shortTitle: {
  33. ja_JP: 'GROWI に関するアンケート',
  34. en_US: 'Questions about GROWI',
  35. },
  36. title: {
  37. ja_JP: 'GROWI に関するアンケート',
  38. en_US: 'Questions about GROWI',
  39. },
  40. showFrom: '2022-12-11',
  41. showUntil: '2100-12-12',
  42. questions: [
  43. {
  44. type: 'points',
  45. text: {
  46. ja_JP: 'GROWI は使いやすいですか?',
  47. en_US: 'Is GROWI easy to use?',
  48. },
  49. },
  50. ],
  51. condition: {
  52. user: {
  53. types: ['admin', 'general'],
  54. },
  55. growi: {
  56. types: ['cloud', 'private-cloud'],
  57. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  58. },
  59. },
  60. createdAt: '2022-12-01',
  61. updatedAt: '2022-12-01',
  62. __v: 0,
  63. },
  64. // not saved, not finished
  65. {
  66. _id: '63a8354837e7aa378e16f0b2',
  67. shortTitle: {
  68. ja_JP: 'GROWI に関するアンケート',
  69. en_US: 'Questions about GROWI',
  70. },
  71. title: {
  72. ja_JP: 'GROWI に関するアンケート',
  73. en_US: 'Questions about GROWI',
  74. },
  75. showFrom: '2021-12-11',
  76. showUntil: '2100-12-12',
  77. questions: [
  78. {
  79. type: 'points',
  80. text: {
  81. ja_JP: 'アンケート機能は正常動作していますか?',
  82. en_US: 'Is this questionnaire functioning properly?',
  83. },
  84. },
  85. ],
  86. condition: {
  87. user: {
  88. types: ['general'],
  89. },
  90. growi: {
  91. types: ['cloud'],
  92. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  93. },
  94. },
  95. createdAt: '2022-12-02',
  96. updatedAt: '2022-12-02',
  97. __v: 0,
  98. },
  99. // not saved, finished
  100. {
  101. _id: '63a8354837e7aa378e16f0b3',
  102. shortTitle: {
  103. ja_JP: 'GROWI に関するアンケート',
  104. en_US: 'Questions about GROWI',
  105. },
  106. title: {
  107. ja_JP: 'GROWI に関するアンケート',
  108. en_US: 'Questions about GROWI',
  109. },
  110. showFrom: '2021-12-11',
  111. showUntil: '2021-12-12',
  112. questions: [
  113. {
  114. type: 'points',
  115. text: {
  116. ja_JP: 'これはいい質問ですか?',
  117. en_US: 'Is this a good question?',
  118. },
  119. },
  120. ],
  121. condition: {
  122. user: {
  123. types: ['general'],
  124. },
  125. growi: {
  126. types: ['cloud'],
  127. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  128. },
  129. },
  130. createdAt: '2022-12-03',
  131. updatedAt: '2022-12-03',
  132. __v: 0,
  133. },
  134. ],
  135. },
  136. };
  137. beforeAll(async() => {
  138. crowi = await getInstance();
  139. const User = crowi.model('User');
  140. User.deleteMany({}); // clear users
  141. await User.create({
  142. name: 'Example for Questionnaire Service Test',
  143. username: 'questionnaire cron test user',
  144. email: 'questionnaireCronTestUser@example.com',
  145. password: 'usertestpass',
  146. createdAt: '2020-01-01',
  147. });
  148. });
  149. beforeEach(async() => {
  150. // insert initial db data
  151. await QuestionnaireOrder.insertMany([
  152. {
  153. _id: '63a8354837e7aa378e16f0b1',
  154. shortTitle: {
  155. ja_JP: 'GROWI に関するアンケート',
  156. en_US: 'Questions about GROWI',
  157. },
  158. title: {
  159. ja_JP: 'GROWI に関するアンケート',
  160. en_US: 'Questions about GROWI',
  161. },
  162. showFrom: '2022-12-11',
  163. showUntil: '2100-12-12',
  164. questions: [
  165. {
  166. type: 'points',
  167. text: {
  168. ja_JP: 'GROWI は使いやすいですか?',
  169. en_US: 'Is GROWI easy to use?',
  170. },
  171. },
  172. ],
  173. condition: {
  174. user: {
  175. types: ['general'],
  176. },
  177. growi: {
  178. types: ['cloud', 'private-cloud'],
  179. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  180. },
  181. },
  182. },
  183. // finished
  184. {
  185. _id: '63a8354837e7aa378e16f0b4',
  186. shortTitle: {
  187. ja_JP: 'GROWI に関するアンケート',
  188. en_US: 'Questions about GROWI',
  189. },
  190. title: {
  191. ja_JP: 'GROWI に関するアンケート',
  192. en_US: 'Questions about GROWI',
  193. },
  194. showFrom: '2020-12-11',
  195. showUntil: '2021-12-12',
  196. questions: [
  197. {
  198. type: 'points',
  199. text: {
  200. ja_JP: 'ver 2.0 は 1.0 より良いですか?',
  201. en_US: 'Is ver 2.0 better than 1.0?',
  202. },
  203. },
  204. ],
  205. condition: {
  206. user: {
  207. types: ['general'],
  208. },
  209. growi: {
  210. types: ['cloud'],
  211. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  212. },
  213. },
  214. },
  215. // questionnaire that doesn't exist in questionnaire server
  216. {
  217. _id: '63a8354837e7aa378e16f0b5',
  218. shortTitle: {
  219. ja_JP: 'GROWI に関するアンケート',
  220. en_US: 'Questions about GROWI',
  221. },
  222. title: {
  223. ja_JP: 'GROWI に関するアンケート',
  224. en_US: 'Questions about GROWI',
  225. },
  226. showFrom: '2020-12-11',
  227. showUntil: '2100-12-12',
  228. questions: [
  229. {
  230. type: 'points',
  231. text: {
  232. ja_JP: '新しいデザインは良いですか?',
  233. en_US: 'How would you rate the latest design?',
  234. },
  235. },
  236. ],
  237. condition: {
  238. user: {
  239. types: ['general'],
  240. },
  241. growi: {
  242. types: ['cloud'],
  243. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  244. },
  245. },
  246. },
  247. ]);
  248. await QuestionnaireAnswerStatus.insertMany([
  249. {
  250. user: new mongoose.Types.ObjectId(),
  251. questionnaireOrderId: '63a8354837e7aa378e16f0b1',
  252. status: StatusType.skipped,
  253. },
  254. {
  255. user: new mongoose.Types.ObjectId(),
  256. questionnaireOrderId: '63a8354837e7aa378e16f0b1',
  257. status: StatusType.answered,
  258. },
  259. {
  260. user: new mongoose.Types.ObjectId(),
  261. questionnaireOrderId: '63a8354837e7aa378e16f0b1',
  262. status: StatusType.not_answered,
  263. },
  264. ]);
  265. const validQuestionnaireAnswer: IQuestionnaireAnswer = {
  266. answers: [{
  267. question: '63c6da88143e531d95346188',
  268. value: '1',
  269. }],
  270. answeredAt: new Date(),
  271. growiInfo: {
  272. version: '1.0',
  273. appSiteUrl: 'https://example.com',
  274. serviceInstanceId: '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed',
  275. type: GrowiServiceType.cloud,
  276. wikiType: GrowiWikiType.open,
  277. deploymentType: GrowiDeploymentType.others,
  278. osInfo: {
  279. type: 'Linux',
  280. platform: 'linux',
  281. arch: 'x64',
  282. totalmem: 8589934592,
  283. },
  284. additionalInfo: {
  285. installedAt: new Date('2000-01-01'),
  286. installedAtByOldestUser: new Date('2020-01-01'),
  287. currentUsersCount: 100,
  288. currentActiveUsersCount: 50,
  289. attachmentType: AttachmentMethodType.aws,
  290. },
  291. },
  292. userInfo: {
  293. userIdHash: '542bcc3bc5bc61b840017a18',
  294. type: 'general',
  295. userCreatedAt: new Date(),
  296. },
  297. questionnaireOrder: '63a8354837e7aa378e16f0b1',
  298. };
  299. const validQuestionnaireAnswerLegacy: IQuestionnaireAnswerLegacy = {
  300. answers: [{
  301. question: '63c6da88143e531d95346188',
  302. value: '1',
  303. }],
  304. answeredAt: new Date(),
  305. growiInfo: {
  306. version: '1.0',
  307. appSiteUrl: 'https://example.com',
  308. appSiteUrlHashed: 'hashed',
  309. serviceInstanceId: '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed',
  310. type: GrowiServiceType.cloud,
  311. wikiType: GrowiWikiType.open,
  312. deploymentType: GrowiDeploymentType.others,
  313. installedAt: new Date('2000-01-01'),
  314. installedAtByOldestUser: new Date('2020-01-01'),
  315. currentUsersCount: 100,
  316. currentActiveUsersCount: 50,
  317. osInfo: {
  318. type: 'Linux',
  319. platform: 'linux',
  320. arch: 'x64',
  321. totalmem: 8589934592,
  322. },
  323. attachmentType: AttachmentMethodType.aws,
  324. },
  325. userInfo: {
  326. userIdHash: '542bcc3bc5bc61b840017a18',
  327. type: 'general',
  328. userCreatedAt: new Date(),
  329. },
  330. questionnaireOrder: '63a8354837e7aa378e16f0b1',
  331. };
  332. await QuestionnaireAnswer.insertMany([
  333. validQuestionnaireAnswer,
  334. validQuestionnaireAnswer,
  335. validQuestionnaireAnswer,
  336. validQuestionnaireAnswerLegacy,
  337. validQuestionnaireAnswerLegacy,
  338. ]);
  339. const validProactiveQuestionnaireAnswer: IProactiveQuestionnaireAnswer = {
  340. satisfaction: 1,
  341. commentText: 'answer text',
  342. growiInfo: {
  343. version: '1.0',
  344. appSiteUrl: 'https://example.com',
  345. serviceInstanceId: '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed',
  346. type: GrowiServiceType.cloud,
  347. wikiType: GrowiWikiType.open,
  348. deploymentType: GrowiDeploymentType.others,
  349. osInfo: {
  350. type: 'Linux',
  351. platform: 'linux',
  352. arch: 'x64',
  353. totalmem: 8589934592,
  354. },
  355. additionalInfo: {
  356. installedAt: new Date('2000-01-01'),
  357. installedAtByOldestUser: new Date('2020-01-01'),
  358. currentUsersCount: 100,
  359. currentActiveUsersCount: 50,
  360. attachmentType: AttachmentMethodType.aws,
  361. },
  362. },
  363. userInfo: {
  364. userIdHash: '542bcc3bc5bc61b840017a18',
  365. type: 'general',
  366. userCreatedAt: new Date(),
  367. },
  368. answeredAt: new Date(),
  369. };
  370. const validProactiveQuestionnaireAnswerLegacy: IProactiveQuestionnaireAnswerLegacy = {
  371. satisfaction: 1,
  372. commentText: 'answer text',
  373. growiInfo: {
  374. version: '1.0',
  375. appSiteUrl: 'https://example.com',
  376. appSiteUrlHashed: 'hashed',
  377. serviceInstanceId: '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed',
  378. type: GrowiServiceType.cloud,
  379. wikiType: GrowiWikiType.open,
  380. deploymentType: GrowiDeploymentType.others,
  381. osInfo: {
  382. type: 'Linux',
  383. platform: 'linux',
  384. arch: 'x64',
  385. totalmem: 8589934592,
  386. },
  387. // legacy properties
  388. installedAt: new Date('2000-01-01'),
  389. installedAtByOldestUser: new Date('2020-01-01'),
  390. currentUsersCount: 100,
  391. currentActiveUsersCount: 50,
  392. attachmentType: AttachmentMethodType.aws,
  393. },
  394. userInfo: {
  395. userIdHash: '542bcc3bc5bc61b840017a18',
  396. type: 'general',
  397. userCreatedAt: new Date(),
  398. },
  399. answeredAt: new Date(),
  400. };
  401. await ProactiveQuestionnaireAnswer.insertMany([
  402. validProactiveQuestionnaireAnswer,
  403. validProactiveQuestionnaireAnswer,
  404. validProactiveQuestionnaireAnswer,
  405. validProactiveQuestionnaireAnswerLegacy,
  406. validProactiveQuestionnaireAnswerLegacy,
  407. ]);
  408. crowi.setupCron();
  409. spyAxiosGet.mockResolvedValue(mockResponse);
  410. spyAxiosPost.mockResolvedValue({ data: { result: 'success' } });
  411. });
  412. afterAll(() => {
  413. crowi.questionnaireCronService.stopCron(); // jest will not finish until cronjob stops
  414. });
  415. test('Job execution should save(update) quesionnaire orders, delete outdated ones, update skipped answer statuses, and delete resent answers', async() => {
  416. // testing the cronjob from schedule has untrivial overhead, so test job execution in place
  417. await crowi.questionnaireCronService.executeJob();
  418. const savedOrders = await QuestionnaireOrder.find()
  419. .select('-condition._id -questions._id -questions.createdAt -questions.updatedAt')
  420. .sort({ _id: 1 });
  421. expect(JSON.parse(JSON.stringify(savedOrders))).toEqual([
  422. {
  423. _id: '63a8354837e7aa378e16f0b1',
  424. shortTitle: {
  425. ja_JP: 'GROWI に関するアンケート',
  426. en_US: 'Questions about GROWI',
  427. },
  428. title: {
  429. ja_JP: 'GROWI に関するアンケート',
  430. en_US: 'Questions about GROWI',
  431. },
  432. showFrom: '2022-12-11T00:00:00.000Z',
  433. showUntil: '2100-12-12T00:00:00.000Z',
  434. questions: [
  435. {
  436. type: 'points',
  437. text: {
  438. ja_JP: 'GROWI は使いやすいですか?',
  439. en_US: 'Is GROWI easy to use?',
  440. },
  441. },
  442. ],
  443. condition: {
  444. user: {
  445. types: ['admin', 'general'],
  446. },
  447. growi: {
  448. types: ['cloud', 'private-cloud'],
  449. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  450. },
  451. },
  452. createdAt: '2022-12-01T00:00:00.000Z',
  453. updatedAt: '2022-12-01T00:00:00.000Z',
  454. __v: 0,
  455. },
  456. {
  457. _id: '63a8354837e7aa378e16f0b2',
  458. shortTitle: {
  459. ja_JP: 'GROWI に関するアンケート',
  460. en_US: 'Questions about GROWI',
  461. },
  462. title: {
  463. ja_JP: 'GROWI に関するアンケート',
  464. en_US: 'Questions about GROWI',
  465. },
  466. showFrom: '2021-12-11T00:00:00.000Z',
  467. showUntil: '2100-12-12T00:00:00.000Z',
  468. questions: [
  469. {
  470. type: 'points',
  471. text: {
  472. ja_JP: 'アンケート機能は正常動作していますか?',
  473. en_US: 'Is this questionnaire functioning properly?',
  474. },
  475. },
  476. ],
  477. condition: {
  478. user: {
  479. types: ['general'],
  480. },
  481. growi: {
  482. types: ['cloud'],
  483. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  484. },
  485. },
  486. createdAt: '2022-12-02T00:00:00.000Z',
  487. updatedAt: '2022-12-02T00:00:00.000Z',
  488. __v: 0,
  489. },
  490. ]);
  491. expect((await QuestionnaireAnswerStatus.find({ status: StatusType.not_answered })).length).toEqual(2);
  492. expect((await QuestionnaireAnswer.find()).length).toEqual(0);
  493. expect((await ProactiveQuestionnaireAnswer.find()).length).toEqual(0);
  494. });
  495. });