test_bottle.py 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. 'details': {
  55. 'email_address': ['Missing data for required field.'],
  56. 'username': ['Missing data for required field.'],
  57. 'display_name': ['Missing data for required field.'],
  58. 'last_name': ['Missing data for required field.'],
  59. 'first_name': ['Missing data for required field.'],
  60. 'company': ['Missing data for required field.']},
  61. 'message': 'Validation error of input data'}
  62. user = {
  63. 'email_address': 'some.user@hapic.com',
  64. 'username': 'some_user',
  65. 'display_name': 'Damien Accorsi',
  66. 'last_name': 'Accorsi',
  67. 'first_name': 'Damien',
  68. 'company': 'Algoo',
  69. }
  70. resp = app.post_json('/users/', user)
  71. assert resp.status_int == 200
  72. assert resp.json == {
  73. 'last_name': 'Accorsi',
  74. 'username': 'some_user',
  75. 'first_name': 'Damien',
  76. 'id': 4,
  77. 'display_name': 'Damien Accorsi',
  78. 'email_address': 'some.user@hapic.com',
  79. 'company': 'Algoo',
  80. }
  81. resp = app.delete('/users/1', status='*')
  82. assert resp.status_int == 204