test_pagure_flask_api_group.py 20 KB

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