Bladeren bron

fix some typing and naming issue related to userlib

Guénaël Muller 6 jaren geleden
bovenliggende
commit
a204b4ff69
3 gewijzigde bestanden met toevoegingen van 20 en 12 verwijderingen
  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 Bestand weergeven

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

+ 15 - 7
tracim/lib/core/user.py Bestand weergeven

4
 import transaction
4
 import transaction
5
 import typing as typing
5
 import typing as typing
6
 
6
 
7
+from sqlalchemy.orm import Session
8
+
9
+from tracim import CFG
7
 from tracim.models.auth import User
10
 from tracim.models.auth import User
8
 from sqlalchemy.orm.exc import NoResultFound
11
 from sqlalchemy.orm.exc import NoResultFound
9
-from tracim.exceptions import BadUserPassword, UserNotExist
12
+from tracim.exceptions import WrongUserPassword, UserNotExist
10
 from tracim.exceptions import AuthenticationFailed
13
 from tracim.exceptions import AuthenticationFailed
11
 from tracim.models.context_models import UserInContext
14
 from tracim.models.context_models import UserInContext
12
 
15
 
13
 
16
 
14
 class UserApi(object):
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
         self._session = session
25
         self._session = session
18
         self._user = current_user
26
         self._user = current_user
19
         self._config = config
27
         self._config = config
65
 
73
 
66
     # Check methods
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
         try:
77
         try:
70
             self.get_one_by_email(email)
78
             self.get_one_by_email(email)
71
             return True
79
             return True
86
             if user.validate_password(password):
94
             if user.validate_password(password):
87
                 return user
95
                 return user
88
             else:
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
     # Actions
101
     # Actions
94
 
102
 
99
             email: str=None,
107
             email: str=None,
100
             do_save=True,
108
             do_save=True,
101
             timezone: str='',
109
             timezone: str='',
102
-    ):
110
+    ) -> None:
103
         if name is not None:
111
         if name is not None:
104
             user.display_name = name
112
             user.display_name = name
105
 
113
 

+ 4 - 4
tracim/tests/library/test_user_api.py Bestand weergeven

127
         assert isinstance(user, User)
127
         assert isinstance(user, User)
128
         assert user.email == 'admin@admin.admin'
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
         api = UserApi(
131
         api = UserApi(
132
             current_user=None,
132
             current_user=None,
133
             session=self.session,
133
             session=self.session,
134
             config=self.config,
134
             config=self.config,
135
         )
135
         )
136
         with pytest.raises(AuthenticationFailed):
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
         api = UserApi(
140
         api = UserApi(
141
             current_user=None,
141
             current_user=None,
142
             session=self.session,
142
             session=self.session,
143
             config=self.config,
143
             config=self.config,
144
         )
144
         )
145
         with pytest.raises(AuthenticationFailed):
145
         with pytest.raises(AuthenticationFailed):
146
-            api.authenticate_user('unknown_user', 'bad_password')
146
+            api.authenticate_user('unknown_user', 'wrong_password')