|
| 1 | +# Copyright The IETF Trust 2021, All Rights Reserved |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +"""Tests of nomcom management commands""" |
| 4 | +import mock |
| 5 | + |
| 6 | +from collections import namedtuple |
| 7 | + |
| 8 | +from django.core.management import call_command |
| 9 | +from django.test.utils import override_settings |
| 10 | + |
| 11 | +from ietf.nomcom.factories import NomComFactory |
| 12 | +from ietf.utils.test_utils import TestCase, name_of_file_containing |
| 13 | + |
| 14 | + |
| 15 | +@override_settings(ADMINS=(('Some Admin', 'admin@example.com'),)) |
| 16 | +class FeedbackEmailTests(TestCase): |
| 17 | + def setUp(self): |
| 18 | + self.year = 2021 |
| 19 | + self.nomcom = NomComFactory(group__acronym=f'nomcom{self.year}') |
| 20 | + |
| 21 | + @mock.patch('ietf.utils.management.base.send_smtp') |
| 22 | + def test_send_error_to_admins(self, send_smtp_mock): |
| 23 | + """If a nomcom chair cannot be identified, mail goes to admins |
| 24 | +
|
| 25 | + This email should not contain either the full traceback or the original message. |
| 26 | + """ |
| 27 | + # Call with the wrong nomcom year so the admin will be contacted |
| 28 | + with name_of_file_containing('feedback message') as filename: |
| 29 | + call_command('feedback_email', nomcom_year=self.year + 1, email_file=filename) |
| 30 | + |
| 31 | + self.assertTrue(send_smtp_mock.called) |
| 32 | + (msg,) = send_smtp_mock.call_args.args # get the message to be sent |
| 33 | + self.assertEqual(msg['to'], 'admin@example.com', 'Email recipient should be the admins') |
| 34 | + self.assertIn('error', msg['subject'], 'Email subject should indicate error') |
| 35 | + self.assertFalse(msg.is_multipart(), 'Nomcom feedback error sent to admin should not have attachments') |
| 36 | + content = msg.get_payload() |
| 37 | + self.assertIn('CommandError', content, 'Admin email should contain error type') |
| 38 | + self.assertIn('feedback_email.py', content, 'Admin email should contain file where error occurred') |
| 39 | + self.assertNotIn('traceback', content.lower(), 'Admin email should not contain traceback') |
| 40 | + self.assertNotIn(f'NomCom {self.year} does not exist', content, |
| 41 | + 'Admin email should not contain error message') |
| 42 | + # not going to check the line - that's too likely to change |
| 43 | + |
| 44 | + @mock.patch('ietf.utils.management.base.send_smtp') |
| 45 | + @mock.patch('ietf.nomcom.management.commands.feedback_email.create_feedback_email') |
| 46 | + def test_send_error_to_chair(self, create_feedback_mock, send_smtp_mock): |
| 47 | + # mock an exception in create_feedback_email() |
| 48 | + create_feedback_mock.side_effect = RuntimeError('mock error') |
| 49 | + |
| 50 | + with name_of_file_containing('feedback message') as filename: |
| 51 | + call_command('feedback_email', nomcom_year=self.year, email_file=filename) |
| 52 | + |
| 53 | + self.assertTrue(send_smtp_mock.called) |
| 54 | + (msg,) = send_smtp_mock.call_args.args # get the message to be sent |
| 55 | + self.assertCountEqual( |
| 56 | + [addr.strip() for addr in msg['to'].split(',')], |
| 57 | + self.nomcom.chair_emails(), |
| 58 | + 'Email recipient should be the nomcom chair(s)', |
| 59 | + ) |
| 60 | + self.assertIn('error', msg['subject'], 'Email subject should indicate error') |
| 61 | + self.assertTrue(msg.is_multipart(), 'Chair feedback error should have attachments') |
| 62 | + parts = msg.get_payload() |
| 63 | + content = parts[0].get_payload() |
| 64 | + # decode=True decodes the base64 encoding, .decode() converts the octet-stream bytes to a string |
| 65 | + attachment = parts[1].get_payload(decode=True).decode() |
| 66 | + self.assertIn('RuntimeError', content, 'Nomcom email should contain error type') |
| 67 | + self.assertIn('mock.py', content, 'Nomcom email should contain file where error occurred') |
| 68 | + self.assertIn('feedback message', attachment, 'Nomcom email should include original message') |
| 69 | + |
| 70 | + @mock.patch('ietf.nomcom.management.commands.feedback_email.create_feedback_email') |
| 71 | + def test_feedback_email(self, create_feedback_mock): |
| 72 | + """The feedback_email command should create feedback""" |
| 73 | + # mock up the return value |
| 74 | + create_feedback_mock.return_value = namedtuple('mock_feedback', 'author')('author@example.com') |
| 75 | + |
| 76 | + with name_of_file_containing('feedback message') as filename: |
| 77 | + call_command('feedback_email', nomcom_year=self.year, email_file=filename) |
| 78 | + |
| 79 | + self.assertEqual(create_feedback_mock.call_count, 1, 'create_feedback_email() should be called once') |
| 80 | + self.assertEqual( |
| 81 | + create_feedback_mock.call_args.args, |
| 82 | + (self.nomcom, b'feedback message'), |
| 83 | + 'feedback_email should process the correct email for the correct nomcom' |
| 84 | + ) |
0 commit comments