mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-08-07 05:53:59 -06:00
STAR-322: Using QNetworkReply.finished signal instead of QNetworkAccessManager.finished
This commit is contained in:
parent
4dc8edb996
commit
2f08854097
9 changed files with 152 additions and 164 deletions
|
@ -4,11 +4,11 @@ import json
|
|||
from json import JSONDecodeError
|
||||
from typing import Callable, List, Type, TypeVar, Union, Optional, Tuple, Dict, Any
|
||||
|
||||
from PyQt5.QtNetwork import QNetworkRequest, QNetworkReply
|
||||
from PyQt5.QtCore import QObject, QUrl
|
||||
from PyQt5.QtNetwork import QNetworkRequest, QNetworkReply, QNetworkAccessManager
|
||||
|
||||
from UM.Logger import Logger
|
||||
from cura.API import Account
|
||||
from cura.NetworkClient import NetworkClient
|
||||
from .ResumableUpload import ResumableUpload
|
||||
from ..Models import BaseModel
|
||||
from .Models.CloudClusterResponse import CloudClusterResponse
|
||||
|
@ -21,7 +21,7 @@ from .Models.CloudPrintJobResponse import CloudPrintJobResponse
|
|||
|
||||
## The cloud API client is responsible for handling the requests and responses from the cloud.
|
||||
# Each method should only handle models instead of exposing Any HTTP details.
|
||||
class CloudApiClient(NetworkClient):
|
||||
class CloudApiClient:
|
||||
|
||||
# The cloud URL to use for this remote cluster.
|
||||
# TODO: Make sure that this URL goes to the live api before release
|
||||
|
@ -34,6 +34,7 @@ class CloudApiClient(NetworkClient):
|
|||
# \param on_error: The callback to be called whenever we receive errors from the server.
|
||||
def __init__(self, account: Account, on_error: Callable[[List[CloudErrorObject]], None]) -> None:
|
||||
super().__init__()
|
||||
self._manager = QNetworkAccessManager()
|
||||
self._account = account
|
||||
self._on_error = on_error
|
||||
|
||||
|
@ -46,14 +47,18 @@ class CloudApiClient(NetworkClient):
|
|||
# \param on_finished: The function to be called after the result is parsed.
|
||||
def getClusters(self, on_finished: Callable[[List[CloudClusterResponse]], Any]) -> None:
|
||||
url = "{}/clusters".format(self.CLUSTER_API_ROOT)
|
||||
self.get(url, on_finished=self._wrapCallback(on_finished, CloudClusterResponse))
|
||||
reply = self._manager.get(self._createEmptyRequest(url))
|
||||
callback = self._wrapCallback(reply, on_finished, CloudClusterResponse)
|
||||
reply.finished.connect(callback)
|
||||
|
||||
## Retrieves the status of the given cluster.
|
||||
# \param cluster_id: The ID of the cluster.
|
||||
# \param on_finished: The function to be called after the result is parsed.
|
||||
def getClusterStatus(self, cluster_id: str, on_finished: Callable[[CloudClusterStatus], Any]) -> None:
|
||||
url = "{}/clusters/{}/status".format(self.CLUSTER_API_ROOT, cluster_id)
|
||||
self.get(url, on_finished=self._wrapCallback(on_finished, CloudClusterStatus))
|
||||
reply = self._manager.get(self._createEmptyRequest(url))
|
||||
callback = self._wrapCallback(reply, on_finished, CloudClusterStatus)
|
||||
reply.finished.connect(callback)
|
||||
|
||||
## Requests the cloud to register the upload of a print job mesh.
|
||||
# \param request: The request object.
|
||||
|
@ -62,7 +67,9 @@ class CloudApiClient(NetworkClient):
|
|||
) -> None:
|
||||
url = "{}/jobs/upload".format(self.CURA_API_ROOT)
|
||||
body = json.dumps({"data": request.toDict()})
|
||||
self.put(url, body, on_finished=self._wrapCallback(on_finished, CloudPrintJobResponse))
|
||||
reply = self._manager.put(self._createEmptyRequest(url), body.encode())
|
||||
callback = self._wrapCallback(reply, on_finished, CloudPrintJobResponse)
|
||||
reply.finished.connect(callback)
|
||||
|
||||
## Requests the cloud to register the upload of a print job mesh.
|
||||
# \param upload_response: The object received after requesting an upload with `self.requestUpload`.
|
||||
|
@ -72,7 +79,7 @@ class CloudApiClient(NetworkClient):
|
|||
# \param on_error: A function to be called if the upload fails. It receives a dict with the error.
|
||||
def uploadMesh(self, upload_response: CloudPrintJobResponse, mesh: bytes, on_finished: Callable[[], Any],
|
||||
on_progress: Callable[[int], Any], on_error: Callable[[], Any]):
|
||||
ResumableUpload(upload_response.upload_url, upload_response.content_type, mesh, on_finished,
|
||||
ResumableUpload(self._manager, upload_response.upload_url, upload_response.content_type, mesh, on_finished,
|
||||
on_progress, on_error).start()
|
||||
|
||||
# Requests a cluster to print the given print job.
|
||||
|
@ -81,13 +88,17 @@ class CloudApiClient(NetworkClient):
|
|||
# \param on_finished: The function to be called after the result is parsed.
|
||||
def requestPrint(self, cluster_id: str, job_id: str, on_finished: Callable[[CloudPrintResponse], Any]) -> None:
|
||||
url = "{}/clusters/{}/print/{}".format(self.CLUSTER_API_ROOT, cluster_id, job_id)
|
||||
self.post(url, data = "", on_finished=self._wrapCallback(on_finished, CloudPrintResponse))
|
||||
reply = self._manager.post(self._createEmptyRequest(url), b"")
|
||||
callback = self._wrapCallback(reply, on_finished, CloudPrintResponse)
|
||||
reply.finished.connect(callback)
|
||||
|
||||
## We override _createEmptyRequest in order to add the user credentials.
|
||||
# \param url: The URL to request
|
||||
# \param content_type: The type of the body contents.
|
||||
def _createEmptyRequest(self, path: str, content_type: Optional[str] = "application/json") -> QNetworkRequest:
|
||||
request = super()._createEmptyRequest(path, content_type)
|
||||
request = QNetworkRequest(QUrl(path))
|
||||
if content_type:
|
||||
request.setHeader(QNetworkRequest.ContentTypeHeader, content_type)
|
||||
if self._account.isLoggedIn:
|
||||
request.setRawHeader(b"Authorization", "Bearer {}".format(self._account.accessToken).encode())
|
||||
Logger.log("i", "Created request for URL %s. Logged in = %s", path, self._account.isLoggedIn)
|
||||
|
@ -132,10 +143,11 @@ class CloudApiClient(NetworkClient):
|
|||
# \param model: The type of the model to convert the response to. It may either be a single record or a list.
|
||||
# \return: A function that can be passed to the
|
||||
def _wrapCallback(self,
|
||||
reply: QNetworkReply,
|
||||
on_finished: Callable[[Union[Model, List[Model]]], Any],
|
||||
model: Type[Model],
|
||||
) -> Callable[[QNetworkReply], None]:
|
||||
def parse(reply: QNetworkReply) -> None:
|
||||
def parse() -> None:
|
||||
status_code, response = self._parseReply(reply)
|
||||
return self._parseModels(response, on_finished, model)
|
||||
return parse
|
||||
|
|
|
@ -172,8 +172,6 @@ class CloudOutputDevice(NetworkedPrinterOutputDevice):
|
|||
if self._last_response_time and time() - self._last_response_time < self.CHECK_CLUSTER_INTERVAL:
|
||||
return # avoid calling the cloud too often
|
||||
|
||||
Logger.log("i", "Requesting update for %s after %s", self._device_id,
|
||||
self._last_response_time and time() - self._last_response_time)
|
||||
if self._account.isLoggedIn:
|
||||
self.setAuthenticationState(AuthState.Authenticated)
|
||||
self._api.getClusterStatus(self._device_id, self._onStatusCallFinished)
|
||||
|
|
|
@ -125,13 +125,14 @@ class CloudOutputDeviceManager:
|
|||
## Handles an API error received from the cloud.
|
||||
# \param errors: The errors received
|
||||
def _onApiError(self, errors: List[CloudErrorObject]) -> None:
|
||||
message = ". ".join(e.title for e in errors) # TODO: translate errors
|
||||
Message(
|
||||
text = message,
|
||||
text = ". ".join(e.title for e in errors) # TODO: translate errors
|
||||
message = Message(
|
||||
text = text,
|
||||
title = self.I18N_CATALOG.i18nc("@info:title", "Error"),
|
||||
lifetime = 10,
|
||||
dismissable = True
|
||||
).show()
|
||||
)
|
||||
message.show()
|
||||
|
||||
def start(self):
|
||||
if self._running:
|
||||
|
@ -141,7 +142,6 @@ class CloudOutputDeviceManager:
|
|||
# When switching machines we check if we have to activate a remote cluster.
|
||||
application.globalContainerStackChanged.connect(self._connectToActiveMachine)
|
||||
self._update_timer.timeout.connect(self._getRemoteClusters)
|
||||
self._api.start()
|
||||
self._onLoginStateChanged(is_logged_in = self._account.isLoggedIn)
|
||||
|
||||
def stop(self):
|
||||
|
@ -152,5 +152,4 @@ class CloudOutputDeviceManager:
|
|||
# When switching machines we check if we have to activate a remote cluster.
|
||||
application.globalContainerStackChanged.disconnect(self._connectToActiveMachine)
|
||||
self._update_timer.timeout.disconnect(self._getRemoteClusters)
|
||||
self._api.stop()
|
||||
self._onLoginStateChanged(is_logged_in = False)
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
# Copyright (c) 2018 Ultimaker B.V.
|
||||
# !/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
from PyQt5.QtNetwork import QNetworkRequest, QNetworkReply
|
||||
from PyQt5.QtCore import QUrl
|
||||
from PyQt5.QtNetwork import QNetworkRequest, QNetworkReply, QNetworkAccessManager
|
||||
from typing import Optional, Callable, Any, Tuple
|
||||
|
||||
from UM.Logger import Logger
|
||||
from cura.NetworkClient import NetworkClient
|
||||
|
||||
|
||||
class ResumableUpload(NetworkClient):
|
||||
class ResumableUpload:
|
||||
MAX_RETRIES = 10
|
||||
BYTES_PER_REQUEST = 256 * 1024
|
||||
RETRY_HTTP_CODES = {500, 502, 503, 504}
|
||||
|
@ -18,9 +18,9 @@ class ResumableUpload(NetworkClient):
|
|||
# \param content_length: The total content length of the file, in bytes.
|
||||
# \param http_method: The HTTP method to be used, e.g. "POST" or "PUT".
|
||||
# \param timeout: The timeout for each chunk upload. Important: If None, no timeout is applied at all.
|
||||
def __init__(self, url: str, content_type: str, data: bytes,
|
||||
def __init__(self, manager: QNetworkAccessManager, url: str, content_type: str, data: bytes,
|
||||
on_finished: Callable[[], Any], on_progress: Callable[[int], Any], on_error: Callable[[], Any]):
|
||||
super().__init__()
|
||||
self._manager = manager
|
||||
self._url = url
|
||||
self._content_type = content_type
|
||||
self._data = data
|
||||
|
@ -32,13 +32,15 @@ class ResumableUpload(NetworkClient):
|
|||
self._sent_bytes = 0
|
||||
self._retries = 0
|
||||
self._finished = False
|
||||
self._reply = None # type: Optional[QNetworkReply]
|
||||
|
||||
## We override _createEmptyRequest in order to add the user credentials.
|
||||
## We override _createRequest in order to add the user credentials.
|
||||
# \param url: The URL to request
|
||||
# \param content_type: The type of the body contents.
|
||||
def _createEmptyRequest(self, path: str, content_type: Optional[str] = "application/json") -> QNetworkRequest:
|
||||
request = super()._createEmptyRequest(path, content_type = self._content_type)
|
||||
|
||||
def _createRequest(self) -> QNetworkRequest:
|
||||
request = QNetworkRequest(QUrl(self._url))
|
||||
request.setHeader(QNetworkRequest.ContentTypeHeader, self._content_type)
|
||||
|
||||
first_byte, last_byte = self._chunkRange()
|
||||
content_range = "bytes {}-{}/{}".format(first_byte, last_byte - 1, len(self._data))
|
||||
request.setRawHeader(b"Content-Range", content_range.encode())
|
||||
|
@ -51,7 +53,6 @@ class ResumableUpload(NetworkClient):
|
|||
return self._sent_bytes, last_byte
|
||||
|
||||
def start(self) -> None:
|
||||
super().start()
|
||||
if self._finished:
|
||||
self._sent_bytes = 0
|
||||
self._retries = 0
|
||||
|
@ -59,7 +60,6 @@ class ResumableUpload(NetworkClient):
|
|||
self._uploadChunk()
|
||||
|
||||
def stop(self):
|
||||
super().stop()
|
||||
Logger.log("i", "Stopped uploading")
|
||||
self._finished = True
|
||||
|
||||
|
@ -68,47 +68,43 @@ class ResumableUpload(NetworkClient):
|
|||
raise ValueError("The upload is already finished")
|
||||
|
||||
first_byte, last_byte = self._chunkRange()
|
||||
# self.put(self._url, data = self._data[first_byte:last_byte], content_type = self._content_type,
|
||||
# on_finished = self.finishedCallback, on_progress = self._progressCallback)
|
||||
request = self._createEmptyRequest(self._url, content_type=self._content_type)
|
||||
request = self._createRequest()
|
||||
|
||||
reply = self._manager.put(request, self._data[first_byte:last_byte])
|
||||
reply.finished.connect(lambda: self._finishedCallback(reply))
|
||||
reply.uploadProgress.connect(self._progressCallback)
|
||||
reply.error.connect(self._errorCallback)
|
||||
if reply.isFinished():
|
||||
self._finishedCallback(reply)
|
||||
self._reply = self._manager.put(request, self._data[first_byte:last_byte])
|
||||
self._reply.finished.connect(self._finishedCallback)
|
||||
self._reply.uploadProgress.connect(self._progressCallback)
|
||||
self._reply.error.connect(self._errorCallback)
|
||||
|
||||
def _progressCallback(self, bytes_sent: int, bytes_total: int) -> None:
|
||||
Logger.log("i", "Progress callback %s / %s", bytes_sent, bytes_total)
|
||||
if bytes_total:
|
||||
self._on_progress(int((self._sent_bytes + bytes_sent) / len(self._data) * 100))
|
||||
|
||||
def _errorCallback(self, reply: QNetworkReply) -> None:
|
||||
body = bytes(reply.readAll()).decode()
|
||||
def _errorCallback(self) -> None:
|
||||
body = bytes(self._reply.readAll()).decode()
|
||||
Logger.log("e", "Received error while uploading: %s", body)
|
||||
self.stop()
|
||||
self._on_error()
|
||||
|
||||
def _finishedCallback(self, reply: QNetworkReply) -> None:
|
||||
def _finishedCallback(self) -> None:
|
||||
Logger.log("i", "Finished callback %s %s",
|
||||
reply.attribute(QNetworkRequest.HttpStatusCodeAttribute), reply.url().toString())
|
||||
self._reply.attribute(QNetworkRequest.HttpStatusCodeAttribute), self._reply.url().toString())
|
||||
|
||||
status_code = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)
|
||||
status_code = self._reply.attribute(QNetworkRequest.HttpStatusCodeAttribute)
|
||||
|
||||
if self._retries < self.MAX_RETRIES and status_code in self.RETRY_HTTP_CODES:
|
||||
self._retries += 1
|
||||
Logger.log("i", "Retrying %s/%s request %s", self._retries, self.MAX_RETRIES, reply.url().toString())
|
||||
Logger.log("i", "Retrying %s/%s request %s", self._retries, self.MAX_RETRIES, self._reply.url().toString())
|
||||
self._uploadChunk()
|
||||
return
|
||||
|
||||
if status_code > 308:
|
||||
self._errorCallback(reply)
|
||||
self._errorCallback()
|
||||
return
|
||||
|
||||
body = bytes(reply.readAll()).decode()
|
||||
body = bytes(self._reply.readAll()).decode()
|
||||
Logger.log("w", "status_code: %s, Headers: %s, body: %s", status_code,
|
||||
[bytes(header).decode() for header in reply.rawHeaderList()], body)
|
||||
[bytes(header).decode() for header in self._reply.rawHeaderList()], body)
|
||||
|
||||
first_byte, last_byte = self._chunkRange()
|
||||
self._sent_bytes += last_byte - first_byte
|
||||
|
|
|
@ -4,12 +4,27 @@ import json
|
|||
from typing import Dict, Tuple, Union, Optional
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkReply, QNetworkRequest
|
||||
from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest
|
||||
|
||||
from UM.Logger import Logger
|
||||
from UM.Signal import Signal
|
||||
|
||||
|
||||
class FakeSignal:
|
||||
def __init__(self):
|
||||
self._callbacks = []
|
||||
|
||||
def connect(self, callback):
|
||||
self._callbacks.append(callback)
|
||||
|
||||
def disconnect(self, callback):
|
||||
self._callbacks.remove(callback)
|
||||
|
||||
def emit(self, *args, **kwargs):
|
||||
for callback in self._callbacks:
|
||||
callback(*args, **kwargs)
|
||||
|
||||
|
||||
## This class can be used to mock the QNetworkManager class and test the code using it.
|
||||
# After patching the QNetworkManager class, requests are prepared before they can be executed.
|
||||
# Any requests not prepared beforehand will cause KeyErrors.
|
||||
|
@ -27,7 +42,7 @@ class NetworkManagerMock:
|
|||
## Initializes the network manager mock.
|
||||
def __init__(self) -> None:
|
||||
# a dict with the prepared replies, using the format {(http_method, url): reply}
|
||||
self.replies = {} # type: Dict[Tuple[str, str], QNetworkReply]
|
||||
self.replies = {} # type: Dict[Tuple[str, str], MagicMock]
|
||||
self.request_bodies = {} # type: Dict[Tuple[str, str], bytes]
|
||||
|
||||
# signals used in the network manager.
|
||||
|
@ -64,6 +79,8 @@ class NetworkManagerMock:
|
|||
reply_mock.url().toString.return_value = url
|
||||
reply_mock.operation.return_value = self._OPERATIONS[method]
|
||||
reply_mock.attribute.return_value = status_code
|
||||
reply_mock.finished = FakeSignal()
|
||||
reply_mock.isFinished.return_value = False
|
||||
reply_mock.readAll.return_value = response if isinstance(response, bytes) else json.dumps(response).encode()
|
||||
self.replies[method, url] = reply_mock
|
||||
Logger.log("i", "Prepared mock {}-response to {} {}", status_code, method, url)
|
||||
|
@ -78,6 +95,8 @@ class NetworkManagerMock:
|
|||
def flushReplies(self) -> None:
|
||||
for key, reply in self.replies.items():
|
||||
Logger.log("i", "Flushing reply to {} {}", *key)
|
||||
reply.isFinished.return_value = True
|
||||
reply.finished.emit()
|
||||
self.finished.emit(reply)
|
||||
self.reset()
|
||||
|
||||
|
|
|
@ -8,6 +8,8 @@ from unittest.mock import patch, MagicMock
|
|||
|
||||
from cura.CuraApplication import CuraApplication
|
||||
from src.Cloud.CloudApiClient import CloudApiClient
|
||||
from src.Cloud.Models.CloudClusterResponse import CloudClusterResponse
|
||||
from src.Cloud.Models.CloudClusterStatus import CloudClusterStatus
|
||||
from src.Cloud.Models.CloudPrintJobResponse import CloudPrintJobResponse
|
||||
from src.Cloud.Models.CloudPrintJobUploadRequest import CloudPrintJobUploadRequest
|
||||
from src.Cloud.Models.CloudErrorObject import CloudErrorObject
|
||||
|
@ -15,7 +17,6 @@ from tests.Cloud.Fixtures import readFixture, parseFixture
|
|||
from .NetworkManagerMock import NetworkManagerMock
|
||||
|
||||
|
||||
@patch("cura.NetworkClient.QNetworkAccessManager")
|
||||
class TestCloudApiClient(TestCase):
|
||||
def _errorHandler(self, errors: List[CloudErrorObject]):
|
||||
raise Exception("Received unexpected error: {}".format(errors))
|
||||
|
@ -27,15 +28,14 @@ class TestCloudApiClient(TestCase):
|
|||
|
||||
self.app = CuraApplication.getInstance()
|
||||
self.network = NetworkManagerMock()
|
||||
self.api = CloudApiClient(self.account, self._errorHandler)
|
||||
|
||||
def test_GetClusters(self, network_mock):
|
||||
network_mock.return_value = self.network
|
||||
with patch("src.Cloud.CloudApiClient.QNetworkAccessManager", return_value = self.network):
|
||||
self.api = CloudApiClient(self.account, self._errorHandler)
|
||||
|
||||
def test_getClusters(self):
|
||||
result = []
|
||||
|
||||
with open("{}/Fixtures/getClusters.json".format(os.path.dirname(__file__)), "rb") as f:
|
||||
response = f.read()
|
||||
response = readFixture("getClusters")
|
||||
data = parseFixture("getClusters")["data"]
|
||||
|
||||
self.network.prepareReply("GET", "https://api-staging.ultimaker.com/connect/v1/clusters", 200, response)
|
||||
# the callback is a function that adds the result of the call to getClusters to the result list
|
||||
|
@ -43,32 +43,26 @@ class TestCloudApiClient(TestCase):
|
|||
|
||||
self.network.flushReplies()
|
||||
|
||||
self.assertEqual(2, len(result))
|
||||
|
||||
def test_getClusterStatus(self, network_mock):
|
||||
network_mock.return_value = self.network
|
||||
self.assertEqual([CloudClusterResponse(**data[0]), CloudClusterResponse(**data[1])], result)
|
||||
|
||||
def test_getClusterStatus(self):
|
||||
result = []
|
||||
|
||||
with open("{}/Fixtures/getClusterStatusResponse.json".format(os.path.dirname(__file__)), "rb") as f:
|
||||
response = f.read()
|
||||
response = readFixture("getClusterStatusResponse")
|
||||
data = parseFixture("getClusterStatusResponse")["data"]
|
||||
|
||||
self.network.prepareReply("GET",
|
||||
"https://api-staging.ultimaker.com/connect/v1/clusters/R0YcLJwar1ugh0ikEZsZs8NWKV6vJP_LdYsXgXqAcaNC/status",
|
||||
200, response
|
||||
)
|
||||
self.api.getClusterStatus("R0YcLJwar1ugh0ikEZsZs8NWKV6vJP_LdYsXgXqAcaNC", lambda status: result.append(status))
|
||||
self.api.getClusterStatus("R0YcLJwar1ugh0ikEZsZs8NWKV6vJP_LdYsXgXqAcaNC", lambda s: result.append(s))
|
||||
|
||||
self.network.flushReplies()
|
||||
|
||||
self.assertEqual(len(result), 1)
|
||||
status = result[0]
|
||||
self.assertEqual([CloudClusterStatus(**data)], result)
|
||||
|
||||
self.assertEqual(len(status.printers), 2)
|
||||
self.assertEqual(len(status.print_jobs), 1)
|
||||
|
||||
def test_requestUpload(self, network_mock):
|
||||
network_mock.return_value = self.network
|
||||
def test_requestUpload(self):
|
||||
|
||||
results = []
|
||||
|
||||
response = readFixture("putJobUploadResponse")
|
||||
|
@ -78,11 +72,11 @@ class TestCloudApiClient(TestCase):
|
|||
self.api.requestUpload(request, lambda r: results.append(r))
|
||||
self.network.flushReplies()
|
||||
|
||||
self.assertEqual(results[0].content_type, "text/plain")
|
||||
self.assertEqual(results[0].status, "uploading")
|
||||
self.assertEqual(["text/plain"], [r.content_type for r in results])
|
||||
self.assertEqual(["uploading"], [r.status for r in results])
|
||||
|
||||
def test_uploadMesh(self, network_mock):
|
||||
network_mock.return_value = self.network
|
||||
def test_uploadMesh(self):
|
||||
|
||||
results = []
|
||||
progress = MagicMock()
|
||||
|
||||
|
@ -101,8 +95,8 @@ class TestCloudApiClient(TestCase):
|
|||
|
||||
self.assertEqual(["sent"], results)
|
||||
|
||||
def test_requestPrint(self, network_mock):
|
||||
network_mock.return_value = self.network
|
||||
def test_requestPrint(self):
|
||||
|
||||
results = []
|
||||
|
||||
response = readFixture("postJobPrintResponse")
|
||||
|
@ -120,7 +114,6 @@ class TestCloudApiClient(TestCase):
|
|||
|
||||
self.network.flushReplies()
|
||||
|
||||
self.assertEqual(len(results), 1)
|
||||
self.assertEqual(results[0].job_id, job_id)
|
||||
self.assertEqual(results[0].cluster_job_id, cluster_job_id)
|
||||
self.assertEqual(results[0].status, "queued")
|
||||
self.assertEqual([job_id], [r.job_id for r in results])
|
||||
self.assertEqual([cluster_job_id], [r.cluster_job_id for r in results])
|
||||
self.assertEqual(["queued"], [r.status for r in results])
|
||||
|
|
|
@ -13,7 +13,6 @@ from tests.Cloud.Fixtures import readFixture, parseFixture
|
|||
from .NetworkManagerMock import NetworkManagerMock
|
||||
|
||||
|
||||
@patch("cura.NetworkClient.QNetworkAccessManager")
|
||||
class TestCloudOutputDevice(TestCase):
|
||||
CLUSTER_ID = "RIZ6cZbWA_Ua7RZVJhrdVfVpf0z-MqaSHQE4v8aRTtYq"
|
||||
JOB_ID = "ABCDefGHIjKlMNOpQrSTUvYxWZ0-1234567890abcDE="
|
||||
|
@ -30,7 +29,9 @@ class TestCloudOutputDevice(TestCase):
|
|||
self.network = NetworkManagerMock()
|
||||
self.account = MagicMock(isLoggedIn=True, accessToken="TestAccessToken")
|
||||
self.onError = MagicMock()
|
||||
self.device = CloudOutputDevice(CloudApiClient(self.account, self.onError), self.CLUSTER_ID, self.HOST_NAME)
|
||||
with patch("src.Cloud.CloudApiClient.QNetworkAccessManager", return_value = self.network):
|
||||
self._api = CloudApiClient(self.account, self.onError)
|
||||
self.device = CloudOutputDevice(self._api, self.CLUSTER_ID, self.HOST_NAME)
|
||||
self.cluster_status = parseFixture("getClusterStatusResponse")
|
||||
self.network.prepareReply("GET", self.STATUS_URL, 200, readFixture("getClusterStatusResponse"))
|
||||
|
||||
|
@ -38,8 +39,7 @@ class TestCloudOutputDevice(TestCase):
|
|||
super().tearDown()
|
||||
self.network.flushReplies()
|
||||
|
||||
def test_status(self, network_mock):
|
||||
network_mock.return_value = self.network
|
||||
def test_status(self):
|
||||
self.device._update()
|
||||
self.network.flushReplies()
|
||||
|
||||
|
@ -69,32 +69,34 @@ class TestCloudOutputDevice(TestCase):
|
|||
self.assertEqual({job["name"] for job in self.cluster_status["data"]["print_jobs"]},
|
||||
{job.name for job in self.device.printJobs})
|
||||
|
||||
def test_remove_print_job(self, network_mock):
|
||||
network_mock.return_value = self.network
|
||||
def test_remove_print_job(self):
|
||||
self.device._update()
|
||||
self.network.flushReplies()
|
||||
self.assertEqual(1, len(self.device.printJobs))
|
||||
|
||||
self.cluster_status["data"]["print_jobs"].clear()
|
||||
self.network.prepareReply("GET", self.STATUS_URL, 200, self.cluster_status)
|
||||
|
||||
self.device._last_response_time = None
|
||||
self.device._update()
|
||||
self.network.flushReplies()
|
||||
self.assertEqual([], self.device.printJobs)
|
||||
|
||||
def test_remove_printers(self, network_mock):
|
||||
network_mock.return_value = self.network
|
||||
def test_remove_printers(self):
|
||||
self.device._update()
|
||||
self.network.flushReplies()
|
||||
self.assertEqual(2, len(self.device.printers))
|
||||
|
||||
self.cluster_status["data"]["printers"].clear()
|
||||
self.network.prepareReply("GET", self.STATUS_URL, 200, self.cluster_status)
|
||||
|
||||
self.device._last_response_time = None
|
||||
self.device._update()
|
||||
self.network.flushReplies()
|
||||
self.assertEqual([], self.device.printers)
|
||||
|
||||
@patch("cura.CuraApplication.CuraApplication.getGlobalContainerStack")
|
||||
def test_print_to_cloud(self, global_container_stack_mock, network_mock):
|
||||
def test_print_to_cloud(self, global_container_stack_mock):
|
||||
active_machine_mock = global_container_stack_mock.return_value
|
||||
active_machine_mock.getMetaDataEntry.side_effect = {"file_formats": "application/gzip"}.get
|
||||
|
||||
|
@ -104,7 +106,6 @@ class TestCloudOutputDevice(TestCase):
|
|||
self.network.prepareReply("PUT", request_upload_response["data"]["upload_url"], 201, b"{}")
|
||||
self.network.prepareReply("POST", self.PRINT_URL, 200, request_print_response)
|
||||
|
||||
network_mock.return_value = self.network
|
||||
file_handler = MagicMock()
|
||||
file_handler.getSupportedFileTypesWrite.return_value = [{
|
||||
"extension": "gcode.gz",
|
||||
|
|
|
@ -10,7 +10,6 @@ from tests.Cloud.Fixtures import parseFixture, readFixture
|
|||
from .NetworkManagerMock import NetworkManagerMock
|
||||
|
||||
|
||||
@patch("cura.NetworkClient.QNetworkAccessManager")
|
||||
class TestCloudOutputDeviceManager(TestCase):
|
||||
URL = "https://api-staging.ultimaker.com/connect/v1/clusters"
|
||||
|
||||
|
@ -18,13 +17,15 @@ class TestCloudOutputDeviceManager(TestCase):
|
|||
super().setUp()
|
||||
self.app = CuraApplication.getInstance()
|
||||
self.network = NetworkManagerMock()
|
||||
self.manager = CloudOutputDeviceManager()
|
||||
with patch("src.Cloud.CloudApiClient.QNetworkAccessManager", return_value = self.network):
|
||||
self.manager = CloudOutputDeviceManager()
|
||||
self.clusters_response = parseFixture("getClusters")
|
||||
self.network.prepareReply("GET", self.URL, 200, readFixture("getClusters"))
|
||||
|
||||
def tearDown(self):
|
||||
try:
|
||||
self._beforeTearDown()
|
||||
self.manager.stop()
|
||||
finally:
|
||||
super().tearDown()
|
||||
|
||||
|
@ -47,17 +48,17 @@ class TestCloudOutputDeviceManager(TestCase):
|
|||
device_manager.removeOutputDevice(device["cluster_id"])
|
||||
|
||||
## Runs the initial request to retrieve the clusters.
|
||||
def _loadData(self, network_mock):
|
||||
network_mock.return_value = self.network
|
||||
self.manager._account.loginStateChanged.emit(True)
|
||||
self.manager._update_timer.timeout.emit()
|
||||
def _loadData(self):
|
||||
self.manager.start()
|
||||
self.manager._onLoginStateChanged(is_logged_in = True)
|
||||
self.network.flushReplies()
|
||||
|
||||
def test_device_is_created(self, network_mock):
|
||||
def test_device_is_created(self):
|
||||
# just create the cluster, it is checked at tearDown
|
||||
self._loadData(network_mock)
|
||||
self._loadData()
|
||||
|
||||
def test_device_is_updated(self, network_mock):
|
||||
self._loadData(network_mock)
|
||||
def test_device_is_updated(self):
|
||||
self._loadData()
|
||||
|
||||
# update the cluster from member variable, which is checked at tearDown
|
||||
self.clusters_response["data"][0]["host_name"] = "New host name"
|
||||
|
@ -65,8 +66,8 @@ class TestCloudOutputDeviceManager(TestCase):
|
|||
|
||||
self.manager._update_timer.timeout.emit()
|
||||
|
||||
def test_device_is_removed(self, network_mock):
|
||||
self._loadData(network_mock)
|
||||
def test_device_is_removed(self):
|
||||
self._loadData()
|
||||
|
||||
# delete the cluster from member variable, which is checked at tearDown
|
||||
del self.clusters_response["data"][1]
|
||||
|
@ -75,41 +76,39 @@ class TestCloudOutputDeviceManager(TestCase):
|
|||
self.manager._update_timer.timeout.emit()
|
||||
|
||||
@patch("cura.CuraApplication.CuraApplication.getGlobalContainerStack")
|
||||
def test_device_connects_by_cluster_id(self, global_container_stack_mock, network_mock):
|
||||
def test_device_connects_by_cluster_id(self, global_container_stack_mock):
|
||||
active_machine_mock = global_container_stack_mock.return_value
|
||||
cluster1, cluster2 = self.clusters_response["data"]
|
||||
cluster_id = cluster1["cluster_id"]
|
||||
active_machine_mock.getMetaDataEntry.side_effect = {"um_cloud_cluster_id": cluster_id}.get
|
||||
|
||||
self._loadData(network_mock)
|
||||
self.network.flushReplies()
|
||||
self._loadData()
|
||||
|
||||
self.assertTrue(self.app.getOutputDeviceManager().getOutputDevice(cluster1["cluster_id"]).isConnected())
|
||||
self.assertFalse(self.app.getOutputDeviceManager().getOutputDevice(cluster2["cluster_id"]).isConnected())
|
||||
self.assertEquals([], active_machine_mock.setMetaDataEntry.mock_calls)
|
||||
|
||||
@patch("cura.CuraApplication.CuraApplication.getGlobalContainerStack")
|
||||
def test_device_connects_by_network_key(self, global_container_stack_mock, network_mock):
|
||||
def test_device_connects_by_network_key(self, global_container_stack_mock):
|
||||
active_machine_mock = global_container_stack_mock.return_value
|
||||
|
||||
cluster1, cluster2 = self.clusters_response["data"]
|
||||
network_key = cluster2["host_name"] + ".ultimaker.local"
|
||||
active_machine_mock.getMetaDataEntry.side_effect = {"um_network_key": network_key}.get
|
||||
|
||||
self._loadData(network_mock)
|
||||
self.network.flushReplies()
|
||||
self._loadData()
|
||||
|
||||
self.assertFalse(self.app.getOutputDeviceManager().getOutputDevice(cluster1["cluster_id"]).isConnected())
|
||||
self.assertTrue(self.app.getOutputDeviceManager().getOutputDevice(cluster2["cluster_id"]).isConnected())
|
||||
|
||||
active_machine_mock.setMetaDataEntry.assert_called_with("um_cloud_cluster_id", cluster2["cluster_id"])
|
||||
|
||||
@patch("UM.Message.Message.show")
|
||||
def test_api_error(self, message_mock, network_mock):
|
||||
@patch("src.Cloud.CloudOutputDeviceManager.Message")
|
||||
def test_api_error(self, message_mock):
|
||||
self.clusters_response = {
|
||||
"errors": [{"id": "notFound", "title": "Not found!", "http_status": "404", "code": "notFound"}]
|
||||
}
|
||||
self.network.prepareReply("GET", self.URL, 200, self.clusters_response)
|
||||
self._loadData(network_mock)
|
||||
self.network.flushReplies()
|
||||
message_mock.assert_called_once_with()
|
||||
self._loadData()
|
||||
message_mock.assert_called_once_with(text='Not found!', title='Error', lifetime=10, dismissable=True)
|
||||
message_mock.return_value.show.assert_called_once_with()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue