Browse Source

fix some typing and naming issue related to userlib

Guénaël Muller 6 years ago
parent
commit
a204b4ff69
3 changed files with 20 additions and 12 deletions
  1. 1 1
      tracim/exceptions.py
  2. 15 7
      tracim/lib/core/user.py
  3. 4 4
      tracim/tests/library/test_user_api.py

+ 1 - 1
tracim/exceptions.py View File

@@ -89,7 +89,7 @@ class AuthenticationFailed(TracimException):
89 89
     pass
90 90
 
91 91
 
92
-class BadUserPassword(TracimException):
92
+class WrongUserPassword(TracimException):
93 93
     pass
94 94
 
95 95
 class UserNotExist(TracimException):

+ 15 - 7
tracim/lib/core/user.py View File

@@ -4,16 +4,24 @@ import threading
4 4
 import transaction
5 5
 import typing as typing
6 6
 
7
+from sqlalchemy.orm import Session
8
+
9
+from tracim import CFG
7 10
 from tracim.models.auth import User
8 11
 from sqlalchemy.orm.exc import NoResultFound
9
-from tracim.exceptions import BadUserPassword, UserNotExist
12
+from tracim.exceptions import WrongUserPassword, UserNotExist
10 13
 from tracim.exceptions import AuthenticationFailed
11 14
 from tracim.models.context_models import UserInContext
12 15
 
13 16
 
14 17
 class UserApi(object):
15 18
 
16
-    def __init__(self, current_user: typing.Optional[User], session, config):
19
+    def __init__(
20
+            self,
21
+            current_user: typing.Optional[User],
22
+            session: Session,
23
+            config: CFG,
24
+    ) -> None:
17 25
         self._session = session
18 26
         self._user = current_user
19 27
         self._config = config
@@ -65,7 +73,7 @@ class UserApi(object):
65 73
 
66 74
     # Check methods
67 75
 
68
-    def user_with_email_exists(self, email: str):
76
+    def user_with_email_exists(self, email: str) -> bool:
69 77
         try:
70 78
             self.get_one_by_email(email)
71 79
             return True
@@ -86,9 +94,9 @@ class UserApi(object):
86 94
             if user.validate_password(password):
87 95
                 return user
88 96
             else:
89
-                raise BadUserPassword()
90
-        except (BadUserPassword, NoResultFound):
91
-            raise AuthenticationFailed
97
+                raise WrongUserPassword()
98
+        except (WrongUserPassword, NoResultFound):
99
+            raise AuthenticationFailed()
92 100
 
93 101
     # Actions
94 102
 
@@ -99,7 +107,7 @@ class UserApi(object):
99 107
             email: str=None,
100 108
             do_save=True,
101 109
             timezone: str='',
102
-    ):
110
+    ) -> None:
103 111
         if name is not None:
104 112
             user.display_name = name
105 113
 

+ 4 - 4
tracim/tests/library/test_user_api.py View File

@@ -127,20 +127,20 @@ class TestUserApi(DefaultTest):
127 127
         assert isinstance(user, User)
128 128
         assert user.email == 'admin@admin.admin'
129 129
 
130
-    def test_unit__authenticate_user___err__bad_password(self):
130
+    def test_unit__authenticate_user___err__wrong_password(self):
131 131
         api = UserApi(
132 132
             current_user=None,
133 133
             session=self.session,
134 134
             config=self.config,
135 135
         )
136 136
         with pytest.raises(AuthenticationFailed):
137
-            api.authenticate_user('admin@admin.admin', 'bad_password')
137
+            api.authenticate_user('admin@admin.admin', 'wrong_password')
138 138
 
139
-    def test_unit__authenticate_user___err__bad_user(self):
139
+    def test_unit__authenticate_user___err__wrong_user(self):
140 140
         api = UserApi(
141 141
             current_user=None,
142 142
             session=self.session,
143 143
             config=self.config,
144 144
         )
145 145
         with pytest.raises(AuthenticationFailed):
146
-            api.authenticate_user('unknown_user', 'bad_password')
146
+            api.authenticate_user('unknown_user', 'wrong_password')