Browse Source

merge better_session_api

Guénaël Muller 6 years ago
parent
commit
10cfffd1c5

+ 4 - 6
tracim/__init__.py View File

@@ -13,9 +13,9 @@ from tracim.lib.utils.authentification import basic_auth_check_credentials
13 13
 from tracim.lib.utils.authentification import BASIC_AUTH_WEBUI_REALM
14 14
 from tracim.lib.utils.authorization import AcceptAllAuthorizationPolicy
15 15
 from tracim.lib.utils.authorization import TRACIM_DEFAULT_PERM
16
+from tracim.views import BASE_API_V2
16 17
 from tracim.views.core_api.session_controller import SessionController
17
-from tracim.views.default.default_controller import DefaultController
18
-from tracim.views.errors import Error
18
+from tracim.views.errors import ErrorSchema
19 19
 
20 20
 
21 21
 def main(global_config, **settings):
@@ -48,14 +48,12 @@ def main(global_config, **settings):
48 48
     hapic.set_context(
49 49
         PyramidContext(
50 50
             configurator=configurator,
51
-            default_error_builder=Error()
51
+            default_error_builder=ErrorSchema()
52 52
         )
53 53
     )
54 54
     # Add controllers
55
-    default_controllers = DefaultController()
56
-    default_controllers.bind(configurator)
57 55
     session_api = SessionController()
58
-    session_api.bind(configurator)
56
+    configurator.include(session_api.bind, route_prefix=BASE_API_V2)
59 57
     hapic.add_documentation_view(
60 58
         '/api/v2/doc',
61 59
         'Tracim v2 API',

+ 25 - 0
tracim/tests/functional/test_session.py View File

@@ -1,4 +1,7 @@
1 1
 # coding=utf-8
2
+import pytest
3
+from sqlalchemy.exc import OperationalError
4
+
2 5
 from tracim.tests import FunctionalTest
3 6
 from tracim.tests import FunctionalTestNoDB
4 7
 
@@ -14,6 +17,8 @@ class TestLogoutEndpoint(FunctionalTest):
14 17
 
15 18
 class TestLoginEndpointUnititedDB(FunctionalTestNoDB):
16 19
 
20
+    @pytest.mark.xfail(raises=OperationalError,
21
+                       reason='Not supported yet by hapic')
17 22
     def test_api__try_login_enpoint__err_500__no_inited_db(self):
18 23
         params = {
19 24
             'email': 'admin@admin.admin',
@@ -24,6 +29,10 @@ class TestLoginEndpointUnititedDB(FunctionalTestNoDB):
24 29
             params=params,
25 30
             status=500,
26 31
         )
32
+        assert isinstance(res.json, dict)
33
+        assert 'code' in res.json.keys()
34
+        assert 'message' in res.json.keys()
35
+        assert 'details' in res.json.keys()
27 36
 
28 37
 
29 38
 class TestLoginEndpoint(FunctionalTest):
@@ -49,6 +58,10 @@ class TestLoginEndpoint(FunctionalTest):
49 58
             status=400,
50 59
             params=params,
51 60
         )
61
+        assert isinstance(res.json, dict)
62
+        assert 'code' in res.json.keys()
63
+        assert 'message' in res.json.keys()
64
+        assert 'details' in res.json.keys()
52 65
 
53 66
     def test_api__try_login_enpoint__err_400__unregistered_user(self):
54 67
         params = {
@@ -60,9 +73,17 @@ class TestLoginEndpoint(FunctionalTest):
60 73
             status=400,
61 74
             params=params,
62 75
         )
76
+        assert isinstance(res.json, dict)
77
+        assert 'code' in res.json.keys()
78
+        assert 'message' in res.json.keys()
79
+        assert 'details' in res.json.keys()
63 80
 
64 81
     def test_api__try_login_enpoint__err_400__no_json_body(self):
65 82
         res = self.testapp.post_json('/api/v2/sessions/login', status=400)
83
+        assert isinstance(res.json, dict)
84
+        assert 'code' in res.json.keys()
85
+        assert 'message' in res.json.keys()
86
+        assert 'details' in res.json.keys()
66 87
 
67 88
 
68 89
 class TestWhoamiEndpoint(FunctionalTest):
@@ -95,3 +116,7 @@ class TestWhoamiEndpoint(FunctionalTest):
95 116
             )
96 117
         )
97 118
         res = self.testapp.get('/api/v2/sessions/whoami', status=401)
119
+        assert isinstance(res.json, dict)
120
+        assert 'code' in res.json.keys()
121
+        assert 'message' in res.json.keys()
122
+        assert 'details' in res.json.keys()

+ 13 - 52
tracim/views/core_api/session_controller.py View File

@@ -1,32 +1,21 @@
1 1
 # coding=utf-8
2
-import os
3
-from http.client import HTTPException
4
-
5
-from pyramid.httpexceptions import HTTPNoContent
6
-from pyramid.response import Response
7
-from sqlalchemy.orm.exc import NoResultFound
2
+from pyramid.config import Configurator
3
+try:  # Python 3.5+
4
+    from http import HTTPStatus
5
+except ImportError:
6
+    from http import client as HTTPStatus
8 7
 
9 8
 from tracim import TracimRequest
10 9
 from tracim.extensions import hapic
11 10
 from tracim.lib.core.user import UserApi
12
-from tracim.models.context_models import UserInContext
13 11
 from tracim.views.controllers import Controller
14
-from pyramid.config import Configurator
15
-
16
-from tracim.views import BASE_API_V2
17 12
 from tracim.views.core_api.schemas import UserSchema
18 13
 from tracim.views.core_api.schemas import NoContentSchema
19
-
20 14
 from tracim.views.core_api.schemas import LoginOutputHeaders
21 15
 from tracim.views.core_api.schemas import BasicAuthSchema
22 16
 from tracim.exceptions import NotAuthentificated
23 17
 from tracim.exceptions import AuthenticationFailed
24 18
 
25
-try:  # Python 3.5+
26
-    from http import HTTPStatus
27
-except ImportError:
28
-    from http import client as HTTPStatus
29
-
30 19
 
31 20
 class SessionController(Controller):
32 21
 
@@ -78,41 +67,13 @@ class SessionController(Controller):
78 67
     def bind(self, configurator: Configurator):
79 68
 
80 69
         # Login
81
-        configurator.add_route(
82
-            'login',
83
-            os.path.join(BASE_API_V2, 'sessions', 'login'),
84
-            request_method='POST',
85
-        )
86
-        configurator.add_view(
87
-            self.login,
88
-            route_name='login',
89
-        )
70
+        configurator.add_route('login', '/sessions/login', request_method='POST')  # nopep8
71
+        configurator.add_view(self.login, route_name='login')
90 72
         # Logout
91
-        configurator.add_route(
92
-            'post_logout',
93
-            os.path.join(BASE_API_V2, 'sessions', 'logout'),
94
-            request_method='POST',
95
-        )
96
-        configurator.add_route(
97
-            'get_logout',
98
-            os.path.join(BASE_API_V2, 'sessions', 'logout'),
99
-            request_method='GET',
100
-        )
101
-        configurator.add_view(
102
-            self.logout,
103
-            route_name='get_logout',
104
-        )
105
-        configurator.add_view(
106
-            self.logout,
107
-            route_name='post_logout',
108
-        )
73
+        configurator.add_route('logout', '/sessions/logout', request_method='POST')  # nopep8
74
+        configurator.add_view(self.logout, route_name='logout')
75
+        configurator.add_route('logout_get', '/sessions/logout', request_method='GET')  # nopep8
76
+        configurator.add_view(self.logout, route_name='logout_get')
109 77
         # Whoami
110
-        configurator.add_route(
111
-            'whoami',
112
-            os.path.join(BASE_API_V2, 'sessions', 'whoami'),
113
-            request_method='GET',
114
-        )
115
-        configurator.add_view(
116
-            self.whoami,
117
-            route_name='whoami',
118
-        )
78
+        configurator.add_route('whoami', '/sessions/whoami', request_method='GET')  # nopep8
79
+        configurator.add_view(self.whoami, route_name='whoami',)

+ 0 - 0
tracim/views/default/__init__.py View File


+ 0 - 55
tracim/views/default/default_controller.py View File

@@ -1,55 +0,0 @@
1
-# coding=utf-8
2
-from pyramid.httpexceptions import HTTPNotFound
3
-
4
-from tracim import TracimRequest
5
-from tracim.extensions import hapic
6
-from tracim.views.controllers import Controller
7
-from tracim.views.errors import Error
8
-from pyramid.config import Configurator
9
-
10
-
11
-class DefaultController(Controller):
12
-
13
-    def notfound_view(
14
-            self,
15
-            exception: HTTPNotFound,
16
-            request: TracimRequest
17
-    ):
18
-        """
19
-        Catch Not Found Exception
20
-        :param exception: Exception Object
21
-        :param request: current Request
22
-        :return: 500 Internal Server Error with same format as others errors
23
-        """
24
-        request.response.status = 404
25
-        return hapic.context.get_default_error_builder().build_from_exception(
26
-            exception=exception
27
-        )
28
-
29
-    def exception_view(
30
-            self,
31
-            exception: Exception,
32
-            request: TracimRequest
33
-    ):
34
-        """
35
-        Catch all exceptions not handled in view
36
-        :param exception: Exception Object
37
-        :param request: current Request
38
-        :return: 500 Internal Server Error with same format as others errors
39
-        """
40
-        request.response.status = 500
41
-        return hapic.context.get_default_error_builder().build_from_exception(
42
-            exception=exception
43
-        )
44
-
45
-    def bind(self, configurator: Configurator):
46
-        configurator.add_view(
47
-            self.notfound_view,
48
-            renderer='json',
49
-            context=HTTPNotFound,
50
-        )
51
-        configurator.add_view(
52
-            self.exception_view,
53
-            renderer='json',
54
-            context=Exception,
55
-        )

+ 6 - 1
tracim/views/errors.py View File

@@ -1,5 +1,10 @@
1 1
 from hapic.error import DefaultErrorBuilder
2 2
 
3 3
 
4
-class Error(DefaultErrorBuilder):
4
+class ErrorSchema(DefaultErrorBuilder):
5
+    """
6
+    This class is both a builder and a Marshmallow Schema, His named is used for
7
+    swagger ui error schema. That's why we call it ErrorSchema To have
8
+    a nice naming in swagger ui.
9
+    """
5 10
     pass