test_pagure_flask_api_group.py 19 KB

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