test_pagure_flask_api.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015-2016 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. __requires__ = ['SQLAlchemy >= 0.8']
  8. import pkg_resources
  9. import unittest
  10. import shutil
  11. import sys
  12. import os
  13. import json
  14. from mock import patch
  15. sys.path.insert(0, os.path.join(os.path.dirname(
  16. os.path.abspath(__file__)), '..'))
  17. import pagure.lib
  18. import tests
  19. class PagureFlaskApitests(tests.Modeltests):
  20. """ Tests for flask API controller of pagure """
  21. def setUp(self):
  22. """ Set up the environnment, ran before every tests. """
  23. super(PagureFlaskApitests, self).setUp()
  24. pagure.APP.config['TESTING'] = True
  25. pagure.SESSION = self.session
  26. pagure.api.SESSION = self.session
  27. self.app = pagure.APP.test_client()
  28. def test_api_version(self):
  29. """ Test the api_version function. """
  30. output = self.app.get('/api/0/version')
  31. self.assertEqual(output.status_code, 200)
  32. data = json.loads(output.get_data(as_text=True))
  33. self.assertEqual(data['version'], pagure.__api_version__)
  34. self.assertEqual(list(data.keys()), ['version'])
  35. def test_api_users(self):
  36. """ Test the api_users function. """
  37. output = self.app.get('/api/0/users')
  38. self.assertEqual(output.status_code, 200)
  39. data = json.loads(output.get_data(as_text=True))
  40. self.assertEqual(sorted(data['users']), ['foo', 'pingou'])
  41. self.assertEqual(
  42. sorted(list(data.keys())),
  43. ['mention', 'total_users', 'users'])
  44. self.assertEqual(data['total_users'], 2)
  45. output = self.app.get('/api/0/users?pattern=p')
  46. self.assertEqual(output.status_code, 200)
  47. data = json.loads(output.get_data(as_text=True))
  48. self.assertEqual(data['users'], ['pingou'])
  49. self.assertEqual(
  50. sorted(list(data.keys())),
  51. ['mention', 'total_users', 'users'])
  52. self.assertEqual(data['total_users'], 1)
  53. def test_api_project_tags(self):
  54. """ Test the api_project_tags function. """
  55. tests.create_projects(self.session)
  56. output = self.app.get('/api/0/foo/tags/')
  57. self.assertEqual(output.status_code, 404)
  58. data = json.loads(output.get_data(as_text=True))
  59. self.assertEqual(sorted(list(data.keys())), ['error', 'output'])
  60. self.assertEqual(data['output'], 'notok')
  61. self.assertEqual(data['error'], 'Project not found')
  62. output = self.app.get('/api/0/test/tags/')
  63. self.assertEqual(output.status_code, 200)
  64. data = json.loads(output.get_data(as_text=True))
  65. self.assertEqual(sorted(list(data.keys())), ['tags', 'total_tags'])
  66. self.assertEqual(data['tags'], [])
  67. self.assertEqual(data['total_tags'], 0)
  68. # Add an issue and tag it so that we can list them
  69. item = pagure.lib.model.Issue(
  70. id=1,
  71. uid='foobar',
  72. project_id=1,
  73. title='issue',
  74. content='a bug report',
  75. user_id=1, # pingou
  76. )
  77. self.session.add(item)
  78. self.session.commit()
  79. item = pagure.lib.model.Tag(
  80. tag='tag1',
  81. )
  82. self.session.add(item)
  83. self.session.commit()
  84. item = pagure.lib.model.TagIssue(
  85. tag='tag1',
  86. issue_uid='foobar',
  87. )
  88. self.session.add(item)
  89. self.session.commit()
  90. output = self.app.get('/api/0/test/tags/')
  91. self.assertEqual(output.status_code, 200)
  92. data = json.loads(output.get_data(as_text=True))
  93. self.assertEqual(sorted(list(data.keys())), ['tags', 'total_tags'])
  94. self.assertEqual(data['tags'], ['tag1'])
  95. self.assertEqual(data['total_tags'], 1)
  96. output = self.app.get('/api/0/test/tags/?pattern=t')
  97. self.assertEqual(output.status_code, 200)
  98. data = json.loads(output.get_data(as_text=True))
  99. self.assertEqual(sorted(list(data.keys())), ['tags', 'total_tags'])
  100. self.assertEqual(data['tags'], ['tag1'])
  101. self.assertEqual(data['total_tags'], 1)
  102. output = self.app.get('/api/0/test/tags/?pattern=p')
  103. self.assertEqual(output.status_code, 200)
  104. data = json.loads(output.get_data(as_text=True))
  105. self.assertEqual(sorted(list(data.keys())), ['tags', 'total_tags'])
  106. self.assertEqual(data['tags'], [])
  107. self.assertEqual(data['total_tags'], 0)
  108. def test_api_groups(self):
  109. """ Test the api_groups function. """
  110. # Add a couple of groups so that we can list them
  111. item = pagure.lib.model.PagureGroup(
  112. group_name='group1',
  113. group_type='user',
  114. display_name='User group',
  115. user_id=1, # pingou
  116. )
  117. self.session.add(item)
  118. item = pagure.lib.model.PagureGroup(
  119. group_name='rel-eng',
  120. group_type='user',
  121. display_name='Release engineering group',
  122. user_id=1, # pingou
  123. )
  124. self.session.add(item)
  125. self.session.commit()
  126. output = self.app.get('/api/0/groups')
  127. self.assertEqual(output.status_code, 200)
  128. data = json.loads(output.get_data(as_text=True))
  129. self.assertEqual(data['groups'], ['group1', 'rel-eng'])
  130. self.assertEqual(sorted(list(data.keys())), ['groups', 'total_groups'])
  131. self.assertEqual(data['total_groups'], 2)
  132. output = self.app.get('/api/0/groups?pattern=re')
  133. self.assertEqual(output.status_code, 200)
  134. data = json.loads(output.get_data(as_text=True))
  135. self.assertEqual(data['groups'], ['rel-eng'])
  136. self.assertEqual(sorted(list(data.keys())), ['groups', 'total_groups'])
  137. self.assertEqual(data['total_groups'], 1)
  138. if __name__ == '__main__':
  139. SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskApitests)
  140. unittest.TextTestRunner(verbosity=2).run(SUITE)