test_pagure_flask_api.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.flask_app
  18. import pagure.lib
  19. import tests
  20. class PagureFlaskApitests(tests.SimplePagureTest):
  21. """ Tests for flask API controller of pagure """
  22. def test_api_version(self):
  23. """ Test the api_version function. """
  24. output = self.app.get('/api/0/version')
  25. self.assertEqual(output.status_code, 200)
  26. data = json.loads(output.data)
  27. self.assertEqual(data['version'], pagure.__api_version__)
  28. self.assertEqual(data.keys(), ['version'])
  29. def test_api_project_tags(self):
  30. """ Test the api_project_tags function. """
  31. tests.create_projects(self.session)
  32. output = self.app.get('/api/0/foo/tags/')
  33. self.assertEqual(output.status_code, 404)
  34. data = json.loads(output.data)
  35. self.assertEqual(set(data.keys()), set(['output', 'error']))
  36. self.assertEqual(data['output'], 'notok')
  37. self.assertEqual(data['error'], 'Project not found')
  38. output = self.app.get('/api/0/test/tags/')
  39. self.assertEqual(output.status_code, 200)
  40. data = json.loads(output.data)
  41. self.assertEqual(sorted(data.keys()), ['tags', 'total_tags'])
  42. self.assertEqual(data['tags'], [])
  43. self.assertEqual(data['total_tags'], 0)
  44. # Add an issue and tag it so that we can list them
  45. item = pagure.lib.model.Issue(
  46. id=1,
  47. uid='foobar',
  48. project_id=1,
  49. title='issue',
  50. content='a bug report',
  51. user_id=1, # pingou
  52. )
  53. self.session.add(item)
  54. self.session.commit()
  55. item = pagure.lib.model.TagColored(
  56. tag='tag1', tag_color='DeepBlueSky', project_id=1,
  57. )
  58. self.session.add(item)
  59. self.session.commit()
  60. item = pagure.lib.model.TagIssueColored(
  61. issue_uid='foobar',
  62. tag_id=item.id
  63. )
  64. self.session.add(item)
  65. self.session.commit()
  66. output = self.app.get('/api/0/test/tags/')
  67. self.assertEqual(output.status_code, 200)
  68. data = json.loads(output.data)
  69. self.assertEqual(sorted(data.keys()), ['tags', 'total_tags'])
  70. self.assertEqual(data['tags'], ['tag1'])
  71. self.assertEqual(data['total_tags'], 1)
  72. output = self.app.get('/api/0/test/tags/?pattern=t')
  73. self.assertEqual(output.status_code, 200)
  74. data = json.loads(output.data)
  75. self.assertEqual(sorted(data.keys()), ['tags', 'total_tags'])
  76. self.assertEqual(data['tags'], ['tag1'])
  77. self.assertEqual(data['total_tags'], 1)
  78. output = self.app.get('/api/0/test/tags/?pattern=p')
  79. self.assertEqual(output.status_code, 200)
  80. data = json.loads(output.data)
  81. self.assertEqual(sorted(data.keys()), ['tags', 'total_tags'])
  82. self.assertEqual(data['tags'], [])
  83. self.assertEqual(data['total_tags'], 0)
  84. if __name__ == '__main__':
  85. unittest.main(verbosity=2)