test_pagure_flask_api_group.py 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2017-2018 - Copyright Red Hat Inc
  4. Authors:
  5. Matt Prahl <mprahl@redhat.com>
  6. Pierre-Yves Chibon <pingou@pingoured.fr>
  7. """
  8. from __future__ import unicode_literals, absolute_import
  9. import unittest
  10. import sys
  11. import os
  12. import json
  13. from unittest.mock import patch
  14. from sqlalchemy.exc import SQLAlchemyError
  15. sys.path.insert(
  16. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  17. )
  18. import pagure.api
  19. import pagure.lib.query
  20. from pagure.exceptions import PagureException
  21. import tests
  22. class PagureFlaskApiGroupTests(tests.SimplePagureTest):
  23. """Tests for the flask API of pagure for issue"""
  24. maxDiff = None
  25. def setUp(self):
  26. """Set up the environnment, ran before every tests."""
  27. super(PagureFlaskApiGroupTests, self).setUp()
  28. pagure.config.config["REQUESTS_FOLDER"] = None
  29. msg = pagure.lib.query.add_group(
  30. self.session,
  31. group_name="some_group",
  32. display_name="Some Group",
  33. description=None,
  34. group_type="bar",
  35. user="pingou",
  36. is_admin=False,
  37. blacklist=[],
  38. )
  39. self.session.commit()
  40. tests.create_projects(self.session)
  41. project = pagure.lib.query._get_project(self.session, "test2")
  42. msg = pagure.lib.query.add_group_to_project(
  43. session=self.session,
  44. project=project,
  45. new_group="some_group",
  46. user="pingou",
  47. )
  48. self.session.commit()
  49. self.assertEqual(msg, "Group added")
  50. def test_api_groups(self):
  51. """Test the api_groups function."""
  52. # Add a couple of groups so that we can list them
  53. item = pagure.lib.model.PagureGroup(
  54. group_name="group1",
  55. group_type="user",
  56. display_name="User group",
  57. user_id=1, # pingou
  58. )
  59. self.session.add(item)
  60. item = pagure.lib.model.PagureGroup(
  61. group_name="rel-eng",
  62. group_type="user",
  63. display_name="Release engineering group",
  64. user_id=1, # pingou
  65. )
  66. self.session.add(item)
  67. self.session.commit()
  68. output = self.app.get("/api/0/groups")
  69. self.assertEqual(output.status_code, 200)
  70. data = json.loads(output.get_data(as_text=True))
  71. self.assertEqual(data["groups"], ["group1", "rel-eng", "some_group"])
  72. self.assertEqual(
  73. sorted(data.keys()), ["groups", "pagination", "total_groups"]
  74. )
  75. self.assertEqual(data["total_groups"], 3)
  76. output = self.app.get("/api/0/groups?pattern=re")
  77. self.assertEqual(output.status_code, 200)
  78. data = json.loads(output.get_data(as_text=True))
  79. self.assertEqual(data["groups"], ["rel-eng"])
  80. self.assertEqual(
  81. sorted(data.keys()), ["groups", "pagination", "total_groups"]
  82. )
  83. self.assertEqual(data["total_groups"], 1)
  84. def test_api_groups_extended(self):
  85. """Test the api_groups function."""
  86. # Add a couple of groups so that we can list them
  87. item = pagure.lib.model.PagureGroup(
  88. group_name="group1",
  89. group_type="user",
  90. display_name="User group",
  91. user_id=1, # pingou
  92. )
  93. self.session.add(item)
  94. item = pagure.lib.model.PagureGroup(
  95. group_name="rel-eng",
  96. group_type="user",
  97. display_name="Release engineering group",
  98. user_id=1, # pingou
  99. )
  100. self.session.add(item)
  101. self.session.commit()
  102. output = self.app.get("/api/0/groups?extended=1")
  103. self.assertEqual(output.status_code, 200)
  104. data = json.loads(output.get_data(as_text=True))
  105. for k in ["first", "last"]:
  106. self.assertIsNotNone(data["pagination"][k])
  107. data["pagination"][k] = "http://localhost..."
  108. self.assertEqual(
  109. data,
  110. {
  111. "groups": [
  112. {"description": None, "name": "group1"},
  113. {"description": None, "name": "rel-eng"},
  114. {"description": None, "name": "some_group"},
  115. ],
  116. "pagination": {
  117. "first": "http://localhost...",
  118. "last": "http://localhost...",
  119. "next": None,
  120. "page": 1,
  121. "pages": 1,
  122. "per_page": 20,
  123. "prev": None,
  124. },
  125. "total_groups": 3,
  126. },
  127. )
  128. def test_api_view_group_authenticated(self):
  129. """
  130. Test the api_view_group method of the flask api with an
  131. authenticated user. The tested group has one member.
  132. """
  133. tests.create_tokens(self.session)
  134. headers = {"Authorization": "token aaabbbcccddd"}
  135. output = self.app.get("/api/0/group/some_group", headers=headers)
  136. self.assertEqual(output.status_code, 200)
  137. exp = {
  138. "display_name": "Some Group",
  139. "full_url": "http://localhost.localdomain/group/some_group",
  140. "description": None,
  141. "creator": {
  142. "fullname": "PY C",
  143. "full_url": "http://localhost.localdomain/user/pingou",
  144. "url_path": "user/pingou",
  145. "default_email": "bar@pingou.com",
  146. "emails": ["bar@pingou.com", "foo@pingou.com"],
  147. "name": "pingou",
  148. },
  149. "members": ["pingou"],
  150. "date_created": "1492020239",
  151. "group_type": "user",
  152. "name": "some_group",
  153. }
  154. data = json.loads(output.get_data(as_text=True))
  155. data["date_created"] = "1492020239"
  156. self.assertDictEqual(data, exp)
  157. def test_api_view_group_unauthenticated(self):
  158. """
  159. Test the api_view_group method of the flask api with an
  160. unauthenticated user. The tested group has one member.
  161. """
  162. output = self.app.get("/api/0/group/some_group")
  163. self.assertEqual(output.status_code, 200)
  164. exp = {
  165. "display_name": "Some Group",
  166. "full_url": "http://localhost.localdomain/group/some_group",
  167. "description": None,
  168. "creator": {
  169. "fullname": "PY C",
  170. "full_url": "http://localhost.localdomain/user/pingou",
  171. "name": "pingou",
  172. "url_path": "user/pingou",
  173. },
  174. "members": ["pingou"],
  175. "date_created": "1492020239",
  176. "group_type": "user",
  177. "name": "some_group",
  178. }
  179. data = json.loads(output.get_data(as_text=True))
  180. data["date_created"] = "1492020239"
  181. self.assertDictEqual(data, exp)
  182. def test_api_view_group_two_members_authenticated(self):
  183. """
  184. Test the api_view_group method of the flask api with an
  185. authenticated user. The tested group has two members.
  186. """
  187. user = pagure.lib.model.User(
  188. user="mprahl",
  189. fullname="Matt Prahl",
  190. password="foo",
  191. default_email="mprahl@redhat.com",
  192. )
  193. self.session.add(user)
  194. self.session.commit()
  195. group = pagure.lib.query.search_groups(
  196. self.session, group_name="some_group"
  197. )
  198. result = pagure.lib.query.add_user_to_group(
  199. self.session, user.username, group, user.username, True
  200. )
  201. self.assertEqual(
  202. result, "User `mprahl` added to the group `some_group`."
  203. )
  204. self.session.commit()
  205. tests.create_tokens(self.session)
  206. headers = {"Authorization": "token aaabbbcccddd"}
  207. output = self.app.get("/api/0/group/some_group", headers=headers)
  208. self.assertEqual(output.status_code, 200)
  209. exp = {
  210. "display_name": "Some Group",
  211. "full_url": "http://localhost.localdomain/group/some_group",
  212. "description": None,
  213. "creator": {
  214. "fullname": "PY C",
  215. "full_url": "http://localhost.localdomain/user/pingou",
  216. "default_email": "bar@pingou.com",
  217. "emails": ["bar@pingou.com", "foo@pingou.com"],
  218. "name": "pingou",
  219. "url_path": "user/pingou",
  220. },
  221. "members": ["pingou", "mprahl"],
  222. "date_created": "1492020239",
  223. "group_type": "user",
  224. "name": "some_group",
  225. }
  226. self.maxDiff = None
  227. data = json.loads(output.get_data(as_text=True))
  228. data["date_created"] = "1492020239"
  229. self.assertDictEqual(data, exp)
  230. def test_api_view_group_no_group_error(self):
  231. """
  232. Test the api_view_group method of the flask api
  233. The tested group has one member.
  234. """
  235. output = self.app.get("/api/0/group/some_group3")
  236. self.assertEqual(output.status_code, 404)
  237. data = json.loads(output.get_data(as_text=True))
  238. self.assertEqual(data["error"], "Group not found")
  239. self.assertEqual(data["error_code"], "ENOGROUP")
  240. def test_api_view_group_w_projects_and_acl(self):
  241. """
  242. Test the api_view_group method with project info and restricted
  243. to the admin ACL
  244. """
  245. tests.create_tokens(self.session)
  246. headers = {"Authorization": "token aaabbbcccddd"}
  247. output = self.app.get(
  248. "/api/0/group/some_group?projects=1", headers=headers
  249. )
  250. self.assertEqual(output.status_code, 200)
  251. exp = {
  252. "display_name": "Some Group",
  253. "full_url": "http://localhost.localdomain/group/some_group",
  254. "description": None,
  255. "creator": {
  256. "fullname": "PY C",
  257. "full_url": "http://localhost.localdomain/user/pingou",
  258. "default_email": "bar@pingou.com",
  259. "emails": ["bar@pingou.com", "foo@pingou.com"],
  260. "name": "pingou",
  261. "url_path": "user/pingou",
  262. },
  263. "members": ["pingou"],
  264. "date_created": "1492020239",
  265. "group_type": "user",
  266. "name": "some_group",
  267. "pagination": {
  268. "first": "http://localhost...",
  269. "last": "http://localhost...",
  270. "next": None,
  271. "page": 1,
  272. "pages": 1,
  273. "per_page": 20,
  274. "prev": None,
  275. },
  276. "projects": [
  277. {
  278. "access_groups": {
  279. "admin": ["some_group"],
  280. "collaborator": [],
  281. "commit": [],
  282. "ticket": [],
  283. },
  284. "access_users": {
  285. "admin": [],
  286. "collaborator": [],
  287. "commit": [],
  288. "owner": ["pingou"],
  289. "ticket": [],
  290. },
  291. "close_status": [
  292. "Invalid",
  293. "Insufficient data",
  294. "Fixed",
  295. "Duplicate",
  296. ],
  297. "custom_keys": [],
  298. "date_created": "1492020239",
  299. "date_modified": "1492020239",
  300. "description": "test project #2",
  301. "full_url": "http://localhost.localdomain/test2",
  302. "fullname": "test2",
  303. "id": 2,
  304. "milestones": {},
  305. "name": "test2",
  306. "namespace": None,
  307. "parent": None,
  308. "priorities": {},
  309. "tags": [],
  310. "url_path": "test2",
  311. "user": {
  312. "fullname": "PY C",
  313. "full_url": "http://localhost.localdomain/user/pingou",
  314. "name": "pingou",
  315. "url_path": "user/pingou",
  316. },
  317. }
  318. ],
  319. "total_projects": 1,
  320. }
  321. data = json.loads(output.get_data(as_text=True))
  322. data["date_created"] = "1492020239"
  323. self.assertIsNotNone(data["pagination"]["first"])
  324. data["pagination"]["first"] = "http://localhost..."
  325. self.assertIsNotNone(data["pagination"]["last"])
  326. data["pagination"]["last"] = "http://localhost..."
  327. projects = []
  328. for p in data["projects"]:
  329. p["date_created"] = "1492020239"
  330. p["date_modified"] = "1492020239"
  331. projects.append(p)
  332. data["projects"] = projects
  333. self.assertDictEqual(data, exp)
  334. output = self.app.get(
  335. "/api/0/group/some_group?projects=1&acl=admin", headers=headers
  336. )
  337. data = json.loads(output.get_data(as_text=True))
  338. data["date_created"] = "1492020239"
  339. self.assertIsNotNone(data["pagination"]["first"])
  340. data["pagination"]["first"] = "http://localhost..."
  341. self.assertIsNotNone(data["pagination"]["last"])
  342. data["pagination"]["last"] = "http://localhost..."
  343. projects = []
  344. for p in data["projects"]:
  345. p["date_created"] = "1492020239"
  346. p["date_modified"] = "1492020239"
  347. projects.append(p)
  348. data["projects"] = projects
  349. self.assertDictEqual(data, exp)
  350. def test_api_view_group_w_projects_and_acl_commit(self):
  351. """
  352. Test the api_view_group method with project info and restricted
  353. to the commit ACL
  354. """
  355. output = self.app.get("/api/0/group/some_group?projects=1&acl=commit")
  356. self.assertEqual(output.status_code, 200)
  357. exp = {
  358. "display_name": "Some Group",
  359. "full_url": "http://localhost.localdomain/group/some_group",
  360. "description": None,
  361. "creator": {
  362. "fullname": "PY C",
  363. "full_url": "http://localhost.localdomain/user/pingou",
  364. "name": "pingou",
  365. "url_path": "user/pingou",
  366. },
  367. "members": ["pingou"],
  368. "date_created": "1492020239",
  369. "group_type": "user",
  370. "name": "some_group",
  371. "pagination": {
  372. "first": "http://localhost...",
  373. "last": "http://localhost...",
  374. "next": None,
  375. "page": 1,
  376. "pages": 1,
  377. "per_page": 20,
  378. "prev": None,
  379. },
  380. "projects": [
  381. {
  382. "access_groups": {
  383. "admin": ["some_group"],
  384. "collaborator": [],
  385. "commit": [],
  386. "ticket": [],
  387. },
  388. "access_users": {
  389. "admin": [],
  390. "collaborator": [],
  391. "commit": [],
  392. "owner": ["pingou"],
  393. "ticket": [],
  394. },
  395. "close_status": [
  396. "Invalid",
  397. "Insufficient data",
  398. "Fixed",
  399. "Duplicate",
  400. ],
  401. "custom_keys": [],
  402. "date_created": "1492020239",
  403. "date_modified": "1492020239",
  404. "description": "test project #2",
  405. "full_url": "http://localhost.localdomain/test2",
  406. "fullname": "test2",
  407. "id": 2,
  408. "milestones": {},
  409. "name": "test2",
  410. "namespace": None,
  411. "parent": None,
  412. "priorities": {},
  413. "tags": [],
  414. "url_path": "test2",
  415. "user": {
  416. "fullname": "PY C",
  417. "full_url": "http://localhost.localdomain/user/pingou",
  418. "name": "pingou",
  419. "url_path": "user/pingou",
  420. },
  421. }
  422. ],
  423. "total_projects": 1,
  424. }
  425. data = json.loads(output.get_data(as_text=True))
  426. data["date_created"] = "1492020239"
  427. self.assertIsNotNone(data["pagination"]["first"])
  428. data["pagination"]["first"] = "http://localhost..."
  429. self.assertIsNotNone(data["pagination"]["last"])
  430. data["pagination"]["last"] = "http://localhost..."
  431. projects = []
  432. for p in data["projects"]:
  433. p["date_created"] = "1492020239"
  434. p["date_modified"] = "1492020239"
  435. projects.append(p)
  436. data["projects"] = projects
  437. self.assertDictEqual(data, exp)
  438. def test_api_view_group_w_projects_and_acl_ticket(self):
  439. """
  440. Test the api_view_group method with project info and restricted
  441. to the ticket ACL
  442. """
  443. output = self.app.get("/api/0/group/some_group?projects=1&acl=ticket")
  444. self.assertEqual(output.status_code, 200)
  445. exp = {
  446. "display_name": "Some Group",
  447. "full_url": "http://localhost.localdomain/group/some_group",
  448. "description": None,
  449. "creator": {
  450. "fullname": "PY C",
  451. "full_url": "http://localhost.localdomain/user/pingou",
  452. "name": "pingou",
  453. "url_path": "user/pingou",
  454. },
  455. "members": ["pingou"],
  456. "date_created": "1492020239",
  457. "group_type": "user",
  458. "name": "some_group",
  459. "pagination": {
  460. "first": "http://localhost...",
  461. "last": "http://localhost...",
  462. "next": None,
  463. "page": 1,
  464. "pages": 1,
  465. "per_page": 20,
  466. "prev": None,
  467. },
  468. "projects": [
  469. {
  470. "access_groups": {
  471. "admin": ["some_group"],
  472. "collaborator": [],
  473. "commit": [],
  474. "ticket": [],
  475. },
  476. "access_users": {
  477. "admin": [],
  478. "collaborator": [],
  479. "commit": [],
  480. "owner": ["pingou"],
  481. "ticket": [],
  482. },
  483. "close_status": [
  484. "Invalid",
  485. "Insufficient data",
  486. "Fixed",
  487. "Duplicate",
  488. ],
  489. "custom_keys": [],
  490. "date_created": "1492020239",
  491. "date_modified": "1492020239",
  492. "description": "test project #2",
  493. "full_url": "http://localhost.localdomain/test2",
  494. "fullname": "test2",
  495. "id": 2,
  496. "milestones": {},
  497. "name": "test2",
  498. "namespace": None,
  499. "parent": None,
  500. "priorities": {},
  501. "tags": [],
  502. "url_path": "test2",
  503. "user": {
  504. "fullname": "PY C",
  505. "full_url": "http://localhost.localdomain/user/pingou",
  506. "name": "pingou",
  507. "url_path": "user/pingou",
  508. },
  509. }
  510. ],
  511. "total_projects": 1,
  512. }
  513. data = json.loads(output.get_data(as_text=True))
  514. data["date_created"] = "1492020239"
  515. self.assertIsNotNone(data["pagination"]["first"])
  516. data["pagination"]["first"] = "http://localhost..."
  517. self.assertIsNotNone(data["pagination"]["last"])
  518. data["pagination"]["last"] = "http://localhost..."
  519. projects = []
  520. for p in data["projects"]:
  521. p["date_created"] = "1492020239"
  522. p["date_modified"] = "1492020239"
  523. projects.append(p)
  524. data["projects"] = projects
  525. self.assertDictEqual(data, exp)
  526. def test_api_view_group_w_projects_and_acl_admin_no_project(self):
  527. """
  528. Test the api_view_group method with project info and restricted
  529. to the admin ACL
  530. """
  531. # Make the group having only commit access
  532. project = pagure.lib.query._get_project(self.session, "test2")
  533. msg = pagure.lib.query.add_group_to_project(
  534. session=self.session,
  535. project=project,
  536. new_group="some_group",
  537. user="pingou",
  538. access="commit",
  539. )
  540. self.session.commit()
  541. self.assertEqual(msg, "Group access updated")
  542. output = self.app.get("/api/0/group/some_group?projects=1&acl=admin")
  543. self.assertEqual(output.status_code, 200)
  544. exp = {
  545. "display_name": "Some Group",
  546. "full_url": "http://localhost.localdomain/group/some_group",
  547. "description": None,
  548. "creator": {
  549. "fullname": "PY C",
  550. "full_url": "http://localhost.localdomain/user/pingou",
  551. "name": "pingou",
  552. "url_path": "user/pingou",
  553. },
  554. "members": ["pingou"],
  555. "date_created": "1492020239",
  556. "group_type": "user",
  557. "name": "some_group",
  558. "pagination": {
  559. "first": "http://localhost...",
  560. "last": "http://localhost...",
  561. "next": None,
  562. "page": 1,
  563. "pages": 0,
  564. "per_page": 20,
  565. "prev": None,
  566. },
  567. "projects": [],
  568. "total_projects": 0,
  569. }
  570. data = json.loads(output.get_data(as_text=True))
  571. data["date_created"] = "1492020239"
  572. self.assertIsNotNone(data["pagination"]["first"])
  573. data["pagination"]["first"] = "http://localhost..."
  574. self.assertIsNotNone(data["pagination"]["last"])
  575. data["pagination"]["last"] = "http://localhost..."
  576. self.assertDictEqual(data, exp)
  577. def test_api_view_group_w_projects_and_acl_commit_no_project(self):
  578. """
  579. Test the api_view_group method with project info and restricted
  580. to the commit ACL
  581. """
  582. # Make the group having only ticket access
  583. project = pagure.lib.query._get_project(self.session, "test2")
  584. msg = pagure.lib.query.add_group_to_project(
  585. session=self.session,
  586. project=project,
  587. new_group="some_group",
  588. user="pingou",
  589. access="ticket",
  590. )
  591. self.session.commit()
  592. self.assertEqual(msg, "Group access updated")
  593. output = self.app.get("/api/0/group/some_group?projects=1&acl=commit")
  594. self.assertEqual(output.status_code, 200)
  595. exp = {
  596. "display_name": "Some Group",
  597. "full_url": "http://localhost.localdomain/group/some_group",
  598. "description": None,
  599. "creator": {
  600. "fullname": "PY C",
  601. "full_url": "http://localhost.localdomain/user/pingou",
  602. "name": "pingou",
  603. "url_path": "user/pingou",
  604. },
  605. "members": ["pingou"],
  606. "date_created": "1492020239",
  607. "group_type": "user",
  608. "name": "some_group",
  609. "pagination": {
  610. "first": "http://localhost...",
  611. "last": "http://localhost...",
  612. "next": None,
  613. "page": 1,
  614. "pages": 0,
  615. "per_page": 20,
  616. "prev": None,
  617. },
  618. "projects": [],
  619. "total_projects": 0,
  620. }
  621. data = json.loads(output.get_data(as_text=True))
  622. data["date_created"] = "1492020239"
  623. self.assertIsNotNone(data["pagination"]["first"])
  624. data["pagination"]["first"] = "http://localhost..."
  625. self.assertIsNotNone(data["pagination"]["last"])
  626. data["pagination"]["last"] = "http://localhost..."
  627. self.assertDictEqual(data, exp)
  628. def test_api_view_group_w_projects_and_acl_ticket_no_project(self):
  629. """
  630. Test the api_view_group method with project info and restricted
  631. to the ticket ACL
  632. """
  633. # Create a group not linked to any project
  634. item = pagure.lib.model.PagureGroup(
  635. group_name="rel-eng",
  636. group_type="user",
  637. display_name="Release engineering group",
  638. user_id=1, # pingou
  639. )
  640. self.session.add(item)
  641. self.session.commit()
  642. output = self.app.get("/api/0/group/rel-eng?projects=1&acl=ticket")
  643. self.assertEqual(output.status_code, 200)
  644. exp = {
  645. "display_name": "Release engineering group",
  646. "full_url": "http://localhost.localdomain/group/rel-eng",
  647. "description": None,
  648. "creator": {
  649. "fullname": "PY C",
  650. "full_url": "http://localhost.localdomain/user/pingou",
  651. "name": "pingou",
  652. "url_path": "user/pingou",
  653. },
  654. "members": [],
  655. "date_created": "1492020239",
  656. "group_type": "user",
  657. "name": "rel-eng",
  658. "pagination": {
  659. "first": "http://localhost...",
  660. "last": "http://localhost...",
  661. "next": None,
  662. "page": 1,
  663. "pages": 0,
  664. "per_page": 20,
  665. "prev": None,
  666. },
  667. "projects": [],
  668. "total_projects": 0,
  669. }
  670. data = json.loads(output.get_data(as_text=True))
  671. data["date_created"] = "1492020239"
  672. self.assertIsNotNone(data["pagination"]["first"])
  673. data["pagination"]["first"] = "http://localhost..."
  674. self.assertIsNotNone(data["pagination"]["last"])
  675. data["pagination"]["last"] = "http://localhost..."
  676. self.assertDictEqual(data, exp)
  677. def test_api_view_group_w_projects_and_acl_pagination(self):
  678. """
  679. Tests the pagination for the api_view_group method
  680. """
  681. project = pagure.lib.query._get_project(self.session, "test2")
  682. msg = pagure.lib.query.add_group_to_project(
  683. session=self.session,
  684. project=project,
  685. new_group="some_group",
  686. user="pingou",
  687. access="commit",
  688. )
  689. self.session.commit()
  690. self.assertEqual(msg, "Group access updated")
  691. project_another = pagure.lib.query._get_project(self.session, "test")
  692. msg = pagure.lib.query.add_group_to_project(
  693. session=self.session,
  694. project=project_another,
  695. new_group="some_group",
  696. user="pingou",
  697. access="commit",
  698. )
  699. self.session.commit()
  700. self.assertEqual(msg, "Group added")
  701. tests.create_tokens(self.session)
  702. headers = {"Authorization": "token aaabbbcccddd"}
  703. output = self.app.get(
  704. "/api/0/group/some_group?per_page=1&projects=1", headers=headers
  705. )
  706. self.assertEqual(output.status_code, 200)
  707. data = json.loads(output.get_data(as_text=True))
  708. projects = [project["name"] for project in data["projects"]]
  709. # Test the result we've got from the first page out of two
  710. assert projects == ["test"]
  711. output_last = self.app.get(
  712. data["pagination"]["next"].replace("http://localhost", ""),
  713. headers=headers,
  714. )
  715. self.assertEqual(output_last.status_code, 200)
  716. data_last = json.loads(output_last.get_data(as_text=True))
  717. projects.extend([project["name"] for project in data_last["projects"]])
  718. # Note that pagure sorts projects alphabetically, so we're comparing
  719. # a different order that was the order of requests
  720. assert projects == ["test", "test2"]
  721. def test_api_group_add_member_authenticated(self):
  722. """
  723. Test the api_group_add_member method of the flask api with an
  724. authenticated user.
  725. """
  726. tests.create_tokens(self.session)
  727. tests.create_tokens_acl(self.session)
  728. headers = {"Authorization": "token aaabbbcccddd"}
  729. payload = {"user": "foo"}
  730. output = self.app.post(
  731. "/api/0/group/some_group/add", data=payload, headers=headers
  732. )
  733. self.assertEqual(output.status_code, 200)
  734. exp = {
  735. "display_name": "Some Group",
  736. "full_url": "http://localhost.localdomain/group/some_group",
  737. "description": None,
  738. "creator": {
  739. "fullname": "PY C",
  740. "full_url": "http://localhost.localdomain/user/pingou",
  741. "url_path": "user/pingou",
  742. "default_email": "bar@pingou.com",
  743. "emails": ["bar@pingou.com", "foo@pingou.com"],
  744. "name": "pingou",
  745. },
  746. "members": ["pingou", "foo"],
  747. "date_created": "1492020239",
  748. "group_type": "user",
  749. "name": "some_group",
  750. }
  751. data = json.loads(output.get_data(as_text=True))
  752. data["date_created"] = "1492020239"
  753. self.assertDictEqual(data, exp)
  754. def test_api_group_add_member_unauthenticated(self):
  755. """
  756. Assert that api_group_add_member method will fail with
  757. unauthenticated user.
  758. """
  759. headers = {"Authorization": "token aaabbbcccddd"}
  760. payload = {"user": "foo"}
  761. output = self.app.post(
  762. "/api/0/group/some_group/add", data=payload, headers=headers
  763. )
  764. self.assertEqual(output.status_code, 401)
  765. exp = {
  766. "error": (
  767. "Invalid or expired token. "
  768. "Please visit "
  769. "http://localhost.localdomain/settings#nav-api-tab "
  770. "to get or renew your API token."
  771. ),
  772. "error_code": "EINVALIDTOK",
  773. "errors": "Invalid token",
  774. }
  775. data = json.loads(output.get_data(as_text=True))
  776. self.assertDictEqual(data, exp)
  777. def test_api_group_add_member_no_permission(self):
  778. """
  779. Assert that api_group_add_member method will fail with
  780. user that don't have permissions to add member to group.
  781. """
  782. # Create tokens for foo user
  783. tests.create_tokens(self.session, user_id=2)
  784. tests.create_tokens_acl(self.session)
  785. headers = {"Authorization": "token aaabbbcccddd"}
  786. payload = {"user": "foo"}
  787. output = self.app.post(
  788. "/api/0/group/some_group/add", data=payload, headers=headers
  789. )
  790. self.assertEqual(output.status_code, 400)
  791. exp = {
  792. "error": (
  793. "An error occurred at the database level "
  794. "and prevent the action from reaching completion"
  795. ),
  796. "error_code": "EDBERROR",
  797. "errors": ["You are not allowed to add user to this group"],
  798. }
  799. data = json.loads(output.get_data(as_text=True))
  800. self.assertDictEqual(data, exp)
  801. def test_api_group_add_member_no_group(self):
  802. """
  803. Assert that api_group_add_member method will fail when group doesn't
  804. exist.
  805. """
  806. tests.create_tokens(self.session)
  807. tests.create_tokens_acl(self.session)
  808. headers = {"Authorization": "token aaabbbcccddd"}
  809. payload = {"user": "foo"}
  810. output = self.app.post(
  811. "/api/0/group/no_group/add", data=payload, headers=headers
  812. )
  813. self.assertEqual(output.status_code, 404)
  814. exp = {"error": "Group not found", "error_code": "ENOGROUP"}
  815. data = json.loads(output.get_data(as_text=True))
  816. self.assertDictEqual(data, exp)
  817. def test_api_group_add_member_invalid_request(self):
  818. """
  819. Assert that api_group_add_member method will fail when request
  820. is invalid.
  821. """
  822. tests.create_tokens(self.session)
  823. tests.create_tokens_acl(self.session)
  824. headers = {"Authorization": "token aaabbbcccddd"}
  825. payload = {"dummy": "foo"}
  826. output = self.app.post(
  827. "/api/0/group/some_group/add", data=payload, headers=headers
  828. )
  829. self.assertEqual(output.status_code, 400)
  830. exp = {
  831. "error": "Invalid or incomplete input submitted",
  832. "error_code": "EINVALIDREQ",
  833. "errors": {"user": ["This field is required."]},
  834. }
  835. data = json.loads(output.get_data(as_text=True))
  836. self.assertDictEqual(data, exp)
  837. @patch("pagure.lib.query.add_user_to_group")
  838. def test_api_group_add_member_pagure_error(self, mock_add_user):
  839. """
  840. Assert that api_group_add_member method will fail when pagure
  841. throws exception.
  842. """
  843. tests.create_tokens(self.session)
  844. tests.create_tokens_acl(self.session)
  845. mock_add_user.side_effect = PagureException("Error")
  846. headers = {"Authorization": "token aaabbbcccddd"}
  847. payload = {"user": "foo"}
  848. output = self.app.post(
  849. "/api/0/group/some_group/add", data=payload, headers=headers
  850. )
  851. self.assertEqual(output.status_code, 400)
  852. exp = {
  853. "error": (
  854. "An error occurred at the database level "
  855. "and prevent the action from reaching completion"
  856. ),
  857. "error_code": "EDBERROR",
  858. "errors": ["Error"],
  859. }
  860. data = json.loads(output.get_data(as_text=True))
  861. self.assertDictEqual(data, exp)
  862. @patch("pagure.lib.query.add_user_to_group")
  863. def test_api_group_add_member_sqlalchemy_error(self, mock_add_user):
  864. """
  865. Assert that api_group_add_member method will fail when SQLAlchemy
  866. throws exception.
  867. """
  868. tests.create_tokens(self.session)
  869. tests.create_tokens_acl(self.session)
  870. mock_add_user.side_effect = SQLAlchemyError("Error")
  871. headers = {"Authorization": "token aaabbbcccddd"}
  872. payload = {"user": "foo"}
  873. output = self.app.post(
  874. "/api/0/group/some_group/add", data=payload, headers=headers
  875. )
  876. self.assertEqual(output.status_code, 400)
  877. exp = {
  878. "error": (
  879. "An error occurred at the database level "
  880. "and prevent the action from reaching completion"
  881. ),
  882. "error_code": "EDBERROR",
  883. "errors": ["Error"],
  884. }
  885. data = json.loads(output.get_data(as_text=True))
  886. self.assertDictEqual(data, exp)
  887. def test_api_group_remove_member_authenticated(self):
  888. """
  889. Test the api_group_remove_member method of the flask api with an
  890. authenticated user.
  891. """
  892. tests.create_tokens(self.session)
  893. tests.create_tokens_acl(self.session)
  894. headers = {"Authorization": "token aaabbbcccddd"}
  895. payload = {"user": "foo"}
  896. # Add user first
  897. output = self.app.post(
  898. "/api/0/group/some_group/add", data=payload, headers=headers
  899. )
  900. exp = {
  901. "display_name": "Some Group",
  902. "full_url": "http://localhost.localdomain/group/some_group",
  903. "description": None,
  904. "creator": {
  905. "fullname": "PY C",
  906. "full_url": "http://localhost.localdomain/user/pingou",
  907. "url_path": "user/pingou",
  908. "default_email": "bar@pingou.com",
  909. "emails": ["bar@pingou.com", "foo@pingou.com"],
  910. "name": "pingou",
  911. },
  912. "members": ["pingou", "foo"],
  913. "date_created": "1492020239",
  914. "group_type": "user",
  915. "name": "some_group",
  916. }
  917. data = json.loads(output.get_data(as_text=True))
  918. data["date_created"] = "1492020239"
  919. self.assertDictEqual(data, exp)
  920. # Then remove it
  921. output = self.app.post(
  922. "/api/0/group/some_group/remove", data=payload, headers=headers
  923. )
  924. self.assertEqual(output.status_code, 200)
  925. exp = {
  926. "display_name": "Some Group",
  927. "full_url": "http://localhost.localdomain/group/some_group",
  928. "description": None,
  929. "creator": {
  930. "fullname": "PY C",
  931. "full_url": "http://localhost.localdomain/user/pingou",
  932. "url_path": "user/pingou",
  933. "default_email": "bar@pingou.com",
  934. "emails": ["bar@pingou.com", "foo@pingou.com"],
  935. "name": "pingou",
  936. },
  937. "members": ["pingou"],
  938. "date_created": "1492020239",
  939. "group_type": "user",
  940. "name": "some_group",
  941. }
  942. data = json.loads(output.get_data(as_text=True))
  943. data["date_created"] = "1492020239"
  944. self.assertDictEqual(data, exp)
  945. def test_api_group_remove_member_unauthenticated(self):
  946. """
  947. Assert that api_group_remove_member method will fail with
  948. unauthenticated user.
  949. """
  950. headers = {"Authorization": "token aaabbbcccddd"}
  951. payload = {"user": "foo"}
  952. output = self.app.post(
  953. "/api/0/group/some_group/remove", data=payload, headers=headers
  954. )
  955. self.assertEqual(output.status_code, 401)
  956. exp = {
  957. "error": (
  958. "Invalid or expired token. "
  959. "Please visit "
  960. "http://localhost.localdomain/settings#nav-api-tab "
  961. "to get or renew your API token."
  962. ),
  963. "error_code": "EINVALIDTOK",
  964. "errors": "Invalid token",
  965. }
  966. data = json.loads(output.get_data(as_text=True))
  967. self.assertDictEqual(data, exp)
  968. def test_api_group_remove_member_no_permission(self):
  969. """
  970. Assert that api_group_remove_member method will fail with
  971. user that don't have permissions to remove member to group.
  972. """
  973. # Create tokens for foo user
  974. tests.create_tokens(self.session, user_id=2)
  975. tests.create_tokens_acl(self.session)
  976. headers = {"Authorization": "token aaabbbcccddd"}
  977. payload = {"user": "foo"}
  978. output = self.app.post(
  979. "/api/0/group/some_group/remove", data=payload, headers=headers
  980. )
  981. self.assertEqual(output.status_code, 400)
  982. exp = {
  983. "error": (
  984. "An error occurred at the database level "
  985. "and prevent the action from reaching completion"
  986. ),
  987. "error_code": "EDBERROR",
  988. "errors": ["You are not allowed to remove user from this group"],
  989. }
  990. data = json.loads(output.get_data(as_text=True))
  991. self.assertDictEqual(data, exp)
  992. def test_api_group_remove_member_no_group(self):
  993. """
  994. Assert that api_group_remove_member method will fail when group doesn't
  995. exist.
  996. """
  997. tests.create_tokens(self.session)
  998. tests.create_tokens_acl(self.session)
  999. headers = {"Authorization": "token aaabbbcccddd"}
  1000. payload = {"user": "foo"}
  1001. output = self.app.post(
  1002. "/api/0/group/no_group/remove", data=payload, headers=headers
  1003. )
  1004. self.assertEqual(output.status_code, 404)
  1005. exp = {"error": "Group not found", "error_code": "ENOGROUP"}
  1006. data = json.loads(output.get_data(as_text=True))
  1007. self.assertDictEqual(data, exp)
  1008. def test_api_group_remove_member_invalid_request(self):
  1009. """
  1010. Assert that api_group_remove_member method will fail when request
  1011. is invalid.
  1012. """
  1013. tests.create_tokens(self.session)
  1014. tests.create_tokens_acl(self.session)
  1015. headers = {"Authorization": "token aaabbbcccddd"}
  1016. payload = {"dummy": "foo"}
  1017. output = self.app.post(
  1018. "/api/0/group/some_group/remove", data=payload, headers=headers
  1019. )
  1020. self.assertEqual(output.status_code, 400)
  1021. exp = {
  1022. "error": "Invalid or incomplete input submitted",
  1023. "error_code": "EINVALIDREQ",
  1024. "errors": {"user": ["This field is required."]},
  1025. }
  1026. data = json.loads(output.get_data(as_text=True))
  1027. self.assertDictEqual(data, exp)
  1028. @patch("pagure.lib.query.delete_user_of_group")
  1029. def test_api_group_remove_member_pagure_error(self, mock_remove_user):
  1030. """
  1031. Assert that api_group_remove_member method will fail when pagure
  1032. throws exception.
  1033. """
  1034. tests.create_tokens(self.session)
  1035. tests.create_tokens_acl(self.session)
  1036. mock_remove_user.side_effect = PagureException("Error")
  1037. headers = {"Authorization": "token aaabbbcccddd"}
  1038. payload = {"user": "foo"}
  1039. output = self.app.post(
  1040. "/api/0/group/some_group/remove", data=payload, headers=headers
  1041. )
  1042. self.assertEqual(output.status_code, 400)
  1043. exp = {
  1044. "error": (
  1045. "An error occurred at the database level "
  1046. "and prevent the action from reaching completion"
  1047. ),
  1048. "error_code": "EDBERROR",
  1049. "errors": ["Error"],
  1050. }
  1051. data = json.loads(output.get_data(as_text=True))
  1052. self.assertDictEqual(data, exp)
  1053. @patch("pagure.lib.query.delete_user_of_group")
  1054. def test_api_group_remove_member_sqlalchemy_error(self, mock_remove_user):
  1055. """
  1056. Assert that api_group_remove_member method will fail when SQLAlchemy
  1057. throws exception.
  1058. """
  1059. tests.create_tokens(self.session)
  1060. tests.create_tokens_acl(self.session)
  1061. mock_remove_user.side_effect = SQLAlchemyError("Error")
  1062. headers = {"Authorization": "token aaabbbcccddd"}
  1063. payload = {"user": "foo"}
  1064. output = self.app.post(
  1065. "/api/0/group/some_group/remove", data=payload, headers=headers
  1066. )
  1067. self.assertEqual(output.status_code, 400)
  1068. exp = {
  1069. "error": (
  1070. "An error occurred at the database level "
  1071. "and prevent the action from reaching completion"
  1072. ),
  1073. "error_code": "EDBERROR",
  1074. "errors": ["Error"],
  1075. }
  1076. data = json.loads(output.get_data(as_text=True))
  1077. self.assertDictEqual(data, exp)
  1078. if __name__ == "__main__":
  1079. unittest.main(verbosity=2)