<commit_before>import os
import platform

import pytest
from egcg_core.config import cfg

from EPPs.common import SendMailEPP
from scripts.email_data_release import DataReleaseEmail
from scripts.email_data_trigger import DataReleaseEmailAndUpdateEPP
from scripts.email_fluidx_sample_receipt import FluidXSampleReceiptEmail
from scripts.email_receive_sample import ReceiveSampleEmail
from scripts.email_data_release_facility_manager import DataReleaseFMEmail
from tests.test_common import TestEPP, NamedMock
from unittest.mock import Mock, patch, PropertyMock


class TestEmailEPP(TestEPP):

    def setUp(self):
        super().setUp()
        cfg.load_config_file(os.path.join(self.etc_path, 'example_clarity_script.yml'))

        project1 = NamedMock(real_name='project1')
        project2 = NamedMock(real_name='project2')

        self.patch_project_multi = patch.object(
            SendMailEPP,
            'projects',
            new_callable=PropertyMock(return_value=[project1, project2])
        )

        project1 = NamedMock(real_name='project1')
        self.patch_project_single = patch.object(
            SendMailEPP,
            'projects',
            new_callable=PropertyMock(return_value=[project1])
        )
        self.patch_process = self.create_patch_process(SendMailEPP)

        sample1 = NamedMock(real_name='sample1')
        sample2 = NamedMock(real_name='sample2')
        self.patch_samples = patch.object(
            SendMailEPP,
            'samples',
            new_callable=PropertyMock(return_value=[sample1, sample2])
        )

        self.patch_email = patch('egcg_core.notifications.email.send_email')

    def _test_only_one_project(self, epp):
        with pytest.raises(ValueError):
            with self.patch_project_multi:
                epp._run()


    def create_epp(self, klass):
        return klass(
            'http://server:8080/a_step_uri',
            'a_user',
            'a_password',
            self.log_file
        )

    @staticmethod
    def create_patch_process(klass, udfs=None):
        return patch.object(
            klass,
            'process',
            new_callable=PropertyMock(return_value=Mock(udf=udfs))
        )


class TestDataReleaseEmailAndUpdateEPP(TestEmailEPP):
    def setUp(self):
        super().setUp()
        self.epp = self.create_epp(DataReleaseEmailAndUpdateEPP)
        self.patch_process = self.create_patch_process(
            DataReleaseEmailAndUpdateEPP,
            {
                'Data Download Contact Name 1': '<NAME>',
                'Data Download Contact Name 2': '<NAME>',
                'Is Contact 1 A New or Existing User?': 'New User',
                'Is Contact 2 A New or Existing User?': 'Existing User'
            })

    def test_send_email(self):
        self._test_only_one_project(self.epp)

        with self.patch_project_single, self.patch_process, self.patch_samples, self.patch_email as mocked_send_email:
            self.epp._run()
            msg = '''Hi Bioinformatics,

Please release the data for 2 sample(s) from project project1 shown at the link below:

https://{localmachine}/clarity/work-details/tep_uri

The data contacts are:

<NAME> (New User)
<NAME> (Existing User)

Kind regards,
ClarityX'''
            msg = msg.format(localmachine=platform.node())
            mocked_send_email.assert_called_with(
                msg=msg,<commit_msg>Data Download Contact Name changed to Data Download Contact Username to match UDF change in LIMS<commit_after>import os
import platform

import pytest
from egcg_core.config import cfg

from EPPs.common import SendMailEPP
from scripts.email_data_release import DataReleaseEmail
from scripts.email_data_trigger import DataReleaseEmailAndUpdateEPP
from scripts.email_fluidx_sample_receipt import FluidXSampleReceiptEmail
from scripts.email_receive_sample import ReceiveSampleEmail
from scripts.email_data_release_facility_manager import DataReleaseFMEmail
from tests.test_common import TestEPP, NamedMock
from unittest.mock import Mock, patch, PropertyMock


class TestEmailEPP(TestEPP):

    def setUp(self):
        super().setUp()
        cfg.load_config_file(os.path.join(self.etc_path, 'example_clarity_script.yml'))

        project1 = NamedMock(real_name='project1')
        project2 = NamedMock(real_name='project2')

        self.patch_project_multi = patch.object(
            SendMailEPP,
            'projects',
            new_callable=PropertyMock(return_value=[project1, project2])
        )

        project1 = NamedMock(real_name='project1')
        self.patch_project_single = patch.object(
            SendMailEPP,
            'projects',
            new_callable=PropertyMock(return_value=[project1])
        )
        self.patch_process = self.create_patch_process(SendMailEPP)

        sample1 = NamedMock(real_name='sample1')
        sample2 = NamedMock(real_name='sample2')
        self.patch_samples = patch.object(
            SendMailEPP,
            'samples',
            new_callable=PropertyMock(return_value=[sample1, sample2])
        )

        self.patch_email = patch('egcg_core.notifications.email.send_email')

    def _test_only_one_project(self, epp):
        with pytest.raises(ValueError):
            with self.patch_project_multi:
                epp._run()


    def create_epp(self, klass):
        return klass(
            'http://server:8080/a_step_uri',
            'a_user',
            'a_password',
            self.log_file
        )

    @staticmethod
    def create_patch_process(klass, udfs=None):
        return patch.object(
            klass,
            'process',
            new_callable=PropertyMock(return_value=Mock(udf=udfs))
        )


class TestDataReleaseEmailAndUpdateEPP(TestEmailEPP):
    def setUp(self):
        super().setUp()
        self.epp = self.create_epp(DataReleaseEmailAndUpdateEPP)
        self.patch_process = self.create_patch_process(
            DataReleaseEmailAndUpdateEPP,
            {
                'Data Download Contact Username 1': '<NAME>',
                'Data Download Contact Username 2': '<NAME>',
                'Is Contact 1 A New or Existing User?': 'New User',
                'Is Contact 2 A New or Existing User?': 'Existing User'
            })

    def test_send_email(self):
        self._test_only_one_project(self.epp)

        with self.patch_project_single, self.patch_process, self.patch_samples, self.patch_email as mocked_send_email:
            self.epp._run()
            msg = '''Hi Bioinformatics,

Please release the data for 2 sample(s) from project project1 shown at the link below:

https://{localmachine}/clarity/work-details/tep_uri

The data contacts are:

<NAME> (New User)
<NAME> (Existing User)

Kind regards,
ClarityX'''
            msg = msg.format(localmachine=platform.node())
            mocked_send_email.assert_called_with(
                msg=msg,
{'fraction_non_alphanumeric': 0.08622441573523008, 'fraction_numerical': 0.007257947452460444, 'mean_word_length': 2.752723311546841, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4079878', 'n_tokens_mistral': 2063, 'n_tokens_neox': 2008, 'n_words': 410}