questionnaire-cron.test.ts 9.7 KB

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