test_flask.py 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. assert doc == SWAGGER_DOC_API
  15. resp = app.get('/about')
  16. assert resp.status_int == 200
  17. assert resp.json == {'datetime': '2017-12-07T10:55:08.488996+00:00',
  18. 'version': '1.2.3'}
  19. resp = app.get('/users')
  20. assert resp.status_int == 200
  21. assert resp.json == {
  22. 'items':
  23. [
  24. {
  25. 'username': 'some_user',
  26. 'display_name': 'Damien Accorsi',
  27. 'company': 'Algoo', 'id': 4
  28. }
  29. ],
  30. 'pagination': {
  31. 'first_id': 0,
  32. 'last_id': 5,
  33. 'current_id': 0,
  34. },
  35. 'item_nb': 1,
  36. }
  37. resp = app.get('/users/1')
  38. assert resp.status_int == 200
  39. assert resp.json == {
  40. 'last_name': 'Accorsi',
  41. 'username': 'some_user',
  42. 'first_name': 'Damien',
  43. 'id': 4,
  44. 'display_name': 'Damien Accorsi',
  45. 'email_address': 'some.user@hapic.com',
  46. 'company': 'Algoo'
  47. }
  48. resp = app.post('/users/', status='*')
  49. assert resp.status_int == 400
  50. assert resp.json == {
  51. 'details': {
  52. 'email_address': ['Missing data for required field.'],
  53. 'username': ['Missing data for required field.'],
  54. 'display_name': ['Missing data for required field.'],
  55. 'last_name': ['Missing data for required field.'],
  56. 'first_name': ['Missing data for required field.'],
  57. 'company': ['Missing data for required field.']},
  58. 'message': 'Validation error of input data'}
  59. user = {
  60. 'email_address': 'some.user@hapic.com',
  61. 'username': 'some_user',
  62. 'display_name': 'Damien Accorsi',
  63. 'last_name': 'Accorsi',
  64. 'first_name': 'Damien',
  65. 'company': 'Algoo',
  66. }
  67. resp = app.post_json('/users/', user)
  68. assert resp.status_int == 200
  69. assert resp.json == {
  70. 'last_name': 'Accorsi',
  71. 'username': 'some_user',
  72. 'first_name': 'Damien',
  73. 'id': 4,
  74. 'display_name': 'Damien Accorsi',
  75. 'email_address': 'some.user@hapic.com',
  76. 'company': 'Algoo',
  77. }
  78. # INFO - G.M - 2017-12-07 - Warning due to Webtest check
  79. # Webtest check content_type. Up to know flask_api return json as
  80. # content_type with 204 NO CONTENT status code which return a warning in
  81. # WebTest check.
  82. resp = app.delete('/users/1', status='*')
  83. assert resp.status_int == 204