Example #1
0
    def test_task_completed_notify_admin(self, mock_send_email):
        task_completed(12)
        expected_calls = [
            call.send_email('*****@*****.**',
                            'supervisor-task-completed'),
            call.send_email('*****@*****.**',
                            'supervisor-task-completed'),
            call.send_email('*****@*****.**',
                            'supervisor-task-completed'),
            call.send_email('*****@*****.**',
                            'supervisor-task-completed'),
            call.send_email('*****@*****.**', 'supervisor-task-completed')
        ]

        mock_send_email.assert_has_calls(expected_calls)
Example #2
0
 def test_send_private_email(self):
     self.moderator.send_email(self.sam, "Test", "Test body.")
     assert_equal(self.moderator.mailgun.mock_calls, [
         call.send_email(
             Email(recipients=[self.address(self.sam)],
                   cc=["*****@*****.**"],
                   subject="Test",
                   body="Test body."))
     ])
Example #3
0
 def test_send_public_email(self):
     self.moderator.send_email(events.PUBLIC, "Test", "Test body.")
     assert_equal(self.moderator.mailgun.mock_calls, [
         call.send_email(
             Email(recipients=[self.address(p) for p in self.game.players],
                   cc=["*****@*****.**", "*****@*****.**"],
                   subject="Test",
                   body="Test body."))
     ])
Example #4
0
 def test_send_group_email(self):
   self.moderator.send_email([self.sam, self.frodo], "Test", "Test body.")
   assert_equal(self.moderator.mailgun.mock_calls, [
     call.send_email(Email(
       recipients=[self.address(self.sam), self.address(self.frodo)],
       cc=["*****@*****.**"],
       subject="Test",
       body="Test body.",
     ))
   ])
Example #5
0
 def test_send_public_email(self):
   self.moderator.send_email(events.PUBLIC, "Test", "Test body.")
   assert_equal(self.moderator.mailgun.mock_calls, [
     call.send_email(Email(
       recipients=[self.address(p) for p in self.game.players],
       cc=["*****@*****.**", "*****@*****.**"],
       subject="Test",
       body="Test body.")
     )
   ])
Example #6
0
    def test_run_exception_default(self):
        m_finished = Mock()
        m_unfinished = Mock()
        m_excs = Mock()
        m_start_dt = Mock()
        m_end_dt = Mock()

        self.client.send_email.side_effect = RuntimeError('foo')
        self.failure_html_path = None
        m_open = mock_open()
        with patch('%s._make_report' % pb) as mock_mr:
            with patch('%s.open' % pbm, m_open, create=True):
                with patch.multiple(pbm,
                                    mkstemp=DEFAULT,
                                    Popen=DEFAULT,
                                    os_close=DEFAULT) as mocks:
                    mocks['mkstemp'].return_value = (999, '/tmp/path')
                    mock_mr.return_value = 'my_html_report'
                    with pytest.raises(RuntimeError) as exc:
                        self.cls.run(m_finished, m_unfinished, m_excs,
                                     m_start_dt, m_end_dt)
        assert str(exc.value) == 'foo'
        assert mock_mr.mock_calls == [
            call(m_finished, m_unfinished, m_excs, m_start_dt, m_end_dt)
        ]
        assert self.client.mock_calls == [
            call.send_email(
                Source='*****@*****.**',
                Destination={'ToAddresses': ['*****@*****.**', '*****@*****.**']},
                Message={
                    'Subject': {
                        'Data': 'MySubject',
                        'Charset': 'utf-8'
                    },
                    'Body': {
                        'Html': {
                            'Data': 'my_html_report',
                            'Charset': 'utf-8'
                        }
                    }
                },
                ReturnPath='*****@*****.**')
        ]
        assert m_open.mock_calls == [
            call('/tmp/path', 'w'),
            call().__enter__(),
            call().write('my_html_report'),
            call().__exit__(None, None, None)
        ]
        assert mocks['mkstemp'].mock_calls == [
            call(prefix='ecsjobs', text=True, suffix='.html')
        ]
        assert mocks['Popen'].mock_calls == []
        assert mocks['os_close'].mock_calls == [call(999)]
Example #7
0
    def test_run(self):
        m_finished = Mock()
        m_unfinished = Mock()
        m_excs = Mock()
        m_start_dt = Mock()
        m_end_dt = Mock()

        m_open = mock_open()
        with patch('%s._make_report' % pb) as mock_mr:
            with patch('%s.open' % pbm, m_open, create=True):
                with patch.multiple(pbm,
                                    mkstemp=DEFAULT,
                                    Popen=DEFAULT,
                                    os_close=DEFAULT) as mocks:
                    mocks['mkstemp'].return_value = (999, '/tmp/path')
                    mock_mr.return_value = 'my_html_report'
                    self.cls.run(m_finished, m_unfinished, m_excs, m_start_dt,
                                 m_end_dt)
        assert mock_mr.mock_calls == [
            call(m_finished, m_unfinished, m_excs, m_start_dt, m_end_dt)
        ]
        assert self.client.mock_calls == [
            call.send_email(
                Source='*****@*****.**',
                Destination={'ToAddresses': ['*****@*****.**', '*****@*****.**']},
                Message={
                    'Subject': {
                        'Data': 'MySubject',
                        'Charset': 'utf-8'
                    },
                    'Body': {
                        'Html': {
                            'Data': 'my_html_report',
                            'Charset': 'utf-8'
                        }
                    }
                },
                ReturnPath='*****@*****.**')
        ]
        assert m_open.mock_calls == []
        assert mocks['mkstemp'].mock_calls == []
        assert mocks['Popen'].mock_calls == []
        assert mocks['os_close'].mock_calls == []
Example #8
0
    def test_generate_and_send_email_enabled(self):
        with patch('%s.logger' % pbm, autospec=True) as mock_logger, \
            patch('%s.open' % pbm, mock_open(read_data='foo'),
                  create=True) as m_open:

            cloudmapper_filename = datetime.datetime.now().strftime(
                'cloudmapper_report_%Y-%m-%d.html')

            self.cls.generate_and_send_email()

            assert m_open.mock_calls == [
                call('/opt/manheim_cloudmapper/web/account-data/report.html',
                     'r'),
                call().__enter__(),
                call().read(),
                call().__exit__(None, None, None),
                call('/opt/manheim_cloudmapper/web/js/chart.js', 'r'),
                call().__enter__(),
                call().read(),
                call().__exit__(None, None, None),
                call('/opt/manheim_cloudmapper/web/js/report.js', 'r'),
                call().__enter__(),
                call().read(),
                call().__exit__(None, None, None)
            ]

            assert self.mock_ses.mock_calls == [
                call.send_email(
                    '*****@*****.**', 'AWS SES <*****@*****.**>',
                    '[cloudmapper foo] Cloudmapper audit findings',
                    'Please see the attached file for '
                    'cloudmapper results.', '<html><head></head><body><p>foo'
                    '</p></body></html>', {
                        cloudmapper_filename:
                        '<html><head></head><body><p>foo'
                        '</p></body></html>'
                    })
            ]

            assert mock_logger.mock_calls == [call.info("Sending SES Email.")]
Example #9
0
    def test_run_exception_failure_cmd(self):
        m_finished = Mock()
        m_unfinished = Mock()
        m_excs = Mock()
        m_start_dt = Mock()
        m_end_dt = Mock()

        self.client.send_email.side_effect = RuntimeError('foo')
        self.failure_command = ['/bin/something', 'foo']
        m_open = mock_open()
        m_popen = Mock()
        m_popen.communicate.return_value = ('my_output', None)
        type(m_popen).returncode = 0
        with patch('%s._make_report' % pb) as mock_mr:
            with patch('%s.open' % pbm, m_open, create=True):
                with patch.multiple(pbm,
                                    mkstemp=DEFAULT,
                                    Popen=DEFAULT,
                                    os_close=DEFAULT) as mocks:
                    mocks['mkstemp'].return_value = (999, '/tmp/path')
                    mock_mr.return_value = 'my_html_report'
                    mocks['Popen'].return_value = m_popen
                    with pytest.raises(RuntimeError) as exc:
                        self.cls.run(m_finished, m_unfinished, m_excs,
                                     m_start_dt, m_end_dt)
        assert str(exc.value) == 'foo'
        assert mock_mr.mock_calls == [
            call(m_finished, m_unfinished, m_excs, m_start_dt, m_end_dt)
        ]
        assert self.client.mock_calls == [
            call.send_email(
                Source='*****@*****.**',
                Destination={'ToAddresses': ['*****@*****.**', '*****@*****.**']},
                Message={
                    'Subject': {
                        'Data': 'MySubject',
                        'Charset': 'utf-8'
                    },
                    'Body': {
                        'Html': {
                            'Data': 'my_html_report',
                            'Charset': 'utf-8'
                        }
                    }
                },
                ReturnPath='*****@*****.**')
        ]
        assert m_open.mock_calls == [
            call('/tmp/path', 'w'),
            call().__enter__(),
            call().write('my_html_report'),
            call().__exit__(None, None, None)
        ]
        assert mocks['mkstemp'].mock_calls == [
            call(prefix='ecsjobs', text=True, suffix='.html')
        ]
        assert mocks['Popen'].mock_calls == [
            call('/bin/something',
                 'foo',
                 stdin=PIPE,
                 stdout=PIPE,
                 stderr=STDOUT,
                 universal_newlines=True),
            call().communicate(input='\n\nmy_html_report', timeout=120)
        ]
        assert mocks['os_close'].mock_calls == [call(999)]