test_bottle.py 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from webtest import TestApp
  2. from bottle import Bottle
  3. from hapic.ext.bottle import BottleContext
  4. from example.fake_api.bottle_api import BottleController
  5. from tests.func.fake_api.common import SWAGGER_DOC_API
  6. from example.fake_api.bottle_api import hapic
  7. def test_func_bottle_fake_api():
  8. bottle_app = Bottle()
  9. controllers = BottleController()
  10. controllers.bind(bottle_app)
  11. hapic.set_context(BottleContext(bottle_app))
  12. app = TestApp(bottle_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. 'code': None,
  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. resp = app.delete('/users/1', status='*')
  83. assert resp.status_int == 204