Skip to content

Commit

Permalink
Merge pull request #35144 from dimagi/jc/disable-inviting-deactivated…
Browse files Browse the repository at this point in the history
…-user

Disabling invite a deactivated user
  • Loading branch information
jingcheng16 authored Sep 19, 2024
2 parents f6ae9e8 + 409c134 commit 7fb17e3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
5 changes: 4 additions & 1 deletion corehq/apps/registration/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from corehq.apps.hqwebapp import crispy as hqcrispy
from corehq.apps.programs.models import Program
from corehq.apps.users.forms import SelectUserLocationForm, BaseTableauUserForm
from corehq.apps.users.models import CouchUser
from corehq.apps.users.models import CouchUser, WebUser


class RegisterWebUserForm(forms.Form):
Expand Down Expand Up @@ -592,6 +592,9 @@ def clean_email(self):
if email.lower() in self.excluded_emails:
raise forms.ValidationError(_("A user with this email address is already in "
"this project or has a pending invitation."))
web_user = WebUser.get_by_username(email)
if web_user and not web_user.is_active:
raise forms.ValidationError(_("A user with this email address is deactivated. "))
return email

def clean(self):
Expand Down
26 changes: 24 additions & 2 deletions corehq/apps/registration/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from testil import Regex

from ..forms import AdminInvitesUserForm
from corehq.apps.users.models import WebUser


patch_query = patch.object(
Expand All @@ -12,16 +13,24 @@
lambda *ignore: True,
)

mock_couch_user = WebUser(
username="testuser",
_id="user123",
domain="test-domain",
)


@patch_query
def test_minimal_valid_form():
@patch("corehq.apps.users.models.WebUser.get_by_username", return_value=None)
def test_minimal_valid_form(mock_web_user):
form = create_form()

assert form.is_valid(), form.errors


@patch_query
def test_form_is_invalid_when_invite_existing_email_with_case_mismatch():
@patch("corehq.apps.users.models.WebUser.get_by_username", return_value=None)
def test_form_is_invalid_when_invite_existing_email_with_case_mismatch(mock_web_user):
form = create_form(
{"email": "[email protected]"},
excluded_emails=["[email protected]"],
Expand All @@ -32,6 +41,19 @@ def test_form_is_invalid_when_invite_existing_email_with_case_mismatch():
assert form.errors["email"] == [Regex(msg)], form.errors


@patch_query
@patch("corehq.apps.users.models.WebUser.get_by_username", return_value=mock_couch_user)
def test_form_is_invalid_when_invite_deactivated_user(mock_web_user):
mock_web_user.is_active = False
form = create_form(
{"email": "[email protected]"},
)

msg = "A user with this email address is deactivated."
assert not form.is_valid()
assert form.errors["email"] == [Regex(msg)], form.errors


def create_form(data=None, **kw):
form_defaults = {"email": "[email protected]", "role": "admin"}
request = RequestFactory().post("/", form_defaults | (data or {}))
Expand Down

0 comments on commit 7fb17e3

Please sign in to comment.