Skip to content

Commit

Permalink
Fix candidate demo account login button
Browse files Browse the repository at this point in the history
The action_url for job-seeker login has changed, causing the JavaScript handling demo account login to break. This commit fixes the action_url and adds a test to that effect
  • Loading branch information
calummackervoy committed Jan 24, 2025
1 parent f80cb80 commit e262d0a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
1 change: 1 addition & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
ROOT_DIR = os.path.abspath(os.path.join(_current_dir, "../.."))

APPS_DIR = os.path.abspath(os.path.join(ROOT_DIR, "itou"))
FIXTURE_DIRS = [os.path.abspath(os.path.join(ROOT_DIR, "itou/fixtures/django"))]

SECRET_KEY = os.getenv("DJANGO_SECRET_KEY")

Expand Down
19 changes: 17 additions & 2 deletions itou/utils/templatetags/demo_accounts.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import logging

from django import template
from django.urls import reverse

from itou.users.enums import UserKind
from itou.users.models import User


logger = logging.getLogger(__name__)
register = template.Library()


Expand Down Expand Up @@ -79,5 +85,14 @@ def prescribers_accounts_tag():

@register.simple_tag
def job_seekers_accounts_tag():
action_url = reverse("login:job_seeker")
return [{"email": "test+de@inclusion.gouv.fr", "image": "de.svg", "action_url": action_url}]
user_email = "test+de@inclusion.gouv.fr"
try:
user_public_id = User.objects.get(kind=UserKind.JOB_SEEKER, email=user_email).public_id
except User.DoesNotExist:
logger.warning(
f"Unable to initialise job_seekers_accounts_tag: no job seeker with email='{user_email}' found !"
)
return [] # Fail.

action_url = reverse("login:existing_user", kwargs={"user_public_id": user_public_id})
return [{"email": user_email, "image": "de.svg", "action_url": action_url}]
42 changes: 42 additions & 0 deletions tests/utils/test_templatetags.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import pytest
from django import forms
from django.contrib.auth import authenticate, get_user
from django.core.management import call_command
from django.template import Context, Template
from django.test import override_settings
from django.urls import reverse

from itou.users.enums import UserKind
from itou.users.models import User
from itou.utils.templatetags.demo_accounts import (
employers_accounts_tag,
job_seekers_accounts_tag,
prescribers_accounts_tag,
)
from itou.utils.templatetags.nav import NAV_ENTRIES


Expand Down Expand Up @@ -103,3 +113,35 @@ class NIRForm(forms.Form):
template = Template("{% load theme_inclusion %}" + field_markup * 2)
with pytest.raises(NotImplementedError):
template.render(Context({"form": NIRForm()}))


@pytest.fixture
def load_test_users():
call_command("loaddata", "05_test_users.json")
call_command("loaddata", "06_confirmed_emails.json")


class TestDemoAccount:
@pytest.mark.parametrize(
"user_kind,template_tag",
[
(UserKind.EMPLOYER, employers_accounts_tag),
(UserKind.PRESCRIBER, prescribers_accounts_tag),
(UserKind.JOB_SEEKER, job_seekers_accounts_tag),
],
)
@override_settings(SHOW_DEMO_ACCOUNTS_BANNER=True)
def test_can_login_to_demo_account(self, client, load_test_users, user_kind, template_tag):
password = "password"

for account in template_tag():
email = account["email"]
user = User.objects.get(kind=user_kind, email=email)
assert authenticate(email=email, password=password) == user

response = client.post(
account["action_url"], {"login": email, "password": password, "demo_banner_account": True}
)
# NOTE: Login redirects tested elsewhere.
assert response.status_code == 302
assert get_user(client).is_authenticated is True

0 comments on commit e262d0a

Please sign in to comment.