test_pagure_lib_watch_list.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2016 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources
  9. import unittest
  10. import shutil
  11. import sys
  12. import os
  13. import mock
  14. sys.path.insert(0, os.path.join(os.path.dirname(
  15. os.path.abspath(__file__)), '..'))
  16. import pagure.lib
  17. import pagure.lib.model
  18. import tests
  19. @mock.patch(
  20. 'pagure.lib.git.update_git', mock.MagicMock(return_value=True))
  21. @mock.patch(
  22. 'pagure.lib.notify.send_email', mock.MagicMock(return_value=True))
  23. class PagureLibGetWatchListtests(tests.Modeltests):
  24. """ Tests for pagure.lib.get_watch_list """
  25. def test_get_watch_list_invalid_object(self):
  26. """ Test get_watch_list when given an invalid object """
  27. # Create a project ns/test
  28. item = pagure.lib.model.Project(
  29. user_id=1, # pingou
  30. name='test3',
  31. namespace='ns',
  32. description='test project #1',
  33. hook_token='aaabbbcccdd',
  34. )
  35. item.close_status = ['Invalid', 'Insufficient data', 'Fixed']
  36. self.session.add(item)
  37. self.session.commit()
  38. self.assertRaises(
  39. pagure.exceptions.InvalidObjectException,
  40. pagure.lib.get_watch_list,
  41. self.session,
  42. item
  43. )
  44. def test_get_watch_list_simple(self):
  45. """ Test get_watch_list when the creator of the ticket is the
  46. creator of the project """
  47. # Create a project ns/test
  48. item = pagure.lib.model.Project(
  49. user_id=1, # pingou
  50. name='test3',
  51. namespace='ns',
  52. description='test project #1',
  53. hook_token='aaabbbcccdd',
  54. )
  55. item.close_status = ['Invalid', 'Insufficient data', 'Fixed']
  56. self.session.add(item)
  57. self.session.commit()
  58. # Create the ticket
  59. iss = pagure.lib.new_issue(
  60. issue_id=4,
  61. session=self.session,
  62. repo=item,
  63. title='test issue',
  64. content='content test issue',
  65. user='pingou',
  66. ticketfolder=None,
  67. )
  68. self.session.commit()
  69. self.assertEqual(iss.id, 4)
  70. self.assertEqual(iss.title, 'test issue')
  71. self.assertEqual(
  72. pagure.lib.get_watch_list(self.session, iss),
  73. set(['pingou'])
  74. )
  75. def test_get_watch_list_different_creator(self):
  76. """ Test get_watch_list when the creator of the ticket is not the
  77. creator of the project """
  78. # Create a project ns/test
  79. item = pagure.lib.model.Project(
  80. user_id=1, # pingou
  81. name='test3',
  82. namespace='ns',
  83. description='test project #1',
  84. hook_token='aaabbbcccdd',
  85. )
  86. item.close_status = ['Invalid', 'Insufficient data', 'Fixed']
  87. self.session.add(item)
  88. self.session.commit()
  89. # Create the ticket
  90. iss = pagure.lib.new_issue(
  91. issue_id=4,
  92. session=self.session,
  93. repo=item,
  94. title='test issue',
  95. content='content test issue',
  96. user='foo',
  97. ticketfolder=None,
  98. )
  99. self.session.commit()
  100. self.assertEqual(iss.id, 4)
  101. self.assertEqual(iss.title, 'test issue')
  102. self.assertEqual(
  103. pagure.lib.get_watch_list(self.session, iss),
  104. set(['pingou', 'foo'])
  105. )
  106. def test_get_watch_list_project_w_contributor(self):
  107. """ Test get_watch_list when the project has more than one
  108. contributor """
  109. # Create a project ns/test3
  110. item = pagure.lib.model.Project(
  111. user_id=1, # pingou
  112. name='test3',
  113. namespace='ns',
  114. description='test project #1',
  115. hook_token='aaabbbcccdd',
  116. )
  117. item.close_status = ['Invalid', 'Insufficient data', 'Fixed']
  118. self.session.add(item)
  119. self.session.commit()
  120. # Add a contributor to the project
  121. item = pagure.lib.model.User(
  122. user='bar',
  123. fullname='bar foo',
  124. password='foo',
  125. default_email='bar@bar.com',
  126. )
  127. self.session.add(item)
  128. item = pagure.lib.model.UserEmail(
  129. user_id=3,
  130. email='bar@bar.com')
  131. self.session.add(item)
  132. project = pagure.lib._get_project(
  133. self.session, 'test3', namespace='ns')
  134. msg = pagure.lib.add_user_to_project(
  135. session=self.session,
  136. project=project,
  137. new_user='bar',
  138. user='pingou',
  139. )
  140. self.session.commit()
  141. self.assertEqual(msg, 'User added')
  142. # Create the ticket
  143. iss = pagure.lib.new_issue(
  144. issue_id=4,
  145. session=self.session,
  146. repo=project,
  147. title='test issue',
  148. content='content test issue',
  149. user='foo',
  150. ticketfolder=None,
  151. )
  152. self.session.commit()
  153. self.assertEqual(iss.id, 4)
  154. self.assertEqual(iss.title, 'test issue')
  155. self.assertEqual(
  156. pagure.lib.get_watch_list(self.session, iss),
  157. set(['pingou', 'foo', 'bar'])
  158. )
  159. def test_get_watch_list_user_in_group(self):
  160. """ Test get_watch_list when the project has groups of contributors
  161. """
  162. # Create a project ns/test3
  163. item = pagure.lib.model.Project(
  164. user_id=1, # pingou
  165. name='test3',
  166. namespace='ns',
  167. description='test project #1',
  168. hook_token='aaabbbcccdd',
  169. )
  170. item.close_status = ['Invalid', 'Insufficient data', 'Fixed']
  171. self.session.add(item)
  172. self.session.commit()
  173. # Create a third user
  174. item = pagure.lib.model.User(
  175. user='bar',
  176. fullname='bar foo',
  177. password='foo',
  178. default_email='bar@bar.com',
  179. )
  180. self.session.add(item)
  181. item = pagure.lib.model.UserEmail(
  182. user_id=3,
  183. email='bar@bar.com')
  184. self.session.add(item)
  185. # Create a group
  186. msg = pagure.lib.add_group(
  187. self.session,
  188. group_name='foo',
  189. display_name='foo group',
  190. description=None,
  191. group_type='bar',
  192. user='pingou',
  193. is_admin=False,
  194. blacklist=[],
  195. )
  196. self.session.commit()
  197. self.assertEqual(msg, 'User `pingou` added to the group `foo`.')
  198. # Add user to group
  199. group = pagure.lib.search_groups(self.session, group_name='foo')
  200. msg = pagure.lib.add_user_to_group(
  201. self.session,
  202. username='bar',
  203. group=group,
  204. user='pingou',
  205. is_admin=False,
  206. )
  207. self.session.commit()
  208. self.assertEqual(msg, 'User `bar` added to the group `foo`.')
  209. project = pagure.lib._get_project(
  210. self.session, 'test3', namespace='ns')
  211. # Add group to project
  212. msg = pagure.lib.add_group_to_project(
  213. session=self.session,
  214. project=project,
  215. new_group='foo',
  216. user='pingou',
  217. )
  218. self.session.commit()
  219. self.assertEqual(msg, 'Group added')
  220. # Create the ticket
  221. iss = pagure.lib.new_issue(
  222. issue_id=4,
  223. session=self.session,
  224. repo=project,
  225. title='test issue',
  226. content='content test issue',
  227. user='foo',
  228. ticketfolder=None,
  229. )
  230. self.session.commit()
  231. self.assertEqual(iss.id, 4)
  232. self.assertEqual(iss.title, 'test issue')
  233. self.assertEqual(
  234. pagure.lib.get_watch_list(self.session, iss),
  235. set(['pingou', 'foo', 'bar'])
  236. )
  237. def test_get_watch_list_project_w_contributor_out(self):
  238. """ Test get_watch_list when the project has one contributor not
  239. watching the project """
  240. # Create a project ns/test3
  241. item = pagure.lib.model.Project(
  242. user_id=1, # pingou
  243. name='test3',
  244. namespace='ns',
  245. description='test project #1',
  246. hook_token='aaabbbcccdd',
  247. )
  248. item.close_status = ['Invalid', 'Insufficient data', 'Fixed']
  249. self.session.add(item)
  250. self.session.commit()
  251. # Add a contributor to the project
  252. item = pagure.lib.model.User(
  253. user='bar',
  254. fullname='bar foo',
  255. password='foo',
  256. default_email='bar@bar.com',
  257. )
  258. self.session.add(item)
  259. item = pagure.lib.model.UserEmail(
  260. user_id=3,
  261. email='bar@bar.com')
  262. self.session.add(item)
  263. project = pagure.lib._get_project(
  264. self.session, 'test3', namespace='ns')
  265. msg = pagure.lib.add_user_to_project(
  266. session=self.session,
  267. project=project,
  268. new_user='bar',
  269. user='pingou',
  270. )
  271. self.session.commit()
  272. self.assertEqual(msg, 'User added')
  273. # Set the user `pingou` to not watch the project
  274. msg = pagure.lib.update_watch_status(
  275. session=self.session,
  276. project=project,
  277. user='pingou',
  278. watch='0',
  279. )
  280. self.session.commit()
  281. self.assertEqual(msg, 'You are no longer watching this project')
  282. # Create the ticket
  283. iss = pagure.lib.new_issue(
  284. issue_id=4,
  285. session=self.session,
  286. repo=project,
  287. title='test issue',
  288. content='content test issue',
  289. user='foo',
  290. ticketfolder=None,
  291. )
  292. self.session.commit()
  293. self.assertEqual(iss.id, 4)
  294. self.assertEqual(iss.title, 'test issue')
  295. self.assertEqual(
  296. pagure.lib.get_watch_list(self.session, iss),
  297. set(['foo', 'bar'])
  298. )
  299. def test_get_watch_list_project_w_contributor_out_pr(self):
  300. """ Test get_watch_list when the project has one contributor not
  301. watching the pull-request """
  302. # Create a project ns/test3
  303. item = pagure.lib.model.Project(
  304. user_id=1, # pingou
  305. name='test3',
  306. namespace='ns',
  307. description='test project #1',
  308. hook_token='aaabbbcccdd',
  309. )
  310. item.close_status = ['Invalid', 'Insufficient data', 'Fixed']
  311. self.session.add(item)
  312. self.session.commit()
  313. # Add a contributor to the project
  314. item = pagure.lib.model.User(
  315. user='bar',
  316. fullname='bar foo',
  317. password='foo',
  318. default_email='bar@bar.com',
  319. )
  320. self.session.add(item)
  321. item = pagure.lib.model.UserEmail(
  322. user_id=3,
  323. email='bar@bar.com')
  324. self.session.add(item)
  325. project = pagure.lib._get_project(
  326. self.session, 'test3', namespace='ns')
  327. msg = pagure.lib.add_user_to_project(
  328. session=self.session,
  329. project=project,
  330. new_user='bar',
  331. user='pingou',
  332. )
  333. self.session.commit()
  334. self.assertEqual(msg, 'User added')
  335. # Create the pull-request
  336. req = pagure.lib.new_pull_request(
  337. session=self.session,
  338. repo_from=project,
  339. branch_from='dev',
  340. repo_to=project,
  341. branch_to='master',
  342. title='test pull-request',
  343. user='foo',
  344. requestfolder=None,
  345. )
  346. self.session.commit()
  347. self.assertEqual(req.id, 1)
  348. self.assertEqual(req.title, 'test pull-request')
  349. # Set the user `pingou` to not watch the pull-request
  350. out = pagure.lib.set_watch_obj(self.session, 'pingou', req, False)
  351. self.assertEqual(
  352. out, 'You are no longer watching this pull-request')
  353. self.assertEqual(
  354. pagure.lib.get_watch_list(self.session, req),
  355. set(['foo', 'bar'])
  356. )
  357. def test_get_watch_list_project_w_contributor_watching_project(self):
  358. """ Test get_watch_list when the project has one contributor watching
  359. the project """
  360. # Create a project ns/test3
  361. item = pagure.lib.model.Project(
  362. user_id=1, # pingou
  363. name='test3',
  364. namespace='ns',
  365. description='test project #1',
  366. hook_token='aaabbbcccdd',
  367. )
  368. item.close_status = ['Invalid', 'Insufficient data', 'Fixed']
  369. self.session.add(item)
  370. self.session.commit()
  371. # Add a new user
  372. item = pagure.lib.model.User(
  373. user='bar',
  374. fullname='bar foo',
  375. password='foo',
  376. default_email='bar@bar.com',
  377. )
  378. self.session.add(item)
  379. item = pagure.lib.model.UserEmail(
  380. user_id=3,
  381. email='bar@bar.com')
  382. self.session.add(item)
  383. # Set the user `bar` to watch the project
  384. project = pagure.lib._get_project(
  385. self.session, 'test3', namespace='ns')
  386. msg = pagure.lib.update_watch_status(
  387. session=self.session,
  388. project=project,
  389. user='bar',
  390. watch='1',
  391. )
  392. self.session.commit()
  393. self.assertEqual(
  394. msg, 'You are now watching issues and PRs on this project')
  395. # Create the pull-request
  396. req = pagure.lib.new_pull_request(
  397. session=self.session,
  398. repo_from=project,
  399. branch_from='dev',
  400. repo_to=project,
  401. branch_to='master',
  402. title='test pull-request',
  403. user='foo',
  404. requestfolder=None,
  405. )
  406. self.session.commit()
  407. self.assertEqual(req.id, 1)
  408. self.assertEqual(req.title, 'test pull-request')
  409. self.assertEqual(
  410. pagure.lib.get_watch_list(self.session, req),
  411. set(['foo', 'bar', 'pingou'])
  412. )
  413. def test_get_watch_list_project_w_private_issue(self):
  414. """ Test get_watch_list when the project has one contributor watching
  415. the project and the issue is private """
  416. # Create a project ns/test3
  417. item = pagure.lib.model.Project(
  418. user_id=1, # pingou
  419. name='test3',
  420. namespace='ns',
  421. description='test project #1',
  422. hook_token='aaabbbcccdd',
  423. )
  424. item.close_status = ['Invalid', 'Insufficient data', 'Fixed']
  425. self.session.add(item)
  426. self.session.commit()
  427. # Add a new user
  428. item = pagure.lib.model.User(
  429. user='bar',
  430. fullname='bar foo',
  431. password='foo',
  432. default_email='bar@bar.com',
  433. )
  434. self.session.add(item)
  435. item = pagure.lib.model.UserEmail(
  436. user_id=3,
  437. email='bar@bar.com')
  438. self.session.add(item)
  439. # Set the user `bar` to watch the project
  440. project = pagure.lib.get_authorized_project(
  441. self.session, 'test3', namespace='ns')
  442. msg = pagure.lib.update_watch_status(
  443. session=self.session,
  444. project=project,
  445. user='bar',
  446. watch='1',
  447. )
  448. self.session.commit()
  449. self.assertEqual(
  450. msg, 'You are now watching issues and PRs on this project')
  451. # Create the ticket
  452. iss = pagure.lib.new_issue(
  453. issue_id=4,
  454. session=self.session,
  455. repo=project,
  456. title='test issue',
  457. content='content test issue',
  458. user='pingou',
  459. private=True,
  460. ticketfolder=None,
  461. )
  462. self.session.commit()
  463. self.assertEqual(iss.id, 4)
  464. self.assertEqual(iss.title, 'test issue')
  465. self.assertEqual(
  466. pagure.lib.get_watch_list(self.session, iss),
  467. set(['pingou'])
  468. )
  469. if __name__ == '__main__':
  470. unittest.main(verbosity=2)