Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
felixrindt committed Jan 15, 2021
1 parent 9abc244 commit 266b49f
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<div class="col-lg-8">
<dl>
<dt>
{% translate "Confirmed" %}
{% translate "Signed up" %}
</dt>
<dd>
<ul>
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ too-few-public-methods, no-self-use, too-many-arguments, cyclic-import, inconsis
max-line-length = "100"

[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "ephios.settings"
DJANGO_SETTINGS_MODULE = "tests.settings"
addopts = "--fail-on-template-vars"

[build-system]
Expand Down
7 changes: 6 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
)


@pytest.fixture
def csrf_exempt_django_app(django_app_factory):
return django_app_factory(csrf_checks=False)


@pytest.fixture
def tz():
return pytz.timezone("Europe/Berlin")
Expand Down Expand Up @@ -62,7 +67,7 @@ def planner():


@pytest.fixture
def volunteer():
def volunteer(qualifications):
return UserProfile.objects.create(
first_name="Heinrich",
last_name="Helper",
Expand Down
57 changes: 57 additions & 0 deletions tests/plugins/basesignup/test_requestconfirm_signup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import pytest
from django.urls import reverse
from guardian.shortcuts import get_users_with_perms

from ephios.event_management.models import AbstractParticipation, LocalParticipation


@pytest.mark.django_db
def test_request_confirm_signup_flow(django_app, volunteer, planner, event):
# request a participation as volunteer
assert volunteer in get_users_with_perms(event, only_with_perms_in=["view_event"])
response = django_app.get(
reverse("event_management:event_detail", kwargs=dict(pk=event.pk)), user=volunteer
)
response.form.submit(name="signup_choice", value="sign_up")
shift = event.shifts.first()
assert (
LocalParticipation.objects.get(user=volunteer, shift=shift).state
== AbstractParticipation.States.REQUESTED
)

response = django_app.get(
reverse("event_management:event_detail", kwargs=dict(pk=event.pk)), user=volunteer
)
assert "already requested" in response

# confirm the participation as planner
response = django_app.get(
reverse("basesignup:shift_disposition_requestconfirm", kwargs=dict(pk=shift.pk)),
user=planner,
)
response.form["form-0-state"] = AbstractParticipation.States.CONFIRMED.value
response.form.submit()
assert (
LocalParticipation.objects.get(user=volunteer, shift=shift).state
== AbstractParticipation.States.CONFIRMED
)


@pytest.mark.django_db
def test_request_confirm_decline_flow(django_app, volunteer, planner, event):
# decline a participation as volunteer
assert volunteer in get_users_with_perms(event, only_with_perms_in=["view_event"])
response = django_app.get(
reverse("event_management:event_detail", kwargs=dict(pk=event.pk)), user=volunteer
)
response.form.submit(name="signup_choice", value="decline")
shift = event.shifts.first()
assert (
LocalParticipation.objects.get(user=volunteer, shift=shift).state
== AbstractParticipation.States.USER_DECLINED
)

response = django_app.get(
reverse("event_management:event_detail", kwargs=dict(pk=event.pk)), user=volunteer
)
assert "already declined" in response
145 changes: 145 additions & 0 deletions tests/plugins/basesignup/test_sectionbased_signup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
from collections import OrderedDict
from datetime import datetime

import pytest
from django.urls import reverse

from ephios.event_management.models import AbstractParticipation, LocalParticipation, Shift
from ephios.plugins.basesignup.signup.section_based import SectionBasedSignupMethod
from ephios.user_management.models import QualificationGrant

KTW_UUID = "f92fe836-6dc5-4afd-b488-aee3fe7eda32"


@pytest.fixture
def sectioned_shift(event, tz, qualifications):
return Shift.objects.create(
event=event,
meeting_time=datetime(2099, 7, 1, 7, 0).astimezone(tz),
start_time=datetime(2099, 7, 1, 8, 0).astimezone(tz),
end_time=datetime(2099, 7, 1, 20, 0).astimezone(tz),
signup_method_slug=SectionBasedSignupMethod.slug,
signup_configuration={
"minimum_age": None,
"signup_until": None,
"user_can_decline_confirmed": False,
"choose_preferred_section": True,
"sections": [
{
"title": "KTW",
"qualifications": [qualifications.b, qualifications.rs],
"min_count": 2,
"uuid": KTW_UUID,
},
{
"title": "ITS",
"qualifications": [qualifications.na],
"min_count": 1,
"uuid": "e2df480f-1f11-442c-be11-3cccf7e6ea19",
},
],
},
)


@pytest.mark.django_db
@pytest.mark.ignore_template_errors # shift_form uses unbound object to look special for creation
def test_configuration(csrf_exempt_django_app, planner, event, qualifications):
POST_DATA = OrderedDict(
{
"choose_preferred_section": "on",
"date": "2023-06-30",
"end_time": "01:00:00",
"meeting_time": "15:30:00",
"minimum_age": "",
"sections": "",
"sections-0-min_count": "2",
"sections-0-qualifications": str(qualifications.rs.id),
"sections-0-title": "KTW 10-85-1",
"sections-0-uuid": "",
"sections-1-min_count": "2",
"sections-1-qualifications": str(qualifications.na.id),
"sections-1-title": "ITS",
"sections-1-uuid": "",
"sections-INITIAL_FORMS": "2",
"sections-MAX_NUM_FORMS": "1000",
"sections-MIN_NUM_FORMS": "1",
"sections-TOTAL_FORMS": "2",
"signup_method_slug": "section_based",
"signup_until_0": "",
"signup_until_1": "",
"start_time": "16:00:00",
}
)

csrf_exempt_django_app.get(
reverse("event_management:event_createshift", kwargs=dict(pk=event.pk)),
user=planner,
)
# no csrf check for plain post
csrf_exempt_django_app.post(
reverse("event_management:event_createshift", kwargs=dict(pk=event.pk)),
user=planner,
params=POST_DATA,
).follow()
_, shift = event.shifts.order_by("id")
assert shift.signup_method_slug == SectionBasedSignupMethod.slug
assert len(shift.signup_method.configuration.sections) == 2


@pytest.fixture
def qualified_volunteer(volunteer, qualifications, tz):
QualificationGrant.objects.create(
user=volunteer,
qualification=qualifications.nfs,
expires=datetime(2064, 4, 1).astimezone(tz),
)
QualificationGrant.objects.create(
user=volunteer, qualification=qualifications.c, expires=datetime(2090, 4, 1).astimezone(tz)
)
QualificationGrant.objects.create(user=volunteer, qualification=qualifications.b, expires=None)
return volunteer


@pytest.mark.django_db
def test_signup_flow(django_app, qualified_volunteer, planner, event, sectioned_shift):
# request a participation as volunteer on *second* shift
response = (
django_app.get(
reverse("event_management:event_detail", kwargs=dict(pk=event.pk)),
user=qualified_volunteer,
)
.forms[1]
.submit(name="signup_choice", value="sign_up")
.follow()
)
response.form["section"] = KTW_UUID
response.form.submit()
assert (
LocalParticipation.objects.get(user=qualified_volunteer, shift=sectioned_shift).state
== AbstractParticipation.States.REQUESTED
)

# test we can't signup again
assert (
"You can not sign up for this shift."
in django_app.get(
reverse("event_management:event_detail", kwargs=dict(pk=event.pk)),
user=qualified_volunteer,
)
.forms[1]
.submit(name="signup_choice", value="sign_up")
.follow()
)

# confirm the participation as planner
response = django_app.get(
reverse("basesignup:shift_disposition_section_based", kwargs=dict(pk=sectioned_shift.pk)),
user=planner,
)
response.form["participations-0-state"] = AbstractParticipation.States.CONFIRMED.value
response.form.submit()
participation = LocalParticipation.objects.get(user=qualified_volunteer, shift=sectioned_shift)
assert participation.state == AbstractParticipation.States.CONFIRMED
assert participation.data.get("preferred_section_uuid") == KTW_UUID
assert participation.data.get("dispatched_section_uuid") == KTW_UUID
29 changes: 0 additions & 29 deletions tests/plugins/test_requestconfirm_signup.py

This file was deleted.

3 changes: 3 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from ephios.settings import * # no-qa

LANGUAGE_CODE = "en"
4 changes: 2 additions & 2 deletions tests/user_management/test_workinghours.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class TestWorkingHours:
def test_profile_without_hours(self, django_app, volunteer):
response = django_app.get(reverse("user_management:profile"), user=volunteer)
assert response.status_code == 200
assert response.html.find(text="0,0 Stunden")
assert response.html.find(text="0.0 hours")

def test_profile_with_hours(self, django_app, volunteer, workinghours):
response = django_app.get(reverse("user_management:profile"), user=volunteer)
assert response.html.find(text=workinghours[0].reason)
assert response.html.find(text=workinghours[1].reason)
total_hours = workinghours[0].hours + workinghours[1].hours
assert response.html.find(text=f"{total_hours:.1f} Stunden".replace(".", ","))
assert response.html.find(text=f"{total_hours:.1f} hours")

0 comments on commit 266b49f

Please sign in to comment.