Browse Source

Add functional test for fake_api example

Guénaël Muller 6 years ago
parent
commit
28f34cf14a

+ 0 - 0
tests/func/fake_api/__init__.py View File


+ 2 - 0
tests/func/fake_api/common.py View File

@@ -0,0 +1,2 @@
1
+from collections import OrderedDict
2
+SWAGGER_DOC_API = {'paths': OrderedDict([('/about', {'get': {'description': 'General information about this API.', 'responses': {200: {'schema': {'$ref': '#/definitions/AboutResponseSchema'}, 'description': 'HTTPStatus.OK'}}}}), ('/users', {'get': {'description': 'Obtain users list.', 'responses': {200: {'schema': {'$ref': '#/definitions/ListsUserSchema'}, 'description': 'HTTPStatus.OK'}}}}), ('/users/{id}', {'get': {'description': 'Obtain one user', 'responses': {200: {'schema': {'$ref': '#/definitions/UserSchema'}, 'description': 'HTTPStatus.OK'}}, 'parameters': [{'in': 'path', 'name': 'id', 'required': True, 'type': 'integer'}]}, 'delete': {'description': 'delete user', 'responses': {204: {'schema': {'$ref': '#/definitions/NoContentSchema'}, 'description': '204'}}, 'parameters': [{'in': 'path', 'name': 'id', 'required': True, 'type': 'integer'}]}}), ('/users/', {'post': {'description': 'Add new user', 'responses': {200: {'schema': {'$ref': '#/definitions/UserSchema'}, 'description': 'HTTPStatus.OK'}}, 'parameters': [{'in': 'body', 'name': 'body', 'schema': {'$ref': '#/definitions/UserSchema'}}]}})]), 'definitions': {'ListsUserSchema': {'properties': {'pagination': {'$ref': '#/definitions/PaginationSchema'}, 'items': {'type': 'array', 'items': {'$ref': '#/definitions/UserSchema'}}, 'item_nb': {'minimum': 0, 'type': 'integer', 'format': 'int32'}}, 'type': 'object', 'required': ['item_nb']}, 'AboutResponseSchema': {'properties': {'version': {'type': 'string'}, 'datetime': {'type': 'string', 'format': 'date-time'}}, 'type': 'object', 'required': ['datetime', 'version']}, 'NoContentSchema': {'properties': {}, 'type': 'object'}, 'UserSchema': {'properties': {'display_name': {'type': 'string'}, 'first_name': {'type': 'string'}, 'id': {'type': 'integer', 'format': 'int32'}, 'last_name': {'type': 'string'}, 'company': {'type': 'string'}, 'username': {'type': 'string'}, 'email_address': {'type': 'string', 'format': 'email'}}, 'type': 'object', 'required': ['company', 'display_name', 'email_address', 'first_name', 'id', 'last_name', 'username']}, 'PaginationSchema': {'properties': {'last_id': {'type': 'integer', 'format': 'int32'}, 'current_id': {'type': 'integer', 'format': 'int32'}, 'first_id': {'type': 'integer', 'format': 'int32'}}, 'type': 'object', 'required': ['current_id', 'first_id', 'last_id']}}, 'info': {'title': 'Swagger Petstore', 'version': '1.0.0'}, 'tags': [], 'swagger': '2.0', 'parameters': {}}

+ 91 - 0
tests/func/fake_api/test_bottle.py View File

@@ -0,0 +1,91 @@
1
+from webtest import TestApp
2
+from bottle import Bottle
3
+
4
+from hapic.ext.bottle import BottleContext
5
+from example.fake_api.bottle_api import BottleController
6
+from tests.func.fake_api.common import SWAGGER_DOC_API
7
+from example.fake_api.bottle_api import hapic
8
+
9
+
10
+def test_func_bottle_fake_api():
11
+    bottle_app = Bottle()
12
+    controllers = BottleController()
13
+    controllers.bind(bottle_app)
14
+
15
+    hapic.set_context(BottleContext(bottle_app))
16
+    app = TestApp(bottle_app)
17
+    doc = hapic.generate_doc()
18
+
19
+    assert doc == SWAGGER_DOC_API
20
+    resp = app.get('/about')
21
+    assert resp.status_int == 200
22
+    assert resp.json == {'datetime': '2017-12-07T10:55:08.488996+00:00',
23
+                         'version': '1.2.3'}
24
+
25
+    resp = app.get('/users')
26
+    assert resp.status_int == 200
27
+    assert resp.json == {
28
+        'items':
29
+        [
30
+            {
31
+                'username': 'some_user',
32
+                'display_name': 'Damien Accorsi',
33
+                'company': 'Algoo', 'id': 4
34
+            }
35
+        ],
36
+        'pagination': {
37
+            'first_id': 0,
38
+            'last_id': 5,
39
+            'current_id': 0,
40
+        },
41
+        'item_nb': 1,
42
+    }
43
+
44
+    resp = app.get('/users/1')
45
+    assert resp.status_int == 200
46
+    assert resp.json == {
47
+        'last_name': 'Accorsi',
48
+        'username': 'some_user',
49
+        'first_name': 'Damien',
50
+        'id': 4,
51
+        'display_name': 'Damien Accorsi',
52
+        'email_address': 'some.user@hapic.com',
53
+        'company': 'Algoo'
54
+    }
55
+
56
+    resp = app.post('/users/', status='*')
57
+    assert resp.status_int == 400
58
+    assert resp.json == {
59
+        'details': {
60
+            'email_address': ['Missing data for required field.'],
61
+            'username': ['Missing data for required field.'],
62
+            'display_name': ['Missing data for required field.'],
63
+            'last_name': ['Missing data for required field.'],
64
+            'first_name': ['Missing data for required field.'],
65
+            'company': ['Missing data for required field.']},
66
+        'message': 'Validation error of input data'}
67
+
68
+    user = {
69
+        'email_address': 'some.user@hapic.com',
70
+        'username': 'some_user',
71
+        'display_name': 'Damien Accorsi',
72
+        'last_name': 'Accorsi',
73
+        'first_name': 'Damien',
74
+        'company': 'Algoo',
75
+    }
76
+
77
+    resp = app.post_json('/users/', user)
78
+    assert resp.status_int == 200
79
+    assert resp.json == {
80
+        'last_name': 'Accorsi',
81
+        'username': 'some_user',
82
+        'first_name': 'Damien',
83
+        'id': 4,
84
+        'display_name': 'Damien Accorsi',
85
+        'email_address': 'some.user@hapic.com',
86
+        'company': 'Algoo',
87
+    }
88
+
89
+    resp = app.delete('/users/1', status='*')
90
+    assert resp.status_int == 204
91
+

+ 91 - 0
tests/func/fake_api/test_flask.py View File

@@ -0,0 +1,91 @@
1
+from webtest import TestApp
2
+from hapic.ext.flask import FlaskContext
3
+from flask import Flask
4
+from example.fake_api.flask_api import FlaskController
5
+from example.fake_api.flask_api import hapic
6
+from tests.func.fake_api.common import SWAGGER_DOC_API
7
+
8
+
9
+def test_func_flask_fake_api():
10
+    flask_app = Flask(__name__)
11
+    controllers = FlaskController()
12
+    controllers.bind(flask_app)
13
+    hapic.set_context(FlaskContext(flask_app))
14
+    app = TestApp(flask_app)
15
+    doc = hapic.generate_doc()
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
+
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
+
42
+    resp = app.get('/users/1')
43
+    assert resp.status_int == 200
44
+    assert resp.json == {
45
+        'last_name': 'Accorsi',
46
+        'username': 'some_user',
47
+        'first_name': 'Damien',
48
+        'id': 4,
49
+        'display_name': 'Damien Accorsi',
50
+        'email_address': 'some.user@hapic.com',
51
+        'company': 'Algoo'
52
+    }
53
+
54
+    resp = app.post('/users/', status='*')
55
+    assert resp.status_int == 400
56
+    assert resp.json == {
57
+        'details': {
58
+            'email_address': ['Missing data for required field.'],
59
+            'username': ['Missing data for required field.'],
60
+            'display_name': ['Missing data for required field.'],
61
+            'last_name': ['Missing data for required field.'],
62
+            'first_name': ['Missing data for required field.'],
63
+            'company': ['Missing data for required field.']},
64
+        'message': 'Validation error of input data'}
65
+
66
+    user = {
67
+        'email_address': 'some.user@hapic.com',
68
+        'username': 'some_user',
69
+        'display_name': 'Damien Accorsi',
70
+        'last_name': 'Accorsi',
71
+        'first_name': 'Damien',
72
+        'company': 'Algoo',
73
+    }
74
+
75
+    resp = app.post_json('/users/', user)
76
+    assert resp.status_int == 200
77
+    assert resp.json == {
78
+        'last_name': 'Accorsi',
79
+        'username': 'some_user',
80
+        'first_name': 'Damien',
81
+        'id': 4,
82
+        'display_name': 'Damien Accorsi',
83
+        'email_address': 'some.user@hapic.com',
84
+        'company': 'Algoo',
85
+    }
86
+    # INFO - G.M - 2017-12-07 - Warning due to Webtest check
87
+    # Webtest check content_type. Up to know flask_api return json as
88
+    # content_type with 204 NO CONTENT status code which return a warning in
89
+    # WebTest check.
90
+    resp = app.delete('/users/1', status='*')
91
+    assert resp.status_int == 204

+ 93 - 0
tests/func/fake_api/test_pyramid.py View File

@@ -0,0 +1,93 @@
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
+
9
+
10
+def test_func_pyramid_fake_api_doc():
11
+    configurator = Configurator(autocommit=True)
12
+    controllers = PyramidController()
13
+    controllers.bind(configurator)
14
+    hapic.set_context(PyramidContext(configurator))
15
+    app = TestApp(configurator.make_wsgi_app())
16
+    doc = hapic.generate_doc()
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
+
24
+    resp = app.get('/users')
25
+    assert resp.status_int == 200
26
+    assert resp.json == {
27
+        'items':
28
+        [
29
+            {
30
+                'username': 'some_user',
31
+                'display_name': 'Damien Accorsi',
32
+                'company': 'Algoo', 'id': 4
33
+            }
34
+        ],
35
+        'pagination': {
36
+            'first_id': 0,
37
+            'last_id': 5,
38
+            'current_id': 0,
39
+        },
40
+        'item_nb': 1,
41
+    }
42
+
43
+    resp = app.get('/users/1')
44
+    assert resp.status_int == 200
45
+    assert resp.json == {
46
+        'last_name': 'Accorsi',
47
+        'username': 'some_user',
48
+        'first_name': 'Damien',
49
+        'id': 4,
50
+        'display_name': 'Damien Accorsi',
51
+        'email_address': 'some.user@hapic.com',
52
+        'company': 'Algoo'
53
+    }
54
+
55
+    resp = app.post('/users/', status='*')
56
+    assert resp.status_int == 400
57
+    assert resp.json == {
58
+        'details': {
59
+            'email_address': ['Missing data for required field.'],
60
+            'username': ['Missing data for required field.'],
61
+            'display_name': ['Missing data for required field.'],
62
+            'last_name': ['Missing data for required field.'],
63
+            'first_name': ['Missing data for required field.'],
64
+            'company': ['Missing data for required field.']},
65
+        'message': 'Validation error of input data'}
66
+
67
+    user = {
68
+        'email_address': 'some.user@hapic.com',
69
+        'username': 'some_user',
70
+        'display_name': 'Damien Accorsi',
71
+        'last_name': 'Accorsi',
72
+        'first_name': 'Damien',
73
+        'company': 'Algoo',
74
+    }
75
+
76
+    resp = app.post_json('/users/', user)
77
+    assert resp.status_int == 200
78
+    assert resp.json == {
79
+        'last_name': 'Accorsi',
80
+        'username': 'some_user',
81
+        'first_name': 'Damien',
82
+        'id': 4,
83
+        'display_name': 'Damien Accorsi',
84
+        'email_address': 'some.user@hapic.com',
85
+        'company': 'Algoo',
86
+    }
87
+
88
+    # INFO - G.M - 2017-12-07 - Test for delete desactivated(Webtest check fail)
89
+    # Webtest check content_type. Up to know pyramid_api return json as
90
+    # content_type with 204 NO CONTENT status code which return an error in
91
+    # WebTest check.
92
+    # resp = app.delete('/users/1', status='*')
93
+    # assert resp.status_int == 204