Ver código fonte

better user lib : clear error messages + renaming Exception

Guénaël Muller 6 anos atrás
pai
commit
bda335a563

+ 1 - 1
tracim/exceptions.py Ver arquivo

@@ -93,5 +93,5 @@ class WrongUserPassword(TracimException):
93 93
     pass
94 94
 
95 95
 
96
-class UserNotExist(TracimException):
96
+class UserDoesNotExist(TracimException):
97 97
     pass

+ 7 - 7
tracim/lib/core/user.py Ver arquivo

@@ -9,7 +9,7 @@ from sqlalchemy.orm import Session
9 9
 from tracim import CFG
10 10
 from tracim.models.auth import User, Group
11 11
 from sqlalchemy.orm.exc import NoResultFound
12
-from tracim.exceptions import WrongUserPassword, UserNotExist
12
+from tracim.exceptions import WrongUserPassword, UserDoesNotExist
13 13
 from tracim.exceptions import AuthenticationFailed
14 14
 from tracim.models.context_models import UserInContext
15 15
 
@@ -48,8 +48,8 @@ class UserApi(object):
48 48
         """
49 49
         try:
50 50
             user = self._base_query().filter(User.user_id == user_id).one()
51
-        except NoResultFound:
52
-            raise UserNotExist()
51
+        except NoResultFound as exc:
52
+            raise UserDoesNotExist('User "{}" not found in database'.format(user_id)) from exc  # nopep8
53 53
         return user
54 54
 
55 55
     def get_one_by_email(self, email: str) -> User:
@@ -60,8 +60,8 @@ class UserApi(object):
60 60
         """
61 61
         try:
62 62
             user = self._base_query().filter(User.email == email).one()
63
-        except NoResultFound:
64
-            raise UserNotExist()
63
+        except NoResultFound as exc:
64
+            raise UserDoesNotExist('User "{}" not found in database'.format(email)) from exc  # nopep8
65 65
         return user
66 66
 
67 67
     # FIXME - G.M - 24-04-2018 - Duplicate method with get_one.
@@ -73,7 +73,7 @@ class UserApi(object):
73 73
         Get current_user
74 74
         """
75 75
         if not self._user:
76
-            raise UserNotExist()
76
+            raise UserDoesNotExist()
77 77
         return self._user
78 78
 
79 79
     def get_all(self) -> typing.Iterable[User]:
@@ -103,7 +103,7 @@ class UserApi(object):
103 103
                 return user
104 104
             else:
105 105
                 raise WrongUserPassword()
106
-        except (WrongUserPassword, NoResultFound, UserNotExist):
106
+        except (WrongUserPassword, UserDoesNotExist):
107 107
             raise AuthenticationFailed()
108 108
 
109 109
     # Actions

+ 2 - 2
tracim/lib/utils/authentification.py Ver arquivo

@@ -4,7 +4,7 @@ from pyramid.request import Request
4 4
 from sqlalchemy.orm.exc import NoResultFound
5 5
 
6 6
 from tracim import TracimRequest
7
-from tracim.exceptions import UserNotExist
7
+from tracim.exceptions import UserDoesNotExist
8 8
 from tracim.lib.core.user import UserApi
9 9
 from tracim.models import User
10 10
 
@@ -51,6 +51,6 @@ def _get_basic_auth_unsafe_user(
51 51
         if not login:
52 52
             return None
53 53
         user = uapi.get_one_by_email(login)
54
-    except (NoResultFound, UserNotExist):
54
+    except (NoResultFound, UserDoesNotExist):
55 55
         return None
56 56
     return user

+ 2 - 2
tracim/lib/utils/request.py Ver arquivo

@@ -5,7 +5,7 @@ TracimRequest and related functions
5 5
 from pyramid.request import Request
6 6
 from sqlalchemy.orm.exc import NoResultFound
7 7
 
8
-from tracim.exceptions import NotAuthentificated, UserNotExist
8
+from tracim.exceptions import NotAuthentificated, UserDoesNotExist
9 9
 from tracim.exceptions import WorkspaceNotFound
10 10
 from tracim.exceptions import ImmutableAttribute
11 11
 from tracim.lib.core.user import UserApi
@@ -142,7 +142,7 @@ def get_candidate_user(
142 142
         if 'user_id' in request.matchdict:
143 143
             login = request.matchdict['user_id']
144 144
         if not login:
145
-            raise UserNotExist('no user_id found, incorrect request ?')
145
+            raise UserDoesNotExist('no user_id found, incorrect request ?')
146 146
         user = uapi.get_one(login)
147 147
     except NoResultFound:
148 148
         raise NotAuthentificated('User not found')

+ 3 - 3
tracim/tests/library/test_user_api.py Ver arquivo

@@ -4,7 +4,7 @@ from sqlalchemy.orm.exc import NoResultFound
4 4
 
5 5
 import transaction
6 6
 
7
-from tracim.exceptions import UserNotExist, AuthenticationFailed
7
+from tracim.exceptions import UserDoesNotExist, AuthenticationFailed
8 8
 from tracim.lib.core.user import UserApi
9 9
 from tracim.models import User
10 10
 from tracim.models.context_models import UserInContext
@@ -60,7 +60,7 @@ class TestUserApi(DefaultTest):
60 60
             session=self.session,
61 61
             config=self.config,
62 62
         )
63
-        with pytest.raises(UserNotExist):
63
+        with pytest.raises(UserDoesNotExist):
64 64
             api.get_one_by_email('unknown')
65 65
 
66 66
     # def test_unit__get_all__ok__nominal_case(self):
@@ -127,7 +127,7 @@ class TestUserApi(DefaultTest):
127 127
             session=self.session,
128 128
             config=self.config,
129 129
         )
130
-        with pytest.raises(UserNotExist):
130
+        with pytest.raises(UserDoesNotExist):
131 131
             api.get_current_user()
132 132
 
133 133
     def test_unit__authenticate_user___ok__nominal_case(self):

+ 2 - 2
tracim/views/core_api/user_controller.py Ver arquivo

@@ -12,7 +12,7 @@ except ImportError:
12 12
 
13 13
 from tracim import hapic, TracimRequest
14 14
 from tracim.exceptions import NotAuthentificated, InsufficientUserProfile, \
15
-    UserNotExist
15
+    UserDoesNotExist
16 16
 from tracim.lib.core.user import UserApi
17 17
 from tracim.lib.core.workspace import WorkspaceApi
18 18
 from tracim.views.controllers import Controller
@@ -25,7 +25,7 @@ class UserController(Controller):
25 25
     @hapic.with_api_doc()
26 26
     @hapic.handle_exception(NotAuthentificated, HTTPStatus.UNAUTHORIZED)
27 27
     @hapic.handle_exception(InsufficientUserProfile, HTTPStatus.FORBIDDEN)
28
-    @hapic.handle_exception(UserNotExist, HTTPStatus.NOT_FOUND)
28
+    @hapic.handle_exception(UserDoesNotExist, HTTPStatus.NOT_FOUND)
29 29
     @require_same_user_or_profile(Group.TIM_ADMIN)
30 30
     @hapic.input_path(UserIdPathSchema())
31 31
     @hapic.output_body(WorkspaceDigestSchema(many=True),)