diff --git a/tests/test_email_script.py b/tests/test_email_script.py index d1520314..980d0c06 100644 --- a/tests/test_email_script.py +++ b/tests/test_email_script.py @@ -1,5 +1,7 @@ from tests.base_test import BaseTest -from crc import mail +from crc import mail, session +from crc.models.study import StudyModel +from crc.services.study_service import StudyService import json @@ -22,22 +24,21 @@ class TestEmailScript(BaseTest): first_task = self.get_workflow_api(workflow).next_task workflow = self.get_workflow_api(workflow) - self.assertEqual('dan@sartography.com', workflow.next_task.data['email_address']) - self.complete_form(workflow, first_task, {'email_address': 'test@example.com'}) + self.complete_form(workflow, first_task, {'subject': 'My Email Subject', 'recipients': 'test@example.com'}) self.assertEqual(1, len(outbox)) self.assertEqual('My Email Subject', outbox[0].subject) self.assertEqual(['test@example.com'], outbox[0].recipients) + self.assertIn('Thank you for using this email example', outbox[0].body) def test_email_script_multiple(self): with mail.record_messages() as outbox: workflow = self.create_workflow('email_script') + workflow_api = self.get_workflow_api(workflow) + first_task = workflow_api.next_task - first_task = self.get_workflow_api(workflow).next_task - - workflow = self.get_workflow_api(workflow) - self.complete_form(workflow, first_task, {'email_address': ['test@example.com', 'test2@example.com']}) + self.complete_form(workflow, first_task, {'subject': 'My Email Subject', 'recipients': ['test@example.com', 'test2@example.com']}) self.assertEqual(1, len(outbox)) self.assertEqual("My Email Subject", outbox[0].subject) @@ -47,18 +48,54 @@ class TestEmailScript(BaseTest): def test_bad_email_address_1(self): workflow = self.create_workflow('email_script') - first_task = self.get_workflow_api(workflow).next_task - workflow = self.get_workflow_api(workflow) + with self.assertRaises(AssertionError): - self.complete_form(workflow, first_task, {'email_address': 'test@example'}) + self.complete_form(workflow, first_task, {'recipients': 'test@example'}) def test_bad_email_address_2(self): workflow = self.create_workflow('email_script') - first_task = self.get_workflow_api(workflow).next_task - workflow = self.get_workflow_api(workflow) + with self.assertRaises(AssertionError): - self.complete_form(workflow, first_task, {'email_address': 'test'}) + self.complete_form(workflow, first_task, {'recipients': 'test'}) + + def test_email_script_associated(self): + workflow = self.create_workflow('email_script') + workflow_api = self.get_workflow_api(workflow) + + # Only dhf8r is in testing DB. + # We want to test multiple associates, and lb3dp is already in testing LDAP + self.create_user(uid='lb3dp', email='lb3dp@virginia.edu', display_name='Laura Barnes') + StudyService.update_study_associates(workflow.study_id, + [{'uid': 'dhf8r', 'role': 'Chief Bee Keeper', 'send_email': True, 'access': True}, + {'uid': 'lb3dp', 'role': 'Chief Cat Herder', 'send_email': True, 'access': True}]) + + first_task = workflow_api.next_task + + with mail.record_messages() as outbox: + self.complete_form(workflow, first_task, {'subject': 'My Test Subject', 'recipients': ['user@example.com', 'associated']}) + + self.assertEqual(1, len(outbox)) + self.assertIn(outbox[0].recipients[0], ['user@example.com', 'dhf8r@virginia.edu', 'lb3dp@virginia.edu']) + self.assertIn(outbox[0].recipients[1], ['user@example.com', 'dhf8r@virginia.edu', 'lb3dp@virginia.edu']) + self.assertIn(outbox[0].recipients[2], ['user@example.com', 'dhf8r@virginia.edu', 'lb3dp@virginia.edu']) + + def test_email_script_cc(self): + workflow = self.create_workflow('email_script') + workflow_api = self.get_workflow_api(workflow) + self.create_user(uid='lb3dp', email='lb3dp@virginia.edu', display_name='Laura Barnes') + StudyService.update_study_associates(workflow.study_id, + [{'uid': 'dhf8r', 'role': 'Chief Bee Keeper', 'send_email': True, 'access': True}, + {'uid': 'lb3dp', 'role': 'Chief Cat Herder', 'send_email': True, 'access': True}]) + first_task = workflow_api.next_task + with mail.record_messages() as outbox: + + self.complete_form(workflow, first_task, {'subject': 'My Test Subject', 'recipients': 'user@example.com', 'cc': 'associated'}) + + self.assertEqual(1, len(outbox)) + self.assertEqual('user@example.com', outbox[0].recipients[0]) + self.assertIn(outbox[0].cc[0], ['dhf8r@virginia.edu', 'lb3dp@virginia.edu']) + self.assertIn(outbox[0].cc[1], ['dhf8r@virginia.edu', 'lb3dp@virginia.edu'])