4
4
# SPDX-License-Identifier: MIT
5
5
6
6
from allauth .account .views import EmailVerificationSentView
7
+ from django .contrib .auth .models import User
7
8
from django .test import override_settings
8
9
from django .urls import path , re_path , reverse
9
10
from rest_framework import status
10
11
from rest_framework .authtoken .models import Token
11
- from rest_framework .test import APIClient , APITestCase
12
12
13
- from cvat .apps .iam .urls import urlpatterns as iam_url_patterns
13
+ from cvat .apps .engine .tests .test_rest_api import create_db_users
14
+ from cvat .apps .engine .tests .utils import ApiTestBase
15
+ from cvat .urls import urlpatterns as original_urlpatterns
14
16
from cvat .apps .iam .views import ConfirmEmailViewEx
15
17
16
- urlpatterns = iam_url_patterns + [
18
+ urlpatterns = original_urlpatterns + [
17
19
re_path (
18
20
r"^account-confirm-email/(?P<key>[-:\w]+)/$" ,
19
21
ConfirmEmailViewEx .as_view (),
27
29
]
28
30
29
31
30
- class ForceLogin :
31
- def __init__ (self , user , client ):
32
- self .user = user
33
- self .client = client
34
-
35
- def __enter__ (self ):
36
- if self .user :
37
- self .client .force_login (self .user , backend = "django.contrib.auth.backends.ModelBackend" )
38
-
39
- return self
40
-
41
- def __exit__ (self , exception_type , exception_value , traceback ):
42
- if self .user :
43
- self .client .logout ()
44
-
45
-
46
- class UserRegisterAPITestCase (APITestCase ):
47
-
32
+ class UserRegisterAPITestCase (ApiTestBase ):
48
33
user_data = {
49
34
"first_name" : "test_first" ,
50
35
"last_name" : "test_last" ,
@@ -55,8 +40,10 @@ class UserRegisterAPITestCase(APITestCase):
55
40
"confirmations" : [],
56
41
}
57
42
58
- def setUp (self ):
59
- self .client = APIClient ()
43
+ @classmethod
44
+ def setUpTestData (cls ):
45
+ # create only admin account
46
+ create_db_users (cls , primary = False , extra = False )
60
47
61
48
def _run_api_v2_user_register (self , data ):
62
49
url = reverse ("rest_register" )
@@ -130,3 +117,50 @@ def test_register_account_with_email_verification_mandatory(self):
130
117
"key" : None ,
131
118
},
132
119
)
120
+
121
+ @override_settings (
122
+ ACCOUNT_EMAIL_REQUIRED = True ,
123
+ ACCOUNT_EMAIL_VERIFICATION = "mandatory" ,
124
+ EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" ,
125
+ ROOT_URLCONF = __name__ ,
126
+ )
127
+ def test_register_account_with_different_email_case_than_in_invitation (self ):
128
+ """
129
+ Ensure a user can log in to the account after being invited to an organization
130
+ and then registering with the same email but in a different case.
131
+ """
132
+ org_slug = "testorg"
133
+ response = self ._post_request (
134
+ "/api/organizations" , self .admin , data = {"slug" : org_slug , "name" : "Test organization" }
135
+ )
136
+ self .assertTrue (response .status_code == status .HTTP_201_CREATED )
137
+ response = self ._post_request (
138
+ "/api/invitations" ,
139
+ self .admin ,
140
+ data = {"role" : "worker" , "email" : self .user_data ["email" ].upper ()},
141
+ query_params = {"org" : org_slug },
142
+ )
143
+ self .assertTrue (response .status_code == status .HTTP_201_CREATED )
144
+
145
+ response = self ._run_api_v2_user_register (self .user_data )
146
+ self ._check_response (
147
+ response ,
148
+ {
149
+ "first_name" : "test_first" ,
150
+ "last_name" : "test_last" ,
151
+ "username" : "test_username" ,
152
+
153
+ "email_verification_required" : True ,
154
+ "key" : None ,
155
+ },
156
+ )
157
+ invited_db_user = User .objects .get (email = self .user_data ["email" ])
158
+ self .assertTrue (invited_db_user .emailaddress_set .update (verified = True ))
159
+ response = self .client .post (
160
+ "/api/auth/login" ,
161
+ format = "json" ,
162
+ data = {"email" : self .user_data ["email" ], "password" : self .user_data ["password1" ]},
163
+ )
164
+ self .assertTrue (response .status_code == status .HTTP_200_OK )
165
+ self .assertIn ("sessionid" , response .cookies )
166
+ self .assertIn ("csrftoken" , response .cookies )
0 commit comments