questionnaire-cron.integ.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 { configManager } from '~/server/service/config-manager';
  6. import { AttachmentMethodType } from '../../../../interfaces/attachment';
  7. import type {
  8. IProactiveQuestionnaireAnswer, IProactiveQuestionnaireAnswerLegacy,
  9. } from '../../interfaces/proactive-questionnaire-answer';
  10. import type { IQuestionnaireAnswer, IQuestionnaireAnswerLegacy } from '../../interfaces/questionnaire-answer';
  11. import { StatusType } from '../../interfaces/questionnaire-answer-status';
  12. import ProactiveQuestionnaireAnswer from '../models/proactive-questionnaire-answer';
  13. import QuestionnaireAnswer from '../models/questionnaire-answer';
  14. import QuestionnaireAnswerStatus from '../models/questionnaire-answer-status';
  15. import QuestionnaireOrder from '../models/questionnaire-order';
  16. import questionnaireCronService from './questionnaire-cron';
  17. // TODO: use actual user model after ~/server/models/user.js becomes importable in vitest
  18. // ref: https://github.com/vitest-dev/vitest/issues/846
  19. const userSchema = new mongoose.Schema({
  20. name: { type: String },
  21. username: { type: String, required: true, unique: true },
  22. email: { type: String, unique: true, sparse: true },
  23. }, {
  24. timestamps: true,
  25. });
  26. const User = mongoose.model('User', userSchema);
  27. describe('QuestionnaireCronService', () => {
  28. const mockResponse = {
  29. data: {
  30. questionnaireOrders: [
  31. // saved in db、not finished (user.types is updated from the time it was saved)
  32. {
  33. _id: '63a8354837e7aa378e16f0b1',
  34. shortTitle: {
  35. ja_JP: 'GROWI に関するアンケート',
  36. en_US: 'Questions about GROWI',
  37. },
  38. title: {
  39. ja_JP: 'GROWI に関するアンケート',
  40. en_US: 'Questions about GROWI',
  41. },
  42. showFrom: '2022-12-11',
  43. showUntil: '2100-12-12',
  44. questions: [
  45. {
  46. type: 'points',
  47. text: {
  48. ja_JP: 'GROWI は使いやすいですか?',
  49. en_US: 'Is GROWI easy to use?',
  50. },
  51. },
  52. ],
  53. condition: {
  54. user: {
  55. types: ['admin', 'general'],
  56. },
  57. growi: {
  58. types: ['cloud', 'private-cloud'],
  59. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  60. },
  61. },
  62. createdAt: '2022-12-01',
  63. updatedAt: '2022-12-01',
  64. __v: 0,
  65. },
  66. // not saved, not finished
  67. {
  68. _id: '63a8354837e7aa378e16f0b2',
  69. shortTitle: {
  70. ja_JP: 'GROWI に関するアンケート',
  71. en_US: 'Questions about GROWI',
  72. },
  73. title: {
  74. ja_JP: 'GROWI に関するアンケート',
  75. en_US: 'Questions about GROWI',
  76. },
  77. showFrom: '2021-12-11',
  78. showUntil: '2100-12-12',
  79. questions: [
  80. {
  81. type: 'points',
  82. text: {
  83. ja_JP: 'アンケート機能は正常動作していますか?',
  84. en_US: 'Is this questionnaire functioning properly?',
  85. },
  86. },
  87. ],
  88. condition: {
  89. user: {
  90. types: ['general'],
  91. },
  92. growi: {
  93. types: ['cloud'],
  94. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  95. },
  96. },
  97. createdAt: '2022-12-02',
  98. updatedAt: '2022-12-02',
  99. __v: 0,
  100. },
  101. // not saved, finished
  102. {
  103. _id: '63a8354837e7aa378e16f0b3',
  104. shortTitle: {
  105. ja_JP: 'GROWI に関するアンケート',
  106. en_US: 'Questions about GROWI',
  107. },
  108. title: {
  109. ja_JP: 'GROWI に関するアンケート',
  110. en_US: 'Questions about GROWI',
  111. },
  112. showFrom: '2021-12-11',
  113. showUntil: '2021-12-12',
  114. questions: [
  115. {
  116. type: 'points',
  117. text: {
  118. ja_JP: 'これはいい質問ですか?',
  119. en_US: 'Is this a good question?',
  120. },
  121. },
  122. ],
  123. condition: {
  124. user: {
  125. types: ['general'],
  126. },
  127. growi: {
  128. types: ['cloud'],
  129. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  130. },
  131. },
  132. createdAt: '2022-12-03',
  133. updatedAt: '2022-12-03',
  134. __v: 0,
  135. },
  136. ],
  137. },
  138. };
  139. beforeAll(async() => {
  140. await configManager.loadConfigs();
  141. await configManager.updateConfig('app:questionnaireCronMaxHoursUntilRequest', 0);
  142. await User.create({
  143. name: 'Example for Questionnaire Service Test',
  144. username: 'questionnaire cron test user',
  145. email: 'questionnaireCronTestUser@example.com',
  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. questionnaireCronService.startCron();
  409. vi.spyOn(axios, 'get').mockResolvedValue(mockResponse);
  410. vi.spyOn(axios, 'post').mockResolvedValue({ data: { result: 'success' } });
  411. });
  412. afterAll(() => {
  413. questionnaireCronService.stopCron(); // vitest 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 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. });