questionnaire.test.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. import mongoose from 'mongoose';
  2. import { StatusType } from '../../../src/features/questionnaire/interfaces/questionnaire-answer-status';
  3. import QuestionnaireAnswerStatus from '../../../src/features/questionnaire/server/models/questionnaire-answer-status';
  4. import QuestionnaireOrder from '../../../src/features/questionnaire/server/models/questionnaire-order';
  5. import { getInstance } from '../setup-crowi';
  6. describe('QuestionnaireService', () => {
  7. let crowi;
  8. let user;
  9. beforeAll(async() => {
  10. process.env.APP_SITE_URL = 'http://growi.test.jp';
  11. process.env.DEPLOYMENT_TYPE = 'growi-docker-compose';
  12. process.env.SAML_ENABLED = 'true';
  13. crowi = await getInstance();
  14. crowi.configManager.updateConfigs({
  15. 'security:passport-saml:isEnabled': true,
  16. 'security:passport-github:isEnabled': true,
  17. });
  18. await mongoose.model('Config').create({
  19. key: 'app:installed',
  20. value: true,
  21. createdAt: '2000-01-01',
  22. });
  23. crowi.setupQuestionnaireService();
  24. const User = crowi.model('User');
  25. user = await User.create({
  26. name: 'Example for Questionnaire Service Test',
  27. username: 'questionnaire test user',
  28. email: 'questionnaireTestUser@example.com',
  29. password: 'usertestpass',
  30. createdAt: '2000-01-01',
  31. });
  32. });
  33. describe('getGrowiInfo', () => {
  34. test('Should get correct GROWI info', async() => {
  35. const growiInfo = await crowi.questionnaireService.getGrowiInfo();
  36. expect(growiInfo.appSiteUrlHashed).toBeTruthy();
  37. expect(growiInfo.appSiteUrlHashed).not.toBe('http://growi.test.jp');
  38. expect(growiInfo.osInfo.type).toBeTruthy();
  39. expect(growiInfo.osInfo.platform).toBeTruthy();
  40. expect(growiInfo.osInfo.arch).toBeTruthy();
  41. expect(growiInfo.osInfo.totalmem).toBeTruthy();
  42. delete growiInfo.appSiteUrlHashed;
  43. delete growiInfo.currentActiveUsersCount;
  44. delete growiInfo.currentUsersCount;
  45. delete growiInfo.osInfo;
  46. expect(growiInfo).toEqual({
  47. activeExternalAccountTypes: ['saml', 'github'],
  48. appSiteUrl: 'http://growi.test.jp',
  49. installedAt: new Date('2000-01-01'),
  50. installedAtByOldestUser: new Date('2000-01-01'),
  51. attachmentType: 'aws',
  52. deploymentType: 'growi-docker-compose',
  53. type: 'on-premise',
  54. version: crowi.version,
  55. wikiType: 'closed',
  56. });
  57. });
  58. describe('When url hash settings is on', () => {
  59. beforeEach(async() => {
  60. process.env.QUESTIONNAIRE_IS_APP_SITE_URL_HASHED = 'true';
  61. await crowi.setupConfigManager();
  62. });
  63. test('Should return app url string', async() => {
  64. const growiInfo = await crowi.questionnaireService.getGrowiInfo();
  65. expect(growiInfo.appSiteUrl).toBe(null);
  66. expect(growiInfo.appSiteUrlHashed).not.toBe('http://growi.test.jp');
  67. expect(growiInfo.appSiteUrlHashed).toBeTruthy();
  68. });
  69. });
  70. });
  71. describe('getUserInfo', () => {
  72. test('Should get correct user info when user given', () => {
  73. const userInfo = crowi.questionnaireService.getUserInfo(user, 'growiurlhashfortest');
  74. expect(userInfo.userIdHash).toBeTruthy();
  75. expect(userInfo.userIdHash).not.toBe(user._id);
  76. delete userInfo.userIdHash;
  77. expect(userInfo).toEqual({ type: 'general', userCreatedAt: new Date('2000-01-01') });
  78. });
  79. test('Should get correct user info when user is null', () => {
  80. const userInfo = crowi.questionnaireService.getUserInfo(null, '');
  81. expect(userInfo).toEqual({ type: 'guest' });
  82. });
  83. });
  84. describe('getQuestionnaireOrdersToShow', () => {
  85. beforeAll(async() => {
  86. const questionnaireToBeShown = {
  87. _id: '63b8354837e7aa378e16f0b1',
  88. shortTitle: {
  89. ja_JP: 'GROWI に関するアンケート',
  90. en_US: 'Questions about GROWI',
  91. },
  92. title: {
  93. ja_JP: 'GROWI に関するアンケート',
  94. en_US: 'Questions about GROWI',
  95. },
  96. showFrom: '2022-12-11',
  97. showUntil: '2100-12-12',
  98. condition: {
  99. user: {
  100. types: ['general'],
  101. daysSinceCreation: {
  102. moreThanOrEqualTo: 365,
  103. lessThanOrEqualTo: 365 * 1000,
  104. },
  105. },
  106. growi: {
  107. types: ['on-premise'],
  108. versionRegExps: [crowi.version],
  109. },
  110. },
  111. createdAt: '2023-01-01',
  112. updatedAt: '2023-01-01',
  113. };
  114. // insert initial db data
  115. await QuestionnaireOrder.insertMany([
  116. questionnaireToBeShown,
  117. // finished
  118. {
  119. ...questionnaireToBeShown,
  120. _id: '63b8354837e7aa378e16f0b2',
  121. showFrom: '2020-12-11',
  122. showUntil: '2021-12-12',
  123. },
  124. // for admin or guest
  125. {
  126. ...questionnaireToBeShown,
  127. _id: '63b8354837e7aa378e16f0b3',
  128. condition: {
  129. user: {
  130. types: ['admin', 'guest'],
  131. },
  132. growi: {
  133. types: ['on-premise'],
  134. versionRegExps: [crowi.version],
  135. },
  136. },
  137. },
  138. // answered
  139. {
  140. ...questionnaireToBeShown,
  141. _id: '63b8354837e7aa378e16f0b4',
  142. },
  143. // skipped
  144. {
  145. ...questionnaireToBeShown,
  146. _id: '63b8354837e7aa378e16f0b5',
  147. },
  148. // denied
  149. {
  150. ...questionnaireToBeShown,
  151. _id: '63b8354837e7aa378e16f0b6',
  152. },
  153. // for different growi type
  154. {
  155. ...questionnaireToBeShown,
  156. _id: '63b8354837e7aa378e16f0b7',
  157. condition: {
  158. user: {
  159. types: ['general'],
  160. },
  161. growi: {
  162. types: ['cloud'],
  163. versionRegExps: [crowi.version],
  164. },
  165. },
  166. },
  167. // for different growi version
  168. {
  169. ...questionnaireToBeShown,
  170. _id: '63b8354837e7aa378e16f0b8',
  171. condition: {
  172. user: {
  173. types: ['general'],
  174. },
  175. growi: {
  176. types: ['on-premise'],
  177. versionRegExps: ['1.0.0-alpha'],
  178. },
  179. },
  180. },
  181. // for users that used GROWI for less than or equal to a year
  182. {
  183. ...questionnaireToBeShown,
  184. _id: '63b8354837e7aa378e16f0b9',
  185. condition: {
  186. user: {
  187. types: ['general'],
  188. daysSinceCreation: {
  189. lessThanOrEqualTo: 365,
  190. },
  191. },
  192. growi: {
  193. types: ['on-premise'],
  194. versionRegExps: [crowi.version],
  195. },
  196. },
  197. },
  198. // for users that used GROWI for more than or equal to 1000 years
  199. {
  200. ...questionnaireToBeShown,
  201. _id: '63b8354837e7aa378e16f0c1',
  202. condition: {
  203. user: {
  204. types: ['general'],
  205. daysSinceCreation: {
  206. moreThanOrEqualTo: 365 * 1000,
  207. },
  208. },
  209. growi: {
  210. types: ['on-premise'],
  211. versionRegExps: [crowi.version],
  212. },
  213. },
  214. },
  215. // for users that used GROWI for more than a month and less than 6 months
  216. {
  217. ...questionnaireToBeShown,
  218. _id: '63b8354837e7aa378e16f0c2',
  219. condition: {
  220. user: {
  221. types: ['general'],
  222. daysSinceCreation: {
  223. moreThanOrEqualTo: 30,
  224. lessThanOrEqualTo: 30 * 6,
  225. },
  226. },
  227. growi: {
  228. types: ['on-premise'],
  229. versionRegExps: [crowi.version],
  230. },
  231. },
  232. },
  233. ]);
  234. await QuestionnaireAnswerStatus.insertMany([
  235. {
  236. user: user._id,
  237. questionnaireOrderId: '63b8354837e7aa378e16f0b4',
  238. status: StatusType.answered,
  239. },
  240. {
  241. user: user._id,
  242. questionnaireOrderId: '63b8354837e7aa378e16f0b5',
  243. status: StatusType.skipped,
  244. },
  245. {
  246. user: user._id,
  247. questionnaireOrderId: '63b8354837e7aa378e16f0b6',
  248. status: StatusType.skipped,
  249. },
  250. ]);
  251. });
  252. test('Should get questionnaire orders to show', async() => {
  253. const growiInfo = await crowi.questionnaireService.getGrowiInfo();
  254. const userInfo = crowi.questionnaireService.getUserInfo(user, growiInfo.appSiteUrlHashed);
  255. const questionnaireOrderDocuments = await crowi.questionnaireService.getQuestionnaireOrdersToShow(userInfo, growiInfo, user._id);
  256. const questionnaireOrderObjects = questionnaireOrderDocuments.map((document) => {
  257. const qo = document.toObject();
  258. delete qo.condition._id;
  259. return { ...qo, _id: qo._id.toString() };
  260. });
  261. expect(questionnaireOrderObjects).toEqual([
  262. {
  263. _id: '63b8354837e7aa378e16f0b1',
  264. __v: 0,
  265. shortTitle: {
  266. ja_JP: 'GROWI に関するアンケート',
  267. en_US: 'Questions about GROWI',
  268. },
  269. title: {
  270. ja_JP: 'GROWI に関するアンケート',
  271. en_US: 'Questions about GROWI',
  272. },
  273. showFrom: new Date('2022-12-11'),
  274. showUntil: new Date('2100-12-12'),
  275. questions: [],
  276. condition: {
  277. user: {
  278. types: ['general'],
  279. daysSinceCreation: {
  280. moreThanOrEqualTo: 365,
  281. lessThanOrEqualTo: 365 * 1000,
  282. },
  283. },
  284. growi: {
  285. types: ['on-premise'],
  286. versionRegExps: [crowi?.version],
  287. },
  288. },
  289. createdAt: new Date('2023-01-01'),
  290. updatedAt: new Date('2023-01-01'),
  291. },
  292. ]);
  293. });
  294. });
  295. });