This repository was archived by the owner on May 26, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +24
-1
lines changed Expand file tree Collapse file tree 3 files changed +24
-1
lines changed Original file line number Diff line number Diff line change @@ -41,10 +41,11 @@ def jwt_payload_handler(user):
41
41
42
42
payload = {
43
43
'user_id' : user .pk ,
44
- 'email' : user .email ,
45
44
'username' : username ,
46
45
'exp' : datetime .utcnow () + api_settings .JWT_EXPIRATION_DELTA
47
46
}
47
+ if hasattr (user , 'email' ):
48
+ payload ['email' ] = user .email
48
49
if isinstance (user .pk , uuid .UUID ):
49
50
payload ['user_id' ] = str (user .pk )
50
51
Original file line number Diff line number Diff line change @@ -20,6 +20,17 @@ class Meta:
20
20
app_label = 'tests'
21
21
22
22
23
+ class CustomUserWithoutEmail (AbstractBaseUser ):
24
+ username = models .CharField (max_length = 255 , unique = True )
25
+
26
+ objects = BaseUserManager ()
27
+
28
+ USERNAME_FIELD = 'username'
29
+
30
+ class Meta :
31
+ app_label = 'tests'
32
+
33
+
23
34
class CustomUserUUID (AbstractBaseUser ):
24
35
id = models .UUIDField (primary_key = True , default = uuid .uuid4 , editable = False )
25
36
email = models .EmailField (max_length = 255 , unique = True )
Original file line number Diff line number Diff line change 8
8
from rest_framework_jwt import utils
9
9
from rest_framework_jwt .compat import get_user_model
10
10
from rest_framework_jwt .settings import api_settings , DEFAULTS
11
+ from tests .models import CustomUserWithoutEmail
11
12
12
13
User = get_user_model ()
13
14
@@ -38,6 +39,16 @@ def test_jwt_payload_handler(self):
38
39
self .assertEqual (payload ['username' ], self .username )
39
40
self .assertTrue ('exp' in payload )
40
41
42
+ def test_jwt_payload_handler_no_email_address (self ):
43
+ user = CustomUserWithoutEmail .objects .create (username = self .username )
44
+
45
+ payload = utils .jwt_payload_handler (user )
46
+ self .assertTrue (isinstance (payload , dict ))
47
+ self .assertFalse (hasattr (payload , 'email' ))
48
+ self .assertEqual (payload ['user_id' ], self .user .pk )
49
+ self .assertEqual (payload ['username' ], self .username )
50
+ self .assertTrue ('exp' in payload )
51
+
41
52
def test_jwt_encode (self ):
42
53
payload = utils .jwt_payload_handler (self .user )
43
54
token = utils .jwt_encode_handler (payload )
You can’t perform that action at this time.
0 commit comments