Skip to content
Snippets Groups Projects
Commit f3ae0542 authored by Stein Elgethun's avatar Stein Elgethun
Browse files

Test that existing user overwrites invite properly

Issue: greg-105
parent 0b75f3b7
No related branches found
No related tags found
1 merge request!225Login with existing user and invite
Pipeline #109315 passed
......@@ -4,7 +4,7 @@ from django.test.client import RequestFactory
from greg.models import Identity
from gregui.authentication.auth_backends import GregOIDCBackend
from gregui.models import GregUserProfile
from gregui.models import GregUserProfile, Person
@pytest.mark.django_db
......@@ -265,3 +265,56 @@ def test_invited_user_no_id(invited_person_no_ids):
assert len(person_feide_email) == 1
assert person_feide_email[0].value == feide_id
assert person_feide_email[0].verified == Identity.Verified.AUTOMATIC
@pytest.mark.django_db
def test_invited_existing_user(invited_person, user_person):
"""
Invitation login, user with existing person and GregUserProfile
Expect the person connected to the invite to be deleted and replaced by
the existing person
"""
old_user = user_person
old_person = GregUserProfile.objects.get(user=old_user).person
feide_id = (
old_person.identities.filter(type=Identity.IdentityType.FEIDE_ID).first().value
)
person, invitation_link = invited_person
auth_request = RequestFactory().get("/foo", {"code": "foo", "state": "bar"})
auth_request.session = {"invite_id": invitation_link.uuid}
backend = GregOIDCBackend()
backend.request = auth_request
with pytest.raises(GregUserProfile.DoesNotExist):
GregUserProfile.objects.get(person=person)
claims = {
"sub": f"{old_user.username}",
"connect-userid_sec": [f"feide:{feide_id}"],
"dataporten-userid_sec": [f"feide:{feide_id}", f"nin:{old_person.fnr.value}"],
"name": f"{old_person.first_name} {old_person.last_name}",
"email": f"{feide_id}",
"email_verified": True,
"picture": "https://foo.org/p:2192dff7-6989-4244-83cc-ae5e78875bdd",
}
# set username like this user was created by logging in
old_user.username = backend.get_username(old_user.username)
old_user.save()
assert invitation_link.invitation.role.person == person
user = backend.update_user(old_user, claims)
invitation_link.refresh_from_db()
assert invitation_link.invitation.role.person == old_person
old_person.refresh_from_db()
with pytest.raises(Person.DoesNotExist):
person.refresh_from_db()
user_profile = GregUserProfile.objects.get(user=user)
assert user_profile.person == old_person
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment