test_pyramid.py 3.1KB

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