mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-05-15 17:17:02 +02:00
Retrieve domain invitations on first login
This commit is contained in:
parent
8338704315
commit
e54dda3ddd
4 changed files with 45 additions and 0 deletions
|
@ -49,6 +49,8 @@ class OpenIdConnectBackend(ModelBackend):
|
||||||
user, created = UserModel.objects.update_or_create(**args)
|
user, created = UserModel.objects.update_or_create(**args)
|
||||||
if created:
|
if created:
|
||||||
user = self.configure_user(user, **kwargs)
|
user = self.configure_user(user, **kwargs)
|
||||||
|
# run a newly created user's callback for a first-time login
|
||||||
|
user.first_login()
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
user = UserModel.objects.get_by_natural_key(username)
|
user = UserModel.objects.get_by_natural_key(username)
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
from django.contrib.auth.models import AbstractUser
|
from django.contrib.auth.models import AbstractUser
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
|
from .domain_invitation import DomainInvitation
|
||||||
|
|
||||||
from phonenumber_field.modelfields import PhoneNumberField # type: ignore
|
from phonenumber_field.modelfields import PhoneNumberField # type: ignore
|
||||||
|
|
||||||
|
|
||||||
|
@ -31,3 +33,14 @@ class User(AbstractUser):
|
||||||
return self.email
|
return self.email
|
||||||
else:
|
else:
|
||||||
return self.username
|
return self.username
|
||||||
|
|
||||||
|
def first_login(self):
|
||||||
|
"""Callback when the user is authenticated for the very first time.
|
||||||
|
|
||||||
|
When a user first arrives on the site, we need to retrieve any domain
|
||||||
|
invitations that match their email address.
|
||||||
|
"""
|
||||||
|
for invitation in DomainInvitation.objects.filter(
|
||||||
|
email=self.email, status=DomainInvitation.SENT
|
||||||
|
):
|
||||||
|
invitation.retrieve()
|
||||||
|
|
|
@ -193,6 +193,11 @@ class TestInvitations(TestCase):
|
||||||
with self.assertRaises(RuntimeError):
|
with self.assertRaises(RuntimeError):
|
||||||
self.invitation.retrieve()
|
self.invitation.retrieve()
|
||||||
|
|
||||||
|
def test_retrieve_on_first_login(self):
|
||||||
|
"""A new user's first_login callback retrieves their invitations."""
|
||||||
|
self.user.first_login()
|
||||||
|
self.assertTrue(UserDomainRole.objects.get(user=self.user, domain=self.domain))
|
||||||
|
|
||||||
|
|
||||||
@skip("Not implemented yet.")
|
@skip("Not implemented yet.")
|
||||||
class TestDomainApplicationLifeCycle(TestCase):
|
class TestDomainApplicationLifeCycle(TestCase):
|
||||||
|
|
|
@ -1196,3 +1196,28 @@ class TestDomainDetail(TestWithDomainPermissions, WebTest):
|
||||||
self.client.post(reverse("invitation-delete", kwargs={"pk": invitation.id}))
|
self.client.post(reverse("invitation-delete", kwargs={"pk": invitation.id}))
|
||||||
with self.assertRaises(DomainInvitation.DoesNotExist):
|
with self.assertRaises(DomainInvitation.DoesNotExist):
|
||||||
DomainInvitation.objects.get(id=invitation.id)
|
DomainInvitation.objects.get(id=invitation.id)
|
||||||
|
|
||||||
|
@boto3_mocking.patching
|
||||||
|
def test_domain_invitation_flow(self):
|
||||||
|
"""Send an invitation to a new user, log in and load the dashboard."""
|
||||||
|
EMAIL = "mayor@igorville.gov"
|
||||||
|
User.objects.filter(email=EMAIL).delete()
|
||||||
|
|
||||||
|
add_page = self.app.get(
|
||||||
|
reverse("domain-users-add", kwargs={"pk": self.domain.id})
|
||||||
|
)
|
||||||
|
session_id = self.app.cookies[settings.SESSION_COOKIE_NAME]
|
||||||
|
add_page.form["email"] = EMAIL
|
||||||
|
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session_id)
|
||||||
|
add_page.form.submit()
|
||||||
|
|
||||||
|
# user was invited, create them
|
||||||
|
new_user = User.objects.create(username=EMAIL, email=EMAIL)
|
||||||
|
# log them in to `self.app`
|
||||||
|
self.app.set_user(new_user.username)
|
||||||
|
# and manually call the first login callback
|
||||||
|
new_user.first_login()
|
||||||
|
|
||||||
|
# Now load the home page and make sure our domain appears there
|
||||||
|
home_page = self.app.get(reverse("home"))
|
||||||
|
self.assertContains(home_page, self.domain.name)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue