test_pagure_flask_api_project_contributors.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2020 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import unicode_literals, absolute_import
  8. import arrow
  9. import copy
  10. import datetime
  11. import unittest
  12. import shutil
  13. import sys
  14. import time
  15. import os
  16. import flask
  17. import json
  18. import munch
  19. from mock import patch, MagicMock
  20. from sqlalchemy.exc import SQLAlchemyError
  21. sys.path.insert(
  22. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  23. )
  24. import pagure.lib.query
  25. import tests
  26. class PagureFlaskApiProjectContributorsTests(tests.SimplePagureTest):
  27. """Tests for the flask API of pagure for listing contributors of a project"""
  28. maxDiff = None
  29. @patch("pagure.lib.git.update_git", MagicMock(return_value=True))
  30. @patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
  31. def setUp(self):
  32. """Set up the environnment, ran before every tests."""
  33. super(PagureFlaskApiProjectContributorsTests, self).setUp()
  34. tests.create_projects(self.session)
  35. tests.create_projects_git(os.path.join(self.path, "repos"), bare=True)
  36. def test_private_project(self):
  37. project = pagure.lib.query.get_authorized_project(self.session, "test")
  38. project.private = True
  39. self.session.add(project)
  40. self.session.commit()
  41. output = self.app.get("/api/0/test/contributors")
  42. self.assertEqual(output.status_code, 404)
  43. expected_rv = {
  44. "error": "Project not found",
  45. "error_code": "ENOPROJECT",
  46. }
  47. data = json.loads(output.get_data(as_text=True))
  48. self.assertDictEqual(data, expected_rv)
  49. def test_just_main_admin(self):
  50. output = self.app.get("/api/0/test/contributors")
  51. self.assertEqual(output.status_code, 200)
  52. expected_rv = {
  53. "groups": {
  54. "admin": [],
  55. "collaborators": [],
  56. "commit": [],
  57. "ticket": [],
  58. },
  59. "users": {
  60. "admin": ["pingou"],
  61. "collaborators": [],
  62. "commit": [],
  63. "ticket": [],
  64. },
  65. }
  66. data = json.loads(output.get_data(as_text=True))
  67. self.assertDictEqual(data, expected_rv)
  68. def test_admin_user(self):
  69. project = pagure.lib.query.get_authorized_project(self.session, "test")
  70. msg = pagure.lib.query.add_user_to_project(
  71. session=self.session,
  72. project=project,
  73. new_user="foo",
  74. user="pingou",
  75. access="admin",
  76. branches=None,
  77. required_groups=None,
  78. )
  79. self.session.commit()
  80. self.assertEqual(msg, "User added")
  81. output = self.app.get("/api/0/test/contributors")
  82. self.assertEqual(output.status_code, 200)
  83. expected_rv = {
  84. "groups": {
  85. "admin": [],
  86. "collaborators": [],
  87. "commit": [],
  88. "ticket": [],
  89. },
  90. "users": {
  91. "admin": ["foo", "pingou"],
  92. "collaborators": [],
  93. "commit": [],
  94. "ticket": [],
  95. },
  96. }
  97. data = json.loads(output.get_data(as_text=True))
  98. self.assertDictEqual(data, expected_rv)
  99. def test_admin_user_and_commit(self):
  100. tests.create_user(self.session, "baz", "foo baz", ["baz@bar.com"])
  101. project = pagure.lib.query.get_authorized_project(self.session, "test")
  102. msg = pagure.lib.query.add_user_to_project(
  103. session=self.session,
  104. project=project,
  105. new_user="foo",
  106. user="pingou",
  107. access="admin",
  108. branches=None,
  109. required_groups=None,
  110. )
  111. self.session.commit()
  112. self.assertEqual(msg, "User added")
  113. msg = pagure.lib.query.add_user_to_project(
  114. session=self.session,
  115. project=project,
  116. new_user="baz",
  117. user="pingou",
  118. access="commit",
  119. branches=None,
  120. required_groups=None,
  121. )
  122. self.session.commit()
  123. self.assertEqual(msg, "User added")
  124. output = self.app.get("/api/0/test/contributors")
  125. self.assertEqual(output.status_code, 200)
  126. expected_rv = {
  127. "groups": {
  128. "admin": [],
  129. "collaborators": [],
  130. "commit": [],
  131. "ticket": [],
  132. },
  133. "users": {
  134. "admin": ["foo", "pingou"],
  135. "collaborators": [],
  136. "commit": ["baz"],
  137. "ticket": [],
  138. },
  139. }
  140. data = json.loads(output.get_data(as_text=True))
  141. self.assertDictEqual(data, expected_rv)
  142. def test_admin_user_and_commit_and_ticket(self):
  143. tests.create_user(self.session, "baz", "foo baz", ["baz@bar.com"])
  144. tests.create_user(
  145. self.session, "alex", "Alex Ander", ["alex@ander.com"]
  146. )
  147. project = pagure.lib.query.get_authorized_project(self.session, "test")
  148. msg = pagure.lib.query.add_user_to_project(
  149. session=self.session,
  150. project=project,
  151. new_user="foo",
  152. user="pingou",
  153. access="admin",
  154. branches=None,
  155. required_groups=None,
  156. )
  157. self.session.commit()
  158. self.assertEqual(msg, "User added")
  159. msg = pagure.lib.query.add_user_to_project(
  160. session=self.session,
  161. project=project,
  162. new_user="baz",
  163. user="pingou",
  164. access="commit",
  165. branches=None,
  166. required_groups=None,
  167. )
  168. self.session.commit()
  169. self.assertEqual(msg, "User added")
  170. msg = pagure.lib.query.add_user_to_project(
  171. session=self.session,
  172. project=project,
  173. new_user="alex",
  174. user="pingou",
  175. access="ticket",
  176. branches=None,
  177. required_groups=None,
  178. )
  179. self.session.commit()
  180. self.assertEqual(msg, "User added")
  181. output = self.app.get("/api/0/test/contributors")
  182. self.assertEqual(output.status_code, 200)
  183. expected_rv = {
  184. "groups": {
  185. "admin": [],
  186. "collaborators": [],
  187. "commit": [],
  188. "ticket": [],
  189. },
  190. "users": {
  191. "admin": ["foo", "pingou"],
  192. "collaborators": [],
  193. "commit": ["baz"],
  194. "ticket": ["alex"],
  195. },
  196. }
  197. data = json.loads(output.get_data(as_text=True))
  198. self.assertDictEqual(data, expected_rv)
  199. def test_admin_user_and_commit_and_ticket_and_contributors(self):
  200. tests.create_user(self.session, "baz", "foo baz", ["baz@bar.com"])
  201. tests.create_user(
  202. self.session, "alex", "Alex Ander", ["alex@ander.com"]
  203. )
  204. tests.create_user(self.session, "ralph", "Ralph B.", ["ralph@b.com"])
  205. tests.create_user(self.session, "kevin", "Kevin F.", ["kevin@f.com"])
  206. project = pagure.lib.query.get_authorized_project(self.session, "test")
  207. msg = pagure.lib.query.add_user_to_project(
  208. session=self.session,
  209. project=project,
  210. new_user="foo",
  211. user="pingou",
  212. access="admin",
  213. branches=None,
  214. required_groups=None,
  215. )
  216. self.session.commit()
  217. self.assertEqual(msg, "User added")
  218. msg = pagure.lib.query.add_user_to_project(
  219. session=self.session,
  220. project=project,
  221. new_user="baz",
  222. user="pingou",
  223. access="commit",
  224. branches=None,
  225. required_groups=None,
  226. )
  227. self.session.commit()
  228. self.assertEqual(msg, "User added")
  229. msg = pagure.lib.query.add_user_to_project(
  230. session=self.session,
  231. project=project,
  232. new_user="alex",
  233. user="pingou",
  234. access="ticket",
  235. branches=None,
  236. required_groups=None,
  237. )
  238. self.session.commit()
  239. self.assertEqual(msg, "User added")
  240. msg = pagure.lib.query.add_user_to_project(
  241. session=self.session,
  242. project=project,
  243. new_user="ralph",
  244. user="pingou",
  245. access="collaborator",
  246. branches="epel*",
  247. required_groups=None,
  248. )
  249. self.session.commit()
  250. self.assertEqual(msg, "User added")
  251. msg = pagure.lib.query.add_user_to_project(
  252. session=self.session,
  253. project=project,
  254. new_user="kevin",
  255. user="pingou",
  256. access="collaborator",
  257. branches="f*",
  258. required_groups=None,
  259. )
  260. self.session.commit()
  261. self.assertEqual(msg, "User added")
  262. output = self.app.get("/api/0/test/contributors")
  263. self.assertEqual(output.status_code, 200)
  264. expected_rv = {
  265. "groups": {
  266. "admin": [],
  267. "collaborators": [],
  268. "commit": [],
  269. "ticket": [],
  270. },
  271. "users": {
  272. "admin": ["foo", "pingou"],
  273. "collaborators": [
  274. {"branches": "f*", "user": "kevin"},
  275. {"branches": "epel*", "user": "ralph"},
  276. ],
  277. "commit": ["baz"],
  278. "ticket": ["alex"],
  279. },
  280. }
  281. data = json.loads(output.get_data(as_text=True))
  282. self.assertDictEqual(data, expected_rv)
  283. def test_admin_group(self):
  284. project = pagure.lib.query.get_authorized_project(self.session, "test")
  285. msg = pagure.lib.query.add_group_to_project(
  286. session=self.session,
  287. project=project,
  288. new_group="admin_groups",
  289. user="pingou",
  290. access="admin",
  291. branches=None,
  292. create=True,
  293. is_admin=False,
  294. )
  295. self.session.commit()
  296. self.assertEqual(msg, "Group added")
  297. output = self.app.get("/api/0/test/contributors")
  298. self.assertEqual(output.status_code, 200)
  299. expected_rv = {
  300. "groups": {
  301. "admin": ["admin_groups"],
  302. "collaborators": [],
  303. "commit": [],
  304. "ticket": [],
  305. },
  306. "users": {
  307. "admin": ["pingou"],
  308. "collaborators": [],
  309. "commit": [],
  310. "ticket": [],
  311. },
  312. }
  313. data = json.loads(output.get_data(as_text=True))
  314. self.assertDictEqual(data, expected_rv)
  315. def test_admin_and_commit_groups(self):
  316. project = pagure.lib.query.get_authorized_project(self.session, "test")
  317. msg = pagure.lib.query.add_group_to_project(
  318. session=self.session,
  319. project=project,
  320. new_group="admin_groups",
  321. user="pingou",
  322. access="admin",
  323. branches=None,
  324. create=True,
  325. is_admin=False,
  326. )
  327. self.session.commit()
  328. self.assertEqual(msg, "Group added")
  329. msg = pagure.lib.query.add_group_to_project(
  330. session=self.session,
  331. project=project,
  332. new_group="commit_group",
  333. user="pingou",
  334. access="commit",
  335. branches=None,
  336. create=True,
  337. is_admin=False,
  338. )
  339. self.session.commit()
  340. self.assertEqual(msg, "Group added")
  341. output = self.app.get("/api/0/test/contributors")
  342. self.assertEqual(output.status_code, 200)
  343. expected_rv = {
  344. "groups": {
  345. "admin": ["admin_groups"],
  346. "collaborators": [],
  347. "commit": ["commit_group"],
  348. "ticket": [],
  349. },
  350. "users": {
  351. "admin": ["pingou"],
  352. "collaborators": [],
  353. "commit": [],
  354. "ticket": [],
  355. },
  356. }
  357. data = json.loads(output.get_data(as_text=True))
  358. self.assertDictEqual(data, expected_rv)
  359. def test_admin_and_commit_and_ticket_groups(self):
  360. project = pagure.lib.query.get_authorized_project(self.session, "test")
  361. msg = pagure.lib.query.add_group_to_project(
  362. session=self.session,
  363. project=project,
  364. new_group="admin_groups",
  365. user="pingou",
  366. access="admin",
  367. branches=None,
  368. create=True,
  369. is_admin=False,
  370. )
  371. self.session.commit()
  372. self.assertEqual(msg, "Group added")
  373. msg = pagure.lib.query.add_group_to_project(
  374. session=self.session,
  375. project=project,
  376. new_group="commit_group",
  377. user="pingou",
  378. access="commit",
  379. branches=None,
  380. create=True,
  381. is_admin=False,
  382. )
  383. self.session.commit()
  384. self.assertEqual(msg, "Group added")
  385. msg = pagure.lib.query.add_group_to_project(
  386. session=self.session,
  387. project=project,
  388. new_group="ticket_group",
  389. user="pingou",
  390. access="ticket",
  391. branches=None,
  392. create=True,
  393. is_admin=False,
  394. )
  395. self.session.commit()
  396. self.assertEqual(msg, "Group added")
  397. output = self.app.get("/api/0/test/contributors")
  398. self.assertEqual(output.status_code, 200)
  399. expected_rv = {
  400. "groups": {
  401. "admin": ["admin_groups"],
  402. "collaborators": [],
  403. "commit": ["commit_group"],
  404. "ticket": ["ticket_group"],
  405. },
  406. "users": {
  407. "admin": ["pingou"],
  408. "collaborators": [],
  409. "commit": [],
  410. "ticket": [],
  411. },
  412. }
  413. data = json.loads(output.get_data(as_text=True))
  414. self.assertDictEqual(data, expected_rv)
  415. def test_admin_and_commit_and_ticket_and_collaborators_groups(self):
  416. project = pagure.lib.query.get_authorized_project(self.session, "test")
  417. msg = pagure.lib.query.add_group_to_project(
  418. session=self.session,
  419. project=project,
  420. new_group="admin_groups",
  421. user="pingou",
  422. access="admin",
  423. branches=None,
  424. create=True,
  425. is_admin=False,
  426. )
  427. self.session.commit()
  428. self.assertEqual(msg, "Group added")
  429. msg = pagure.lib.query.add_group_to_project(
  430. session=self.session,
  431. project=project,
  432. new_group="commit_group",
  433. user="pingou",
  434. access="commit",
  435. branches=None,
  436. create=True,
  437. is_admin=False,
  438. )
  439. self.session.commit()
  440. self.assertEqual(msg, "Group added")
  441. msg = pagure.lib.query.add_group_to_project(
  442. session=self.session,
  443. project=project,
  444. new_group="ticket_group",
  445. user="pingou",
  446. access="ticket",
  447. branches=None,
  448. create=True,
  449. is_admin=False,
  450. )
  451. self.session.commit()
  452. self.assertEqual(msg, "Group added")
  453. msg = pagure.lib.query.add_group_to_project(
  454. session=self.session,
  455. project=project,
  456. new_group="epel_group",
  457. user="pingou",
  458. access="collaborator",
  459. branches="epel*",
  460. create=True,
  461. is_admin=False,
  462. )
  463. self.session.commit()
  464. self.assertEqual(msg, "Group added")
  465. msg = pagure.lib.query.add_group_to_project(
  466. session=self.session,
  467. project=project,
  468. new_group="fedora_group",
  469. user="pingou",
  470. access="collaborator",
  471. branches="f*",
  472. create=True,
  473. is_admin=False,
  474. )
  475. self.session.commit()
  476. self.assertEqual(msg, "Group added")
  477. output = self.app.get("/api/0/test/contributors")
  478. self.assertEqual(output.status_code, 200)
  479. expected_rv = {
  480. "groups": {
  481. "admin": ["admin_groups"],
  482. "collaborators": [
  483. {"branches": "epel*", "user": "epel_group"},
  484. {"branches": "f*", "user": "fedora_group"},
  485. ],
  486. "commit": ["commit_group"],
  487. "ticket": ["ticket_group"],
  488. },
  489. "users": {
  490. "admin": ["pingou"],
  491. "collaborators": [],
  492. "commit": [],
  493. "ticket": [],
  494. },
  495. }
  496. data = json.loads(output.get_data(as_text=True))
  497. self.assertDictEqual(data, expected_rv)
  498. def test_full(self):
  499. tests.create_user(self.session, "baz", "foo baz", ["baz@bar.com"])
  500. tests.create_user(
  501. self.session, "alex", "Alex Ander", ["alex@ander.com"]
  502. )
  503. tests.create_user(self.session, "ralph", "Ralph B.", ["ralph@b.com"])
  504. tests.create_user(self.session, "kevin", "Kevin F.", ["kevin@f.com"])
  505. project = pagure.lib.query.get_authorized_project(self.session, "test")
  506. # Add users for all kinds of access
  507. msg = pagure.lib.query.add_user_to_project(
  508. session=self.session,
  509. project=project,
  510. new_user="foo",
  511. user="pingou",
  512. access="admin",
  513. branches=None,
  514. required_groups=None,
  515. )
  516. self.session.commit()
  517. self.assertEqual(msg, "User added")
  518. msg = pagure.lib.query.add_user_to_project(
  519. session=self.session,
  520. project=project,
  521. new_user="baz",
  522. user="pingou",
  523. access="commit",
  524. branches=None,
  525. required_groups=None,
  526. )
  527. self.session.commit()
  528. self.assertEqual(msg, "User added")
  529. msg = pagure.lib.query.add_user_to_project(
  530. session=self.session,
  531. project=project,
  532. new_user="alex",
  533. user="pingou",
  534. access="ticket",
  535. branches=None,
  536. required_groups=None,
  537. )
  538. self.session.commit()
  539. self.assertEqual(msg, "User added")
  540. msg = pagure.lib.query.add_user_to_project(
  541. session=self.session,
  542. project=project,
  543. new_user="ralph",
  544. user="pingou",
  545. access="collaborator",
  546. branches="epel*",
  547. required_groups=None,
  548. )
  549. self.session.commit()
  550. self.assertEqual(msg, "User added")
  551. msg = pagure.lib.query.add_user_to_project(
  552. session=self.session,
  553. project=project,
  554. new_user="kevin",
  555. user="pingou",
  556. access="collaborator",
  557. branches="f*",
  558. required_groups=None,
  559. )
  560. self.session.commit()
  561. self.assertEqual(msg, "User added")
  562. # Create groups for all kinds of access
  563. msg = pagure.lib.query.add_group_to_project(
  564. session=self.session,
  565. project=project,
  566. new_group="admin_groups",
  567. user="pingou",
  568. access="admin",
  569. branches=None,
  570. create=True,
  571. is_admin=False,
  572. )
  573. self.session.commit()
  574. self.assertEqual(msg, "Group added")
  575. msg = pagure.lib.query.add_group_to_project(
  576. session=self.session,
  577. project=project,
  578. new_group="commit_group",
  579. user="pingou",
  580. access="commit",
  581. branches=None,
  582. create=True,
  583. is_admin=False,
  584. )
  585. self.session.commit()
  586. self.assertEqual(msg, "Group added")
  587. msg = pagure.lib.query.add_group_to_project(
  588. session=self.session,
  589. project=project,
  590. new_group="ticket_group",
  591. user="pingou",
  592. access="ticket",
  593. branches=None,
  594. create=True,
  595. is_admin=False,
  596. )
  597. self.session.commit()
  598. self.assertEqual(msg, "Group added")
  599. msg = pagure.lib.query.add_group_to_project(
  600. session=self.session,
  601. project=project,
  602. new_group="epel_group",
  603. user="pingou",
  604. access="collaborator",
  605. branches="epel*",
  606. create=True,
  607. is_admin=False,
  608. )
  609. self.session.commit()
  610. self.assertEqual(msg, "Group added")
  611. msg = pagure.lib.query.add_group_to_project(
  612. session=self.session,
  613. project=project,
  614. new_group="fedora_group",
  615. user="pingou",
  616. access="collaborator",
  617. branches="f*",
  618. create=True,
  619. is_admin=False,
  620. )
  621. self.session.commit()
  622. self.assertEqual(msg, "Group added")
  623. output = self.app.get("/api/0/test/contributors")
  624. self.assertEqual(output.status_code, 200)
  625. expected_rv = {
  626. "groups": {
  627. "admin": ["admin_groups"],
  628. "collaborators": [
  629. {"branches": "epel*", "user": "epel_group"},
  630. {"branches": "f*", "user": "fedora_group"},
  631. ],
  632. "commit": ["commit_group"],
  633. "ticket": ["ticket_group"],
  634. },
  635. "users": {
  636. "admin": ["foo", "pingou"],
  637. "collaborators": [
  638. {"branches": "f*", "user": "kevin"},
  639. {"branches": "epel*", "user": "ralph"},
  640. ],
  641. "commit": ["baz"],
  642. "ticket": ["alex"],
  643. },
  644. }
  645. data = json.loads(output.get_data(as_text=True))
  646. self.assertDictEqual(data, expected_rv)
  647. if __name__ == "__main__":
  648. unittest.main(verbosity=2)