test_flask.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from webtest import TestApp
  2. from hapic.ext.flask import FlaskContext
  3. from flask import Flask
  4. from example.fake_api.flask_api import FlaskController
  5. from example.fake_api.flask_api import hapic
  6. from tests.func.fake_api.common import SWAGGER_DOC_API
  7. def test_func_flask_fake_api():
  8. flask_app = Flask(__name__)
  9. controllers = FlaskController()
  10. controllers.bind(flask_app)
  11. hapic.set_context(FlaskContext(flask_app))
  12. app = TestApp(flask_app)
  13. doc = hapic.generate_doc(
  14. title='Fake API',
  15. description='just an example of hapic API'
  16. )
  17. assert doc == SWAGGER_DOC_API
  18. resp = app.get('/about', status='*')
  19. assert resp.status_int == 200
  20. assert resp.json == {'datetime': '2017-12-07T10:55:08.488996+00:00',
  21. 'version': '1.2.3'}
  22. resp = app.get('/users')
  23. assert resp.status_int == 200
  24. assert resp.json == {
  25. 'items':
  26. [
  27. {
  28. 'username': 'some_user',
  29. 'display_name': 'Damien Accorsi',
  30. 'company': 'Algoo', 'id': 4
  31. }
  32. ],
  33. 'pagination': {
  34. 'first_id': 0,
  35. 'last_id': 5,
  36. 'current_id': 0,
  37. },
  38. 'item_nb': 1,
  39. }
  40. resp = app.get('/users/1')
  41. assert resp.status_int == 200
  42. assert resp.json == {
  43. 'last_name': 'Accorsi',
  44. 'username': 'some_user',
  45. 'first_name': 'Damien',
  46. 'id': 4,
  47. 'display_name': 'Damien Accorsi',
  48. 'email_address': 'some.user@hapic.com',
  49. 'company': 'Algoo'
  50. }
  51. resp = app.post('/users/', status='*')
  52. assert resp.status_int == 400
  53. assert resp.json == {
  54. 'code': None,
  55. 'details': {
  56. 'email_address': ['Missing data for required field.'],
  57. 'username': ['Missing data for required field.'],
  58. 'display_name': ['Missing data for required field.'],
  59. 'last_name': ['Missing data for required field.'],
  60. 'first_name': ['Missing data for required field.'],
  61. 'company': ['Missing data for required field.']},
  62. 'message': 'Validation error of input data'}
  63. user = {
  64. 'email_address': 'some.user@hapic.com',
  65. 'username': 'some_user',
  66. 'display_name': 'Damien Accorsi',
  67. 'last_name': 'Accorsi',
  68. 'first_name': 'Damien',
  69. 'company': 'Algoo',
  70. }
  71. resp = app.post_json('/users/', user)
  72. assert resp.status_int == 200
  73. assert resp.json == {
  74. 'last_name': 'Accorsi',
  75. 'username': 'some_user',
  76. 'first_name': 'Damien',
  77. 'id': 4,
  78. 'display_name': 'Damien Accorsi',
  79. 'email_address': 'some.user@hapic.com',
  80. 'company': 'Algoo',
  81. }
  82. # INFO - G.M - 2017-12-07 - Warning due to Webtest check
  83. # Webtest check content_type. Up to know flask_api return json as
  84. # content_type with 204 NO CONTENT status code which return a warning in
  85. # WebTest check.
  86. resp = app.delete('/users/1', status='*')
  87. assert resp.status_int == 204