questionnaire-cron.test.ts 12 KB

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