test_pagure_flask_api_project_hascommit.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2021 - 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 ANY, 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 PagureFlaskApiProjectHascommitTests(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(PagureFlaskApiProjectHascommitTests, 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/hascommit?user=pingou")
  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_missing_branch(self):
  50. output = self.app.get("/api/0/test/hascommit?user=pingou")
  51. self.assertEqual(output.status_code, 400)
  52. expected_rv = {
  53. "error": "Invalid or incomplete input submitted",
  54. "error_code": "EINVALIDREQ",
  55. }
  56. data = json.loads(output.get_data(as_text=True))
  57. self.assertDictEqual(data, expected_rv)
  58. def test_missing_user(self):
  59. output = self.app.get("/api/0/test/hascommit?branch=user")
  60. self.assertEqual(output.status_code, 400)
  61. expected_rv = {
  62. "error": "Invalid or incomplete input submitted",
  63. "error_code": "EINVALIDREQ",
  64. }
  65. data = json.loads(output.get_data(as_text=True))
  66. self.assertDictEqual(data, expected_rv)
  67. def test_just_main_admin(self):
  68. output = self.app.get("/api/0/test/hascommit?user=pingou&branch=main")
  69. self.assertEqual(output.status_code, 200)
  70. expected_rv = {
  71. "args": {
  72. "branch": "main",
  73. "project": {
  74. "access_groups": {
  75. "admin": [],
  76. "collaborator": [],
  77. "commit": [],
  78. "ticket": [],
  79. },
  80. "access_users": {
  81. "admin": [],
  82. "collaborator": [],
  83. "commit": [],
  84. "owner": ["pingou"],
  85. "ticket": [],
  86. },
  87. "close_status": [
  88. "Invalid",
  89. "Insufficient data",
  90. "Fixed",
  91. "Duplicate",
  92. ],
  93. "custom_keys": [],
  94. "date_created": ANY,
  95. "date_modified": ANY,
  96. "description": "test project #1",
  97. "full_url": "http://localhost.localdomain/test",
  98. "fullname": "test",
  99. "id": 1,
  100. "milestones": {},
  101. "name": "test",
  102. "namespace": None,
  103. "parent": None,
  104. "priorities": {},
  105. "tags": [],
  106. "url_path": "test",
  107. "user": {
  108. "full_url": "http://localhost.localdomain/user/pingou",
  109. "fullname": "PY C",
  110. "name": "pingou",
  111. "url_path": "user/pingou",
  112. },
  113. },
  114. "user": "pingou",
  115. },
  116. "hascommit": True,
  117. }
  118. data = json.loads(output.get_data(as_text=True))
  119. self.assertDictEqual(data, expected_rv)
  120. def test_admin_user(self):
  121. project = pagure.lib.query.get_authorized_project(self.session, "test")
  122. msg = pagure.lib.query.add_user_to_project(
  123. session=self.session,
  124. project=project,
  125. new_user="foo",
  126. user="pingou",
  127. access="admin",
  128. branches=None,
  129. required_groups=None,
  130. )
  131. self.session.commit()
  132. self.assertEqual(msg, "User added")
  133. output = self.app.get("/api/0/test/hascommit?user=foo&branch=main")
  134. self.assertEqual(output.status_code, 200)
  135. expected_rv = {
  136. "args": {
  137. "branch": "main",
  138. "project": {
  139. "access_groups": {
  140. "admin": [],
  141. "collaborator": [],
  142. "commit": [],
  143. "ticket": [],
  144. },
  145. "access_users": {
  146. "admin": ["foo"],
  147. "collaborator": [],
  148. "commit": [],
  149. "owner": ["pingou"],
  150. "ticket": [],
  151. },
  152. "close_status": [
  153. "Invalid",
  154. "Insufficient data",
  155. "Fixed",
  156. "Duplicate",
  157. ],
  158. "custom_keys": [],
  159. "date_created": ANY,
  160. "date_modified": ANY,
  161. "description": "test project #1",
  162. "full_url": "http://localhost.localdomain/test",
  163. "fullname": "test",
  164. "id": 1,
  165. "milestones": {},
  166. "name": "test",
  167. "namespace": None,
  168. "parent": None,
  169. "priorities": {},
  170. "tags": [],
  171. "url_path": "test",
  172. "user": {
  173. "full_url": "http://localhost.localdomain/user/pingou",
  174. "fullname": "PY C",
  175. "name": "pingou",
  176. "url_path": "user/pingou",
  177. },
  178. },
  179. "user": "foo",
  180. },
  181. "hascommit": True,
  182. }
  183. data = json.loads(output.get_data(as_text=True))
  184. self.assertDictEqual(data, expected_rv)
  185. def test_admin_user_and_commit(self):
  186. tests.create_user(self.session, "baz", "foo baz", ["baz@bar.com"])
  187. project = pagure.lib.query.get_authorized_project(self.session, "test")
  188. msg = pagure.lib.query.add_user_to_project(
  189. session=self.session,
  190. project=project,
  191. new_user="foo",
  192. user="pingou",
  193. access="admin",
  194. branches=None,
  195. required_groups=None,
  196. )
  197. self.session.commit()
  198. self.assertEqual(msg, "User added")
  199. msg = pagure.lib.query.add_user_to_project(
  200. session=self.session,
  201. project=project,
  202. new_user="baz",
  203. user="pingou",
  204. access="commit",
  205. branches=None,
  206. required_groups=None,
  207. )
  208. self.session.commit()
  209. self.assertEqual(msg, "User added")
  210. output = self.app.get("/api/0/test/hascommit?user=baz&branch=main")
  211. self.assertEqual(output.status_code, 200)
  212. expected_rv = {
  213. "args": {
  214. "branch": "main",
  215. "project": {
  216. "access_groups": {
  217. "admin": [],
  218. "collaborator": [],
  219. "commit": [],
  220. "ticket": [],
  221. },
  222. "access_users": {
  223. "admin": ["foo"],
  224. "collaborator": [],
  225. "commit": ["baz"],
  226. "owner": ["pingou"],
  227. "ticket": [],
  228. },
  229. "close_status": [
  230. "Invalid",
  231. "Insufficient data",
  232. "Fixed",
  233. "Duplicate",
  234. ],
  235. "custom_keys": [],
  236. "date_created": ANY,
  237. "date_modified": ANY,
  238. "description": "test project #1",
  239. "full_url": "http://localhost.localdomain/test",
  240. "fullname": "test",
  241. "id": 1,
  242. "milestones": {},
  243. "name": "test",
  244. "namespace": None,
  245. "parent": None,
  246. "priorities": {},
  247. "tags": [],
  248. "url_path": "test",
  249. "user": {
  250. "full_url": "http://localhost.localdomain/user/pingou",
  251. "fullname": "PY C",
  252. "name": "pingou",
  253. "url_path": "user/pingou",
  254. },
  255. },
  256. "user": "baz",
  257. },
  258. "hascommit": True,
  259. }
  260. data = json.loads(output.get_data(as_text=True))
  261. self.assertDictEqual(data, expected_rv)
  262. def test_admin_user_and_commit_and_ticket(self):
  263. tests.create_user(self.session, "baz", "foo baz", ["baz@bar.com"])
  264. tests.create_user(
  265. self.session, "alex", "Alex Ander", ["alex@ander.com"]
  266. )
  267. project = pagure.lib.query.get_authorized_project(self.session, "test")
  268. msg = pagure.lib.query.add_user_to_project(
  269. session=self.session,
  270. project=project,
  271. new_user="foo",
  272. user="pingou",
  273. access="admin",
  274. branches=None,
  275. required_groups=None,
  276. )
  277. self.session.commit()
  278. self.assertEqual(msg, "User added")
  279. msg = pagure.lib.query.add_user_to_project(
  280. session=self.session,
  281. project=project,
  282. new_user="baz",
  283. user="pingou",
  284. access="commit",
  285. branches=None,
  286. required_groups=None,
  287. )
  288. self.session.commit()
  289. self.assertEqual(msg, "User added")
  290. msg = pagure.lib.query.add_user_to_project(
  291. session=self.session,
  292. project=project,
  293. new_user="alex",
  294. user="pingou",
  295. access="ticket",
  296. branches=None,
  297. required_groups=None,
  298. )
  299. self.session.commit()
  300. self.assertEqual(msg, "User added")
  301. output = self.app.get("/api/0/test/hascommit?user=alex&branch=main")
  302. self.assertEqual(output.status_code, 200)
  303. expected_rv = {
  304. "args": {
  305. "branch": "main",
  306. "project": {
  307. "access_groups": {
  308. "admin": [],
  309. "collaborator": [],
  310. "commit": [],
  311. "ticket": [],
  312. },
  313. "access_users": {
  314. "admin": ["foo"],
  315. "collaborator": [],
  316. "commit": ["baz"],
  317. "owner": ["pingou"],
  318. "ticket": ["alex"],
  319. },
  320. "close_status": [
  321. "Invalid",
  322. "Insufficient data",
  323. "Fixed",
  324. "Duplicate",
  325. ],
  326. "custom_keys": [],
  327. "date_created": ANY,
  328. "date_modified": ANY,
  329. "description": "test project #1",
  330. "full_url": "http://localhost.localdomain/test",
  331. "fullname": "test",
  332. "id": 1,
  333. "milestones": {},
  334. "name": "test",
  335. "namespace": None,
  336. "parent": None,
  337. "priorities": {},
  338. "tags": [],
  339. "url_path": "test",
  340. "user": {
  341. "full_url": "http://localhost.localdomain/user/pingou",
  342. "fullname": "PY C",
  343. "name": "pingou",
  344. "url_path": "user/pingou",
  345. },
  346. },
  347. "user": "alex",
  348. },
  349. "hascommit": False,
  350. }
  351. data = json.loads(output.get_data(as_text=True))
  352. self.assertDictEqual(data, expected_rv)
  353. def test_admin_user_and_commit_and_ticket_and_contributors(self):
  354. tests.create_user(self.session, "baz", "foo baz", ["baz@bar.com"])
  355. tests.create_user(
  356. self.session, "alex", "Alex Ander", ["alex@ander.com"]
  357. )
  358. tests.create_user(self.session, "ralph", "Ralph B.", ["ralph@b.com"])
  359. tests.create_user(self.session, "kevin", "Kevin F.", ["kevin@f.com"])
  360. project = pagure.lib.query.get_authorized_project(self.session, "test")
  361. msg = pagure.lib.query.add_user_to_project(
  362. session=self.session,
  363. project=project,
  364. new_user="foo",
  365. user="pingou",
  366. access="admin",
  367. branches=None,
  368. required_groups=None,
  369. )
  370. self.session.commit()
  371. self.assertEqual(msg, "User added")
  372. msg = pagure.lib.query.add_user_to_project(
  373. session=self.session,
  374. project=project,
  375. new_user="baz",
  376. user="pingou",
  377. access="commit",
  378. branches=None,
  379. required_groups=None,
  380. )
  381. self.session.commit()
  382. self.assertEqual(msg, "User added")
  383. msg = pagure.lib.query.add_user_to_project(
  384. session=self.session,
  385. project=project,
  386. new_user="alex",
  387. user="pingou",
  388. access="ticket",
  389. branches=None,
  390. required_groups=None,
  391. )
  392. self.session.commit()
  393. self.assertEqual(msg, "User added")
  394. msg = pagure.lib.query.add_user_to_project(
  395. session=self.session,
  396. project=project,
  397. new_user="ralph",
  398. user="pingou",
  399. access="collaborator",
  400. branches="epel*",
  401. required_groups=None,
  402. )
  403. self.session.commit()
  404. self.assertEqual(msg, "User added")
  405. msg = pagure.lib.query.add_user_to_project(
  406. session=self.session,
  407. project=project,
  408. new_user="kevin",
  409. user="pingou",
  410. access="collaborator",
  411. branches="f*",
  412. required_groups=None,
  413. )
  414. self.session.commit()
  415. self.assertEqual(msg, "User added")
  416. output = self.app.get("/api/0/test/hascommit?user=kevin&branch=main")
  417. self.assertEqual(output.status_code, 200)
  418. expected_rv = {
  419. "args": {
  420. "branch": "main",
  421. "project": {
  422. "access_groups": {
  423. "admin": [],
  424. "collaborator": [],
  425. "commit": [],
  426. "ticket": [],
  427. },
  428. "access_users": {
  429. "admin": ["foo"],
  430. "collaborator": ["kevin", "ralph"],
  431. "commit": ["baz"],
  432. "owner": ["pingou"],
  433. "ticket": ["alex"],
  434. },
  435. "close_status": [
  436. "Invalid",
  437. "Insufficient data",
  438. "Fixed",
  439. "Duplicate",
  440. ],
  441. "custom_keys": [],
  442. "date_created": ANY,
  443. "date_modified": ANY,
  444. "description": "test project #1",
  445. "full_url": "http://localhost.localdomain/test",
  446. "fullname": "test",
  447. "id": 1,
  448. "milestones": {},
  449. "name": "test",
  450. "namespace": None,
  451. "parent": None,
  452. "priorities": {},
  453. "tags": [],
  454. "url_path": "test",
  455. "user": {
  456. "full_url": "http://localhost.localdomain/user/pingou",
  457. "fullname": "PY C",
  458. "name": "pingou",
  459. "url_path": "user/pingou",
  460. },
  461. },
  462. "user": "kevin",
  463. },
  464. "hascommit": False,
  465. }
  466. data = json.loads(output.get_data(as_text=True))
  467. self.assertDictEqual(data, expected_rv)
  468. output = self.app.get("/api/0/test/hascommit?user=kevin&branch=f33")
  469. self.assertEqual(output.status_code, 200)
  470. expected_rv = {
  471. "args": {
  472. "branch": "f33",
  473. "project": {
  474. "access_groups": {
  475. "admin": [],
  476. "collaborator": [],
  477. "commit": [],
  478. "ticket": [],
  479. },
  480. "access_users": {
  481. "admin": ["foo"],
  482. "collaborator": ["kevin", "ralph"],
  483. "commit": ["baz"],
  484. "owner": ["pingou"],
  485. "ticket": ["alex"],
  486. },
  487. "close_status": [
  488. "Invalid",
  489. "Insufficient data",
  490. "Fixed",
  491. "Duplicate",
  492. ],
  493. "custom_keys": [],
  494. "date_created": ANY,
  495. "date_modified": ANY,
  496. "description": "test project #1",
  497. "full_url": "http://localhost.localdomain/test",
  498. "fullname": "test",
  499. "id": 1,
  500. "milestones": {},
  501. "name": "test",
  502. "namespace": None,
  503. "parent": None,
  504. "priorities": {},
  505. "tags": [],
  506. "url_path": "test",
  507. "user": {
  508. "full_url": "http://localhost.localdomain/user/pingou",
  509. "fullname": "PY C",
  510. "name": "pingou",
  511. "url_path": "user/pingou",
  512. },
  513. },
  514. "user": "kevin",
  515. },
  516. "hascommit": True,
  517. }
  518. data = json.loads(output.get_data(as_text=True))
  519. self.assertDictEqual(data, expected_rv)