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()

[((15, 5, 15, 33), 'unittest.mock.patch', 'mock.patch', ({(15, 16, 15, 32): '"""subprocess.run"""'}, {}), "('subprocess.run')", False, 'from unittest import mock\n'), ((42, 4, 42, 19), 'unittest.main', 'unittest.main', ({}, {}), '()', False, 'import unittest\n'), ((12, 19, 12, 38), 'simplegallery.upload.uploader_factory.get_uploader', 'get_uploader', ({(12, 32, 12, 37): '"""aws"""'}, {}), "('aws')", False, 'from simplegallery.upload.uploader_factory import get_uploader\n'), ((17, 38, 17, 83), 'subprocess.CompletedProcess', 'subprocess.CompletedProcess', (), '', False, 'import subprocess\n'), ((19, 13, 19, 28), 'testfixtures.TempDirectory', 'TempDirectory', ({}, {}), '()', False, 'from testfixtures import TempDirectory\n'), ((22, 27, 22, 67), 'os.path.join', 'os.path.join', ({(22, 40, 22, 52): 'tempdir.path', (22, 54, 22, 66): '"""index.html"""'}, {}), "(tempdir.path, 'index.html')", False, 'import os\n'), ((23, 23, 23, 42), 'simplegallery.upload.uploader_factory.get_uploader', 'get_uploader', ({(23, 36, 23, 41): '"""aws"""'}, {}), "('aws')", False, 'from simplegallery.upload.uploader_factory import get_uploader\n')]