Skip to content

Commit 8104567

Browse files
authored
Merge pull request #43 from City-of-Helsinki/max-num-of-children
Limit number of children per guardian
2 parents b9335e8 + 03ba0bd commit 8104567

3 files changed

Lines changed: 39 additions & 0 deletions

File tree

children/schema.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import graphene
2+
from django.conf import settings
23
from django.contrib.auth import get_user_model
34
from django.core.exceptions import ValidationError
45
from django.db import transaction
@@ -113,6 +114,9 @@ def mutate_and_get_payload(cls, root, info, **kwargs):
113114
if not children_data:
114115
raise KukkuuGraphQLError("At least one child is required.")
115116

117+
if len(children_data) > settings.KUKKUU_MAX_NUM_OF_CHILDREN_PER_GUARDIAN:
118+
raise KukkuuGraphQLError("Too many children.")
119+
116120
guardian_data = kwargs["guardian"]
117121
guardian = Guardian.objects.create(
118122
user=user,
@@ -164,6 +168,12 @@ def mutate_and_get_payload(cls, root, info, **kwargs):
164168
'You need to use "SubmitChildrenAndGuardianMutation" first.'
165169
)
166170

171+
if (
172+
user.guardian.children.count()
173+
>= settings.KUKKUU_MAX_NUM_OF_CHILDREN_PER_GUARDIAN
174+
):
175+
raise KukkuuGraphQLError("Too many children.")
176+
167177
validate_child_data(kwargs)
168178
user = info.context.user
169179
relationship_data = kwargs.pop("relationship", {})

children/tests/test_api.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,20 @@ def test_submit_children_and_guardian_can_be_done_only_once(guardian_api_client)
199199
assert "You have already used this mutation." in str(executed["errors"])
200200

201201

202+
def test_submit_children_and_guardian_children_limit(user_api_client, settings):
203+
variables = deepcopy(SUBMIT_CHILDREN_AND_GUARDIAN_VARIABLES)
204+
variables["input"]["children"] = [
205+
variables["input"]["children"][0]
206+
for _ in range(settings.KUKKUU_MAX_NUM_OF_CHILDREN_PER_GUARDIAN + 1)
207+
]
208+
209+
executed = user_api_client.execute(
210+
SUBMIT_CHILDREN_AND_GUARDIAN_MUTATION, variables=variables,
211+
)
212+
213+
assert "Too many children." in str(executed["errors"])
214+
215+
202216
CHILDREN_QUERY = """
203217
query Children {
204218
children {
@@ -399,6 +413,19 @@ def test_add_child_mutation_requires_guardian(user_api_client):
399413
assert Child.objects.count() == 0
400414

401415

416+
def test_add_child_mutation_children_limit(guardian_api_client, settings):
417+
ChildWithGuardianFactory.create_batch(
418+
settings.KUKKUU_MAX_NUM_OF_CHILDREN_PER_GUARDIAN,
419+
relationship__guardian=guardian_api_client.user.guardian,
420+
)
421+
422+
executed = guardian_api_client.execute(
423+
ADD_CHILD_MUTATION, variables=ADD_CHILD_VARIABLES
424+
)
425+
426+
assert "Too many children." in str(executed["errors"])
427+
428+
402429
UPDATE_CHILD_MUTATION = """
403430
mutation UpdateChild($input: UpdateChildMutationInput!) {
404431
updateChild(input: $input) {

kukkuu/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@
180180

181181
GRAPHQL_JWT = {"JWT_AUTH_HEADER_PREFIX": "Bearer"}
182182

183+
KUKKUU_MAX_NUM_OF_CHILDREN_PER_GUARDIAN = 100
184+
183185
LOGGING = {
184186
"version": 1,
185187
"disable_existing_loggers": False,

0 commit comments

Comments
 (0)