test_pyramid.py 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from webtest import TestApp
  2. from pyramid.config import Configurator
  3. import hapic
  4. from hapic.ext.pyramid import PyramidContext
  5. from example.fake_api.pyramid_api import hapic
  6. from example.fake_api.pyramid_api import PyramidController
  7. from tests.func.fake_api.common import SWAGGER_DOC_API
  8. def test_func_pyramid_fake_api_doc():
  9. configurator = Configurator(autocommit=True)
  10. controllers = PyramidController()
  11. controllers.bind(configurator)
  12. hapic.set_context(PyramidContext(configurator))
  13. app = TestApp(configurator.make_wsgi_app())
  14. doc = hapic.generate_doc(
  15. title='Fake API',
  16. description='just an example of hapic API'
  17. )
  18. assert doc == SWAGGER_DOC_API
  19. resp = app.get('/about')
  20. assert resp.status_int == 200
  21. assert resp.json == {'datetime': '2017-12-07T10:55:08.488996+00:00',
  22. 'version': '1.2.3'}
  23. resp = app.get('/users')
  24. assert resp.status_int == 200
  25. assert resp.json == {
  26. 'items':
  27. [
  28. {
  29. 'username': 'some_user',
  30. 'display_name': 'Damien Accorsi',
  31. 'company': 'Algoo', 'id': 4
  32. }
  33. ],
  34. 'pagination': {
  35. 'first_id': 0,
  36. 'last_id': 5,
  37. 'current_id': 0,
  38. },
  39. 'item_nb': 1,
  40. }
  41. resp = app.get('/users/1')
  42. assert resp.status_int == 200
  43. assert resp.json == {
  44. 'last_name': 'Accorsi',
  45. 'username': 'some_user',
  46. 'first_name': 'Damien',
  47. 'id': 4,
  48. 'display_name': 'Damien Accorsi',
  49. 'email_address': 'some.user@hapic.com',
  50. 'company': 'Algoo'
  51. }
  52. resp = app.post('/users/', status='*')
  53. assert resp.status_int == 400
  54. assert resp.json == {
  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 - Test for delete desactivated(Webtest check fail)
  83. # Webtest check content_type. Up to know pyramid_api return json as
  84. # content_type with 204 NO CONTENT status code which return an error in
  85. # WebTest check.
  86. # resp = app.delete('/users/1', status='*')
  87. # assert resp.status_int == 204