test_pagure_lib_watch_list.py 16 KB

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