test_pagure_flask_api_group.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. "commit": [],
  261. "ticket": [],
  262. },
  263. "access_users": {
  264. "admin": [],
  265. "commit": [],
  266. "owner": ["pingou"],
  267. "ticket": [],
  268. },
  269. "close_status": [
  270. "Invalid",
  271. "Insufficient data",
  272. "Fixed",
  273. "Duplicate",
  274. ],
  275. "custom_keys": [],
  276. "date_created": "1492020239",
  277. "date_modified": "1492020239",
  278. "description": "test project #2",
  279. "fullname": "test2",
  280. "id": 2,
  281. "milestones": {},
  282. "name": "test2",
  283. "namespace": None,
  284. "parent": None,
  285. "priorities": {},
  286. "tags": [],
  287. "url_path": "test2",
  288. "user": {
  289. "fullname": "PY C",
  290. "name": "pingou",
  291. "url_path": "user/pingou",
  292. },
  293. }
  294. ],
  295. }
  296. data = json.loads(output.get_data(as_text=True))
  297. data["date_created"] = "1492020239"
  298. projects = []
  299. for p in data["projects"]:
  300. p["date_created"] = "1492020239"
  301. p["date_modified"] = "1492020239"
  302. projects.append(p)
  303. data["projects"] = projects
  304. self.assertDictEqual(data, exp)
  305. output2 = self.app.get(
  306. "/api/0/group/some_group?projects=1&acl=admin", headers=headers
  307. )
  308. self.assertListEqual(
  309. output.get_data(as_text=True).split("\n"),
  310. output2.get_data(as_text=True).split("\n"),
  311. )
  312. def test_api_view_group_w_projects_and_acl_commit(self):
  313. """
  314. Test the api_view_group method with project info and restricted
  315. to the commit ACL
  316. """
  317. output = self.app.get("/api/0/group/some_group?projects=1&acl=commit")
  318. self.assertEqual(output.status_code, 200)
  319. exp = {
  320. "display_name": "Some Group",
  321. "description": None,
  322. "creator": {
  323. "fullname": "PY C",
  324. "name": "pingou",
  325. "url_path": "user/pingou",
  326. },
  327. "members": ["pingou"],
  328. "date_created": "1492020239",
  329. "group_type": "user",
  330. "name": "some_group",
  331. "projects": [
  332. {
  333. "access_groups": {
  334. "admin": ["some_group"],
  335. "commit": [],
  336. "ticket": [],
  337. },
  338. "access_users": {
  339. "admin": [],
  340. "commit": [],
  341. "owner": ["pingou"],
  342. "ticket": [],
  343. },
  344. "close_status": [
  345. "Invalid",
  346. "Insufficient data",
  347. "Fixed",
  348. "Duplicate",
  349. ],
  350. "custom_keys": [],
  351. "date_created": "1492020239",
  352. "date_modified": "1492020239",
  353. "description": "test project #2",
  354. "fullname": "test2",
  355. "id": 2,
  356. "milestones": {},
  357. "name": "test2",
  358. "namespace": None,
  359. "parent": None,
  360. "priorities": {},
  361. "tags": [],
  362. "url_path": "test2",
  363. "user": {
  364. "fullname": "PY C",
  365. "name": "pingou",
  366. "url_path": "user/pingou",
  367. },
  368. }
  369. ],
  370. }
  371. data = json.loads(output.get_data(as_text=True))
  372. data["date_created"] = "1492020239"
  373. projects = []
  374. for p in data["projects"]:
  375. p["date_created"] = "1492020239"
  376. p["date_modified"] = "1492020239"
  377. projects.append(p)
  378. data["projects"] = projects
  379. self.assertDictEqual(data, exp)
  380. def test_api_view_group_w_projects_and_acl_ticket(self):
  381. """
  382. Test the api_view_group method with project info and restricted
  383. to the ticket ACL
  384. """
  385. output = self.app.get("/api/0/group/some_group?projects=1&acl=ticket")
  386. self.assertEqual(output.status_code, 200)
  387. exp = {
  388. "display_name": "Some Group",
  389. "description": None,
  390. "creator": {
  391. "fullname": "PY C",
  392. "name": "pingou",
  393. "url_path": "user/pingou",
  394. },
  395. "members": ["pingou"],
  396. "date_created": "1492020239",
  397. "group_type": "user",
  398. "name": "some_group",
  399. "projects": [
  400. {
  401. "access_groups": {
  402. "admin": ["some_group"],
  403. "commit": [],
  404. "ticket": [],
  405. },
  406. "access_users": {
  407. "admin": [],
  408. "commit": [],
  409. "owner": ["pingou"],
  410. "ticket": [],
  411. },
  412. "close_status": [
  413. "Invalid",
  414. "Insufficient data",
  415. "Fixed",
  416. "Duplicate",
  417. ],
  418. "custom_keys": [],
  419. "date_created": "1492020239",
  420. "date_modified": "1492020239",
  421. "description": "test project #2",
  422. "fullname": "test2",
  423. "id": 2,
  424. "milestones": {},
  425. "name": "test2",
  426. "namespace": None,
  427. "parent": None,
  428. "priorities": {},
  429. "tags": [],
  430. "url_path": "test2",
  431. "user": {
  432. "fullname": "PY C",
  433. "name": "pingou",
  434. "url_path": "user/pingou",
  435. },
  436. }
  437. ],
  438. }
  439. data = json.loads(output.get_data(as_text=True))
  440. data["date_created"] = "1492020239"
  441. projects = []
  442. for p in data["projects"]:
  443. p["date_created"] = "1492020239"
  444. p["date_modified"] = "1492020239"
  445. projects.append(p)
  446. data["projects"] = projects
  447. self.assertDictEqual(data, exp)
  448. def test_api_view_group_w_projects_and_acl_admin_no_project(self):
  449. """
  450. Test the api_view_group method with project info and restricted
  451. to the admin ACL
  452. """
  453. # Make the group having only commit access
  454. project = pagure.lib.query._get_project(self.session, "test2")
  455. msg = pagure.lib.query.add_group_to_project(
  456. session=self.session,
  457. project=project,
  458. new_group="some_group",
  459. user="pingou",
  460. access="commit",
  461. )
  462. self.session.commit()
  463. self.assertEqual(msg, "Group access updated")
  464. output = self.app.get("/api/0/group/some_group?projects=1&acl=admin")
  465. self.assertEqual(output.status_code, 200)
  466. exp = {
  467. "display_name": "Some Group",
  468. "description": None,
  469. "creator": {
  470. "fullname": "PY C",
  471. "name": "pingou",
  472. "url_path": "user/pingou",
  473. },
  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_commit_no_project(self):
  484. """
  485. Test the api_view_group method with project info and restricted
  486. to the commit ACL
  487. """
  488. # Make the group having only ticket access
  489. project = pagure.lib.query._get_project(self.session, "test2")
  490. msg = pagure.lib.query.add_group_to_project(
  491. session=self.session,
  492. project=project,
  493. new_group="some_group",
  494. user="pingou",
  495. access="ticket",
  496. )
  497. self.session.commit()
  498. self.assertEqual(msg, "Group access updated")
  499. output = self.app.get("/api/0/group/some_group?projects=1&acl=commit")
  500. self.assertEqual(output.status_code, 200)
  501. exp = {
  502. "display_name": "Some Group",
  503. "description": None,
  504. "creator": {
  505. "fullname": "PY C",
  506. "name": "pingou",
  507. "url_path": "user/pingou",
  508. },
  509. "members": ["pingou"],
  510. "date_created": "1492020239",
  511. "group_type": "user",
  512. "name": "some_group",
  513. "projects": [],
  514. }
  515. data = json.loads(output.get_data(as_text=True))
  516. data["date_created"] = "1492020239"
  517. self.assertDictEqual(data, exp)
  518. def test_api_view_group_w_projects_and_acl_ticket_no_project(self):
  519. """
  520. Test the api_view_group method with project info and restricted
  521. to the ticket ACL
  522. """
  523. # Create a group not linked to any project
  524. item = pagure.lib.model.PagureGroup(
  525. group_name="rel-eng",
  526. group_type="user",
  527. display_name="Release engineering group",
  528. user_id=1, # pingou
  529. )
  530. self.session.add(item)
  531. self.session.commit()
  532. output = self.app.get("/api/0/group/rel-eng?projects=1&acl=ticket")
  533. self.assertEqual(output.status_code, 200)
  534. exp = {
  535. "display_name": "Release engineering group",
  536. "description": None,
  537. "creator": {
  538. "fullname": "PY C",
  539. "name": "pingou",
  540. "url_path": "user/pingou",
  541. },
  542. "members": [],
  543. "date_created": "1492020239",
  544. "group_type": "user",
  545. "name": "rel-eng",
  546. "projects": [],
  547. }
  548. data = json.loads(output.get_data(as_text=True))
  549. data["date_created"] = "1492020239"
  550. self.assertDictEqual(data, exp)
  551. if __name__ == "__main__":
  552. unittest.main(verbosity=2)