test_pagure_lib_watch_list.py 16 KB

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