1
0

test_pagure_flask_api_group.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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"], ["group1", "rel-eng", "some_group"])
  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": "group1"},
  110. {"description": None, "name": "rel-eng"},
  111. {"description": None, "name": "some_group"},
  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. "url_path": "user/pingou",
  140. "default_email": "bar@pingou.com",
  141. "emails": ["bar@pingou.com", "foo@pingou.com"],
  142. "name": "pingou",
  143. },
  144. "members": ["pingou"],
  145. "date_created": "1492020239",
  146. "group_type": "user",
  147. "name": "some_group",
  148. }
  149. data = json.loads(output.get_data(as_text=True))
  150. data["date_created"] = "1492020239"
  151. self.assertDictEqual(data, exp)
  152. def test_api_view_group_unauthenticated(self):
  153. """
  154. Test the api_view_group method of the flask api with an
  155. unauthenticated user. The tested group has one member.
  156. """
  157. output = self.app.get("/api/0/group/some_group")
  158. self.assertEqual(output.status_code, 200)
  159. exp = {
  160. "display_name": "Some Group",
  161. "description": None,
  162. "creator": {
  163. "fullname": "PY C",
  164. "name": "pingou",
  165. "url_path": "user/pingou",
  166. },
  167. "members": ["pingou"],
  168. "date_created": "1492020239",
  169. "group_type": "user",
  170. "name": "some_group",
  171. }
  172. data = json.loads(output.get_data(as_text=True))
  173. data["date_created"] = "1492020239"
  174. self.assertDictEqual(data, exp)
  175. def test_api_view_group_two_members_authenticated(self):
  176. """
  177. Test the api_view_group method of the flask api with an
  178. authenticated user. The tested group has two members.
  179. """
  180. user = pagure.lib.model.User(
  181. user="mprahl",
  182. fullname="Matt Prahl",
  183. password="foo",
  184. default_email="mprahl@redhat.com",
  185. )
  186. self.session.add(user)
  187. self.session.commit()
  188. group = pagure.lib.query.search_groups(
  189. self.session, group_name="some_group"
  190. )
  191. result = pagure.lib.query.add_user_to_group(
  192. self.session, user.username, group, user.username, True
  193. )
  194. self.assertEqual(
  195. result, "User `mprahl` added to the group `some_group`."
  196. )
  197. self.session.commit()
  198. tests.create_tokens(self.session)
  199. headers = {"Authorization": "token aaabbbcccddd"}
  200. output = self.app.get("/api/0/group/some_group", headers=headers)
  201. self.assertEqual(output.status_code, 200)
  202. exp = {
  203. "display_name": "Some Group",
  204. "description": None,
  205. "creator": {
  206. "fullname": "PY C",
  207. "default_email": "bar@pingou.com",
  208. "emails": ["bar@pingou.com", "foo@pingou.com"],
  209. "name": "pingou",
  210. "url_path": "user/pingou",
  211. },
  212. "members": ["pingou", "mprahl"],
  213. "date_created": "1492020239",
  214. "group_type": "user",
  215. "name": "some_group",
  216. }
  217. self.maxDiff = None
  218. data = json.loads(output.get_data(as_text=True))
  219. data["date_created"] = "1492020239"
  220. self.assertDictEqual(data, exp)
  221. def test_api_view_group_no_group_error(self):
  222. """
  223. Test the api_view_group method of the flask api
  224. The tested group has one member.
  225. """
  226. output = self.app.get("/api/0/group/some_group3")
  227. self.assertEqual(output.status_code, 404)
  228. data = json.loads(output.get_data(as_text=True))
  229. self.assertEqual(data["error"], "Group not found")
  230. self.assertEqual(data["error_code"], "ENOGROUP")
  231. def test_api_view_group_w_projects_and_acl(self):
  232. """
  233. Test the api_view_group method with project info and restricted
  234. to the admin ACL
  235. """
  236. tests.create_tokens(self.session)
  237. headers = {"Authorization": "token aaabbbcccddd"}
  238. output = self.app.get(
  239. "/api/0/group/some_group?projects=1", headers=headers
  240. )
  241. self.assertEqual(output.status_code, 200)
  242. exp = {
  243. "display_name": "Some Group",
  244. "description": None,
  245. "creator": {
  246. "fullname": "PY C",
  247. "default_email": "bar@pingou.com",
  248. "emails": ["bar@pingou.com", "foo@pingou.com"],
  249. "name": "pingou",
  250. "url_path": "user/pingou",
  251. },
  252. "members": ["pingou"],
  253. "date_created": "1492020239",
  254. "group_type": "user",
  255. "name": "some_group",
  256. "projects": [
  257. {
  258. "access_groups": {
  259. "admin": ["some_group"],
  260. "collaborator": [],
  261. "commit": [],
  262. "ticket": [],
  263. },
  264. "access_users": {
  265. "admin": [],
  266. "collaborator": [],
  267. "commit": [],
  268. "owner": ["pingou"],
  269. "ticket": [],
  270. },
  271. "close_status": [
  272. "Invalid",
  273. "Insufficient data",
  274. "Fixed",
  275. "Duplicate",
  276. ],
  277. "custom_keys": [],
  278. "date_created": "1492020239",
  279. "date_modified": "1492020239",
  280. "description": "test project #2",
  281. "fullname": "test2",
  282. "id": 2,
  283. "milestones": {},
  284. "name": "test2",
  285. "namespace": None,
  286. "parent": None,
  287. "priorities": {},
  288. "tags": [],
  289. "url_path": "test2",
  290. "user": {
  291. "fullname": "PY C",
  292. "name": "pingou",
  293. "url_path": "user/pingou",
  294. },
  295. }
  296. ],
  297. }
  298. data = json.loads(output.get_data(as_text=True))
  299. data["date_created"] = "1492020239"
  300. projects = []
  301. for p in data["projects"]:
  302. p["date_created"] = "1492020239"
  303. p["date_modified"] = "1492020239"
  304. projects.append(p)
  305. data["projects"] = projects
  306. self.assertDictEqual(data, exp)
  307. output2 = self.app.get(
  308. "/api/0/group/some_group?projects=1&acl=admin", headers=headers
  309. )
  310. self.assertListEqual(
  311. output.get_data(as_text=True).split("\n"),
  312. output2.get_data(as_text=True).split("\n"),
  313. )
  314. def test_api_view_group_w_projects_and_acl_commit(self):
  315. """
  316. Test the api_view_group method with project info and restricted
  317. to the commit ACL
  318. """
  319. output = self.app.get("/api/0/group/some_group?projects=1&acl=commit")
  320. self.assertEqual(output.status_code, 200)
  321. exp = {
  322. "display_name": "Some Group",
  323. "description": None,
  324. "creator": {
  325. "fullname": "PY C",
  326. "name": "pingou",
  327. "url_path": "user/pingou",
  328. },
  329. "members": ["pingou"],
  330. "date_created": "1492020239",
  331. "group_type": "user",
  332. "name": "some_group",
  333. "projects": [
  334. {
  335. "access_groups": {
  336. "admin": ["some_group"],
  337. "collaborator": [],
  338. "commit": [],
  339. "ticket": [],
  340. },
  341. "access_users": {
  342. "admin": [],
  343. "collaborator": [],
  344. "commit": [],
  345. "owner": ["pingou"],
  346. "ticket": [],
  347. },
  348. "close_status": [
  349. "Invalid",
  350. "Insufficient data",
  351. "Fixed",
  352. "Duplicate",
  353. ],
  354. "custom_keys": [],
  355. "date_created": "1492020239",
  356. "date_modified": "1492020239",
  357. "description": "test project #2",
  358. "fullname": "test2",
  359. "id": 2,
  360. "milestones": {},
  361. "name": "test2",
  362. "namespace": None,
  363. "parent": None,
  364. "priorities": {},
  365. "tags": [],
  366. "url_path": "test2",
  367. "user": {
  368. "fullname": "PY C",
  369. "name": "pingou",
  370. "url_path": "user/pingou",
  371. },
  372. }
  373. ],
  374. }
  375. data = json.loads(output.get_data(as_text=True))
  376. data["date_created"] = "1492020239"
  377. projects = []
  378. for p in data["projects"]:
  379. p["date_created"] = "1492020239"
  380. p["date_modified"] = "1492020239"
  381. projects.append(p)
  382. data["projects"] = projects
  383. self.assertDictEqual(data, exp)
  384. def test_api_view_group_w_projects_and_acl_ticket(self):
  385. """
  386. Test the api_view_group method with project info and restricted
  387. to the ticket ACL
  388. """
  389. output = self.app.get("/api/0/group/some_group?projects=1&acl=ticket")
  390. self.assertEqual(output.status_code, 200)
  391. exp = {
  392. "display_name": "Some Group",
  393. "description": None,
  394. "creator": {
  395. "fullname": "PY C",
  396. "name": "pingou",
  397. "url_path": "user/pingou",
  398. },
  399. "members": ["pingou"],
  400. "date_created": "1492020239",
  401. "group_type": "user",
  402. "name": "some_group",
  403. "projects": [
  404. {
  405. "access_groups": {
  406. "admin": ["some_group"],
  407. "collaborator": [],
  408. "commit": [],
  409. "ticket": [],
  410. },
  411. "access_users": {
  412. "admin": [],
  413. "collaborator": [],
  414. "commit": [],
  415. "owner": ["pingou"],
  416. "ticket": [],
  417. },
  418. "close_status": [
  419. "Invalid",
  420. "Insufficient data",
  421. "Fixed",
  422. "Duplicate",
  423. ],
  424. "custom_keys": [],
  425. "date_created": "1492020239",
  426. "date_modified": "1492020239",
  427. "description": "test project #2",
  428. "fullname": "test2",
  429. "id": 2,
  430. "milestones": {},
  431. "name": "test2",
  432. "namespace": None,
  433. "parent": None,
  434. "priorities": {},
  435. "tags": [],
  436. "url_path": "test2",
  437. "user": {
  438. "fullname": "PY C",
  439. "name": "pingou",
  440. "url_path": "user/pingou",
  441. },
  442. }
  443. ],
  444. }
  445. data = json.loads(output.get_data(as_text=True))
  446. data["date_created"] = "1492020239"
  447. projects = []
  448. for p in data["projects"]:
  449. p["date_created"] = "1492020239"
  450. p["date_modified"] = "1492020239"
  451. projects.append(p)
  452. data["projects"] = projects
  453. self.assertDictEqual(data, exp)
  454. def test_api_view_group_w_projects_and_acl_admin_no_project(self):
  455. """
  456. Test the api_view_group method with project info and restricted
  457. to the admin ACL
  458. """
  459. # Make the group having only commit access
  460. project = pagure.lib.query._get_project(self.session, "test2")
  461. msg = pagure.lib.query.add_group_to_project(
  462. session=self.session,
  463. project=project,
  464. new_group="some_group",
  465. user="pingou",
  466. access="commit",
  467. )
  468. self.session.commit()
  469. self.assertEqual(msg, "Group access updated")
  470. output = self.app.get("/api/0/group/some_group?projects=1&acl=admin")
  471. self.assertEqual(output.status_code, 200)
  472. exp = {
  473. "display_name": "Some Group",
  474. "description": None,
  475. "creator": {
  476. "fullname": "PY C",
  477. "name": "pingou",
  478. "url_path": "user/pingou",
  479. },
  480. "members": ["pingou"],
  481. "date_created": "1492020239",
  482. "group_type": "user",
  483. "name": "some_group",
  484. "projects": [],
  485. }
  486. data = json.loads(output.get_data(as_text=True))
  487. data["date_created"] = "1492020239"
  488. self.assertDictEqual(data, exp)
  489. def test_api_view_group_w_projects_and_acl_commit_no_project(self):
  490. """
  491. Test the api_view_group method with project info and restricted
  492. to the commit ACL
  493. """
  494. # Make the group having only ticket access
  495. project = pagure.lib.query._get_project(self.session, "test2")
  496. msg = pagure.lib.query.add_group_to_project(
  497. session=self.session,
  498. project=project,
  499. new_group="some_group",
  500. user="pingou",
  501. access="ticket",
  502. )
  503. self.session.commit()
  504. self.assertEqual(msg, "Group access updated")
  505. output = self.app.get("/api/0/group/some_group?projects=1&acl=commit")
  506. self.assertEqual(output.status_code, 200)
  507. exp = {
  508. "display_name": "Some Group",
  509. "description": None,
  510. "creator": {
  511. "fullname": "PY C",
  512. "name": "pingou",
  513. "url_path": "user/pingou",
  514. },
  515. "members": ["pingou"],
  516. "date_created": "1492020239",
  517. "group_type": "user",
  518. "name": "some_group",
  519. "projects": [],
  520. }
  521. data = json.loads(output.get_data(as_text=True))
  522. data["date_created"] = "1492020239"
  523. self.assertDictEqual(data, exp)
  524. def test_api_view_group_w_projects_and_acl_ticket_no_project(self):
  525. """
  526. Test the api_view_group method with project info and restricted
  527. to the ticket ACL
  528. """
  529. # Create a group not linked to any project
  530. item = pagure.lib.model.PagureGroup(
  531. group_name="rel-eng",
  532. group_type="user",
  533. display_name="Release engineering group",
  534. user_id=1, # pingou
  535. )
  536. self.session.add(item)
  537. self.session.commit()
  538. output = self.app.get("/api/0/group/rel-eng?projects=1&acl=ticket")
  539. self.assertEqual(output.status_code, 200)
  540. exp = {
  541. "display_name": "Release engineering group",
  542. "description": None,
  543. "creator": {
  544. "fullname": "PY C",
  545. "name": "pingou",
  546. "url_path": "user/pingou",
  547. },
  548. "members": [],
  549. "date_created": "1492020239",
  550. "group_type": "user",
  551. "name": "rel-eng",
  552. "projects": [],
  553. }
  554. data = json.loads(output.get_data(as_text=True))
  555. data["date_created"] = "1492020239"
  556. self.assertDictEqual(data, exp)
  557. if __name__ == "__main__":
  558. unittest.main(verbosity=2)