questionnaire-cron.test.ts 13 KB

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