Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_download(self, MockHttpProvider, MockAuthProvider):
path = "./myPath/myFile.txt"
response = HttpResponse(200, None, None)
instance = MockHttpProvider.return_value
instance.download.return_value = response
instance = MockAuthProvider.return_value
instance.authenticate.return_value = "blah"
instance.authenticate_request.return_value = None
http_provider = onedrivesdk.HttpProvider()
auth_provider = onedrivesdk.AuthProvider()
client = onedrivesdk.OneDriveClient("onedriveurl/", http_provider, auth_provider)
client.drives["me"].items["root"].children["newFile.txt"].content.request().download(path)
assert client.http_provider.download.call_args[0][2] == path
assert client.http_provider.download.call_args[0][1] == "onedriveurl/drives/me/items/root/children/newFile.txt/content"
def test_error(self, MockHttpProvider, MockAuthProvider):
"""
Test that the error is thrown and can be correctly handled
"""
try:
response = HttpResponse(404, None, json.dumps({"error":{"code":"itemNotFound", "message":"The resource could not be found"}}))
assert False
except OneDriveError as e:
assert e.status_code == 404
assert e.code == ErrorCode.ItemNotFound
try:
response = HttpResponse(403, None, json.dumps({"error":{"code":"generalException", "message":"TestMessage", "innererror":{"code":"accessDenied", "message":"TestMessage", "innererror":{"code":"unauthenticated", "message":"TestMessage"}}}}))
assert False
except OneDriveError as e:
assert e.status_code == 403
assert e.code == ErrorCode.GeneralException
assert e.matches(ErrorCode.AccessDenied)
assert e.matches(ErrorCode.Unauthenticated)
assert not e.matches(ErrorCode.NotSupported)
def test_polling_background_method(self, MockHttpProvider, MockAuthProvider):
"""
Test that polling in the background actually functions as it should and polls
on a seperate thread.
"""
response = HttpResponse(301, {"Location": "statusLocation"}, "")
instance_http = MockHttpProvider.return_value
instance_http.send.return_value = response
instance_auth = MockAuthProvider.return_value
instance_auth.authenticate.return_value = "blah"
instance_auth.authenticate_request.return_value = None
http_provider = onedrivesdk.HttpProvider()
auth_provider = onedrivesdk.AuthProvider()
client = onedrivesdk.OneDriveClient("onedriveurl/", http_provider, auth_provider)
ref = ItemReference()
ref.id = "testing!id"
mock = Mock()
copy_operation = client.drives["me"].items["testitem!id"].copy(parent_reference=ref, name="newName").request().post()
def test_page_creation(self, MockHttpProvider, MockAuthProvider):
"""
Test page creation when there is no nextLink attached to the collection
"""
response = HttpResponse(200, None, json.dumps({"value":[{"name":"test1", "folder":{}}, {"name":"test2"}]}))
instance = MockHttpProvider.return_value
instance.send.return_value = response
instance = MockAuthProvider.return_value
instance.authenticate.return_value = "blah"
instance.authenticate_request.return_value = None
http_provider = onedrivesdk.HttpProvider()
auth_provider = onedrivesdk.AuthProvider()
client = onedrivesdk.OneDriveClient("onedriveurl/", http_provider, auth_provider)
items = client.drives["me"].items["root"].children.request().get()
assert len(items) == 2
assert isinstance(items, ChildrenCollectionPage)
def test_error(self, MockHttpProvider, MockAuthProvider):
"""
Test that the error is thrown and can be correctly handled
"""
try:
response = HttpResponse(404, None, json.dumps({"error":{"code":"itemNotFound", "message":"The resource could not be found"}}))
assert False
except OneDriveError as e:
assert e.status_code == 404
assert e.code == ErrorCode.ItemNotFound
try:
response = HttpResponse(403, None, json.dumps({"error":{"code":"generalException", "message":"TestMessage", "innererror":{"code":"accessDenied", "message":"TestMessage", "innererror":{"code":"unauthenticated", "message":"TestMessage"}}}}))
assert False
except OneDriveError as e:
assert e.status_code == 403
assert e.code == ErrorCode.GeneralException
assert e.matches(ErrorCode.AccessDenied)
assert e.matches(ErrorCode.Unauthenticated)
assert not e.matches(ErrorCode.NotSupported)
client = onedrivesdk.OneDriveClient("onedriveurl/", http_provider, auth_provider)
ref = ItemReference()
ref.id = "testing!id"
mock = Mock()
copy_operation = client.drives["me"].items["testitem!id"].copy(parent_reference=ref, name="newName").request().post()
response = HttpResponse(200, None, json.dumps({"operation":"copy", "percentageComplete":0, "status": "In progress"}))
instance_http.send.return_value = response
time.sleep(0.2)
assert copy_operation.item is None
response = HttpResponse(200, None, json.dumps({"id" : "testitem!id", "name": "newName"}))
instance_http.send.return_value = response
time.sleep(0.1)
assert copy_operation.item is not None
def test_method_collections(self, MockHttpProvider, MockAuthProvider):
"""
Test that collections are returned properly from method calls that return collections
"""
response = HttpResponse(200, None, json.dumps({"@odata.nextLink":"testing", "value":[{"name":"test1", "folder":{}}, {"name":"test2"}]}))
instance = MockHttpProvider.return_value
instance.send.return_value = response
instance = MockAuthProvider.return_value
instance.authenticate.return_value = "blah"
instance.authenticate_request.return_value = None
http_provider = onedrivesdk.HttpProvider()
auth_provider = onedrivesdk.AuthProvider()
client = onedrivesdk.OneDriveClient("onedriveurl", http_provider, auth_provider)
items = client.drives["me"].items["testitem!id"].delta().request().get()
request = onedrivesdk.ItemDeltaRequest.get_next_page_request(items, client, None)
assert type(request) is ItemDeltaRequest
assert type(request.get()) is ItemDeltaCollectionPage
def test_paging(self, MockHttpProvider, MockAuthProvider):
"""
Test paging of a file in situations where more than one page is available
"""
response = HttpResponse(200, None, json.dumps({"@odata.nextLink":"testing", "value":[{"name":"test1", "folder":{}}, {"name":"test2"}]}))
instance = MockHttpProvider.return_value
instance.send.return_value = response
instance = MockAuthProvider.return_value
instance.authenticate.return_value = "blah"
instance.authenticate_request.return_value = None
http_provider = onedrivesdk.HttpProvider()
auth_provider = onedrivesdk.AuthProvider()
client = onedrivesdk.OneDriveClient("onedriveurl/", http_provider, auth_provider)
items = client.drives["me"].items["root"].children.request().get()
assert items._next_page_link is not None
def get(self, filename):
with self.exception_handler():
request = self._get_item(filename).content.request()
self.client.auth_provider.authenticate_request(request)
response = requests.get(request.request_url, headers=request._headers)
if response.status_code != 200:
# create a response. This will throw appropriate errors as needed
custom_response = onedrivesdk.http_response.HttpResponse(response.status_code, response.headers, response.text)
# in case it didn't throw
assert custom_response.status == 200
return ''.join(response.iter_content())