<commit_before>import mock

from changes.config import db
from changes.constants import Result
from changes.models import ProjectOption
from changes.listeners.mail import build_finished_handler, send_notification
from changes.testutils.cases import TestCase


class BuildHandlerTestCase(TestCase):
    @mock.patch('changes.listeners.mail.send_notification')
    def test_default_options(self, send_notifications):
        author = self.create_author('<EMAIL>')
        build = self.create_build(self.project, result=Result.passed, author=author)

        build_finished_handler(build)

        # not failing
        assert not send_notifications.called

        build = self.create_build(self.project, result=Result.failed, author=author)

        build_finished_handler(build)

        # notify author
        send_notifications.assert_called_once_with(
            build, ['Test Case <<EMAIL>>']
        )

    @mock.patch('changes.listeners.mail.send_notification')
    def test_without_author_option(self, send_notifications):
        db.session.add(ProjectOption(
            project=self.project, name='mail.notify-author', value='0'))
        author = self.create_author('<EMAIL>')
        build = self.create_build(self.project, result=Result.failed, author=author)

        build_finished_handler(build)

        assert not send_notifications.called

    @mock.patch('changes.listeners.mail.send_notification')
    def test_with_addressees(self, send_notifications):
        db.session.add(ProjectOption(
            project=self.project, name='mail.notify-author', value='1'))
        db.session.add(ProjectOption(
            project=self.project, name='mail.notify-addresses',
            value='<EMAIL>, <EMAIL>'))

        author = self.create_author('<EMAIL>')
        build = self.create_build(self.project, result=Result.failed, author=author)

        build_finished_handler(build)

        send_notifications.assert_called_once_with(
            build, ['Test Case <<EMAIL>>', '<EMAIL>', '<EMAIL>']
        )


class SendNotificationTestCase(TestCase):
    def test_simple(self):
        build = self.create_build(self.project, result=Result.failed)
        send_notification(build, recipients=['<EMAIL>', 'Bob <<EMAIL>>'])

        assert len(self.outbox) == 1
        msg = self.outbox[0]
        assert msg.subject == 'Build Failed - %s (%s)' % (build.revision_sha, build.project.name)
        assert msg.recipients == ['<EMAIL>', 'Bob <<EMAIL>>']<commit_msg>Add tests for mail.notify-addresses-revisions<commit_after>import mock

from changes.config import db
from changes.constants import Result
from changes.models import ProjectOption, Patch
from changes.listeners.mail import build_finished_handler, send_notification
from changes.testutils.cases import TestCase


class BuildHandlerTestCase(TestCase):
    @mock.patch('changes.listeners.mail.send_notification')
    def test_default_options(self, send_notifications):
        author = self.create_author('<EMAIL>')
        build = self.create_build(self.project, result=Result.passed, author=author)

        build_finished_handler(build)

        # not failing
        assert not send_notifications.called

        build = self.create_build(self.project, result=Result.failed, author=author)

        build_finished_handler(build)

        # notify author
        send_notifications.assert_called_once_with(
            build, ['Test Case <<EMAIL>>']
        )

    @mock.patch('changes.listeners.mail.send_notification')
    def test_without_author_option(self, send_notifications):
        db.session.add(ProjectOption(
            project=self.project, name='mail.notify-author', value='0'))
        author = self.create_author('<EMAIL>')
        build = self.create_build(self.project, result=Result.failed, author=author)

        build_finished_handler(build)

        assert not send_notifications.called

    @mock.patch('changes.listeners.mail.send_notification')
    def test_with_addressees(self, send_notifications):
        db.session.add(ProjectOption(
            project=self.project, name='mail.notify-author', value='1'))
        db.session.add(ProjectOption(
            project=self.project, name='mail.notify-addresses',
            value='<EMAIL>, <EMAIL>'))

        author = self.create_author('<EMAIL>')
        build = self.create_build(self.project, result=Result.failed, author=author)

        build_finished_handler(build)

        send_notifications.assert_called_once_with(
            build, ['Test Case <<EMAIL>>', '<EMAIL>', '<EMAIL>']
        )

    @mock.patch('changes.listeners.mail.send_notification')
    def test_with_revision_addressees(self, send_notifications):
        db.session.add(ProjectOption(
            project=self.project, name='mail.notify-author', value='1'))
        db.session.add(ProjectOption(
            project=self.project, name='mail.notify-addresses-revisions',
            value='<EMAIL>, <EMAIL>'))

        author = self.create_author('<EMAIL>')
        patch = Patch(
            repository=self.repo, project=self.project, label='foo',
            diff='',
        )
        build = self.create_build(
            self.project, result=Result.failed, author=author, patch=patch)

        build_finished_handler(build)

        send_notifications.assert_called_once_with(
            build, ['Test Case <<EMAIL>>']
        )

        send_notifications.reset_mock()

        build = self.create_build(
            self.project, result=Result.failed, author=author)

        build_finished_handler(build)

        send_notifications.assert_called_once_with(
            build, ['Test Case <<EMAIL>>', '<EMAIL>', '<EMAIL>']
        )


class SendNotificationTestCase(TestCase):
    def test_simple(self):
        build = self.create_build(self.project, result=Result.failed)
        send_notification(build, recipients=['<EMAIL>', 'Bob <<EMAIL>>'])

        assert len(self.outbox) == 1
        msg = self.outbox[0]
        assert msg.subject == 'Build Failed - %s (%s)' % (build.revision_sha, build.project.name)
        assert msg.recipients == ['<EMAIL>', 'Bob <<EMAIL>>']
{'fraction_non_alphanumeric': 0.12036583374162992, 'fraction_numerical': 0.0014698677119059284, 'mean_word_length': 3.2975438596491227, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2216234', 'n_tokens_mistral': 1768, 'n_tokens_neox': 1720, 'n_words': 304}