Skip to content

Commit ab99bc3

Browse files
committed
test: introduce validation for html views
1 parent 363067e commit ab99bc3

File tree

2 files changed

+182
-8
lines changed

2 files changed

+182
-8
lines changed

chores/templates/chores.html

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,20 @@
4646
<li style="list-style-type: none; padding-bottom:0 !important">
4747
<div>
4848
{% if volunteer == 'offer_volunteering' %}
49-
<a href="{% url 'signup_chore' chore_id=event.chore.chore_id ts=event.when.timestamp %}?redirect_to=chores" class="btn btn-outline-secondary btn-sm">Add me as volunteer</a>
49+
<a href="{% url 'signup_chore' chore_id=event.chore.chore_id ts=event.when.timestamp %}?redirect_to=chores"
50+
class="btn btn-outline-secondary btn-sm"
51+
data-test-hook="chores-volunteer-button">
52+
Add me as volunteer
53+
</a>
5054
{% elif volunteer is None %}
5155
<i>Volunteer needed</i>
5256
{% elif volunteer.id %}
53-
Volunteer:
54-
<a href="{{volunteer.path}}">
55-
{{ volunteer.first_name }}
56-
</a>
57-
57+
<div data-test-hook="chores-volunteer">
58+
Volunteer:
59+
<a href="{{volunteer.path}}">
60+
{{ volunteer.first_name }}
61+
</a>
62+
</div>
5863
{% endif %}
5964
</div>
6065
</li>
@@ -64,7 +69,10 @@
6469
{% if event.user_volunteered %}
6570
<li style="list-style-type: none; margin-top: 0.3em; padding-bottom:0 !important">
6671
<div>
67-
<a href="{% url 'remove_signup_chore' chore_id=event.chore.chore_id ts=event.when.timestamp %}?redirect_to=chores" class="btn btn-outline-secondary btn-sm">Remove me as volunteer</a>
72+
<a href="{% url 'remove_signup_chore' chore_id=event.chore.chore_id ts=event.when.timestamp %}?redirect_to=chores"
73+
class="btn btn-outline-secondary btn-sm"
74+
data-test-hook="chores-remove-volunteer">
75+
Remove me as volunteer</a>
6876
</div>
6977
</li>
7078
{% endif %}
@@ -77,7 +85,7 @@
7785
</ul>
7886
</div>
7987
{% else %}
80-
<div class="mt-2">
88+
<div data-test-hook="empty-chores" class="mt-2">
8189
<p>No upcoming tasks for the next 3 months.</p>
8290
<p>
8391
As a busy space with plenty to do that seems unlikely.<br/>

chores/tests/test_views.py

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
import json
2+
3+
import time_machine
4+
from django.contrib.auth import get_user_model
5+
from django.test import TestCase
6+
from django.urls import reverse
7+
from faker import Faker
8+
from rest_framework import status
9+
10+
from ..models import Chore, ChoreVolunteer
11+
from .factories import UserFactory
12+
13+
fake = Faker()
14+
15+
User = get_user_model()
16+
17+
18+
# Load the schema from a file
19+
def load_schema(schema_path):
20+
with open(schema_path, "r") as schema_file:
21+
return json.load(schema_file)
22+
23+
24+
class ChoresViewsTest(TestCase):
25+
def setUp(self):
26+
self.user = UserFactory(
27+
password="testpassword"
28+
)
29+
30+
def test_empty_chores_overview_200(self):
31+
"""Test that the empty chores overview returns a 200 response"""
32+
url = reverse("chores")
33+
success = self.client.login(email=self.user.email, password="testpassword")
34+
self.assertTrue(success)
35+
response = self.client.get(url)
36+
self.assertEqual(response.status_code, status.HTTP_200_OK)
37+
self.assertIn('data-test-hook="empty-chores"', response.content.decode("utf-8"))
38+
39+
def test_chores_overview_with_chore_200(self):
40+
url = reverse("chores")
41+
chore = Chore.objects.create(
42+
name=fake.name(),
43+
description="A test chore that needs volunteers.",
44+
class_type="BasicChore",
45+
wiki_url=fake.url(),
46+
configuration={
47+
"min_required_people": 1,
48+
"events_generation": {
49+
"event_type": "recurrent",
50+
"starting_time": "21/7/2021 8:00",
51+
"crontab": "0 22 * * sun",
52+
"take_one_every": 2,
53+
},
54+
"reminders": [
55+
{
56+
"reminder_type": "missing_volunteers",
57+
"when": {"days_before": 10, "time": "17:00"},
58+
"nudges": [
59+
{
60+
"nudge_type": "email",
61+
"nudge_key": "gentle_email_reminder",
62+
"destination": "[email protected]",
63+
"subject_template": "Volunteers needed next week",
64+
"body_template": "Hallo, we hebben {num_volunteers_needed} vrijwilliger nodig volgende week.\n\nOm stof in onze makerspace tegen te gaan willen we je vragen om te stofzuigen in de voorste ruimte en de houtwerkplaats, en eventueel de grote hal. \n\nAls je daar zin in hebt kun je ook de voorste ruimte dweilen met de hippe turbodweil die we hebben, maar als je weinig tijd hebt: Met alleen stofzuigen is al veel te winnen.\n\nDeze taak kan op elk moment gedurende de week worden gedaan. Je helpt wanneer het jou uitkomt, alle hulp is welkom!\n\nInformatie over schoonmaken op de Wiki: https://wiki.makerspaceleiden.nl/mediawiki/index.php/Chore_-_Dedustify \n\n\nClick here to sign up: {signup_url}",
65+
}
66+
],
67+
},
68+
{
69+
"reminder_type": "missing_volunteers",
70+
"when": {"days_before": 6, "time": "17:00"},
71+
"nudges": [
72+
{
73+
"nudge_type": "email",
74+
"nudge_key": "hard_email_reminder",
75+
"destination": "[email protected]",
76+
"subject_template": "Volunteers REALLY needed for this week",
77+
"body_template": "Hallo, we hebben nog steeds {num_volunteers_needed} vrijwilliger nodig voor deze week.\n\nOm stof in onze makerspace tegen te gaan willen we je vragen om te stofzuigen in de voorste ruimte en de houtwerkplaats, en eventueel de grote hal. \n\nAls je daar zin in hebt kun je ook de voorste ruimte dweilen met de hippe turbodweil die we hebben, maar als je weinig tijd hebt: Met alleen stofzuigen is al veel te winnen.\n\nDeze taak kan op elk moment gedurende de week worden gedaan. Je helpt wanneer het jou uitkomt, alle hulp is welkom!\n\nInformatie over schoonmaken op de Wiki: https://wiki.makerspaceleiden.nl/mediawiki/index.php/Chore_-_Dedustify \n\nClick here to sign up: {signup_url}",
78+
}
79+
],
80+
},
81+
{
82+
"reminder_type": "volunteers_who_signed_up",
83+
"when": {"days_before": 7, "time": "19:00"},
84+
},
85+
],
86+
},
87+
creator=self.user,
88+
)
89+
90+
success = self.client.login(email=self.user.email, password="testpassword")
91+
self.assertTrue(success)
92+
response = self.client.get(url)
93+
html = response.content.decode("utf-8")
94+
self.assertEqual(response.status_code, status.HTTP_200_OK)
95+
self.assertIn(chore.description, html)
96+
self.assertIn('data-test-hook="chores-volunteer-button"', html)
97+
self.assertNotIn('data-test-hook="chores-volunteer"', html)
98+
self.assertNotIn('data-test-hook="empty-chores"', html)
99+
100+
101+
def test_chores_overview_with_chore_and_volunteer_200(self):
102+
url = reverse("chores")
103+
chore = Chore.objects.create(
104+
name=fake.name(),
105+
description="A test chore that needs volunteers.",
106+
class_type="BasicChore",
107+
wiki_url=fake.url(),
108+
configuration={
109+
"min_required_people": 1,
110+
"events_generation": {
111+
"event_type": "recurrent",
112+
"starting_time": "21/7/2021 8:00",
113+
"crontab": "0 22 * * sun",
114+
"take_one_every": 2,
115+
},
116+
"reminders": [
117+
{
118+
"reminder_type": "missing_volunteers",
119+
"when": {"days_before": 10, "time": "17:00"},
120+
"nudges": [
121+
{
122+
"nudge_type": "email",
123+
"nudge_key": "gentle_email_reminder",
124+
"destination": "[email protected]",
125+
"subject_template": "Volunteers needed next week",
126+
"body_template": "Hallo, we hebben {num_volunteers_needed} vrijwilliger nodig volgende week.\n\nOm stof in onze makerspace tegen te gaan willen we je vragen om te stofzuigen in de voorste ruimte en de houtwerkplaats, en eventueel de grote hal. \n\nAls je daar zin in hebt kun je ook de voorste ruimte dweilen met de hippe turbodweil die we hebben, maar als je weinig tijd hebt: Met alleen stofzuigen is al veel te winnen.\n\nDeze taak kan op elk moment gedurende de week worden gedaan. Je helpt wanneer het jou uitkomt, alle hulp is welkom!\n\nInformatie over schoonmaken op de Wiki: https://wiki.makerspaceleiden.nl/mediawiki/index.php/Chore_-_Dedustify \n\n\nClick here to sign up: {signup_url}",
127+
}
128+
],
129+
},
130+
{
131+
"reminder_type": "missing_volunteers",
132+
"when": {"days_before": 6, "time": "17:00"},
133+
"nudges": [
134+
{
135+
"nudge_type": "email",
136+
"nudge_key": "hard_email_reminder",
137+
"destination": "[email protected]",
138+
"subject_template": "Volunteers REALLY needed for this week",
139+
"body_template": "Hallo, we hebben nog steeds {num_volunteers_needed} vrijwilliger nodig voor deze week.\n\nOm stof in onze makerspace tegen te gaan willen we je vragen om te stofzuigen in de voorste ruimte en de houtwerkplaats, en eventueel de grote hal. \n\nAls je daar zin in hebt kun je ook de voorste ruimte dweilen met de hippe turbodweil die we hebben, maar als je weinig tijd hebt: Met alleen stofzuigen is al veel te winnen.\n\nDeze taak kan op elk moment gedurende de week worden gedaan. Je helpt wanneer het jou uitkomt, alle hulp is welkom!\n\nInformatie over schoonmaken op de Wiki: https://wiki.makerspaceleiden.nl/mediawiki/index.php/Chore_-_Dedustify \n\nClick here to sign up: {signup_url}",
140+
}
141+
],
142+
},
143+
{
144+
"reminder_type": "volunteers_who_signed_up",
145+
"when": {"days_before": 7, "time": "19:00"},
146+
},
147+
],
148+
},
149+
creator=self.user,
150+
)
151+
volunteer = UserFactory()
152+
ChoreVolunteer.objects.create(
153+
user=volunteer, chore=chore, timestamp=1753041600
154+
)
155+
156+
success = self.client.login(email=self.user.email, password="testpassword")
157+
self.assertTrue(success)
158+
response = self.client.get(url)
159+
html = response.content.decode("utf-8")
160+
self.assertEqual(response.status_code, status.HTTP_200_OK)
161+
self.assertIn(chore.description, html)
162+
self.assertIn('data-test-hook="chores-volunteer"', html)
163+
self.assertIn(volunteer.first_name, html)
164+
self.assertNotIn('data-test-hook="empty-chores"', html)
165+
166+

0 commit comments

Comments
 (0)