questionnaire-cron.test.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. import QuestionnaireOrder from '../../../src/server/models/questionnaire/questionnaire-order';
  2. import { getInstance } from '../setup-crowi';
  3. const axios = require('axios').default;
  4. const rand = require('../../../src/utils/rand');
  5. const spyAxiosGet = jest.spyOn<typeof axios, 'get'>(
  6. axios,
  7. 'get',
  8. );
  9. const spyGetRandomIntInRange = jest.spyOn<typeof rand, 'getRandomIntInRange'>(
  10. rand,
  11. 'getRandomIntInRange',
  12. );
  13. describe('QuestionnaireCronService', () => {
  14. let crowi;
  15. const maxSecondsUntilRequest = 4 * 60 * 60 * 1000;
  16. const secondsUntilRequest = rand.getRandomIntInRange(0, maxSecondsUntilRequest);
  17. const mockResponse = {
  18. data: {
  19. questionnaireOrders: [
  20. // saved in db、not finished (user types is updated from the time it was saved)
  21. {
  22. _id: '63a8354837e7aa378e16f0b1',
  23. shortTitle: {
  24. ja_JP: 'GROWI に関するアンケート',
  25. en_US: 'Questions about GROWI',
  26. },
  27. title: {
  28. ja_JP: 'GROWI に関するアンケート',
  29. en_US: 'Questions about GROWI',
  30. },
  31. showFrom: '2022-12-11',
  32. showUntil: '2100-12-12',
  33. questions: [
  34. {
  35. type: 'points',
  36. text: {
  37. ja_JP: 'GROWI は使いやすいですか?',
  38. en_US: 'Is GROWI easy to use?',
  39. },
  40. },
  41. ],
  42. condition: {
  43. user: {
  44. types: ['admin', 'general'],
  45. },
  46. growi: {
  47. types: ['cloud', 'private-cloud'],
  48. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  49. },
  50. },
  51. createdAt: '2022-12-01',
  52. updatedAt: '2022-12-01',
  53. __v: 0,
  54. },
  55. // not saved, not finished
  56. {
  57. _id: '63a8354837e7aa378e16f0b2',
  58. shortTitle: {
  59. ja_JP: 'GROWI に関するアンケート',
  60. en_US: 'Questions about GROWI',
  61. },
  62. title: {
  63. ja_JP: 'GROWI に関するアンケート',
  64. en_US: 'Questions about GROWI',
  65. },
  66. showFrom: '2021-12-11',
  67. showUntil: '2100-12-12',
  68. questions: [
  69. {
  70. type: 'points',
  71. text: {
  72. ja_JP: 'アンケート機能は正常動作していますか?',
  73. en_US: 'Is this questionnaire functioning properly?',
  74. },
  75. },
  76. ],
  77. condition: {
  78. user: {
  79. types: ['general'],
  80. },
  81. growi: {
  82. types: ['cloud'],
  83. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  84. },
  85. },
  86. createdAt: '2022-12-02',
  87. updatedAt: '2022-12-02',
  88. __v: 0,
  89. },
  90. // not saved, finished
  91. {
  92. _id: '63a8354837e7aa378e16f0b3',
  93. shortTitle: {
  94. ja_JP: 'GROWI に関するアンケート',
  95. en_US: 'Questions about GROWI',
  96. },
  97. title: {
  98. ja_JP: 'GROWI に関するアンケート',
  99. en_US: 'Questions about GROWI',
  100. },
  101. showFrom: '2021-12-11',
  102. showUntil: '2021-12-12',
  103. questions: [
  104. {
  105. type: 'points',
  106. text: {
  107. ja_JP: 'これはいい質問ですか?',
  108. en_US: 'Is this a good question?',
  109. },
  110. },
  111. ],
  112. condition: {
  113. user: {
  114. types: ['general'],
  115. },
  116. growi: {
  117. types: ['cloud'],
  118. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  119. },
  120. },
  121. createdAt: '2022-12-03',
  122. updatedAt: '2022-12-03',
  123. __v: 0,
  124. },
  125. ],
  126. },
  127. };
  128. beforeAll(async() => {
  129. process.env.QUESTIONNAIRE_CRON_SCHEDULE = '0 22 * * *';
  130. process.env.QUESTIONNAIRE_CRON_MAX_HOURS_UNTIL_REQUEST = '4';
  131. crowi = await getInstance();
  132. // reload
  133. await crowi.setupConfigManager();
  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. // mock the date to 5 seconds before cronjob execution
  235. const mockDate = new Date(2022, 0, 1, 21, 59, 55);
  236. jest.useFakeTimers();
  237. jest.setSystemTime(mockDate);
  238. // must be after useFakeTimers for mockDate to be in effect
  239. crowi.setupCron();
  240. spyAxiosGet.mockResolvedValue(mockResponse);
  241. spyGetRandomIntInRange.mockReturnValue(secondsUntilRequest); // static sleep time until request
  242. });
  243. afterAll(() => {
  244. jest.useRealTimers();
  245. crowi.questionnaireCronService.stopCron();
  246. });
  247. test('Should save quesionnaire orders and delete outdated ones', async() => {
  248. jest.advanceTimersByTime(5 * 1000); // advance unitl cronjob execution
  249. jest.advanceTimersByTime(secondsUntilRequest); // advance until request execution
  250. jest.useRealTimers(); // after cronjob starts, undo timer mocks so mongoose can work properly
  251. await new Promise((resolve) => {
  252. // wait until cronjob execution finishes
  253. // refs: https://github.com/node-cron/node-cron/blob/a0be3f4a7a5419af109cecf4a41071ea559b9b3d/src/task.js#L24
  254. crowi.questionnaireCronService.cronJob._task.once('task-finished', resolve);
  255. });
  256. const savedOrders = await QuestionnaireOrder.find()
  257. .select('-condition._id -questions._id')
  258. .sort({ _id: 1 });
  259. expect(JSON.parse(JSON.stringify(savedOrders))).toEqual([
  260. {
  261. _id: '63a8354837e7aa378e16f0b1',
  262. shortTitle: {
  263. ja_JP: 'GROWI に関するアンケート',
  264. en_US: 'Questions about GROWI',
  265. },
  266. title: {
  267. ja_JP: 'GROWI に関するアンケート',
  268. en_US: 'Questions about GROWI',
  269. },
  270. showFrom: '2022-12-11T00:00:00.000Z',
  271. showUntil: '2100-12-12T00:00:00.000Z',
  272. questions: [
  273. {
  274. type: 'points',
  275. text: {
  276. ja_JP: 'GROWI は使いやすいですか?',
  277. en_US: 'Is GROWI easy to use?',
  278. },
  279. },
  280. ],
  281. condition: {
  282. user: {
  283. types: ['admin', 'general'],
  284. },
  285. growi: {
  286. types: ['cloud', 'private-cloud'],
  287. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  288. },
  289. },
  290. createdAt: '2022-12-01T00:00:00.000Z',
  291. updatedAt: '2022-12-01T00:00:00.000Z',
  292. __v: 0,
  293. },
  294. {
  295. _id: '63a8354837e7aa378e16f0b2',
  296. shortTitle: {
  297. ja_JP: 'GROWI に関するアンケート',
  298. en_US: 'Questions about GROWI',
  299. },
  300. title: {
  301. ja_JP: 'GROWI に関するアンケート',
  302. en_US: 'Questions about GROWI',
  303. },
  304. showFrom: '2021-12-11T00:00:00.000Z',
  305. showUntil: '2100-12-12T00:00:00.000Z',
  306. questions: [
  307. {
  308. type: 'points',
  309. text: {
  310. ja_JP: 'アンケート機能は正常動作していますか?',
  311. en_US: 'Is this questionnaire functioning properly?',
  312. },
  313. },
  314. ],
  315. condition: {
  316. user: {
  317. types: ['general'],
  318. },
  319. growi: {
  320. types: ['cloud'],
  321. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  322. },
  323. },
  324. createdAt: '2022-12-02T00:00:00.000Z',
  325. updatedAt: '2022-12-02T00:00:00.000Z',
  326. __v: 0,
  327. },
  328. ]);
  329. });
  330. });