def test_upload_user_file_by_url(data_fixture, tmpdir): user = data_fixture.create_user() storage = FileSystemStorage(location=str(tmpdir), base_url="http://localhost") handler = UserFileHandler() responses.add( responses.GET, "https://baserow.io/test.txt", body=b"Hello World", status=200, content_type="text/plain", stream=True, ) responses.add( responses.GET, "https://baserow.io/not-found.pdf", status=404, ) # Could not be reached because it it responds with a 404 with pytest.raises(FileURLCouldNotBeReached): handler.upload_user_file_by_url( user, "https://baserow.io/not-found.pdf", storage=storage ) # Only the http and https protocol are supported. with pytest.raises(InvalidFileURLError): handler.upload_user_file_by_url( user, "ftp://baserow.io/not-found.pdf", storage=storage ) with freeze_time("2020-01-01 12:00"): user_file = handler.upload_user_file_by_url( user, "https://baserow.io/test.txt", storage=storage ) assert user_file.original_name == "test.txt" assert user_file.original_extension == "txt" assert len(user_file.unique) == 32 assert user_file.size == 11 assert user_file.mime_type == "text/plain" assert user_file.uploaded_by_id == user.id assert user_file.uploaded_at.year == 2020 assert user_file.uploaded_at.month == 1 assert user_file.uploaded_at.day == 1 assert user_file.is_image is False assert user_file.image_width is None assert user_file.image_height is None assert user_file.sha256_hash == ( "a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e" ) file_path = tmpdir.join("user_files", user_file.name) assert file_path.isfile() assert file_path.open().read() == "Hello World"
def test_upload_user_file_by_url(data_fixture, tmpdir): user = data_fixture.create_user() storage = FileSystemStorage(location=str(tmpdir), base_url='http://localhost') handler = UserFileHandler() responses.add( responses.GET, 'http://localhost/test.txt', body=b'Hello World', status=200, content_type="text/plain", stream=True, ) responses.add( responses.GET, 'http://localhost/not-found.pdf', body=b'Hello World', status=404, content_type="application/pdf", stream=True, ) with pytest.raises(FileURLCouldNotBeReached): handler.upload_user_file_by_url(user, 'http://localhost/test2.txt', storage=storage) with freeze_time('2020-01-01 12:00'): user_file = handler.upload_user_file_by_url( user, 'http://localhost/test.txt', storage=storage) with pytest.raises(FileURLCouldNotBeReached): handler.upload_user_file_by_url(user, 'http://localhost/not-found.pdf', storage=storage) assert user_file.original_name == 'test.txt' assert user_file.original_extension == 'txt' assert len(user_file.unique) == 32 assert user_file.size == 11 assert user_file.mime_type == 'text/plain' assert user_file.uploaded_by_id == user.id assert user_file.uploaded_at.year == 2020 assert user_file.uploaded_at.month == 1 assert user_file.uploaded_at.day == 1 assert user_file.is_image is False assert user_file.image_width is None assert user_file.image_height is None assert user_file.sha256_hash == ( 'a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e') file_path = tmpdir.join('user_files', user_file.name) assert file_path.isfile() assert file_path.open().read() == 'Hello World'
def test_upload_user_file_by_url_within_private_network(data_fixture, tmpdir): user = data_fixture.create_user() storage = FileSystemStorage(location=str(tmpdir), base_url="http://localhost") handler = UserFileHandler() # Could not be reached because it is an internal private URL. with pytest.raises(FileURLCouldNotBeReached): handler.upload_user_file_by_url( user, "http://localhost/test.txt", storage=storage ) with pytest.raises(FileURLCouldNotBeReached): handler.upload_user_file_by_url( user, "http://192.168.1.1/test.txt", storage=storage )