questionnaire.test.ts 9.6 KB

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