Skip to content
Snippets Groups Projects
Commit 7abd1219 authored by Sivert Kronen Hatteberg's avatar Sivert Kronen Hatteberg
Browse files

Add the sponsor work-email on login, use the new FEIDE_EMAIL for persons

Issue: GREG-121-FEIDE-EMAIL
parent 23056177
No related branches found
No related tags found
1 merge request!168Sponsor email and feide-email on persons
Pipeline #100964 passed
......@@ -428,12 +428,12 @@ class GregOIDCBackend(ValidatingOIDCBackend):
def _update_person_email(self, person: Person, new_email: str, source: str):
"""Add or update a users email."""
logger.bind(identity_type=Identity.IdentityType.PRIVATE_EMAIL)
logger.bind(identity_type=Identity.IdentityType.FEIDE_EMAIL)
# We only look at emails from the same source.
emails: list[Identity] = [
x
for x in person.identities.filter(
type=Identity.IdentityType.PRIVATE_EMAIL
type=Identity.IdentityType.FEIDE_EMAIL
).all()
if x.source == source
]
......@@ -469,7 +469,7 @@ class GregOIDCBackend(ValidatingOIDCBackend):
email_id = Identity(
person=person,
source=source,
type=Identity.IdentityType.PRIVATE_EMAIL,
type=Identity.IdentityType.FEIDE_EMAIL,
value=new_email,
verified=Identity.Verified.AUTOMATIC,
verified_at=make_aware(datetime.datetime.now()),
......@@ -528,6 +528,7 @@ class GregOIDCBackend(ValidatingOIDCBackend):
if userinfo["auth_type"] == "feide" and sponsor:
sponsor.first_name = userinfo["first_name"]
sponsor.last_name = userinfo["last_name"]
sponsor.work_email = userinfo["email"]
sponsor.save()
def _get_or_create_greg_user_profile(self, userinfo: dict, user: UserModel):
......
......@@ -37,7 +37,7 @@ def test_sponsor_first_login(sponsor_foo):
@pytest.mark.django_db
def test_sponsor_update_name(sponsor_foo):
def test_sponsor_update(sponsor_foo):
auth_request = RequestFactory().get("/foo", {"code": "foo", "state": "bar"})
auth_request.session = {}
......@@ -47,12 +47,15 @@ def test_sponsor_update_name(sponsor_foo):
with pytest.raises(GregUserProfile.DoesNotExist):
GregUserProfile.objects.get(sponsor=sponsor_foo)
assert sponsor_foo.first_name == "Sponsor"
assert sponsor_foo.work_email == "foo@example.org"
claims = {
"sub": "subsub",
"connect-userid_sec": [f"feide:{sponsor_foo.feide_id}"],
"dataporten-userid_sec": [f"feide:{sponsor_foo.feide_id}", "nin:12345612345"],
"name": f"Baz {sponsor_foo.last_name}",
"email": "foo@example.org",
"email": "baz@example.org",
"email_verified": True,
"picture": "https://foo.org/p:2192dff7-6989-4244-83cc-ae5e78875bdd",
}
......@@ -63,6 +66,7 @@ def test_sponsor_update_name(sponsor_foo):
assert user_profile.sponsor == sponsor_foo
assert user_profile.sponsor.first_name == "Baz"
assert user_profile.sponsor.work_email == "baz@example.org"
@pytest.mark.django_db
......@@ -158,7 +162,7 @@ def test_invited_user(invited_person):
"connect-userid_sec": [f"feide:{feide_id}"],
"dataporten-userid_sec": [f"feide:{feide_id}", f"nin:{nin}"],
"name": f"{person.first_name} {person.last_name}",
"email": f"{person.private_email.value}",
"email": f"{feide_id}",
"email_verified": True,
"picture": "https://foo.org/p:2192dff7-6989-4244-83cc-ae5e78875bdd",
}
......@@ -172,18 +176,25 @@ def test_invited_user(invited_person):
person_nins = person.identities.filter(
type=Identity.IdentityType.NORWEGIAN_NATIONAL_ID_NUMBER,
verified=Identity.Verified.AUTOMATIC,
).all()
person_feide_ids = person.identities.filter(
type=Identity.IdentityType.FEIDE_ID, verified=Identity.Verified.AUTOMATIC
).all()
assert len(person_nins) == 1
assert person_nins[0].verified == Identity.Verified.AUTOMATIC
assert person_nins[0].value == nin
person_feide_ids = person.identities.filter(
type=Identity.IdentityType.FEIDE_ID,
).all()
assert len(person_feide_ids) == 1
assert person_feide_ids[0].verified == Identity.Verified.AUTOMATIC
assert person_feide_ids[0].value == feide_id
person_feide_email = person.identities.filter(
type=Identity.IdentityType.FEIDE_EMAIL
).all()
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_user_no_id(invited_person_no_ids):
......@@ -224,7 +235,7 @@ def test_invited_user_no_id(invited_person_no_ids):
"connect-userid_sec": ["feide:{feide_id}"],
"dataporten-userid_sec": [f"feide:{feide_id}", f"nin:{nin}"],
"name": f"{person.first_name} {person.last_name}",
"email": f"{person.private_email.value}",
"email": f"{feide_id}",
"email_verified": True,
"picture": "https://foo.org/p:2192dff7-6989-4244-83cc-ae5e78875bdd",
}
......@@ -236,18 +247,21 @@ def test_invited_user_no_id(invited_person_no_ids):
person_nins = person.identities.filter(
type=Identity.IdentityType.NORWEGIAN_NATIONAL_ID_NUMBER,
verified=Identity.Verified.AUTOMATIC,
).all()
person_feide_ids = person.identities.filter(
type=Identity.IdentityType.FEIDE_ID, verified=Identity.Verified.AUTOMATIC
).all()
assert len(person_nins) == 1
assert person_nins[0].value == nin
assert person_nins[0].verified == Identity.Verified.AUTOMATIC
person_feide_ids = person.identities.filter(
type=Identity.IdentityType.FEIDE_ID
).all()
assert len(person_feide_ids) == 1
assert person_feide_ids[0].value == feide_id
assert person_feide_ids[0].verified == Identity.Verified.AUTOMATIC
def test_nin_feide():
pass
person_feide_email = person.identities.filter(
type=Identity.IdentityType.FEIDE_EMAIL
).all()
assert len(person_feide_email) == 1
assert person_feide_email[0].value == feide_id
assert person_feide_email[0].verified == Identity.Verified.AUTOMATIC
......@@ -82,11 +82,12 @@ def role_type_foo() -> RoleType:
@pytest.fixture
def create_sponsor() -> Callable[[str, str, str, OrganizationalUnit], Sponsor]:
def create_sponsor(feide_id, first_name, last_name, unit):
def create_sponsor(feide_id, first_name, last_name, unit, work_email=None):
sponsor = Sponsor(
feide_id=feide_id,
first_name=first_name,
last_name=last_name,
work_email=work_email,
)
sponsor.save()
......@@ -99,7 +100,12 @@ def create_sponsor() -> Callable[[str, str, str, OrganizationalUnit], Sponsor]:
@pytest.fixture
def sponsor_foo_data() -> dict:
return dict(feide_id="foo@example.org", first_name="Sponsor", last_name="Bar")
return dict(
feide_id="foo@example.org",
first_name="Sponsor",
last_name="Bar",
work_email="foo@example.org",
)
@pytest.fixture
......
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