示例#1
0
    def _save_attachments(value, company):
        """

        """
        if isinstance(value, list):
            return [
                save_attachment(attachment, company) for attachment in value
            ]
        else:
            return save_attachment(value, company)
示例#2
0
    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)
示例#3
0
 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), )