From 4ac1d8f55dc9cbd0fa485bb5cf24d8574d1e6454 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Thu, 2 Oct 2014 02:18:18 +0200 Subject: [PATCH] [backport] Fix wrong handing integrity error on register or accept invitation. --- taiga/auth/services.py | 14 +++++++++---- tests/integration/test_auth_api.py | 32 +++++++++++++++++++++++------- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/taiga/auth/services.py b/taiga/auth/services.py index 72a40af2..725373b5 100644 --- a/taiga/auth/services.py +++ b/taiga/auth/services.py @@ -72,7 +72,7 @@ def is_user_already_registred(*, username:str, email:str, github_id:int=None) -> or_expr = Q(username=username) | Q(email=email) if github_id: - or_expr = or_expr | Q(email=email) + or_expr = or_expr | Q(github_id=github_id) qs = user_model.objects.filter(or_expr) return qs.exists() @@ -113,7 +113,10 @@ def public_register(username:str, password:str, email:str, full_name:str): email=email, full_name=full_name) user.set_password(password) - user.save() + try: + user.save() + except IntegrityError: + raise exc.IntegrityError("User is already register.") # send_public_register_email(user) return user @@ -132,8 +135,11 @@ def private_register_for_existing_user(token:str, username:str, password:str): user = get_and_validate_user(username=username, password=password) membership = get_membership_by_token(token) - membership.user = user - membership.save(update_fields=["user"]) + try: + membership.user = user + membership.save(update_fields=["user"]) + except IntegrityError: + raise exc.IntegrityError("Membership with user is already exists.") # send_private_register_email(user) return user diff --git a/tests/integration/test_auth_api.py b/tests/integration/test_auth_api.py index 1cb81a9b..56a8140f 100644 --- a/tests/integration/test_auth_api.py +++ b/tests/integration/test_auth_api.py @@ -37,18 +37,20 @@ def register_form(): "type": "public"} -def test_respond_201_if_domain_allows_public_registration(client, settings, register_form): +def test_respond_201_when_public_registration_is_enabled(client, settings, register_form): settings.PUBLIC_REGISTER_ENABLED = True response = client.post(reverse("auth-register"), register_form) assert response.status_code == 201 -def test_respond_400_if_domain_does_not_allow_public_registration(client, register_form): +def test_respond_400_when_public_registration_is_disabled(client, register_form, settings): + settings.PUBLIC_REGISTER_ENABLED = False response = client.post(reverse("auth-register"), register_form) assert response.status_code == 400 -def test_respond_201_with_invitation_if_domain_does_not_allows_public_registration(client, register_form): +def test_respond_201_with_invitation_without_public_registration(client, register_form, settings): + settings.PUBLIC_REGISTER_ENABLED = False user = factories.UserFactory() membership = factories.MembershipFactory(user=user) @@ -66,7 +68,8 @@ def test_respond_201_with_invitation_if_domain_does_not_allows_public_registrati assert response.status_code == 201, response.data -def test_response_200_in_registration_with_github_account(client): +def test_response_200_in_registration_with_github_account(client, settings): + settings.PUBLIC_REGISTER_ENABLED = False form = {"type": "github", "code": "xxxxxx"} @@ -87,7 +90,8 @@ def test_response_200_in_registration_with_github_account(client): assert response.data["github_id"] == 1955 -def test_response_200_in_registration_with_github_account_in_a_project(client): +def test_response_200_in_registration_with_github_account_in_a_project(client, settings): + settings.PUBLIC_REGISTER_ENABLED = False membership_model = apps.get_model("projects", "Membership") membership = factories.MembershipFactory(user=None) form = {"type": "github", @@ -106,7 +110,8 @@ def test_response_200_in_registration_with_github_account_in_a_project(client): assert membership_model.objects.get(token=form["token"]).user.username == "mmcfly" -def test_response_404_in_registration_with_github_account_in_a_project_with_invalid_token(client): +def test_response_404_in_registration_with_github_in_a_project_with_invalid_token(client, settings): + settings.PUBLIC_REGISTER_ENABLED = False form = {"type": "github", "code": "xxxxxx", "token": "123456"} @@ -122,7 +127,7 @@ def test_response_404_in_registration_with_github_account_in_a_project_with_inva assert response.status_code == 404 -def test_respond_400_If_username_is_invalid(client, settings, register_form): +def test_respond_400_if_username_is_invalid(client, settings, register_form): settings.PUBLIC_REGISTER_ENABLED = True register_form.update({"username": "User Examp:/e"}) @@ -132,3 +137,16 @@ def test_respond_400_If_username_is_invalid(client, settings, register_form): register_form.update({"username": 300*"a"}) response = client.post(reverse("auth-register"), register_form) assert response.status_code == 400 + + +def test_respond_400_if_username_or_email_is_duplicate(client, settings, register_form): + settings.PUBLIC_REGISTER_ENABLED = True + + response = client.post(reverse("auth-register"), register_form) + assert response.status_code == 201 + + + register_form["username"] = "username" + register_form["email"] = "ff@dd.com" + response = client.post(reverse("auth-register"), register_form) + assert response.status_code == 400