Skip to content

Commit

Permalink
(surveys) manage permission to access survey create form, with tally …
Browse files Browse the repository at this point in the history
…waiting list
  • Loading branch information
vincentporte committed Feb 8, 2024
1 parent f6d4d59 commit ff759e5
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 38 deletions.
27 changes: 15 additions & 12 deletions lacommunaute/surveys/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest # noqa
from django.urls import reverse
from pytest_django.asserts import assertContains, assertNotContains

from django.contrib.auth.models import Permission
from lacommunaute.surveys.factories import DSPFactory
from lacommunaute.surveys.models import DSP
from lacommunaute.users.factories import UserFactory
Expand All @@ -25,6 +25,18 @@ def test_login_required(self, db, client):
response = client.get(url)
assert response.status_code == 302

def test_user_has_no_permission(self, db, client):
client.force_login(UserFactory())
response = client.get(reverse("surveys:dsp_create"))
assertContains(response, '<iframe data-tally-src="https://tally.so/embed')
assertNotContains(response, '<form method="post">')

def test_user_has_permission(self, db, client):
client.force_login(UserFactory(with_perm=[Permission.objects.get(codename="add_dsp")]))
response = client.get(reverse("surveys:dsp_create"))
assertContains(response, '<form method="post">')
assertNotContains(response, '<iframe data-tally-src="https://tally.so/embed')

def test_form_fields(self, db, client):
url = reverse("surveys:dsp_create")
client.force_login(UserFactory())
Expand Down Expand Up @@ -69,15 +81,6 @@ def test_user_can_view_own_survey(self, db, client):

class TestHomeView:
def test_link_to_dsp(self, db, client):
user = UserFactory()
client.force_login(user)
url = reverse("pages:home")
response = client.get(url)
assert response.status_code == 200
assertNotContains(response, reverse("surveys:dsp_create"))

user.is_superuser = True
user.save()
response = client.get(url)
assert response.status_code == 200
client.force_login(UserFactory())
response = client.get(reverse("pages:home"))
assertContains(response, reverse("surveys:dsp_create"))
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
<li>
<a href="{{ calendar_url }}" class="{% if request.path == calendar_url %}is-active{% endif %}">{% trans "Events" %}</a>
</li>
{% if request.user.is_superuser %}
<li>
<a href="{{ dsp_url }}">Diagnostic Parcours IAE</a>
</li>
{% endif %}
<li>
<a href="{{ dsp_url }}" class="matomo-event" data-matomo-category="engagement" data-matomo-action="view" data-matomo-option="dsp">Diagnostic Parcours IAE</a>
</li>
</ul>
59 changes: 38 additions & 21 deletions lacommunaute/templates/surveys/dsp_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,54 @@ <h1 class="s-title-01__title h1">Diagnostic Parcours IAE</h1>
<div class="s-section__container container">
<div class="s-section__row row">
<div class="s-section__col col-12">
<div class="c-form">
<form method="post">
{% csrf_token %}
{% for field in form %}
<fieldset>
{% if perms.surveys.add_dsp %}
<div class="c-form">
<form method="post">
{% csrf_token %}
{% for field in form %}
<fieldset>
<div class="row">
<div class="col-12 col-lg-8">
<legend>{{ field.label_tag }}</legend>
<div class="form-group form-group-required">{{ field }}</div>
</div>
</div>
</fieldset>
<div class="row">
<div class="col-12 col-lg-8">
<legend>{{ field.label_tag }}</legend>
<div class="form-group form-group-required">{{ field }}</div>
<div class="col-12 col-lg-7">
<hr />
</div>
</div>
</fieldset>
{% endfor %}
<div class="row">
<div class="col-12 col-lg-7">
<hr />
</div>
</div>
{% endfor %}
<div class="row">
<div class="col-12 col-lg-7">
<div class="form-row justify-content-end">
<div class="form-group col-12 col-lg-auto">
<button type="submit" class="btn btn-block btn-primary">Valider</button>
<div class="form-row justify-content-end">
<div class="form-group col-12 col-lg-auto">
<button type="submit" class="btn btn-block btn-primary matomo-event" data-matomo-category="engagement" data-matomo-action="dsp" data-matomo-option="submit-dsp">
Valider
</button>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
</form>
</div>
{% else %}
<iframe data-tally-src="https://tally.so/embed/w51b56?alignLeft=1&hideTitle=1&transparentBackground=1&dynamicHeight=1"
loading="lazy"
width="100%"
height="262"
frameborder="0"
marginheight="0"
marginwidth="0"
title="DSP waiting list"></iframe>
{% endif %}
</div>
</div>
</div>
</section>
{% endblock content %}
{% block extra_js %}
{{ block.super }}
<script async src="https://tally.so/widgets/embed.js"></script>
{% endblock extra_js %}
6 changes: 6 additions & 0 deletions lacommunaute/users/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ class Meta:
last_name = factory.Faker("last_name")
email = factory.Faker("email")
password = factory.LazyFunction(default_password)

@factory.post_generation
def with_perm(obj, create, extracted, **kwargs):
if not create or not extracted:
return
obj.user_permissions.add(*extracted)

0 comments on commit ff759e5

Please sign in to comment.