questionnaire-cron.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. });
  136. beforeEach(async() => {
  137. // insert initial db data
  138. await QuestionnaireOrder.insertMany([
  139. {
  140. _id: '63a8354837e7aa378e16f0b1',
  141. shortTitle: {
  142. ja_JP: 'GROWI に関するアンケート',
  143. en_US: 'Questions about GROWI',
  144. },
  145. title: {
  146. ja_JP: 'GROWI に関するアンケート',
  147. en_US: 'Questions about GROWI',
  148. },
  149. showFrom: '2022-12-11',
  150. showUntil: '2100-12-12',
  151. questions: [
  152. {
  153. type: 'points',
  154. text: {
  155. ja_JP: 'GROWI は使いやすいですか?',
  156. en_US: 'Is GROWI easy to use?',
  157. },
  158. },
  159. ],
  160. condition: {
  161. user: {
  162. types: ['general'],
  163. },
  164. growi: {
  165. types: ['cloud', 'private-cloud'],
  166. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  167. },
  168. },
  169. },
  170. // finished
  171. {
  172. _id: '63a8354837e7aa378e16f0b4',
  173. shortTitle: {
  174. ja_JP: 'GROWI に関するアンケート',
  175. en_US: 'Questions about GROWI',
  176. },
  177. title: {
  178. ja_JP: 'GROWI に関するアンケート',
  179. en_US: 'Questions about GROWI',
  180. },
  181. showFrom: '2020-12-11',
  182. showUntil: '2021-12-12',
  183. questions: [
  184. {
  185. type: 'points',
  186. text: {
  187. ja_JP: 'ver 2.0 は 1.0 より良いですか?',
  188. en_US: 'Is ver 2.0 better than 1.0?',
  189. },
  190. },
  191. ],
  192. condition: {
  193. user: {
  194. types: ['general'],
  195. },
  196. growi: {
  197. types: ['cloud'],
  198. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  199. },
  200. },
  201. },
  202. // questionnaire that doesn't exist in questionnaire server
  203. {
  204. _id: '63a8354837e7aa378e16f0b5',
  205. shortTitle: {
  206. ja_JP: 'GROWI に関するアンケート',
  207. en_US: 'Questions about GROWI',
  208. },
  209. title: {
  210. ja_JP: 'GROWI に関するアンケート',
  211. en_US: 'Questions about GROWI',
  212. },
  213. showFrom: '2020-12-11',
  214. showUntil: '2100-12-12',
  215. questions: [
  216. {
  217. type: 'points',
  218. text: {
  219. ja_JP: '新しいデザインは良いですか?',
  220. en_US: 'How would you rate the latest design?',
  221. },
  222. },
  223. ],
  224. condition: {
  225. user: {
  226. types: ['general'],
  227. },
  228. growi: {
  229. types: ['cloud'],
  230. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  231. },
  232. },
  233. },
  234. ]);
  235. await QuestionnaireAnswerStatus.insertMany([
  236. {
  237. user: new mongoose.Types.ObjectId(),
  238. questionnaireOrderId: '63a8354837e7aa378e16f0b1',
  239. status: StatusType.skipped,
  240. },
  241. {
  242. user: new mongoose.Types.ObjectId(),
  243. questionnaireOrderId: '63a8354837e7aa378e16f0b1',
  244. status: StatusType.answered,
  245. },
  246. {
  247. user: new mongoose.Types.ObjectId(),
  248. questionnaireOrderId: '63a8354837e7aa378e16f0b1',
  249. status: StatusType.not_answered,
  250. },
  251. ]);
  252. const validQuestionnaireAnswer: IQuestionnaireAnswer = {
  253. answers: [{
  254. question: '63c6da88143e531d95346188',
  255. value: '1',
  256. }],
  257. answeredAt: new Date(),
  258. growiInfo: {
  259. version: '1.0',
  260. appSiteUrlHashed: 'c83e8d2a1aa87b2a3f90561be372ca523bb931e2d00013c1d204879621a25b90',
  261. type: 'cloud',
  262. currentUsersCount: 100,
  263. currentActiveUsersCount: 50,
  264. wikiType: 'open',
  265. attachmentType: 'aws',
  266. },
  267. userInfo: {
  268. userIdHash: '542bcc3bc5bc61b840017a18',
  269. type: 'general',
  270. userCreatedAt: new Date(),
  271. },
  272. questionnaireOrder: '63a8354837e7aa378e16f0b1',
  273. };
  274. await QuestionnaireAnswer.insertMany([
  275. validQuestionnaireAnswer,
  276. validQuestionnaireAnswer,
  277. validQuestionnaireAnswer,
  278. ]);
  279. const validProactiveQuestionnaireAnswer: IProactiveQuestionnaireAnswer = {
  280. satisfaction: 1,
  281. commentText: 'answer text',
  282. growiInfo: {
  283. version: '1.0',
  284. appSiteUrlHashed: 'c83e8d2a1aa87b2a3f90561be372ca523bb931e2d00013c1d204879621a25b90',
  285. type: 'cloud',
  286. currentUsersCount: 100,
  287. currentActiveUsersCount: 50,
  288. wikiType: 'open',
  289. attachmentType: 'aws',
  290. },
  291. userInfo: {
  292. userIdHash: '542bcc3bc5bc61b840017a18',
  293. type: 'general',
  294. userCreatedAt: new Date(),
  295. },
  296. answeredAt: new Date(),
  297. };
  298. await ProactiveQuestionnaireAnswer.insertMany([
  299. validProactiveQuestionnaireAnswer,
  300. validProactiveQuestionnaireAnswer,
  301. validProactiveQuestionnaireAnswer,
  302. ]);
  303. crowi.setupCron();
  304. spyAxiosGet.mockResolvedValue(mockResponse);
  305. spyAxiosPost.mockResolvedValue({ data: { result: 'success' } });
  306. });
  307. afterAll(() => {
  308. crowi.questionnaireCronService.stopCron(); // jest will not finish until cronjob stops
  309. });
  310. test('Job execution should save(update) quesionnaire orders, delete outdated ones, update skipped answer statuses, and delete resent answers', async() => {
  311. // testing the cronjob from schedule has untrivial overhead, so test job execution in place
  312. await crowi.questionnaireCronService.executeJob();
  313. const savedOrders = await QuestionnaireOrder.find()
  314. .select('-condition._id -questions._id -questions.createdAt -questions.updatedAt')
  315. .sort({ _id: 1 });
  316. expect(JSON.parse(JSON.stringify(savedOrders))).toEqual([
  317. {
  318. _id: '63a8354837e7aa378e16f0b1',
  319. shortTitle: {
  320. ja_JP: 'GROWI に関するアンケート',
  321. en_US: 'Questions about GROWI',
  322. },
  323. title: {
  324. ja_JP: 'GROWI に関するアンケート',
  325. en_US: 'Questions about GROWI',
  326. },
  327. showFrom: '2022-12-11T00:00:00.000Z',
  328. showUntil: '2100-12-12T00:00:00.000Z',
  329. questions: [
  330. {
  331. type: 'points',
  332. text: {
  333. ja_JP: 'GROWI は使いやすいですか?',
  334. en_US: 'Is GROWI easy to use?',
  335. },
  336. },
  337. ],
  338. condition: {
  339. user: {
  340. types: ['admin', 'general'],
  341. },
  342. growi: {
  343. types: ['cloud', 'private-cloud'],
  344. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  345. },
  346. },
  347. createdAt: '2022-12-01T00:00:00.000Z',
  348. updatedAt: '2022-12-01T00:00:00.000Z',
  349. __v: 0,
  350. },
  351. {
  352. _id: '63a8354837e7aa378e16f0b2',
  353. shortTitle: {
  354. ja_JP: 'GROWI に関するアンケート',
  355. en_US: 'Questions about GROWI',
  356. },
  357. title: {
  358. ja_JP: 'GROWI に関するアンケート',
  359. en_US: 'Questions about GROWI',
  360. },
  361. showFrom: '2021-12-11T00:00:00.000Z',
  362. showUntil: '2100-12-12T00:00:00.000Z',
  363. questions: [
  364. {
  365. type: 'points',
  366. text: {
  367. ja_JP: 'アンケート機能は正常動作していますか?',
  368. en_US: 'Is this questionnaire functioning properly?',
  369. },
  370. },
  371. ],
  372. condition: {
  373. user: {
  374. types: ['general'],
  375. },
  376. growi: {
  377. types: ['cloud'],
  378. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  379. },
  380. },
  381. createdAt: '2022-12-02T00:00:00.000Z',
  382. updatedAt: '2022-12-02T00:00:00.000Z',
  383. __v: 0,
  384. },
  385. ]);
  386. expect((await QuestionnaireAnswerStatus.find({ status: StatusType.not_answered })).length).toEqual(2);
  387. expect((await QuestionnaireAnswer.find()).length).toEqual(0);
  388. expect((await ProactiveQuestionnaireAnswer.find()).length).toEqual(0);
  389. });
  390. });