questionnaire-cron.test.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. // reload
  127. await crowi.setupConfigManager();
  128. });
  129. beforeEach(async() => {
  130. // insert initial db data
  131. await QuestionnaireOrder.insertMany([
  132. {
  133. _id: '63a8354837e7aa378e16f0b1',
  134. shortTitle: {
  135. ja_JP: 'GROWI に関するアンケート',
  136. en_US: 'Questions about GROWI',
  137. },
  138. title: {
  139. ja_JP: 'GROWI に関するアンケート',
  140. en_US: 'Questions about GROWI',
  141. },
  142. showFrom: '2022-12-11',
  143. showUntil: '2100-12-12',
  144. questions: [
  145. {
  146. type: 'points',
  147. text: {
  148. ja_JP: 'GROWI は使いやすいですか?',
  149. en_US: 'Is GROWI easy to use?',
  150. },
  151. },
  152. ],
  153. condition: {
  154. user: {
  155. types: ['general'],
  156. },
  157. growi: {
  158. types: ['cloud', 'private-cloud'],
  159. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  160. },
  161. },
  162. },
  163. // finished
  164. {
  165. _id: '63a8354837e7aa378e16f0b4',
  166. shortTitle: {
  167. ja_JP: 'GROWI に関するアンケート',
  168. en_US: 'Questions about GROWI',
  169. },
  170. title: {
  171. ja_JP: 'GROWI に関するアンケート',
  172. en_US: 'Questions about GROWI',
  173. },
  174. showFrom: '2020-12-11',
  175. showUntil: '2021-12-12',
  176. questions: [
  177. {
  178. type: 'points',
  179. text: {
  180. ja_JP: 'ver 2.0 は 1.0 より良いですか?',
  181. en_US: 'Is ver 2.0 better than 1.0?',
  182. },
  183. },
  184. ],
  185. condition: {
  186. user: {
  187. types: ['general'],
  188. },
  189. growi: {
  190. types: ['cloud'],
  191. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  192. },
  193. },
  194. },
  195. // questionnaire that doesn't exist in questionnaire server
  196. {
  197. _id: '63a8354837e7aa378e16f0b5',
  198. shortTitle: {
  199. ja_JP: 'GROWI に関するアンケート',
  200. en_US: 'Questions about GROWI',
  201. },
  202. title: {
  203. ja_JP: 'GROWI に関するアンケート',
  204. en_US: 'Questions about GROWI',
  205. },
  206. showFrom: '2020-12-11',
  207. showUntil: '2100-12-12',
  208. questions: [
  209. {
  210. type: 'points',
  211. text: {
  212. ja_JP: '新しいデザインは良いですか?',
  213. en_US: 'How would you rate the latest design?',
  214. },
  215. },
  216. ],
  217. condition: {
  218. user: {
  219. types: ['general'],
  220. },
  221. growi: {
  222. types: ['cloud'],
  223. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  224. },
  225. },
  226. },
  227. ]);
  228. await QuestionnaireAnswerStatus.insertMany([
  229. {
  230. user: new mongoose.Types.ObjectId(),
  231. questionnaireOrderId: '63a8354837e7aa378e16f0b1',
  232. status: StatusType.skipped,
  233. },
  234. {
  235. user: new mongoose.Types.ObjectId(),
  236. questionnaireOrderId: '63a8354837e7aa378e16f0b1',
  237. status: StatusType.answered,
  238. },
  239. {
  240. user: new mongoose.Types.ObjectId(),
  241. questionnaireOrderId: '63a8354837e7aa378e16f0b1',
  242. status: StatusType.not_answered,
  243. },
  244. ]);
  245. crowi.setupCron();
  246. spyAxiosGet.mockResolvedValue(mockResponse);
  247. });
  248. afterAll(() => {
  249. crowi.questionnaireCronService.stopCron(); // jest will not finish until cronjob stops
  250. });
  251. test('Job execution should save(update) quesionnaire orders, delete outdated ones, and update skipped answer statuses', async() => {
  252. // testing the cronjob from schedule has untrivial overhead, so test job execution in place
  253. await crowi.questionnaireCronService.executeJob();
  254. const savedOrders = await QuestionnaireOrder.find()
  255. .select('-condition._id -questions._id')
  256. .sort({ _id: 1 });
  257. expect(JSON.parse(JSON.stringify(savedOrders))).toEqual([
  258. {
  259. _id: '63a8354837e7aa378e16f0b1',
  260. shortTitle: {
  261. ja_JP: 'GROWI に関するアンケート',
  262. en_US: 'Questions about GROWI',
  263. },
  264. title: {
  265. ja_JP: 'GROWI に関するアンケート',
  266. en_US: 'Questions about GROWI',
  267. },
  268. showFrom: '2022-12-11T00:00:00.000Z',
  269. showUntil: '2100-12-12T00:00:00.000Z',
  270. questions: [
  271. {
  272. type: 'points',
  273. text: {
  274. ja_JP: 'GROWI は使いやすいですか?',
  275. en_US: 'Is GROWI easy to use?',
  276. },
  277. },
  278. ],
  279. condition: {
  280. user: {
  281. types: ['admin', 'general'],
  282. },
  283. growi: {
  284. types: ['cloud', 'private-cloud'],
  285. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  286. },
  287. },
  288. createdAt: '2022-12-01T00:00:00.000Z',
  289. updatedAt: '2022-12-01T00:00:00.000Z',
  290. __v: 0,
  291. },
  292. {
  293. _id: '63a8354837e7aa378e16f0b2',
  294. shortTitle: {
  295. ja_JP: 'GROWI に関するアンケート',
  296. en_US: 'Questions about GROWI',
  297. },
  298. title: {
  299. ja_JP: 'GROWI に関するアンケート',
  300. en_US: 'Questions about GROWI',
  301. },
  302. showFrom: '2021-12-11T00:00:00.000Z',
  303. showUntil: '2100-12-12T00:00:00.000Z',
  304. questions: [
  305. {
  306. type: 'points',
  307. text: {
  308. ja_JP: 'アンケート機能は正常動作していますか?',
  309. en_US: 'Is this questionnaire functioning properly?',
  310. },
  311. },
  312. ],
  313. condition: {
  314. user: {
  315. types: ['general'],
  316. },
  317. growi: {
  318. types: ['cloud'],
  319. versionRegExps: ['2\\.0\\.[0-9]', '1\\.9\\.[0-9]'],
  320. },
  321. },
  322. createdAt: '2022-12-02T00:00:00.000Z',
  323. updatedAt: '2022-12-02T00:00:00.000Z',
  324. __v: 0,
  325. },
  326. ]);
  327. expect((await QuestionnaireAnswerStatus.find({ status: StatusType.not_answered })).length).toEqual(2);
  328. });
  329. });