Skip to content

Commit

Permalink
format commcare username from xform
Browse files Browse the repository at this point in the history
format commcare username from xform
  • Loading branch information
snopoke committed Sep 14, 2023
1 parent 3f99438 commit bcbc0f2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
12 changes: 10 additions & 2 deletions commcare_connect/form_receiver/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,15 @@ def get_app(domain, app_id):


def get_user(xform: XForm):
user = User.objects.filter(connectiduserlink__commcare_username=xform.metadata.username).first()
cc_username = _get_commcare_username(xform)
user = User.objects.filter(connectiduserlink__commcare_username=cc_username).first()
if not user:
raise ProcessingError(f"Commcare User {xform.metadata.username} not found")
raise ProcessingError(f"Commcare User {cc_username} not found")
return user


def _get_commcare_username(xform: XForm):
username = xform.metadata.username
if "@" in username:
return username
return f"{username}@{xform.domain}.commcarehq.org"
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,27 @@
from commcare_connect.form_receiver.tests.xforms import AssessmentStubFactory, LearnModuleJsonFactory, get_form_json
from commcare_connect.opportunity.models import Assessment, CompletedModule, LearnModule, Opportunity, UserVisit
from commcare_connect.opportunity.tests.factories import LearnModuleFactory, OpportunityFactory
from commcare_connect.users.models import User
from commcare_connect.users.models import ConnectIDUserLink, User
from commcare_connect.users.tests.factories import MobileUserFactory


@pytest.fixture()
def opportunity():
return OpportunityFactory()


@pytest.fixture
def mobile_user_with_connect_link(db, opportunity: Opportunity) -> User:
user = MobileUserFactory()
links = [ConnectIDUserLink(user=user, commcare_username=f"test@{opportunity.learn_app.cc_domain}.commcarehq.org")]
if opportunity.learn_app.cc_domain != opportunity.deliver_app.cc_domain:
links.append(
ConnectIDUserLink(user=user, commcare_username=f"test@{opportunity.deliver_app.cc_domain}.commcarehq.org")
)
ConnectIDUserLink.objects.bulk_create(links)
return user


@pytest.mark.django_db
def test_form_receiver_learn_module(
mobile_user_with_connect_link: User, api_client: APIClient, opportunity: Opportunity
Expand Down
4 changes: 3 additions & 1 deletion commcare_connect/users/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ class Meta:


class MobileUserWithConnectIDLink(MobileUserFactory):
connectiduserlink = RelatedFactory(ConnectIdUserLinkFactory, "user", commcare_username="test")
connectiduserlink = RelatedFactory(
ConnectIdUserLinkFactory, "user", commcare_username="[email protected]"
)


class OrganizationFactory(DjangoModelFactory):
Expand Down

0 comments on commit bcbc0f2

Please sign in to comment.