test_pagure_flask.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2017 - 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. import munch
  14. import pygit2
  15. sys.path.insert(
  16. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  17. )
  18. import pagure.lib.query
  19. import pagure.lib.model
  20. import pagure.utils
  21. import tests
  22. class PagureGetRemoteRepoPath(tests.SimplePagureTest):
  23. """Tests for pagure"""
  24. def setUp(self):
  25. """Set up the environnment, ran before every tests."""
  26. super(PagureGetRemoteRepoPath, self).setUp()
  27. tests.create_projects(self.session)
  28. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  29. tests.add_content_git_repo(
  30. os.path.join(self.path, "repos", "test2.git")
  31. )
  32. @mock.patch(
  33. "pagure.lib.repo.PagureRepo.pull",
  34. mock.MagicMock(side_effect=pygit2.GitError),
  35. )
  36. def test_passing(self):
  37. """Test get_remote_repo_path in pagure."""
  38. output = pagure.utils.get_remote_repo_path(
  39. os.path.join(self.path, "repos", "test2.git"),
  40. "master",
  41. ignore_non_exist=True,
  42. )
  43. self.assertTrue(output.endswith("repos_test2.git_master"))
  44. def test_is_repo_committer_logged_out(self):
  45. """Test is_repo_committer in pagure when there is no logged in user."""
  46. repo = pagure.lib.query._get_project(self.session, "test")
  47. with self.app.application.app_context():
  48. output = pagure.utils.is_repo_committer(repo)
  49. self.assertFalse(output)
  50. def test_is_repo_committer_logged_in(self):
  51. """Test is_repo_committer in pagure with the appropriate user logged
  52. in."""
  53. repo = pagure.lib.query._get_project(self.session, "test")
  54. g = munch.Munch()
  55. g.fas_user = tests.FakeUser(username="pingou")
  56. g.authenticated = True
  57. g.session = self.session
  58. with mock.patch("flask.g", g):
  59. output = pagure.utils.is_repo_committer(repo)
  60. self.assertTrue(output)
  61. def test_is_repo_committer_logged_in_in_group(self):
  62. """Test is_repo_committer in pagure with the appropriate user logged
  63. in."""
  64. # Create group
  65. msg = pagure.lib.query.add_group(
  66. self.session,
  67. group_name="packager",
  68. display_name="packager",
  69. description="The Fedora packager groups",
  70. group_type="user",
  71. user="pingou",
  72. is_admin=False,
  73. blacklist=[],
  74. )
  75. self.session.commit()
  76. self.assertEqual(msg, "User `pingou` added to the group `packager`.")
  77. # Add user to group
  78. group = pagure.lib.query.search_groups(
  79. self.session, group_name="packager"
  80. )
  81. msg = pagure.lib.query.add_user_to_group(
  82. self.session,
  83. username="foo",
  84. group=group,
  85. user="pingou",
  86. is_admin=True,
  87. )
  88. self.session.commit()
  89. self.assertEqual(msg, "User `foo` added to the group `packager`.")
  90. # Add group packager to project test
  91. project = pagure.lib.query._get_project(self.session, "test")
  92. msg = pagure.lib.query.add_group_to_project(
  93. self.session, project=project, new_group="packager", user="pingou"
  94. )
  95. self.session.commit()
  96. self.assertEqual(msg, "Group added")
  97. repo = pagure.lib.query._get_project(self.session, "test")
  98. g = munch.Munch()
  99. g.fas_user = tests.FakeUser(username="foo")
  100. g.authenticated = True
  101. g.session = self.session
  102. with mock.patch("flask.g", g):
  103. output = pagure.utils.is_repo_committer(repo)
  104. self.assertTrue(output)
  105. def test_is_repo_committer_logged_in_in_ticket_group(self):
  106. """Test is_repo_committer in pagure with the appropriate user logged
  107. in."""
  108. # Create group
  109. msg = pagure.lib.query.add_group(
  110. self.session,
  111. group_name="packager",
  112. display_name="packager",
  113. description="The Fedora packager groups",
  114. group_type="user",
  115. user="pingou",
  116. is_admin=False,
  117. blacklist=[],
  118. )
  119. self.session.commit()
  120. self.assertEqual(msg, "User `pingou` added to the group `packager`.")
  121. # Add user to group
  122. group = pagure.lib.query.search_groups(
  123. self.session, group_name="packager"
  124. )
  125. msg = pagure.lib.query.add_user_to_group(
  126. self.session,
  127. username="foo",
  128. group=group,
  129. user="pingou",
  130. is_admin=True,
  131. )
  132. self.session.commit()
  133. self.assertEqual(msg, "User `foo` added to the group `packager`.")
  134. # Add group packager to project test
  135. project = pagure.lib.query._get_project(self.session, "test")
  136. msg = pagure.lib.query.add_group_to_project(
  137. self.session,
  138. project=project,
  139. new_group="packager",
  140. user="pingou",
  141. access="ticket",
  142. )
  143. self.session.commit()
  144. self.assertEqual(msg, "Group added")
  145. repo = pagure.lib.query._get_project(self.session, "test")
  146. g = munch.Munch()
  147. g.fas_user = tests.FakeUser(username="foo")
  148. g.authenticated = True
  149. g.session = self.session
  150. with mock.patch("flask.g", g):
  151. output = pagure.utils.is_repo_committer(repo)
  152. self.assertFalse(output)
  153. def test_is_repo_committer_logged_in_wrong_user(self):
  154. """Test is_repo_committer in pagure with the wrong user logged in."""
  155. repo = pagure.lib.query._get_project(self.session, "test")
  156. g = munch.Munch()
  157. g.fas_user = tests.FakeUser()
  158. g.authenticated = True
  159. g.session = self.session
  160. with mock.patch("flask.g", g):
  161. output = pagure.utils.is_repo_committer(repo)
  162. self.assertFalse(output)
  163. # Mocked config
  164. config = {"provenpackager": {}}
  165. @mock.patch.dict("pagure.config.config", {"EXTERNAL_COMMITTER": config})
  166. def test_is_repo_committer_external_committer_generic_no_member(self):
  167. """Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  168. configured to give access to all the provenpackager, but the user
  169. is not one.
  170. """
  171. repo = pagure.lib.query._get_project(self.session, "test")
  172. user = tests.FakeUser()
  173. g = munch.Munch()
  174. g.fas_user = user
  175. g.authenticated = True
  176. g.session = self.session
  177. with mock.patch("flask.g", g):
  178. output = pagure.utils.is_repo_committer(repo)
  179. self.assertFalse(output)
  180. @mock.patch.dict("pagure.config.config", {"EXTERNAL_COMMITTER": config})
  181. def test_is_repo_committer_external_committer_generic_member(self):
  182. """Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  183. configured to give access to all the provenpackager, and the user
  184. is one
  185. """
  186. repo = pagure.lib.query._get_project(self.session, "test")
  187. g = munch.Munch()
  188. g.fas_user = tests.FakeUser(username="foo")
  189. g.fas_user.groups.append("provenpackager")
  190. g.authenticated = True
  191. g.session = self.session
  192. with mock.patch("flask.g", g):
  193. output = pagure.utils.is_repo_committer(repo)
  194. self.assertTrue(output)
  195. config = {"provenpackager": {"exclude": ["test"]}}
  196. @mock.patch.dict("pagure.config.config", {"EXTERNAL_COMMITTER": config})
  197. def test_is_repo_committer_external_committer_excluding_one(self):
  198. """Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  199. configured to give access to all the provenpackager but for this
  200. one repo
  201. """
  202. repo = pagure.lib.query._get_project(self.session, "test")
  203. g = munch.Munch()
  204. g.fas_user = tests.FakeUser()
  205. g.fas_user.groups.append("provenpackager")
  206. g.authenticated = True
  207. g.session = self.session
  208. with mock.patch("flask.g", g):
  209. output = pagure.utils.is_repo_committer(repo)
  210. self.assertFalse(output)
  211. @mock.patch.dict("pagure.config.config", {"EXTERNAL_COMMITTER": config})
  212. def test_is_repo_committer_owner_external_committer_excluding_one(self):
  213. """Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  214. configured to give access to all the provenpackager but for this
  215. one repo, but the user is still a direct committer
  216. """
  217. repo = pagure.lib.query._get_project(self.session, "test")
  218. g = munch.Munch()
  219. g.fas_user = tests.FakeUser(username="pingou")
  220. g.fas_user.groups.append("provenpackager")
  221. g.authenticated = True
  222. g.session = self.session
  223. with mock.patch("flask.g", g):
  224. output = pagure.utils.is_repo_committer(repo)
  225. self.assertTrue(output)
  226. config = {"provenpackager": {"restrict": ["test"]}}
  227. @mock.patch.dict("pagure.config.config", {"EXTERNAL_COMMITTER": config})
  228. def test_is_repo_committer_external_committer_restricted_not_member(self):
  229. """Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  230. configured to give access the provenpackager just for one repo
  231. """
  232. repo = pagure.lib.query._get_project(self.session, "test")
  233. g = munch.Munch()
  234. g.fas_user = tests.FakeUser()
  235. g.authenticated = True
  236. g.session = self.session
  237. with mock.patch("flask.g", g):
  238. output = pagure.utils.is_repo_committer(repo)
  239. self.assertFalse(output)
  240. @mock.patch.dict("pagure.config.config", {"EXTERNAL_COMMITTER": config})
  241. def test_is_repo_committer_external_committer_restricting_to_one(self):
  242. """Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  243. configured to give access the provenpackager just for one repo
  244. """
  245. repo = pagure.lib.query._get_project(self.session, "test")
  246. g = munch.Munch()
  247. g.fas_user = tests.FakeUser(username="foo")
  248. g.authenticated = True
  249. g.fas_user.groups.append("provenpackager")
  250. g.session = self.session
  251. with mock.patch("flask.g", g):
  252. output = pagure.utils.is_repo_committer(repo)
  253. self.assertTrue(output)
  254. @mock.patch.dict("pagure.config.config", {"EXTERNAL_COMMITTER": config})
  255. def test_is_repo_committer_external_committer_restricting_another_one(
  256. self,
  257. ):
  258. """Test is_repo_committer in pagure with EXTERNAL_COMMITTER
  259. configured to give access the provenpackager just for one repo not
  260. this one
  261. """
  262. repo = pagure.lib.query._get_project(self.session, "test2")
  263. g = munch.Munch()
  264. g.fas_user = tests.FakeUser(username="foo")
  265. g.authenticated = True
  266. g.fas_user.groups.append("provenpackager")
  267. g.session = self.session
  268. with mock.patch("flask.g", g):
  269. output = pagure.utils.is_repo_committer(repo)
  270. self.assertFalse(output)
  271. def test_is_repo_collaborator_logged_out(self):
  272. """Test is_repo_committer in pagure when there is no logged in user."""
  273. repo = pagure.lib.query._get_project(self.session, "test")
  274. with self.app.application.app_context():
  275. output = pagure.utils.is_repo_collaborator(repo, "master")
  276. self.assertFalse(output)
  277. def test_is_repo_collaborator_logged_in(self):
  278. """Test is_repo_collaborator in pagure with the appropriate user logged
  279. in."""
  280. repo = pagure.lib.query._get_project(self.session, "test")
  281. g = munch.Munch()
  282. g.fas_user = tests.FakeUser(username="pingou")
  283. g.authenticated = True
  284. g.session = self.session
  285. with mock.patch("flask.g", g):
  286. output = pagure.utils.is_repo_collaborator(
  287. repo, "refs/heads/master"
  288. )
  289. self.assertTrue(output)
  290. def test_is_repo_collaborator_invalid_username(self):
  291. """Test is_repo_collaborator in pagure with the appropriate user logged
  292. in."""
  293. repo = pagure.lib.query._get_project(self.session, "test")
  294. g = munch.Munch()
  295. g.fas_user = tests.FakeUser(username="invalid")
  296. g.authenticated = True
  297. g.session = self.session
  298. with mock.patch("flask.g", g):
  299. output = pagure.utils.is_repo_collaborator(
  300. repo, "refs/heads/master"
  301. )
  302. self.assertFalse(output)
  303. @mock.patch.dict("pagure.config.config", {"PAGURE_ADMIN_USERS": ["foo"]})
  304. def test_is_repo_collaborator_admin_user(self):
  305. """Test is_repo_collaborator in pagure with the appropriate user logged
  306. in."""
  307. repo = pagure.lib.query._get_project(self.session, "test")
  308. g = munch.Munch()
  309. g.fas_user = tests.FakeUser(username="foo")
  310. g.authenticated = True
  311. g.session = self.session
  312. with mock.patch("flask.g", g):
  313. output = pagure.utils.is_repo_collaborator(
  314. repo, "refs/heads/master"
  315. )
  316. self.assertTrue(output)
  317. def test_is_repo_collaborator_not_in_project(self):
  318. """Test is_repo_collaborator in pagure with the appropriate user logged
  319. in."""
  320. repo = pagure.lib.query._get_project(self.session, "test")
  321. g = munch.Munch()
  322. g.fas_user = tests.FakeUser(username="foo")
  323. g.authenticated = True
  324. g.session = self.session
  325. with mock.patch("flask.g", g):
  326. output = pagure.utils.is_repo_collaborator(
  327. repo, "refs/heads/master"
  328. )
  329. self.assertFalse(output)
  330. def test_is_repo_collaborator_in_project(self):
  331. """Test is_repo_collaborator in pagure with the appropriate user logged
  332. in."""
  333. repo = pagure.lib.query._get_project(self.session, "test")
  334. # Add user foo to project test
  335. msg = pagure.lib.query.add_user_to_project(
  336. self.session,
  337. project=repo,
  338. new_user="foo",
  339. user="pingou",
  340. access="collaborator",
  341. branches="epel*",
  342. )
  343. self.session.commit()
  344. g = munch.Munch()
  345. g.fas_user = tests.FakeUser(username="foo")
  346. g.authenticated = True
  347. g.session = self.session
  348. with mock.patch("flask.g", g):
  349. # Collaborator trying to read the project
  350. output = pagure.utils.is_repo_collaborator(repo, None)
  351. self.assertTrue(output)
  352. # Collaborator trying to write to the project
  353. output = pagure.utils.is_repo_collaborator(
  354. repo, "refs/heads/master"
  355. )
  356. self.assertFalse(output)
  357. output = pagure.utils.is_repo_collaborator(repo, "refs/heads/epel")
  358. self.assertTrue(output)
  359. output = pagure.utils.is_repo_collaborator(
  360. repo, "refs/heads/epel8"
  361. )
  362. self.assertTrue(output)
  363. output = pagure.utils.is_repo_collaborator(
  364. repo, "refs/heads/epel8-sig-foobar"
  365. )
  366. self.assertTrue(output)
  367. def test_is_repo_collaborator_logged_in_in_group(self):
  368. """Test is_repo_committer in pagure with the appropriate user logged
  369. in."""
  370. # Create group
  371. msg = pagure.lib.query.add_group(
  372. self.session,
  373. group_name="packager",
  374. display_name="packager",
  375. description="The Fedora packager groups",
  376. group_type="user",
  377. user="pingou",
  378. is_admin=False,
  379. blacklist=[],
  380. )
  381. self.session.commit()
  382. self.assertEqual(msg, "User `pingou` added to the group `packager`.")
  383. # Add user to group
  384. group = pagure.lib.query.search_groups(
  385. self.session, group_name="packager"
  386. )
  387. msg = pagure.lib.query.add_user_to_group(
  388. self.session,
  389. username="foo",
  390. group=group,
  391. user="pingou",
  392. is_admin=True,
  393. )
  394. self.session.commit()
  395. self.assertEqual(msg, "User `foo` added to the group `packager`.")
  396. # Add group packager to project test
  397. project = pagure.lib.query._get_project(self.session, "test")
  398. msg = pagure.lib.query.add_group_to_project(
  399. self.session,
  400. project=project,
  401. new_group="packager",
  402. user="pingou",
  403. access="collaborator",
  404. branches="epel*",
  405. )
  406. self.session.commit()
  407. self.assertEqual(msg, "Group added")
  408. repo = pagure.lib.query._get_project(self.session, "test")
  409. g = munch.Munch()
  410. g.fas_user = tests.FakeUser(username="foo")
  411. g.authenticated = True
  412. g.session = self.session
  413. with mock.patch("flask.g", g):
  414. # Collaborator in the group trying to read the project
  415. output = pagure.utils.is_repo_collaborator(repo, None)
  416. self.assertTrue(output)
  417. # Collaborator in the group trying to write to the project
  418. output = pagure.utils.is_repo_collaborator(
  419. repo, "refs/heads/master"
  420. )
  421. self.assertFalse(output)
  422. output = pagure.utils.is_repo_collaborator(repo, "refs/heads/epel")
  423. self.assertTrue(output)
  424. output = pagure.utils.is_repo_collaborator(
  425. repo, "refs/heads/epel8"
  426. )
  427. self.assertTrue(output)
  428. output = pagure.utils.is_repo_collaborator(
  429. repo, "refs/heads/epel8-sig-foobar"
  430. )
  431. self.assertTrue(output)
  432. if __name__ == "__main__":
  433. unittest.main(verbosity=2)