test_pagure_flask_api_group.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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. sys.path.insert(
  14. 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
  15. )
  16. import pagure.api
  17. import pagure.lib.query
  18. import tests
  19. class PagureFlaskApiGroupTests(tests.SimplePagureTest):
  20. """ Tests for the flask API of pagure for issue """
  21. maxDiff = None
  22. def setUp(self):
  23. """ Set up the environnment, ran before every tests. """
  24. super(PagureFlaskApiGroupTests, self).setUp()
  25. pagure.config.config["REQUESTS_FOLDER"] = None
  26. msg = pagure.lib.query.add_group(
  27. self.session,
  28. group_name="some_group",
  29. display_name="Some Group",
  30. description=None,
  31. group_type="bar",
  32. user="pingou",
  33. is_admin=False,
  34. blacklist=[],
  35. )
  36. self.session.commit()
  37. tests.create_projects(self.session)
  38. project = pagure.lib.query._get_project(self.session, "test2")
  39. msg = pagure.lib.query.add_group_to_project(
  40. session=self.session,
  41. project=project,
  42. new_group="some_group",
  43. user="pingou",
  44. )
  45. self.session.commit()
  46. self.assertEqual(msg, "Group added")
  47. def test_api_groups(self):
  48. """ Test the api_groups function. """
  49. # Add a couple of groups so that we can list them
  50. item = pagure.lib.model.PagureGroup(
  51. group_name="group1",
  52. group_type="user",
  53. display_name="User group",
  54. user_id=1, # pingou
  55. )
  56. self.session.add(item)
  57. item = pagure.lib.model.PagureGroup(
  58. group_name="rel-eng",
  59. group_type="user",
  60. display_name="Release engineering group",
  61. user_id=1, # pingou
  62. )
  63. self.session.add(item)
  64. self.session.commit()
  65. output = self.app.get("/api/0/groups")
  66. self.assertEqual(output.status_code, 200)
  67. data = json.loads(output.get_data(as_text=True))
  68. self.assertEqual(data["groups"], ["some_group", "group1", "rel-eng"])
  69. self.assertEqual(
  70. sorted(data.keys()), ["groups", "pagination", "total_groups"]
  71. )
  72. self.assertEqual(data["total_groups"], 3)
  73. output = self.app.get("/api/0/groups?pattern=re")
  74. self.assertEqual(output.status_code, 200)
  75. data = json.loads(output.get_data(as_text=True))
  76. self.assertEqual(data["groups"], ["rel-eng"])
  77. self.assertEqual(
  78. sorted(data.keys()), ["groups", "pagination", "total_groups"]
  79. )
  80. self.assertEqual(data["total_groups"], 1)
  81. def test_api_groups_extended(self):
  82. """ Test the api_groups function. """
  83. # Add a couple of groups so that we can list them
  84. item = pagure.lib.model.PagureGroup(
  85. group_name="group1",
  86. group_type="user",
  87. display_name="User group",
  88. user_id=1, # pingou
  89. )
  90. self.session.add(item)
  91. item = pagure.lib.model.PagureGroup(
  92. group_name="rel-eng",
  93. group_type="user",
  94. display_name="Release engineering group",
  95. user_id=1, # pingou
  96. )
  97. self.session.add(item)
  98. self.session.commit()
  99. output = self.app.get("/api/0/groups?extended=1")
  100. self.assertEqual(output.status_code, 200)
  101. data = json.loads(output.get_data(as_text=True))
  102. for k in ["first", "last"]:
  103. self.assertIsNotNone(data["pagination"][k])
  104. data["pagination"][k] = "http://localhost..."
  105. self.assertEqual(
  106. data,
  107. {
  108. "groups": [
  109. {"description": None, "name": "some_group"},
  110. {"description": None, "name": "group1"},
  111. {"description": None, "name": "rel-eng"},
  112. ],
  113. "pagination": {
  114. "first": "http://localhost...",
  115. "last": "http://localhost...",
  116. "next": None,
  117. "page": 1,
  118. "pages": 1,
  119. "per_page": 20,
  120. "prev": None,
  121. },
  122. "total_groups": 3,
  123. },
  124. )
  125. def test_api_view_group_authenticated(self):
  126. """
  127. Test the api_view_group method of the flask api with an
  128. authenticated user. The tested group has one member.
  129. """
  130. tests.create_tokens(self.session)
  131. headers = {"Authorization": "token aaabbbcccddd"}
  132. output = self.app.get("/api/0/group/some_group", headers=headers)
  133. self.assertEqual(output.status_code, 200)
  134. exp = {
  135. "display_name": "Some Group",
  136. "description": None,
  137. "creator": {
  138. "fullname": "PY C",
  139. "default_email": "bar@pingou.com",
  140. "emails": ["bar@pingou.com", "foo@pingou.com"],
  141. "name": "pingou",
  142. },
  143. "members": ["pingou"],
  144. "date_created": "1492020239",
  145. "group_type": "user",
  146. "name": "some_group",
  147. }
  148. data = json.loads(output.get_data(as_text=True))
  149. data["date_created"] = "1492020239"
  150. self.assertDictEqual(data, exp)
  151. def test_api_view_group_unauthenticated(self):
  152. """
  153. Test the api_view_group method of the flask api with an
  154. unauthenticated user. The tested group has one member.
  155. """
  156. output = self.app.get("/api/0/group/some_group")
  157. self.assertEqual(output.status_code, 200)
  158. exp = {
  159. "display_name": "Some Group",
  160. "description": None,
  161. "creator": {"fullname": "PY C", "name": "pingou"},
  162. "members": ["pingou"],
  163. "date_created": "1492020239",
  164. "group_type": "user",
  165. "name": "some_group",
  166. }
  167. data = json.loads(output.get_data(as_text=True))
  168. data["date_created"] = "1492020239"
  169. self.assertDictEqual(data, exp)
  170. def test_api_view_group_two_members_authenticated(self):
  171. """
  172. Test the api_view_group method of the flask api with an
  173. authenticated user. The tested group has two members.
  174. """
  175. user = pagure.lib.model.User(
  176. user="mprahl",
  177. fullname="Matt Prahl",
  178. password="foo",
  179. default_email="mprahl@redhat.com",
  180. )
  181. self.session.add(user)
  182. self.session.commit()
  183. group = pagure.lib.query.search_groups(
  184. self.session, group_name="some_group"
  185. )
  186. result = pagure.lib.query.add_user_to_group(
  187. self.session, user.username, group, user.username, True
  188. )
  189. self.assertEqual(
  190. result, "User `mprahl` added to the group `some_group`."
  191. )
  192. self.session.commit()
  193. tests.create_tokens(self.session)
  194. headers = {"Authorization": "token aaabbbcccddd"}
  195. output = self.app.get("/api/0/group/some_group", headers=headers)
  196. self.assertEqual(output.status_code, 200)
  197. exp = {
  198. "display_name": "Some Group",
  199. "description": None,
  200. "creator": {
  201. "fullname": "PY C",
  202. "default_email": "bar@pingou.com",
  203. "emails": ["bar@pingou.com", "foo@pingou.com"],
  204. "name": "pingou",
  205. },
  206. "members": ["pingou", "mprahl"],
  207. "date_created": "1492020239",
  208. "group_type": "user",
  209. "name": "some_group",
  210. }
  211. self.maxDiff = None
  212. data = json.loads(output.get_data(as_text=True))
  213. data["date_created"] = "1492020239"
  214. self.assertDictEqual(data, exp)
  215. def test_api_view_group_no_group_error(self):
  216. """
  217. Test the api_view_group method of the flask api
  218. The tested group has one member.
  219. """
  220. output = self.app.get("/api/0/group/some_group3")
  221. self.assertEqual(output.status_code, 404)
  222. data = json.loads(output.get_data(as_text=True))
  223. self.assertEqual(data["error"], "Group not found")
  224. self.assertEqual(data["error_code"], "ENOGROUP")
  225. def test_api_view_group_w_projects_and_acl(self):
  226. """
  227. Test the api_view_group method with project info and restricted
  228. to the admin ACL
  229. """
  230. tests.create_tokens(self.session)
  231. headers = {"Authorization": "token aaabbbcccddd"}
  232. output = self.app.get(
  233. "/api/0/group/some_group?projects=1", headers=headers
  234. )
  235. self.assertEqual(output.status_code, 200)
  236. exp = {
  237. "display_name": "Some Group",
  238. "description": None,
  239. "creator": {
  240. "fullname": "PY C",
  241. "default_email": "bar@pingou.com",
  242. "emails": ["bar@pingou.com", "foo@pingou.com"],
  243. "name": "pingou",
  244. },
  245. "members": ["pingou"],
  246. "date_created": "1492020239",
  247. "group_type": "user",
  248. "name": "some_group",
  249. "projects": [
  250. {
  251. "access_groups": {
  252. "admin": ["some_group"],
  253. "commit": [],
  254. "ticket": [],
  255. },
  256. "access_users": {
  257. "admin": [],
  258. "commit": [],
  259. "owner": ["pingou"],
  260. "ticket": [],
  261. },
  262. "close_status": [
  263. "Invalid",
  264. "Insufficient data",
  265. "Fixed",
  266. "Duplicate",
  267. ],
  268. "custom_keys": [],
  269. "date_created": "1492020239",
  270. "date_modified": "1492020239",
  271. "description": "test project #2",
  272. "fullname": "test2",
  273. "id": 2,
  274. "milestones": {},
  275. "name": "test2",
  276. "namespace": None,
  277. "parent": None,
  278. "priorities": {},
  279. "tags": [],
  280. "url_path": "test2",
  281. "user": {"fullname": "PY C", "name": "pingou"},
  282. }
  283. ],
  284. }
  285. data = json.loads(output.get_data(as_text=True))
  286. data["date_created"] = "1492020239"
  287. projects = []
  288. for p in data["projects"]:
  289. p["date_created"] = "1492020239"
  290. p["date_modified"] = "1492020239"
  291. projects.append(p)
  292. data["projects"] = projects
  293. self.assertDictEqual(data, exp)
  294. output2 = self.app.get(
  295. "/api/0/group/some_group?projects=1&acl=admin", headers=headers
  296. )
  297. self.assertListEqual(
  298. output.get_data(as_text=True).split("\n"),
  299. output2.get_data(as_text=True).split("\n"),
  300. )
  301. def test_api_view_group_w_projects_and_acl_commit(self):
  302. """
  303. Test the api_view_group method with project info and restricted
  304. to the commit ACL
  305. """
  306. output = self.app.get("/api/0/group/some_group?projects=1&acl=commit")
  307. self.assertEqual(output.status_code, 200)
  308. exp = {
  309. "display_name": "Some Group",
  310. "description": None,
  311. "creator": {"fullname": "PY C", "name": "pingou"},
  312. "members": ["pingou"],
  313. "date_created": "1492020239",
  314. "group_type": "user",
  315. "name": "some_group",
  316. "projects": [
  317. {
  318. "access_groups": {
  319. "admin": ["some_group"],
  320. "commit": [],
  321. "ticket": [],
  322. },
  323. "access_users": {
  324. "admin": [],
  325. "commit": [],
  326. "owner": ["pingou"],
  327. "ticket": [],
  328. },
  329. "close_status": [
  330. "Invalid",
  331. "Insufficient data",
  332. "Fixed",
  333. "Duplicate",
  334. ],
  335. "custom_keys": [],
  336. "date_created": "1492020239",
  337. "date_modified": "1492020239",
  338. "description": "test project #2",
  339. "fullname": "test2",
  340. "id": 2,
  341. "milestones": {},
  342. "name": "test2",
  343. "namespace": None,
  344. "parent": None,
  345. "priorities": {},
  346. "tags": [],
  347. "url_path": "test2",
  348. "user": {"fullname": "PY C", "name": "pingou"},
  349. }
  350. ],
  351. }
  352. data = json.loads(output.get_data(as_text=True))
  353. data["date_created"] = "1492020239"
  354. projects = []
  355. for p in data["projects"]:
  356. p["date_created"] = "1492020239"
  357. p["date_modified"] = "1492020239"
  358. projects.append(p)
  359. data["projects"] = projects
  360. self.assertDictEqual(data, exp)
  361. def test_api_view_group_w_projects_and_acl_ticket(self):
  362. """
  363. Test the api_view_group method with project info and restricted
  364. to the ticket ACL
  365. """
  366. output = self.app.get("/api/0/group/some_group?projects=1&acl=ticket")
  367. self.assertEqual(output.status_code, 200)
  368. exp = {
  369. "display_name": "Some Group",
  370. "description": None,
  371. "creator": {"fullname": "PY C", "name": "pingou"},
  372. "members": ["pingou"],
  373. "date_created": "1492020239",
  374. "group_type": "user",
  375. "name": "some_group",
  376. "projects": [
  377. {
  378. "access_groups": {
  379. "admin": ["some_group"],
  380. "commit": [],
  381. "ticket": [],
  382. },
  383. "access_users": {
  384. "admin": [],
  385. "commit": [],
  386. "owner": ["pingou"],
  387. "ticket": [],
  388. },
  389. "close_status": [
  390. "Invalid",
  391. "Insufficient data",
  392. "Fixed",
  393. "Duplicate",
  394. ],
  395. "custom_keys": [],
  396. "date_created": "1492020239",
  397. "date_modified": "1492020239",
  398. "description": "test project #2",
  399. "fullname": "test2",
  400. "id": 2,
  401. "milestones": {},
  402. "name": "test2",
  403. "namespace": None,
  404. "parent": None,
  405. "priorities": {},
  406. "tags": [],
  407. "url_path": "test2",
  408. "user": {"fullname": "PY C", "name": "pingou"},
  409. }
  410. ],
  411. }
  412. data = json.loads(output.get_data(as_text=True))
  413. data["date_created"] = "1492020239"
  414. projects = []
  415. for p in data["projects"]:
  416. p["date_created"] = "1492020239"
  417. p["date_modified"] = "1492020239"
  418. projects.append(p)
  419. data["projects"] = projects
  420. self.assertDictEqual(data, exp)
  421. def test_api_view_group_w_projects_and_acl_admin_no_project(self):
  422. """
  423. Test the api_view_group method with project info and restricted
  424. to the admin ACL
  425. """
  426. # Make the group having only commit access
  427. project = pagure.lib.query._get_project(self.session, "test2")
  428. msg = pagure.lib.query.add_group_to_project(
  429. session=self.session,
  430. project=project,
  431. new_group="some_group",
  432. user="pingou",
  433. access="commit",
  434. )
  435. self.session.commit()
  436. self.assertEqual(msg, "Group access updated")
  437. output = self.app.get("/api/0/group/some_group?projects=1&acl=admin")
  438. self.assertEqual(output.status_code, 200)
  439. exp = {
  440. "display_name": "Some Group",
  441. "description": None,
  442. "creator": {"fullname": "PY C", "name": "pingou"},
  443. "members": ["pingou"],
  444. "date_created": "1492020239",
  445. "group_type": "user",
  446. "name": "some_group",
  447. "projects": [],
  448. }
  449. data = json.loads(output.get_data(as_text=True))
  450. data["date_created"] = "1492020239"
  451. self.assertDictEqual(data, exp)
  452. def test_api_view_group_w_projects_and_acl_commit_no_project(self):
  453. """
  454. Test the api_view_group method with project info and restricted
  455. to the commit ACL
  456. """
  457. # Make the group having only ticket access
  458. project = pagure.lib.query._get_project(self.session, "test2")
  459. msg = pagure.lib.query.add_group_to_project(
  460. session=self.session,
  461. project=project,
  462. new_group="some_group",
  463. user="pingou",
  464. access="ticket",
  465. )
  466. self.session.commit()
  467. self.assertEqual(msg, "Group access updated")
  468. output = self.app.get("/api/0/group/some_group?projects=1&acl=commit")
  469. self.assertEqual(output.status_code, 200)
  470. exp = {
  471. "display_name": "Some Group",
  472. "description": None,
  473. "creator": {"fullname": "PY C", "name": "pingou"},
  474. "members": ["pingou"],
  475. "date_created": "1492020239",
  476. "group_type": "user",
  477. "name": "some_group",
  478. "projects": [],
  479. }
  480. data = json.loads(output.get_data(as_text=True))
  481. data["date_created"] = "1492020239"
  482. self.assertDictEqual(data, exp)
  483. def test_api_view_group_w_projects_and_acl_ticket_no_project(self):
  484. """
  485. Test the api_view_group method with project info and restricted
  486. to the ticket ACL
  487. """
  488. # Create a group not linked to any project
  489. item = pagure.lib.model.PagureGroup(
  490. group_name="rel-eng",
  491. group_type="user",
  492. display_name="Release engineering group",
  493. user_id=1, # pingou
  494. )
  495. self.session.add(item)
  496. self.session.commit()
  497. output = self.app.get("/api/0/group/rel-eng?projects=1&acl=ticket")
  498. self.assertEqual(output.status_code, 200)
  499. exp = {
  500. "display_name": "Release engineering group",
  501. "description": None,
  502. "creator": {"fullname": "PY C", "name": "pingou"},
  503. "members": [],
  504. "date_created": "1492020239",
  505. "group_type": "user",
  506. "name": "rel-eng",
  507. "projects": [],
  508. }
  509. data = json.loads(output.get_data(as_text=True))
  510. data["date_created"] = "1492020239"
  511. self.assertDictEqual(data, exp)
  512. if __name__ == "__main__":
  513. unittest.main(verbosity=2)