def test_file_not_allowed(): from pyramid_storage import gcloud fs = mock.Mock() fs.filename = "test.jpg" g = gcloud.GoogleCloudStorage(credentials="/secrets/credentials.json", bucket_name="my_bucket", extensions="documents") assert not g.file_allowed(fs)
def test_save_if_file_not_allowed(): from pyramid_storage import gcloud from pyramid_storage.exceptions import FileNotAllowed fs = mock.Mock() fs.filename = "test.jpg" g = gcloud.GoogleCloudStorage(credentials="/secrets/credentials.json", bucket_name="my_bucket", extensions="documents") with pytest.raises(FileNotAllowed): g.save(fs)
def test_delete(): from pyramid_storage import gcloud g = gcloud.GoogleCloudStorage(credentials="/secrets/credentials.json", bucket_name="my_bucket", extensions="images") with mock.patch('pyramid_storage.gcloud.GoogleCloudStorage.get_connection', _get_mock_gcloud_connection): g.delete("test.jpg")
def test_save_file(): from pyramid_storage import gcloud g = gcloud.GoogleCloudStorage(credentials="/secrets/credentials.json", bucket_name="my_bucket", extensions="images") with mock.patch('pyramid_storage.gcloud.GoogleCloudStorage.get_connection', _get_mock_gcloud_connection): with mock.patch('pyramid_storage.gcloud.Blob') as mocked_new_blob: name = g.save_file(BytesIO(), "test.jpg") assert mocked_new_blob.return_value.upload_from_file.called assert name == "test.jpg"
def test_save_if_randomize(): from pyramid_storage import gcloud fs = mock.Mock() fs.filename = "test.jpg" g = gcloud.GoogleCloudStorage(credentials="/secrets/credentials.json", bucket_name="my_bucket", extensions="images") with mock.patch('pyramid_storage.gcloud.GoogleCloudStorage.get_connection', _get_mock_gcloud_connection): with mock.patch('pyramid_storage.gcloud.Blob') as mocked_new_blob: name = g.save(fs, randomize=True) assert mocked_new_blob.return_value.upload_from_file.called assert name != "test.jpg"
def test_save_filename(): from pyramid_storage import gcloud g = gcloud.GoogleCloudStorage(credentials="/secrets/credentials.json", bucket_name="my_bucket", extensions="images") patches = (mock.patch('builtins.open', _mock_open), mock.patch( 'pyramid_storage.gcloud.GoogleCloudStorage.get_connection', _get_mock_gcloud_connection)) for patch in patches: patch.start() with mock.patch('pyramid_storage.gcloud.Blob') as mocked_new_blob: name = g.save_filename("test.jpg", replace=True) assert name == "test.jpg" assert mocked_new_blob.return_value.upload_from_file.called for patch in patches: patch.stop()
def test_extension_not_allowed_if_allowed_if_not_dotted(): from pyramid_storage import gcloud g = gcloud.GoogleCloudStorage(credentials="/secrets/credentials.json", bucket_name="my_bucket", extensions="images") assert not g.extension_allowed("jpg", ("gif", ))
def test_extension_allowed_if_any(): from pyramid_storage import gcloud g = gcloud.GoogleCloudStorage(credentials="/secrets/credentials.json", bucket_name="my_bucket", extensions="any") assert g.extension_allowed(".jpg")