workflow_tests: allow testing redirect_to_url workflow action (#88108)

This commit is contained in:
Valentin Deniaud 2024-03-13 17:15:15 +01:00
parent 03669bb847
commit 0d82f03e59
4 changed files with 126 additions and 1 deletions

View File

@ -440,6 +440,35 @@ def test_workflow_tests_action_assert_anonymise(pub):
assert 'Edit' not in resp.text
def test_workflow_tests_action_assert_redirect(pub):
create_superuser(pub)
formdef = FormDef()
formdef.name = 'test title'
formdef.store()
formdata = formdef.data_class()()
formdata.just_created()
testdef = TestDef.create_from_formdata(formdef, formdata)
testdef.workflow_tests.actions = [
workflow_tests.AssertRedirect(id='1'),
]
testdef.store()
app = login(get_app(pub))
resp = app.get('/backoffice/forms/1/tests/%s/workflow/' % testdef.id)
assert 'not configured' in resp.text
resp = resp.click('Edit')
resp.form['url'] = 'http://example.com'
resp = resp.form.submit().follow()
assert 'not configured' not in resp.text
assert 'http://example.com' in resp.text
def test_workflow_tests_action_assert_backoffice_field(pub):
create_superuser(pub)

View File

@ -648,6 +648,51 @@ def test_workflow_tests_anonymise(pub):
testdef.run(formdef)
def test_workflow_tests_redirect(pub):
user = pub.user_class(name='test user')
user.store()
workflow = Workflow(name='Workflow One')
new_status = workflow.add_status(name='New status')
workflow.store()
formdef = FormDef()
formdef.name = 'test title'
formdef.workflow_id = workflow.id
formdef.store()
formdata = formdef.data_class()()
formdata.just_created()
testdef = TestDef.create_from_formdata(formdef, formdata)
testdef.agent_id = user.id
testdef.workflow_tests.actions = [
workflow_tests.AssertRedirect(url='https://example.com/'),
]
with pytest.raises(WorkflowTestError) as excinfo:
testdef.run(formdef)
assert str(excinfo.value) == 'No redirection occured.'
redirect_action = new_status.add_action('redirect_to_url')
redirect_action.url = 'https://test.com/'
workflow.store()
formdef.refresh_from_storage()
with pytest.raises(WorkflowTestError) as excinfo:
testdef.run(formdef)
assert (
str(excinfo.value)
== 'Expected redirection to https://example.com/ but was redirected to https://test.com/.'
)
testdef.workflow_tests.actions = [
workflow_tests.AssertRedirect(url='https://test.com/'),
]
testdef.run(formdef)
def test_workflow_tests_backoffice_fields(pub):
user = pub.user_class(name='test user')
user.store()
@ -912,6 +957,9 @@ def test_workflow_tests_create_from_formdata(pub, http_requests, freezer):
anonymise_action = transition_status.add_action('anonymise')
anonymise_action.mode = 'intermediate'
redirect_action = transition_status.add_action('redirect_to_url')
redirect_action.url = 'https://test.com/'
jump = transition_status.add_action('jump')
jump.status = end_status.id
@ -944,7 +992,7 @@ def test_workflow_tests_create_from_formdata(pub, http_requests, freezer):
testdef.run(formdef)
actions = testdef.workflow_tests.actions
assert len(actions) == 10
assert len(actions) == 11
assert actions[0].key == 'assert-status'
assert actions[0].status_name == 'Status with timeout jump'
@ -963,6 +1011,7 @@ def test_workflow_tests_create_from_formdata(pub, http_requests, freezer):
assert actions[6].key == 'assert-backoffice-field'
assert actions[7].key == 'assert-sms'
assert actions[8].key == 'assert-anonymise'
assert actions[9].key == 'assert-redirect'
assert actions[-1].key == 'assert-status'
assert actions[-1].status_name == 'End status'

View File

@ -63,5 +63,16 @@ class RedirectToUrlWorkflowStatusItem(WorkflowStatusItem):
return # don't redirect
return url
def get_workflow_test_action(self, formdata, *args, **kwargs):
original_perform = self.perform
def perform(formdata):
url = original_perform(formdata)
formdata.redirect_to_url = url
return url
setattr(self, 'perform', perform)
return self
register_item_class(RedirectToUrlWorkflowStatusItem)

View File

@ -94,6 +94,7 @@ class WorkflowTests(XmlStorableObject):
formdata.sent_emails = []
formdata.used_webservice_responses = self.testdef.used_webservice_responses = []
formdata.anonymisation_performed = False
formdata.redirect_to_url = None
formdata.perform_workflow()
for action in self.actions:
@ -107,6 +108,7 @@ class WorkflowTests(XmlStorableObject):
formdata.sent_emails.clear()
formdata.used_webservice_responses.clear()
formdata.anonymisation_performed = False
formdata.redirect_to_url = None
try:
action.perform(formdata)
@ -149,6 +151,7 @@ class WorkflowTests(XmlStorableObject):
'button': ButtonClick,
'timeout-jump': SkipTime,
'anonymise': AssertAnonymise,
'redirect_to_url': AssertRedirect,
}
previous_trace = None
@ -777,3 +780,36 @@ class AssertAnonymise(WorkflowTestAction):
def perform(self, formdata):
if not formdata.anonymisation_performed:
raise WorkflowTestError(_('Form was not anonymised.'))
class AssertRedirect(WorkflowTestAction):
label = _('Assert redirect is performed')
key = 'assert-redirect'
url = None
XML_NODES = WorkflowTestAction.XML_NODES + [
('url', 'str'),
]
@property
def details_label(self):
return self.url
def perform(self, formdata):
if not formdata.redirect_to_url:
raise WorkflowTestError(_('No redirection occured.'))
if formdata.redirect_to_url != self.url:
raise WorkflowTestError(
_('Expected redirection to %(expected_url)s but was redirected to %(url)s.')
% {'expected_url': self.url, 'url': formdata.redirect_to_url}
)
def fill_admin_form(self, form, formdef):
form.add(
StringWidget,
'url',
title=_('URL'),
value=self.url,
)