import unittest
from unittest import mock
import os
import subprocess
from testfixtures import TempDirectory
from simplegallery.upload.uploader_factory import get_uploader


class AWSUploaderTestCase(unittest.TestCase):

    def test_no_location(self):
        uploader = get_uploader('aws')
        self.assertFalse(uploader.check_location(''))

    @mock.patch('subprocess.run')
    def test_upload_gallery(self, subprocess_run):
        subprocess_run.return_value = subprocess.CompletedProcess([], returncode=0)

        with TempDirectory() as tempdir:
            # Setup mock file and uploader
            tempdir.write('index.html', b'')
            gallery_path = os.path.join(tempdir.path, 'index.html')
            uploader = get_uploader('aws')

            # Test upload to bucket
            uploader.upload_gallery('s3://testbucket/path/', gallery_path)
            subprocess_run.assert_called_with(
                ['aws', 's3', 'sync', gallery_path, 's3://testbucket/path/', '--exclude', '.DS_Store'])

            # Test upload to bucket without prefix
            uploader.upload_gallery('testbucket/path/', gallery_path)
            subprocess_run.assert_called_with(
                ['aws', 's3', 'sync', gallery_path, 's3://testbucket/path/', '--exclude', '.DS_Store'])

            # Test upload to bucket without trailing /
            uploader.upload_gallery('s3://testbucket/path', gallery_path)
            subprocess_run.assert_called_with(
                ['aws', 's3', 'sync', gallery_path, 's3://testbucket/path/', '--exclude', '.DS_Store'])


if __name__ == '__main__':
    unittest.main()

[((352, 380), 'unittest.mock.patch', 'mock.patch', (['"""subprocess.run"""'], {}), "('subprocess.run')\n", (362, 380), False, 'from unittest import mock\n'), ((1607, 1622), 'unittest.main', 'unittest.main', ([], {}), '()\n', (1620, 1622), False, 'import unittest\n'), ((272, 291), 'simplegallery.upload.uploader_factory.get_uploader', 'get_uploader', (['"""aws"""'], {}), "('aws')\n", (284, 291), False, 'from simplegallery.upload.uploader_factory import get_uploader\n'), ((470, 515), 'subprocess.CompletedProcess', 'subprocess.CompletedProcess', (['[]'], {'returncode': '(0)'}), '([], returncode=0)\n', (497, 515), False, 'import subprocess\n'), ((530, 545), 'testfixtures.TempDirectory', 'TempDirectory', ([], {}), '()\n', (543, 545), False, 'from testfixtures import TempDirectory\n'), ((673, 713), 'os.path.join', 'os.path.join', (['tempdir.path', '"""index.html"""'], {}), "(tempdir.path, 'index.html')\n", (685, 713), False, 'import os\n'), ((737, 756), 'simplegallery.upload.uploader_factory.get_uploader', 'get_uploader', (['"""aws"""'], {}), "('aws')\n", (749, 756), False, 'from simplegallery.upload.uploader_factory import get_uploader\n')]