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_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_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), )