questionnaire-cron.test.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. 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. title: {
  55. ja_JP: 'GROWI に関するアンケート',
  56. en_US: 'Questions about GROWI',
  57. },
  58. showFrom: '2021-12-11',
  59. showUntil: '2100-12-12',
  60. questions: [
  61. {
  62. type: 'points',
  63. text: {
  64. ja_JP: 'アンケート機能は正常動作していますか?',
  65. en_US: 'Is this questionnaire functioning properly?',
  66. },
  67. },
  68. ],
  69. condition: {
  70. user: {
  71. types: ['general'],
  72. },
  73. growi: {
  74. types: ['cloud'],
  75. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  76. },
  77. },
  78. createdAt: '2022-12-02',
  79. updatedAt: '2022-12-02',
  80. __v: 0,
  81. },
  82. // not saved, finished
  83. {
  84. _id: '63a8354837e7aa378e16f0b3',
  85. title: {
  86. ja_JP: 'GROWI に関するアンケート',
  87. en_US: 'Questions about GROWI',
  88. },
  89. showFrom: '2021-12-11',
  90. showUntil: '2021-12-12',
  91. questions: [
  92. {
  93. type: 'points',
  94. text: {
  95. ja_JP: 'これはいい質問ですか?',
  96. en_US: 'Is this a good question?',
  97. },
  98. },
  99. ],
  100. condition: {
  101. user: {
  102. types: ['general'],
  103. },
  104. growi: {
  105. types: ['cloud'],
  106. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  107. },
  108. },
  109. createdAt: '2022-12-03',
  110. updatedAt: '2022-12-03',
  111. __v: 0,
  112. },
  113. ],
  114. },
  115. };
  116. beforeAll(async() => {
  117. process.env.QUESTIONNAIRE_CRON_SCHEDULE = '0 22 * * *';
  118. process.env.QUESTIONNAIRE_CRON_MAX_HOURS_UNTIL_REQUEST = '4';
  119. crowi = await getInstance();
  120. // reload
  121. await crowi.setupConfigManager();
  122. });
  123. beforeEach(async() => {
  124. // insert initial db data
  125. await QuestionnaireOrder.insertMany([
  126. {
  127. _id: '63a8354837e7aa378e16f0b1',
  128. title: {
  129. ja_JP: 'GROWI に関するアンケート',
  130. en_US: 'Questions about GROWI',
  131. },
  132. showFrom: '2022-12-11',
  133. showUntil: '2100-12-12',
  134. questions: [
  135. {
  136. type: 'points',
  137. text: {
  138. ja_JP: 'GROWI は使いやすいですか?',
  139. en_US: 'Is GROWI easy to use?',
  140. },
  141. },
  142. ],
  143. condition: {
  144. user: {
  145. types: ['general'],
  146. },
  147. growi: {
  148. types: ['cloud', 'private-cloud'],
  149. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  150. },
  151. },
  152. },
  153. // finished
  154. {
  155. _id: '63a8354837e7aa378e16f0b4',
  156. title: {
  157. ja_JP: 'GROWI に関するアンケート',
  158. en_US: 'Questions about GROWI',
  159. },
  160. showFrom: '2020-12-11',
  161. showUntil: '2021-12-12',
  162. questions: [
  163. {
  164. type: 'points',
  165. text: {
  166. ja_JP: 'ver 2.0 は 1.0 より良いですか?',
  167. en_US: 'Is ver 2.0 better than 1.0?',
  168. },
  169. },
  170. ],
  171. condition: {
  172. user: {
  173. types: ['general'],
  174. },
  175. growi: {
  176. types: ['cloud'],
  177. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  178. },
  179. },
  180. },
  181. // questionnaire that doesn't exist in questionnaire server
  182. {
  183. _id: '63a8354837e7aa378e16f0b5',
  184. title: {
  185. ja_JP: 'GROWI に関するアンケート',
  186. en_US: 'Questions about GROWI',
  187. },
  188. showFrom: '2020-12-11',
  189. showUntil: '2100-12-12',
  190. questions: [
  191. {
  192. type: 'points',
  193. text: {
  194. ja_JP: '新しいデザインは良いですか?',
  195. en_US: 'How would you rate the latest design?',
  196. },
  197. },
  198. ],
  199. condition: {
  200. user: {
  201. types: ['general'],
  202. },
  203. growi: {
  204. types: ['cloud'],
  205. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  206. },
  207. },
  208. },
  209. ]);
  210. // mock the date to 5 seconds before cronjob execution
  211. const mockDate = new Date(2022, 0, 1, 21, 59, 55);
  212. jest.useFakeTimers();
  213. jest.setSystemTime(mockDate);
  214. // must be after useFakeTimers for mockDate to be in effect
  215. crowi.setupCron();
  216. spyAxiosGet.mockResolvedValue(mockResponse);
  217. spyGetRandomIntInRange.mockReturnValue(secondsUntilRequest); // static sleep time until request
  218. });
  219. afterAll(() => {
  220. jest.useRealTimers();
  221. crowi.questionnaireCronService.stopCron();
  222. });
  223. test('Should save quesionnaire orders and delete outdated ones', async() => {
  224. jest.advanceTimersByTime(5 * 1000); // advance unitl cronjob execution
  225. jest.advanceTimersByTime(secondsUntilRequest); // advance until request execution
  226. jest.useRealTimers(); // after cronjob starts, undo timer mocks so mongoose can work properly
  227. await new Promise((resolve) => {
  228. // wait until cronjob execution finishes
  229. // refs: https://github.com/node-cron/node-cron/blob/a0be3f4a7a5419af109cecf4a41071ea559b9b3d/src/task.js#L24
  230. crowi.questionnaireCronService.cronJob._task.once('task-finished', resolve);
  231. });
  232. const savedOrders = await QuestionnaireOrder.find()
  233. .select('-condition._id -questions._id')
  234. .sort({ _id: 1 });
  235. expect(JSON.parse(JSON.stringify(savedOrders))).toEqual([
  236. {
  237. _id: '63a8354837e7aa378e16f0b1',
  238. title: {
  239. ja_JP: 'GROWI に関するアンケート',
  240. en_US: 'Questions about GROWI',
  241. },
  242. showFrom: '2022-12-11T00:00:00.000Z',
  243. showUntil: '2100-12-12T00:00:00.000Z',
  244. questions: [
  245. {
  246. type: 'points',
  247. text: {
  248. ja_JP: 'GROWI は使いやすいですか?',
  249. en_US: 'Is GROWI easy to use?',
  250. },
  251. },
  252. ],
  253. condition: {
  254. user: {
  255. types: ['admin', 'general'],
  256. },
  257. growi: {
  258. types: ['cloud', 'private-cloud'],
  259. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  260. },
  261. },
  262. createdAt: '2022-12-01T00:00:00.000Z',
  263. updatedAt: '2022-12-01T00:00:00.000Z',
  264. __v: 0,
  265. },
  266. {
  267. _id: '63a8354837e7aa378e16f0b2',
  268. title: {
  269. ja_JP: 'GROWI に関するアンケート',
  270. en_US: 'Questions about GROWI',
  271. },
  272. showFrom: '2021-12-11T00:00:00.000Z',
  273. showUntil: '2100-12-12T00:00:00.000Z',
  274. questions: [
  275. {
  276. type: 'points',
  277. text: {
  278. ja_JP: 'アンケート機能は正常動作していますか?',
  279. en_US: 'Is this questionnaire functioning properly?',
  280. },
  281. },
  282. ],
  283. condition: {
  284. user: {
  285. types: ['general'],
  286. },
  287. growi: {
  288. types: ['cloud'],
  289. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  290. },
  291. },
  292. createdAt: '2022-12-02T00:00:00.000Z',
  293. updatedAt: '2022-12-02T00:00:00.000Z',
  294. __v: 0,
  295. },
  296. ]);
  297. });
  298. });