1
0

test_pagure_flask_api.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2015 - 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.data)
  33. self.assertEqual(data['version'], pagure.__api_version__)
  34. self.assertEqual(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.data)
  40. self.assertEqual(sorted(data['users']), ['foo', 'pingou'])
  41. self.assertEqual(sorted(data.keys()), ['mention', 'total_users', 'users'])
  42. self.assertEqual(data['total_users'], 2)
  43. output = self.app.get('/api/0/users?pattern=p')
  44. self.assertEqual(output.status_code, 200)
  45. data = json.loads(output.data)
  46. self.assertEqual(data['users'], ['pingou'])
  47. self.assertEqual(sorted(data.keys()), ['mention', 'total_users', 'users'])
  48. self.assertEqual(data['total_users'], 1)
  49. def test_api_project_tags(self):
  50. """ Test the api_project_tags function. """
  51. tests.create_projects(self.session)
  52. output = self.app.get('/api/0/foo/tags/')
  53. self.assertEqual(output.status_code, 404)
  54. data = json.loads(output.data)
  55. self.assertEqual(data.keys(), ['output', 'error'])
  56. self.assertEqual(data['output'], 'notok')
  57. self.assertEqual(data['error'], 'Project not found')
  58. output = self.app.get('/api/0/test/tags/')
  59. self.assertEqual(output.status_code, 200)
  60. data = json.loads(output.data)
  61. self.assertEqual(sorted(data.keys()), ['tags', 'total_tags'])
  62. self.assertEqual(data['tags'], [])
  63. self.assertEqual(data['total_tags'], 0)
  64. # Add an issue and tag it so that we can list them
  65. item = pagure.lib.model.Issue(
  66. id=1,
  67. uid='foobar',
  68. project_id=1,
  69. title='issue',
  70. content='a bug report',
  71. user_id=1, # pingou
  72. )
  73. self.session.add(item)
  74. self.session.commit()
  75. item = pagure.lib.model.Tag(
  76. tag='tag1',
  77. )
  78. self.session.add(item)
  79. self.session.commit()
  80. item = pagure.lib.model.TagIssue(
  81. tag='tag1',
  82. issue_uid='foobar',
  83. )
  84. self.session.add(item)
  85. self.session.commit()
  86. output = self.app.get('/api/0/test/tags/')
  87. self.assertEqual(output.status_code, 200)
  88. data = json.loads(output.data)
  89. self.assertEqual(sorted(data.keys()), ['tags', 'total_tags'])
  90. self.assertEqual(data['tags'], ['tag1'])
  91. self.assertEqual(data['total_tags'], 1)
  92. output = self.app.get('/api/0/test/tags/?pattern=t')
  93. self.assertEqual(output.status_code, 200)
  94. data = json.loads(output.data)
  95. self.assertEqual(sorted(data.keys()), ['tags', 'total_tags'])
  96. self.assertEqual(data['tags'], ['tag1'])
  97. self.assertEqual(data['total_tags'], 1)
  98. output = self.app.get('/api/0/test/tags/?pattern=p')
  99. self.assertEqual(output.status_code, 200)
  100. data = json.loads(output.data)
  101. self.assertEqual(sorted(data.keys()), ['tags', 'total_tags'])
  102. self.assertEqual(data['tags'], [])
  103. self.assertEqual(data['total_tags'], 0)
  104. def test_api_groups(self):
  105. """ Test the api_groups function. """
  106. # Add a couple of groups so that we can list them
  107. item = pagure.lib.model.PagureGroup(
  108. group_name='group1',
  109. group_type='user',
  110. user_id=1, # pingou
  111. )
  112. self.session.add(item)
  113. item = pagure.lib.model.PagureGroup(
  114. group_name='rel-eng',
  115. group_type='user',
  116. user_id=1, # pingou
  117. )
  118. self.session.add(item)
  119. self.session.commit()
  120. output = self.app.get('/api/0/groups')
  121. self.assertEqual(output.status_code, 200)
  122. data = json.loads(output.data)
  123. self.assertEqual(data['groups'], ['group1', 'rel-eng'])
  124. self.assertEqual(sorted(data.keys()), ['groups', 'total_groups'])
  125. self.assertEqual(data['total_groups'], 2)
  126. output = self.app.get('/api/0/groups?pattern=re')
  127. self.assertEqual(output.status_code, 200)
  128. data = json.loads(output.data)
  129. self.assertEqual(data['groups'], ['rel-eng'])
  130. self.assertEqual(sorted(data.keys()), ['groups', 'total_groups'])
  131. self.assertEqual(data['total_groups'], 1)
  132. if __name__ == '__main__':
  133. SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskApitests)
  134. unittest.TextTestRunner(verbosity=2).run(SUITE)