test_fake_api.py 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import pytest
  2. from flask import Flask
  3. from pyramid.config import Configurator
  4. from webtest import TestApp
  5. from bottle import Bottle
  6. from example.fake_api.flask_api import FlaskController
  7. from example.fake_api.pyramid_api import PyramidController
  8. from hapic.ext.bottle import BottleContext
  9. from example.fake_api.bottle_api import BottleController
  10. from hapic.ext.flask import FlaskContext
  11. from hapic.ext.pyramid import PyramidContext
  12. from tests.func.fake_api.common import SWAGGER_DOC_API
  13. def get_bottle_context():
  14. from example.fake_api.bottle_api import hapic as h
  15. bottle_app = Bottle()
  16. h.reset_context()
  17. h.set_context(BottleContext(bottle_app))
  18. controllers = BottleController()
  19. controllers.bind(bottle_app)
  20. return {
  21. 'hapic': h,
  22. 'app': bottle_app,
  23. }
  24. def get_flask_context():
  25. from example.fake_api.flask_api import hapic as h
  26. flask_app = Flask(__name__)
  27. controllers = FlaskController()
  28. controllers.bind(flask_app)
  29. h.reset_context()
  30. h.set_context(FlaskContext(flask_app))
  31. return {
  32. 'hapic': h,
  33. 'app': flask_app,
  34. }
  35. def get_pyramid_context():
  36. from example.fake_api.pyramid_api import hapic as h
  37. configurator = Configurator(autocommit=True)
  38. controllers = PyramidController()
  39. controllers.bind(configurator)
  40. h.reset_context()
  41. h.set_context(PyramidContext(configurator))
  42. pyramid_app = configurator.make_wsgi_app()
  43. return {
  44. 'hapic': h,
  45. 'app': pyramid_app,
  46. }
  47. @pytest.mark.parametrize('context',
  48. [
  49. get_bottle_context(),
  50. get_flask_context(),
  51. get_pyramid_context()
  52. ])
  53. def test_func__test_fake_api_endpoints_ok__all_framework(context):
  54. hapic = context['hapic']
  55. app = context['app']
  56. app = TestApp(app)
  57. resp = app.get('/about')
  58. assert resp.status_int == 200
  59. assert resp.json == {'datetime': '2017-12-07T10:55:08.488996+00:00',
  60. 'version': '1.2.3'}
  61. resp = app.get('/users')
  62. assert resp.status_int == 200
  63. assert resp.json == {
  64. 'items':
  65. [
  66. {
  67. 'username': 'some_user',
  68. 'display_name': 'Damien Accorsi',
  69. 'company': 'Algoo', 'id': 4
  70. }
  71. ],
  72. 'pagination': {
  73. 'first_id': 0,
  74. 'last_id': 5,
  75. 'current_id': 0,
  76. },
  77. 'item_nb': 1,
  78. }
  79. resp = app.get('/users2')
  80. assert resp.status_int == 200
  81. assert resp.json == [
  82. {
  83. 'username': 'some_user',
  84. 'id': 4,
  85. 'display_name': 'Damien Accorsi',
  86. 'company': 'Algoo'
  87. }
  88. ]
  89. resp = app.get('/users/1')
  90. assert resp.status_int == 200
  91. assert resp.json == {
  92. 'last_name': 'Accorsi',
  93. 'username': 'some_user',
  94. 'first_name': 'Damien',
  95. 'id': 4,
  96. 'display_name': 'Damien Accorsi',
  97. 'email_address': 'some.user@hapic.com',
  98. 'company': 'Algoo'
  99. }
  100. resp = app.post('/users/', status='*')
  101. assert resp.status_int == 400
  102. assert resp.json == {
  103. 'code': None,
  104. 'details': {
  105. 'email_address': ['Missing data for required field.'],
  106. 'username': ['Missing data for required field.'],
  107. 'display_name': ['Missing data for required field.'],
  108. 'last_name': ['Missing data for required field.'],
  109. 'first_name': ['Missing data for required field.'],
  110. 'company': ['Missing data for required field.']},
  111. 'message': 'Validation error of input data'}
  112. user = {
  113. 'email_address': 'some.user@hapic.com',
  114. 'username': 'some_user',
  115. 'display_name': 'Damien Accorsi',
  116. 'last_name': 'Accorsi',
  117. 'first_name': 'Damien',
  118. 'company': 'Algoo',
  119. }
  120. resp = app.post_json('/users/', user)
  121. assert resp.status_int == 200
  122. assert resp.json == {
  123. 'last_name': 'Accorsi',
  124. 'username': 'some_user',
  125. 'first_name': 'Damien',
  126. 'id': 4,
  127. 'display_name': 'Damien Accorsi',
  128. 'email_address': 'some.user@hapic.com',
  129. 'company': 'Algoo',
  130. }
  131. resp = app.delete('/users/1', status='*')
  132. assert resp.status_int == 204
  133. @pytest.mark.parametrize('context',
  134. [
  135. get_bottle_context(),
  136. get_flask_context(),
  137. get_pyramid_context()
  138. ])
  139. def test_func__test_fake_api_doc_ok__all_framework(context):
  140. hapic = context['hapic']
  141. app = context['app']
  142. app = TestApp(app)
  143. doc = hapic.generate_doc(
  144. title='Fake API',
  145. description='just an example of hapic API'
  146. )
  147. assert doc['info'] == SWAGGER_DOC_API['info']
  148. assert doc['tags'] == SWAGGER_DOC_API['tags']
  149. assert doc['swagger'] == SWAGGER_DOC_API['swagger']
  150. assert doc['parameters'] == SWAGGER_DOC_API['parameters']
  151. assert doc['paths']['/about'] == SWAGGER_DOC_API['paths']['/about']
  152. assert doc['paths']['/users'] == SWAGGER_DOC_API['paths']['/users']
  153. assert doc['paths']['/users/{id}'] == SWAGGER_DOC_API['paths']['/users/{id}']
  154. assert doc['paths']['/users/'] == SWAGGER_DOC_API['paths']['/users/']
  155. assert doc['paths']['/users2'] == SWAGGER_DOC_API['paths']['/users2']
  156. assert doc['definitions']['AboutResponseSchema'] == \
  157. SWAGGER_DOC_API['definitions']['AboutResponseSchema']
  158. assert doc['definitions']['ListsUserSchema'] == \
  159. SWAGGER_DOC_API['definitions']['ListsUserSchema']
  160. assert doc['definitions']['NoContentSchema'] == \
  161. SWAGGER_DOC_API['definitions']['NoContentSchema']
  162. assert doc['definitions']['PaginationSchema'] == \
  163. SWAGGER_DOC_API['definitions']['PaginationSchema']
  164. assert doc['definitions']['UserSchema'] == \
  165. SWAGGER_DOC_API['definitions']['UserSchema']
  166. assert doc['definitions']['UserSchema'] == \
  167. SWAGGER_DOC_API['definitions']['UserSchema']
  168. assert doc['definitions']['UserSchema_without_id'] == \
  169. SWAGGER_DOC_API['definitions']['UserSchema_without_id']
  170. assert doc['definitions']['UserSchema_without_email_address_first_name_last_name'] == \
  171. SWAGGER_DOC_API['definitions']['UserSchema_without_email_address_first_name_last_name']