コード例 #1
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)
コード例 #2
0
 def test_get_attachment_path(self):
     """
     Tests the get_attachment_path function when the path contains
     date variables.
     """
     with patch.dict('sifter.mailsifter.attachments.settings.MAILSIFTER',
                     self.mock_mailsifter_settings):
         with patch('sifter.mailsifter.attachments.settings',
                    self.mock_settings):
             actual = attachments.get_attachment_path('attachments/')
             expected = os.path.join(os.path.dirname(__file__),
                                     'attachments/')
             self.assertEqual(actual, expected)