cr-connect-workflow/tests/scripts/test_get_email_data.py

79 lines
3.9 KiB
Python
Raw Normal View History

from tests.base_test import BaseTest
from crc import mail, session
from crc.models.study import StudyModel
2021-10-07 16:11:22 +00:00
from crc.models.workflow import WorkflowModel
from crc.services.email_service import EmailService
class TestGetEmailData(BaseTest):
2021-10-07 16:11:22 +00:00
def test_get_email_data_by_email_id(self):
self.load_example_data()
study = session.query(StudyModel).first()
with mail.record_messages() as outbox:
2021-10-07 16:11:22 +00:00
# Send an email we can use for get_email_data
email_model = EmailService.add_email(subject='My Email Subject',
sender='sender@example.com',
recipients=['joe@example.com'],
content='asdf', content_html=None, study_id=study.id)
workflow = self.create_workflow('get_email_data')
workflow_api = self.get_workflow_api(workflow)
task = workflow_api.next_task
2021-10-07 16:11:22 +00:00
# Run workflow with get_email_data
workflow_api = self.complete_form(workflow, task, {'email_id': email_model.id})
2021-10-07 16:11:22 +00:00
# Make assertions
task = workflow_api.next_task
data = task.data
self.assertIn('email_data', data)
email_data = data['email_data']
self.assertEqual(1, len(email_data))
self.assertEqual('My Email Subject', email_data[0]['subject'])
self.assertEqual('sender@example.com', email_data[0]['sender'])
self.assertEqual('[\'joe@example.com\']', email_data[0]['recipients'])
def test_get_email_data_by_workflow_id(self):
self.load_example_data()
study = session.query(StudyModel).first()
email_workflow = session.query(WorkflowModel).first()
with mail.record_messages() as outbox:
# Send an email we can use for get_email_data
email_model_one = EmailService.add_email(subject='My Email Subject',
sender='sender@example.com',
recipients=['joe@example.com'],
content='asdf',
content_html=None,
study_id=study.id,
workflow_id=email_workflow.id)
email_model_two = EmailService.add_email(subject='My Other Email Subject',
sender='sender2@example.com',
recipients=['joanne@example.com'],
content='xyzpdq',
content_html=None,
study_id=study.id,
workflow_id=email_workflow.id)
workflow = self.create_workflow('get_email_data_by_workflow')
workflow_api = self.get_workflow_api(workflow)
task = workflow_api.next_task
# Run workflow with get_email_data
workflow_api = self.complete_form(workflow, task, {'workflow_id': email_workflow.id})
task = workflow_api.next_task
data = task.data
# Make assertions
self.assertIn('email_data', data)
email_data = data['email_data']
self.assertEqual(2, len(email_data))
self.assertEqual('My Email Subject', email_data[0]['subject'])
self.assertEqual('sender@example.com', email_data[0]['sender'])
self.assertEqual('[\'joe@example.com\']', email_data[0]['recipients'])
self.assertEqual('My Other Email Subject', email_data[1]['subject'])
self.assertEqual('sender2@example.com', email_data[1]['sender'])
self.assertEqual('[\'joanne@example.com\']', email_data[1]['recipients'])