Questions: Validate type changes (Z#23197118) (#5259)

* Questions: Validate type changes (Z#23197118)

* Update src/pretix/base/forms/questions.py

Co-authored-by: Richard Schreiber <schreiber@rami.io>

* Update src/pretix/base/forms/questions.py

Co-authored-by: Richard Schreiber <schreiber@rami.io>

* Update src/pretix/base/forms/questions.py

Co-authored-by: Richard Schreiber <schreiber@rami.io>

* Update src/pretix/base/models/items.py

Co-authored-by: Richard Schreiber <schreiber@rami.io>

* Fix failing test

---------

Co-authored-by: Richard Schreiber <schreiber@rami.io>
This commit is contained in:
Raphael Michel
2025-06-24 17:54:28 +02:00
committed by GitHub
parent 243db008e1
commit 5d3fc62ba4
5 changed files with 95 additions and 3 deletions

View File

@@ -2428,6 +2428,45 @@ def test_question_update(token_client, organizer, event, question):
assert question.type == "N"
@pytest.mark.django_db
def test_question_update_type_changes(token_client, organizer, event, question):
# Allowed because no answers exist
resp = token_client.patch(
'/api/v1/organizers/{}/events/{}/questions/{}/'.format(organizer.slug, event.slug, question.pk),
{
"type": "B",
},
format='json'
)
assert resp.status_code == 200
with scopes_disabled():
question.answers.create(answer="12")
# Allowed change
resp = token_client.patch(
'/api/v1/organizers/{}/events/{}/questions/{}/'.format(organizer.slug, event.slug, question.pk),
{
"type": "S",
},
format='json'
)
assert resp.status_code == 200
# Forbidden change
resp = token_client.patch(
'/api/v1/organizers/{}/events/{}/questions/{}/'.format(organizer.slug, event.slug, question.pk),
{
"type": "B",
},
format='json'
)
assert resp.status_code == 400
assert resp.content.decode() == ('{"type":["The system already contains answers to this question that are not '
'compatible with changing the type of question without data loss. Consider hiding '
'this question and creating a new one instead."]}')
@pytest.mark.django_db
def test_question_update_circular_dependency(token_client, organizer, event, question):
with scopes_disabled():