def test_save_attachment(self): """ Tests the save_attachment function. """ mock_uuid = Mock() mock_uuid.hex = self.uuid self.msg.attach(self.text_attach) attachment = get_first_attachment(self.msg) with patch('sifter.mailsifter.attachments.uuid.uuid4', return_value=mock_uuid): with patch.dict('sifter.mailsifter.attachments.settings.MAILSIFTER', self.mock_mailsifter_settings): with patch('sifter.mailsifter.attachments.settings', self.mock_settings): file_path = attachments.get_file_path(attachment) full_path = attachments.get_attachment_path(file_path) # check that the test file doesn't already exist error_msg = 'Please delete the test file %s' % full_path assert not os.path.isfile(full_path), error_msg actual = attachments.save_attachment(attachment) expected = 'https://www.example.com/media/test-attachments/'\ 'ef739cc0fe5748fd9dabd832f9b3eac4.txt' # check that the test file was created self.assertIs(os.path.isfile(full_path), True) # clean up the test file before further tests os.remove(full_path) self.assertEqual(actual, expected)
def test_none(self): """ Tests the get_first_attachment function for no attachment. """ actual = accessors.get_first_attachment(self.msg) expected = None self.assertEqual(actual, expected)
def test_inline(self): """ Tests the get_first_attachment function for inline content. """ self.msg.attach(self.inline) self.msg.attach(self.pdf) actual = accessors.get_first_attachment(self.msg) expected = self.pdf self.assertEqual(actual, expected)
def test_attachment_single(self): """ Tests the get_first_attachment function for an attachment. """ self.msg.attach(self.pdf) self.msg.attach(self.image) actual = accessors.get_first_attachment(self.msg) expected = self.pdf self.assertEqual(actual, expected)
def test_get_attachment_content(self): """ Tests the get_attachment_content function. """ self.msg.attach(self.image) attachment = get_first_attachment(self.msg) actual = attachments.get_attachment_content(attachment) expected = bytearray(self.image_payload, encoding='utf-8') self.assertEqual(actual, expected)
def test_unallowed_attachment(self): """ Tests the get_first_attachment function for an unallowed file type. """ self.msg.attach(self.java) self.msg.attach(self.image) actual = accessors.get_first_attachment(self.msg) expected = self.image self.assertEqual(actual, expected)
def test_extension_is_empty_str(self): """ Tests the get_extension function when the file name is an empty string but the extension is guessable from the Content-Type. """ self.part1['Content-Disposition'] = 'attachment; filename=""' self.msg.attach(self.image) attachment = get_first_attachment(self.msg) actual = attachments.get_extension(attachment) expected = '.jpeg' self.assertEqual(actual, expected)
def test_no_filename_guessable(self): """ Tests the get_extension function when no file name is present but the extension is guessable from the Content-Type. """ self.image['Content-Disposition'] = 'attachment;' self.msg.attach(self.image) attachment = get_first_attachment(self.msg) actual = attachments.get_extension(attachment) expected = '.jpeg' self.assertEqual(actual, expected)
def test_extension_is_none(self): """ Tests the get_extension function when the file name has no extension but the extension is guessable from the Content-Type. """ self.image['Content-Disposition'] = 'attachment; filename="profile_pic"' self.msg.attach(self.image) attachment = get_first_attachment(self.msg) actual = attachments.get_extension(attachment) expected = '.jpeg' self.assertEqual(actual, expected)
def test_using_file_extension(self): """ Tests the get_first_attachment function using file extension. """ mock_settings_1 = { 'ALLOWED_EMAIL_ATTACHMENTS': (), 'ALLOWED_FILE_EXTENSIONS': ('.jar', ), } with patch.dict('sifter.mailsifter.accessors.settings.MAILSIFTER', mock_settings_1): self.msg.attach(self.application) attachment = accessors.get_first_attachment(self.msg) self.assertIsNotNone(attachment)
def test_extension_not_guessable(self): """ Tests the get_extension function when no file name is present and the extension is not guessable. """ self.image['Content-Disposition'] = 'attachment; filename=""' self.msg.attach(self.image) attachment = get_first_attachment(self.msg) attachment.get_filename = Mock(return_value=None) with patch('sifter.mailsifter.attachments.mimetypes') as mock_mimetypes: mock_mimetypes.guess_extension = Mock(return_value=None) actual = attachments.get_extension(attachment) expected = '.bin' self.assertEqual(actual, expected)
def test_allowed_file(self): """ Tests the get_file_path function for an allowed file type. """ self.msg.attach(self.image) attachment = get_first_attachment(self.msg) mock_uuid = Mock() mock_uuid.hex = self.uuid with patch('sifter.mailsifter.attachments.uuid.uuid4', return_value=mock_uuid): with patch.dict('sifter.mailsifter.attachments.settings.MAILSIFTER', self.mock_mailsifter_settings): actual = attachments.get_file_path(attachment, self.company) expected = '%s/test-attachments/%s.jpeg' \ % (self.company.uuid.hex, self.uuid) self.assertEqual(actual, expected)
def test_no_file_path(self): """ Tests the save_attachment function. """ mock_settings_1 = {'ALLOWED_EMAIL_ATTACHMENTS': ('application/java', )} with patch.dict('sifter.mailsifter.accessors.settings.MAILSIFTER', mock_settings_1): self.msg.attach(self.java) attachment = get_first_attachment(self.msg) with patch('sifter.mailsifter.attachments.settings', self.mock_settings): with LogCapture() as log_capture: actual = attachments.save_attachment(attachment) expected = None self.assertEqual(actual, expected) msg = 'The attachment %s is not an allowed file type' \ % self.java_file log_capture.check( ('sifter.mailsifter.attachments', 'WARNING', msg), )
def test_unallowed_file(self): """ Tests the get_file_path function for an unallowed file type. """ self.mock_mailsifter_settings['ALLOWED_EMAIL_ATTACHMENTS'] = ('application/java',) with patch.dict('sifter.mailsifter.accessors.settings.MAILSIFTER', self.mock_mailsifter_settings): self.msg.attach(self.java) attachment = get_first_attachment(self.msg) with patch('sifter.mailsifter.attachments.settings', return_value=self.mock_settings): with LogCapture() as log_capture: actual = attachments.get_file_path(attachment) expected = None self.assertEqual(actual, expected) msg = 'The attachment %s is not an allowed file type' \ % self.java_file log_capture.check( ('sifter.mailsifter.attachments', 'WARNING', msg), )