test_pagure_flask_api_group.py 20 KB

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