workflow_tests: allow testing history message workflow action (#88108)
This commit is contained in:
parent
e76e33808b
commit
7199e84903
|
@ -469,6 +469,34 @@ def test_workflow_tests_action_assert_redirect(pub):
|
|||
assert 'http://example.com' in resp.text
|
||||
|
||||
|
||||
def test_workflow_tests_action_assert_history_message(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.AssertHistoryMessage(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['message'] = 'Hello'
|
||||
resp = resp.form.submit().follow()
|
||||
|
||||
assert 'not configured' not in resp.text
|
||||
|
||||
|
||||
def test_workflow_tests_action_assert_backoffice_field(pub):
|
||||
create_superuser(pub)
|
||||
|
||||
|
|
|
@ -693,6 +693,50 @@ def test_workflow_tests_redirect(pub):
|
|||
testdef.run(formdef)
|
||||
|
||||
|
||||
def test_workflow_tests_history_message(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.AssertHistoryMessage(message='Hello 42'),
|
||||
]
|
||||
|
||||
with pytest.raises(WorkflowTestError) as excinfo:
|
||||
testdef.run(formdef)
|
||||
assert str(excinfo.value) == 'No history message.'
|
||||
|
||||
register_comment = new_status.add_action('register-comment')
|
||||
register_comment.comment = 'Hello {{ 41|add:1 }}'
|
||||
workflow.store()
|
||||
formdef.refresh_from_storage()
|
||||
|
||||
testdef.run(formdef)
|
||||
|
||||
testdef.workflow_tests.actions = [
|
||||
workflow_tests.AssertHistoryMessage(message='Hello 43'),
|
||||
]
|
||||
|
||||
with pytest.raises(WorkflowTestError) as excinfo:
|
||||
testdef.run(formdef)
|
||||
assert str(excinfo.value) == 'Wrong history message content.'
|
||||
assert 'Displayed history message: <div>Hello 42</div>' in excinfo.value.details
|
||||
assert 'Expected history message: Hello 43' in excinfo.value.details
|
||||
|
||||
|
||||
def test_workflow_tests_backoffice_fields(pub):
|
||||
user = pub.user_class(name='test user')
|
||||
user.store()
|
||||
|
@ -960,6 +1004,9 @@ def test_workflow_tests_create_from_formdata(pub, http_requests, freezer):
|
|||
redirect_action = transition_status.add_action('redirect_to_url')
|
||||
redirect_action.url = 'https://test.com/'
|
||||
|
||||
register_comment = transition_status.add_action('register-comment')
|
||||
register_comment.comment = 'Hello'
|
||||
|
||||
jump = transition_status.add_action('jump')
|
||||
jump.status = end_status.id
|
||||
|
||||
|
@ -992,7 +1039,7 @@ def test_workflow_tests_create_from_formdata(pub, http_requests, freezer):
|
|||
testdef.run(formdef)
|
||||
|
||||
actions = testdef.workflow_tests.actions
|
||||
assert len(actions) == 11
|
||||
assert len(actions) == 12
|
||||
|
||||
assert actions[0].key == 'assert-status'
|
||||
assert actions[0].status_name == 'Status with timeout jump'
|
||||
|
@ -1012,6 +1059,7 @@ def test_workflow_tests_create_from_formdata(pub, http_requests, freezer):
|
|||
assert actions[7].key == 'assert-sms'
|
||||
assert actions[8].key == 'assert-anonymise'
|
||||
assert actions[9].key == 'assert-redirect'
|
||||
assert actions[10].key == 'assert-history-message'
|
||||
|
||||
assert actions[-1].key == 'assert-status'
|
||||
assert actions[-1].status_name == 'End status'
|
||||
|
|
|
@ -228,20 +228,34 @@ class RegisterCommenterWorkflowStatusItem(WorkflowStatusItem):
|
|||
|
||||
# the comment can use attachments done above
|
||||
if comment:
|
||||
try:
|
||||
formdata.evolution[-1].add_part(
|
||||
JournalEvolutionPart(formdata, get_publisher().translate(comment), self.to, self.level)
|
||||
)
|
||||
part = self.get_journal_evolution_part(formdata, comment)
|
||||
if part:
|
||||
formdata.evolution[-1].add_part(part)
|
||||
formdata.store()
|
||||
except TemplateError as e:
|
||||
get_publisher().record_error(
|
||||
_('Error in template, comment could not be generated'), formdata=formdata, exception=e
|
||||
)
|
||||
|
||||
def get_journal_evolution_part(self, formdata, comment):
|
||||
try:
|
||||
return JournalEvolutionPart(formdata, get_publisher().translate(comment), self.to, self.level)
|
||||
except TemplateError as e:
|
||||
get_publisher().record_error(
|
||||
_('Error in template, comment could not be generated'), formdata=formdata, exception=e
|
||||
)
|
||||
|
||||
def i18n_scan(self, base_location):
|
||||
location = '%sitems/%s/' % (base_location, self.id)
|
||||
if not self.comment_template:
|
||||
yield location, None, self.comment
|
||||
|
||||
def get_workflow_test_action(self, formdata, *args, **kwargs):
|
||||
original_get_journal_evolution_part = self.get_journal_evolution_part
|
||||
|
||||
def get_journal_evolution_part(formdata, comment):
|
||||
part = original_get_journal_evolution_part(formdata, comment)
|
||||
formdata.history_messages.append(part.content)
|
||||
return part
|
||||
|
||||
setattr(self, 'get_journal_evolution_part', get_journal_evolution_part)
|
||||
return self
|
||||
|
||||
|
||||
register_item_class(RegisterCommenterWorkflowStatusItem)
|
||||
|
|
|
@ -28,6 +28,7 @@ from wcs.qommon.form import (
|
|||
RadiobuttonsWidget,
|
||||
SingleSelectWidget,
|
||||
StringWidget,
|
||||
TextWidget,
|
||||
WidgetList,
|
||||
)
|
||||
from wcs.qommon.humantime import humanduration2seconds, seconds2humanduration, timewords
|
||||
|
@ -129,6 +130,7 @@ class WorkflowTests(XmlStorableObject):
|
|||
formdata.used_webservice_responses = self.testdef.used_webservice_responses = []
|
||||
formdata.anonymisation_performed = False
|
||||
formdata.redirect_to_url = None
|
||||
formdata.history_messages = []
|
||||
|
||||
def get_new_action_id(self):
|
||||
if not self.actions:
|
||||
|
@ -151,6 +153,7 @@ class WorkflowTests(XmlStorableObject):
|
|||
'timeout-jump': SkipTime,
|
||||
'anonymise': AssertAnonymise,
|
||||
'redirect_to_url': AssertRedirect,
|
||||
'register-comment': AssertHistoryMessage,
|
||||
}
|
||||
|
||||
previous_trace = None
|
||||
|
@ -812,3 +815,37 @@ class AssertRedirect(WorkflowTestAction):
|
|||
title=_('URL'),
|
||||
value=self.url,
|
||||
)
|
||||
|
||||
|
||||
class AssertHistoryMessage(WorkflowTestAction):
|
||||
label = _('Assert history message is displayed')
|
||||
details_label = ''
|
||||
|
||||
key = 'assert-history-message'
|
||||
message = None
|
||||
|
||||
XML_NODES = WorkflowTestAction.XML_NODES + [
|
||||
('message', 'str'),
|
||||
]
|
||||
|
||||
def perform(self, formdata):
|
||||
try:
|
||||
message = formdata.history_messages.pop(0)
|
||||
except IndexError:
|
||||
raise WorkflowTestError(_('No history message.'))
|
||||
|
||||
if self.message not in message:
|
||||
details = [
|
||||
_('Displayed history message: %s') % message,
|
||||
_('Expected history message: %s') % self.message,
|
||||
]
|
||||
raise WorkflowTestError(_('Wrong history message content.'), details=details)
|
||||
|
||||
def fill_admin_form(self, form, formdef):
|
||||
form.add(
|
||||
TextWidget,
|
||||
'message',
|
||||
title=_('Message'),
|
||||
value=self.message,
|
||||
hint=_('Assertion will pass if the text is contained in history message.'),
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue