1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- from webtest import TestApp
- from hapic.ext.flask import FlaskContext
- from flask import Flask
- from example.fake_api.flask_api import FlaskController
- from example.fake_api.flask_api import hapic
- from tests.func.fake_api.common import SWAGGER_DOC_API
-
-
- def test_func_flask_fake_api():
- flask_app = Flask(__name__)
- controllers = FlaskController()
- controllers.bind(flask_app)
- hapic.set_context(FlaskContext(flask_app))
- app = TestApp(flask_app)
- doc = hapic.generate_doc()
-
- assert doc == SWAGGER_DOC_API
- resp = app.get('/about')
- assert resp.status_int == 200
- assert resp.json == {'datetime': '2017-12-07T10:55:08.488996+00:00',
- 'version': '1.2.3'}
-
- resp = app.get('/users')
- assert resp.status_int == 200
- assert resp.json == {
- 'items':
- [
- {
- 'username': 'some_user',
- 'display_name': 'Damien Accorsi',
- 'company': 'Algoo', 'id': 4
- }
- ],
- 'pagination': {
- 'first_id': 0,
- 'last_id': 5,
- 'current_id': 0,
- },
- 'item_nb': 1,
- }
-
- resp = app.get('/users/1')
- assert resp.status_int == 200
- assert resp.json == {
- 'last_name': 'Accorsi',
- 'username': 'some_user',
- 'first_name': 'Damien',
- 'id': 4,
- 'display_name': 'Damien Accorsi',
- 'email_address': 'some.user@hapic.com',
- 'company': 'Algoo'
- }
-
- resp = app.post('/users/', status='*')
- assert resp.status_int == 400
- assert resp.json == {
- 'details': {
- 'email_address': ['Missing data for required field.'],
- 'username': ['Missing data for required field.'],
- 'display_name': ['Missing data for required field.'],
- 'last_name': ['Missing data for required field.'],
- 'first_name': ['Missing data for required field.'],
- 'company': ['Missing data for required field.']},
- 'message': 'Validation error of input data'}
-
- user = {
- 'email_address': 'some.user@hapic.com',
- 'username': 'some_user',
- 'display_name': 'Damien Accorsi',
- 'last_name': 'Accorsi',
- 'first_name': 'Damien',
- 'company': 'Algoo',
- }
-
- resp = app.post_json('/users/', user)
- assert resp.status_int == 200
- assert resp.json == {
- 'last_name': 'Accorsi',
- 'username': 'some_user',
- 'first_name': 'Damien',
- 'id': 4,
- 'display_name': 'Damien Accorsi',
- 'email_address': 'some.user@hapic.com',
- 'company': 'Algoo',
- }
- # INFO - G.M - 2017-12-07 - Warning due to Webtest check
- # Webtest check content_type. Up to know flask_api return json as
- # content_type with 204 NO CONTENT status code which return a warning in
- # WebTest check.
- resp = app.delete('/users/1', status='*')
- assert resp.status_int == 204
|