test_flask.py 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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')
  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. 'details': {
  55. 'email_address': ['Missing data for required field.'],
  56. 'username': ['Missing data for required field.'],
  57. 'display_name': ['Missing data for required field.'],
  58. 'last_name': ['Missing data for required field.'],
  59. 'first_name': ['Missing data for required field.'],
  60. 'company': ['Missing data for required field.']},
  61. 'message': 'Validation error of input data'}
  62. user = {
  63. 'email_address': 'some.user@hapic.com',
  64. 'username': 'some_user',
  65. 'display_name': 'Damien Accorsi',
  66. 'last_name': 'Accorsi',
  67. 'first_name': 'Damien',
  68. 'company': 'Algoo',
  69. }
  70. resp = app.post_json('/users/', user)
  71. assert resp.status_int == 200
  72. assert resp.json == {
  73. 'last_name': 'Accorsi',
  74. 'username': 'some_user',
  75. 'first_name': 'Damien',
  76. 'id': 4,
  77. 'display_name': 'Damien Accorsi',
  78. 'email_address': 'some.user@hapic.com',
  79. 'company': 'Algoo',
  80. }
  81. # INFO - G.M - 2017-12-07 - Warning due to Webtest check
  82. # Webtest check content_type. Up to know flask_api return json as
  83. # content_type with 204 NO CONTENT status code which return a warning in
  84. # WebTest check.
  85. resp = app.delete('/users/1', status='*')
  86. assert resp.status_int == 204