questionnaire-cron.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. appSiteUrlHashed: 'c83e8d2a1aa87b2a3f90561be372ca523bb931e2d00013c1d204879621a25b90',
  274. type: GrowiServiceType.cloud,
  275. wikiType: GrowiWikiType.open,
  276. deploymentType: GrowiDeploymentType.others,
  277. additionalInfo: {
  278. installedAt: new Date('2000-01-01'),
  279. installedAtByOldestUser: new Date('2020-01-01'),
  280. currentUsersCount: 100,
  281. currentActiveUsersCount: 50,
  282. attachmentType: AttachmentMethodType.aws,
  283. },
  284. },
  285. userInfo: {
  286. userIdHash: '542bcc3bc5bc61b840017a18',
  287. type: 'general',
  288. userCreatedAt: new Date(),
  289. },
  290. questionnaireOrder: '63a8354837e7aa378e16f0b1',
  291. };
  292. const validQuestionnaireAnswerLegacy: IQuestionnaireAnswerLegacy = {
  293. answers: [{
  294. question: '63c6da88143e531d95346188',
  295. value: '1',
  296. }],
  297. answeredAt: new Date(),
  298. growiInfo: {
  299. version: '1.0',
  300. appSiteUrlHashed: 'c83e8d2a1aa87b2a3f90561be372ca523bb931e2d00013c1d204879621a25b90',
  301. type: GrowiServiceType.cloud,
  302. wikiType: GrowiWikiType.open,
  303. deploymentType: GrowiDeploymentType.others,
  304. installedAt: new Date('2000-01-01'),
  305. installedAtByOldestUser: new Date('2020-01-01'),
  306. currentUsersCount: 100,
  307. currentActiveUsersCount: 50,
  308. attachmentType: AttachmentMethodType.aws,
  309. },
  310. userInfo: {
  311. userIdHash: '542bcc3bc5bc61b840017a18',
  312. type: 'general',
  313. userCreatedAt: new Date(),
  314. },
  315. questionnaireOrder: '63a8354837e7aa378e16f0b1',
  316. };
  317. await QuestionnaireAnswer.insertMany([
  318. validQuestionnaireAnswer,
  319. validQuestionnaireAnswer,
  320. validQuestionnaireAnswer,
  321. validQuestionnaireAnswerLegacy,
  322. validQuestionnaireAnswerLegacy,
  323. ]);
  324. const validProactiveQuestionnaireAnswer: IProactiveQuestionnaireAnswer = {
  325. satisfaction: 1,
  326. commentText: 'answer text',
  327. growiInfo: {
  328. version: '1.0',
  329. appSiteUrlHashed: 'c83e8d2a1aa87b2a3f90561be372ca523bb931e2d00013c1d204879621a25b90',
  330. type: GrowiServiceType.cloud,
  331. wikiType: GrowiWikiType.open,
  332. deploymentType: GrowiDeploymentType.others,
  333. additionalInfo: {
  334. installedAt: new Date('2000-01-01'),
  335. installedAtByOldestUser: new Date('2020-01-01'),
  336. currentUsersCount: 100,
  337. currentActiveUsersCount: 50,
  338. attachmentType: AttachmentMethodType.aws,
  339. },
  340. },
  341. userInfo: {
  342. userIdHash: '542bcc3bc5bc61b840017a18',
  343. type: 'general',
  344. userCreatedAt: new Date(),
  345. },
  346. answeredAt: new Date(),
  347. };
  348. const validProactiveQuestionnaireAnswerLegacy: IProactiveQuestionnaireAnswerLegacy = {
  349. satisfaction: 1,
  350. commentText: 'answer text',
  351. growiInfo: {
  352. version: '1.0',
  353. appSiteUrlHashed: 'c83e8d2a1aa87b2a3f90561be372ca523bb931e2d00013c1d204879621a25b90',
  354. type: GrowiServiceType.cloud,
  355. wikiType: GrowiWikiType.open,
  356. deploymentType: GrowiDeploymentType.others,
  357. // legacy properties
  358. installedAt: new Date('2000-01-01'),
  359. installedAtByOldestUser: new Date('2020-01-01'),
  360. currentUsersCount: 100,
  361. currentActiveUsersCount: 50,
  362. attachmentType: AttachmentMethodType.aws,
  363. },
  364. userInfo: {
  365. userIdHash: '542bcc3bc5bc61b840017a18',
  366. type: 'general',
  367. userCreatedAt: new Date(),
  368. },
  369. answeredAt: new Date(),
  370. };
  371. await ProactiveQuestionnaireAnswer.insertMany([
  372. validProactiveQuestionnaireAnswer,
  373. validProactiveQuestionnaireAnswer,
  374. validProactiveQuestionnaireAnswer,
  375. validProactiveQuestionnaireAnswerLegacy,
  376. validProactiveQuestionnaireAnswerLegacy,
  377. ]);
  378. crowi.setupCron();
  379. spyAxiosGet.mockResolvedValue(mockResponse);
  380. spyAxiosPost.mockResolvedValue({ data: { result: 'success' } });
  381. });
  382. afterAll(() => {
  383. crowi.questionnaireCronService.stopCron(); // jest will not finish until cronjob stops
  384. });
  385. test('Job execution should save(update) quesionnaire orders, delete outdated ones, update skipped answer statuses, and delete resent answers', async() => {
  386. // testing the cronjob from schedule has untrivial overhead, so test job execution in place
  387. await crowi.questionnaireCronService.executeJob();
  388. const savedOrders = await QuestionnaireOrder.find()
  389. .select('-condition._id -questions._id -questions.createdAt -questions.updatedAt')
  390. .sort({ _id: 1 });
  391. expect(JSON.parse(JSON.stringify(savedOrders))).toEqual([
  392. {
  393. _id: '63a8354837e7aa378e16f0b1',
  394. shortTitle: {
  395. ja_JP: 'GROWI に関するアンケート',
  396. en_US: 'Questions about GROWI',
  397. },
  398. title: {
  399. ja_JP: 'GROWI に関するアンケート',
  400. en_US: 'Questions about GROWI',
  401. },
  402. showFrom: '2022-12-11T00:00:00.000Z',
  403. showUntil: '2100-12-12T00:00:00.000Z',
  404. questions: [
  405. {
  406. type: 'points',
  407. text: {
  408. ja_JP: 'GROWI は使いやすいですか?',
  409. en_US: 'Is GROWI easy to use?',
  410. },
  411. },
  412. ],
  413. condition: {
  414. user: {
  415. types: ['admin', 'general'],
  416. },
  417. growi: {
  418. types: ['cloud', 'private-cloud'],
  419. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  420. },
  421. },
  422. createdAt: '2022-12-01T00:00:00.000Z',
  423. updatedAt: '2022-12-01T00:00:00.000Z',
  424. __v: 0,
  425. },
  426. {
  427. _id: '63a8354837e7aa378e16f0b2',
  428. shortTitle: {
  429. ja_JP: 'GROWI に関するアンケート',
  430. en_US: 'Questions about GROWI',
  431. },
  432. title: {
  433. ja_JP: 'GROWI に関するアンケート',
  434. en_US: 'Questions about GROWI',
  435. },
  436. showFrom: '2021-12-11T00:00:00.000Z',
  437. showUntil: '2100-12-12T00:00:00.000Z',
  438. questions: [
  439. {
  440. type: 'points',
  441. text: {
  442. ja_JP: 'アンケート機能は正常動作していますか?',
  443. en_US: 'Is this questionnaire functioning properly?',
  444. },
  445. },
  446. ],
  447. condition: {
  448. user: {
  449. types: ['general'],
  450. },
  451. growi: {
  452. types: ['cloud'],
  453. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  454. },
  455. },
  456. createdAt: '2022-12-02T00:00:00.000Z',
  457. updatedAt: '2022-12-02T00:00:00.000Z',
  458. __v: 0,
  459. },
  460. ]);
  461. expect((await QuestionnaireAnswerStatus.find({ status: StatusType.not_answered })).length).toEqual(2);
  462. expect((await QuestionnaireAnswer.find()).length).toEqual(0);
  463. expect((await ProactiveQuestionnaireAnswer.find()).length).toEqual(0);
  464. });
  465. });