test_pyramid.py 3.0KB

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