Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create the user in user_stats only when the account is verified #530

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion user_management/src/user/test_friends.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


class FriendsTest(TestCase):
@patch('user.views.sign_up.post_user_stats')
@patch('user.views.verify_email.post_user_stats')
def create_user(self, body, mock_user_stats):
mock_user_stats.return_value = (True, None)
user = User.objects.create(**body, emailVerified=True)
Expand Down
67 changes: 17 additions & 50 deletions user_management/src/user/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ def run_signup_test(self, name, username, email, password, expected_status,
if expected_errors:
self.assertEqual(result.json()['errors'], expected_errors)

@patch('user.views.sign_up.post_user_stats')
def test_signup_valid_username(self, mock_user_stats):
mock_user_stats.return_value = (True, None)
def test_signup_valid_username(self):
password = 'Validpass42*'
expected_status = 201
name = 'Valid Username'
Expand All @@ -59,9 +57,7 @@ def test_signup_valid_username(self, mock_user_stats):
expected_status,
)

@patch('user.views.sign_up.post_user_stats')
def test_signup_invalid_username(self, mock_user_stats):
mock_user_stats.return_value = (True, None)
def test_signup_invalid_username(self):
password = 'Validpass42*'
expected_status = 400
name = 'Invalid Username'
Expand All @@ -84,9 +80,7 @@ def test_signup_invalid_username(self, mock_user_stats):
expected_status,
expected_errors)

@patch('user.views.sign_up.post_user_stats')
def test_signup_valid_email(self, mock_user_stats):
mock_user_stats.return_value = (True, None)
def test_signup_valid_email(self):
password = 'Validpass42*'
expected_status = 201
name = 'Valid Email'
Expand All @@ -100,9 +94,7 @@ def test_signup_valid_email(self, mock_user_stats):
username = 'Aurel' + str(random.randint(0, 100000)) # To avoid `username already taken` error
self.run_signup_test(name, username, email, password, expected_status)

@patch('user.views.sign_up.post_user_stats')
def test_signup_invalid_email(self, mock_user_stats):
mock_user_stats.return_value = (True, None)
def test_signup_invalid_email(self):
password = 'Validpass42*'
expected_status = 400
name = 'Invalid Email'
Expand Down Expand Up @@ -131,9 +123,7 @@ def test_signup_invalid_email(self, mock_user_stats):
expected_status,
expected_errors)

@patch('user.views.sign_up.post_user_stats')
def test_signup_valid_password(self, mock_user_stats):
mock_user_stats.return_value = (True, None)
def test_signup_valid_password(self):
expected_status = 201
name = 'Valid Password'
valid_passwords = [
Expand All @@ -150,9 +140,7 @@ def test_signup_valid_password(self, mock_user_stats):
expected_status,
)

@patch('user.views.sign_up.post_user_stats')
def test_signup_invalid_password(self, mock_user_stats):
mock_user_stats.return_value = (True, None)
def test_signup_invalid_password(self):
expected_status = 400
name = 'Invalid Password'
email = '[email protected]'
Expand All @@ -179,9 +167,7 @@ def test_signup_invalid_password(self, mock_user_stats):
expected_status,
expected_errors)

@patch('user.views.sign_up.post_user_stats')
def test_signup_not_a_json(self, mock_user_stats):
mock_user_stats.return_value = (True, None)
def test_signup_not_a_json(self):
string = 'This is not a JSON'
url = reverse('signup')
result = self.client.post(url, string, content_type='application/json')
Expand All @@ -191,9 +177,7 @@ def test_signup_not_a_json(self, mock_user_stats):

class TestsSignin(TestCase):

@patch('user.views.sign_up.post_user_stats')
def test_signin(self, mock_user_stats):
mock_user_stats.return_value = (True, None)
def test_signin(self):
User.objects.create(
username='aurelien123',
email='[email protected]',
Expand Down Expand Up @@ -226,9 +210,7 @@ def test_signin(self, mock_user_stats):

class TestsUsernameExist(TestCase):

@patch('user.views.sign_up.post_user_stats')
def test_username_exist(self, mock_user_stats):
mock_user_stats.return_value = (True, None)
def test_username_exist(self):
User.objects.create(username='Burel305', email='[email protected]', password='Validpass42*', emailVerified=True)
data_username_exist = {
'username': 'Burel305'
Expand All @@ -251,9 +233,7 @@ def test_username_exist(self, mock_user_stats):

class TestsRefreshJWT(TestCase):

@patch('user.views.sign_up.post_user_stats')
def test_refresh_jwt(self, mock_user_stats):
mock_user_stats.return_value = (True, None)
def test_refresh_jwt(self):
user = User.objects.create(username='Aurel303',
email='[email protected]',
password='Validpass42*',
Expand Down Expand Up @@ -325,9 +305,7 @@ def test_refresh_jwt(self, mock_user_stats):

class TestsEmailExist(TestCase):

@patch('user.views.sign_up.post_user_stats')
def test_email_exist(self, mock_user_stats):
mock_user_stats.return_value = (True, None)
def test_email_exist(self):
User.objects.create(username='Aurel305',
email='[email protected]',
password='Validpass42*',
Expand All @@ -353,9 +331,7 @@ def test_email_exist(self, mock_user_stats):

class UserId(TestCase):

@patch('user.views.sign_up.post_user_stats')
def test_user_id(self, mock_user_stats):
mock_user_stats.return_value = (True, None)
def test_user_id(self):
user = User.objects.create(username='Aurel303',
email='[email protected]',
password='Validpass42*',
Expand All @@ -371,9 +347,7 @@ def test_user_id(self, mock_user_stats):

class Username(TestCase):

@patch('user.views.sign_up.post_user_stats')
def test_username(self, mock_user_stats):
mock_user_stats.return_value = (True, None)
def test_username(self):
user = User.objects.create(username='Aurel303',
email='[email protected]',
password='Validpass42*',
Expand All @@ -399,9 +373,7 @@ def test_invalid_username(self):

class TestsSearchUsername(TestCase):

@patch('user.views.sign_up.post_user_stats')
def test_search_username(self, mock_user_stats):
mock_user_stats.return_value = (True, None)
def test_search_username(self):
for i in range(1, 20):
User.objects.create(username=f'Felix{i}',
email=f'felix{i}@gmail.com',
Expand Down Expand Up @@ -458,9 +430,7 @@ class TestsUserUpdateInfos(TestCase):
4) finally, check if the user infos have been updated with /user/user-id
5) test invalid data"""

@patch('user.views.sign_up.post_user_stats')
def test_user_update_infos(self, mock_user_stats):
mock_user_stats.return_value = (True, None)
def test_user_update_infos(self):
# 1)
user = User.objects.create(username='UpdateThisUser',
email='[email protected]',
Expand Down Expand Up @@ -512,10 +482,7 @@ def test_user_update_infos(self, mock_user_stats):

class TestsTwoFa(TestCase):

@patch('user.views.sign_up.post_user_stats')
def test_two_fa(self, mock_user_stats):
mock_user_stats.return_value = (True, None)

def test_two_fa(self):
user = User.objects.create(username='TestTwoFA',
email='[email protected]',
password='Validpass42*',
Expand Down Expand Up @@ -657,7 +624,7 @@ def test_avatar(self):


class TestEmailVerified(TestCase):
@patch('user.views.sign_up.post_user_stats')
@patch('user.views.verify_email.post_user_stats')
def test_email_verified(self, mock_user_stats):
mock_user_stats.return_value = (True, None)
username = 'testEmailVerified'
Expand Down
6 changes: 1 addition & 5 deletions user_management/src/user/views/sign_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from user.models import User
from user_management import settings
from user_management.utils import (is_valid_email, is_valid_password,
is_valid_username, post_user_stats)
is_valid_username)


def generate_verif_link(user):
Expand All @@ -39,10 +39,6 @@ def post(self, request):
user = User.objects.create(username=json_request['username'],
email=json_request['email'],
password=make_password(json_request['password']))
valid, errors = post_user_stats(user.id)
if not valid:
user.delete()
return JsonResponse(data={'errors': errors}, status=500)
except Exception as e:
return JsonResponse(data={'errors': [f'An error occurred while creating the user : {e}']}, status=500)

Expand Down
4 changes: 4 additions & 0 deletions user_management/src/user/views/verify_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from user.models import User
from user_management.JWTManager import UserRefreshJWTManager
from user_management.utils import post_user_stats


@method_decorator(csrf_exempt, name='dispatch')
Expand All @@ -34,6 +35,9 @@ def post(self, request, user_id, token):
return JsonResponse(
data={'errors': ['an error occurred while removing the expired token']}, status=500)
return JsonResponse(data={'errors': ['verification token expired']}, status=401)
valid, errors = post_user_stats(user.id)
if not valid:
return JsonResponse(data={'errors': errors}, status=500)
try:
user.emailVerified = True
user.save()
Expand Down