TestCloudOutputDeviceManager.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # Copyright (c) 2018 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. from unittest import TestCase
  4. from unittest.mock import patch, MagicMock
  5. from UM.OutputDevice.OutputDeviceManager import OutputDeviceManager
  6. from cura.UltimakerCloudAuthentication import CuraCloudAPIRoot
  7. from ...src.Cloud import CloudApiClient
  8. from ...src.Cloud import CloudOutputDeviceManager
  9. from .Fixtures import parseFixture, readFixture
  10. from .NetworkManagerMock import NetworkManagerMock, FakeSignal
  11. class TestCloudOutputDeviceManager(TestCase):
  12. maxDiff = None
  13. URL = CuraCloudAPIRoot + "/connect/v1/clusters"
  14. def setUp(self):
  15. super().setUp()
  16. self.app = MagicMock()
  17. self.device_manager = OutputDeviceManager()
  18. self.app.getOutputDeviceManager.return_value = self.device_manager
  19. self.patches = [patch("UM.Qt.QtApplication.QtApplication.getInstance", return_value=self.app),
  20. patch("UM.Application.Application.getInstance", return_value=self.app)]
  21. for patched_method in self.patches:
  22. patched_method.start()
  23. self.network = NetworkManagerMock()
  24. self.timer = MagicMock(timeout = FakeSignal())
  25. with patch.object(CloudApiClient, "QNetworkAccessManager", return_value = self.network), \
  26. patch.object(CloudOutputDeviceManager, "QTimer", return_value = self.timer):
  27. self.manager = CloudOutputDeviceManager.CloudOutputDeviceManager()
  28. self.clusters_response = parseFixture("getClusters")
  29. self.network.prepareReply("GET", self.URL, 200, readFixture("getClusters"))
  30. def tearDown(self):
  31. try:
  32. self._beforeTearDown()
  33. self.network.flushReplies()
  34. self.manager.stop()
  35. for patched_method in self.patches:
  36. patched_method.stop()
  37. finally:
  38. super().tearDown()
  39. ## Before tear down method we check whether the state of the output device manager is what we expect based on the
  40. # mocked API response.
  41. def _beforeTearDown(self):
  42. # let the network send replies
  43. self.network.flushReplies()
  44. # get the created devices
  45. devices = self.device_manager.getOutputDevices()
  46. # TODO: Check active device
  47. response_clusters = self.clusters_response.get("data", [])
  48. manager_clusters = sorted([device.clusterData.toDict() for device in self.manager._remote_clusters.values()],
  49. key=lambda cluster: cluster['cluster_id'], reverse=True)
  50. self.assertEqual(response_clusters, manager_clusters)
  51. ## Runs the initial request to retrieve the clusters.
  52. def _loadData(self):
  53. self.manager.start()
  54. self.network.flushReplies()
  55. def test_device_is_created(self):
  56. # just create the cluster, it is checked at tearDown
  57. self._loadData()
  58. def test_device_is_updated(self):
  59. self._loadData()
  60. # update the cluster from member variable, which is checked at tearDown
  61. self.clusters_response["data"][0]["host_name"] = "New host name"
  62. self.network.prepareReply("GET", self.URL, 200, self.clusters_response)
  63. self.manager._update_timer.timeout.emit()
  64. def test_device_is_removed(self):
  65. self._loadData()
  66. # delete the cluster from member variable, which is checked at tearDown
  67. del self.clusters_response["data"][1]
  68. self.network.prepareReply("GET", self.URL, 200, self.clusters_response)
  69. self.manager._update_timer.timeout.emit()
  70. def test_device_connects_by_cluster_id(self):
  71. active_machine_mock = self.app.getGlobalContainerStack.return_value
  72. cluster1, cluster2 = self.clusters_response["data"]
  73. cluster_id = cluster1["cluster_id"]
  74. active_machine_mock.getMetaDataEntry.side_effect = {"um_cloud_cluster_id": cluster_id}.get
  75. self._loadData()
  76. self.assertTrue(self.device_manager.getOutputDevice(cluster1["cluster_id"]).isConnected())
  77. self.assertIsNone(self.device_manager.getOutputDevice(cluster2["cluster_id"]))
  78. self.assertEquals([], active_machine_mock.setMetaDataEntry.mock_calls)
  79. def test_device_connects_by_network_key(self):
  80. active_machine_mock = self.app.getGlobalContainerStack.return_value
  81. cluster1, cluster2 = self.clusters_response["data"]
  82. network_key = cluster2["host_name"] + ".ultimaker.local"
  83. active_machine_mock.getMetaDataEntry.side_effect = {"um_network_key": network_key}.get
  84. self._loadData()
  85. self.assertIsNone(self.device_manager.getOutputDevice(cluster1["cluster_id"]))
  86. self.assertTrue(self.device_manager.getOutputDevice(cluster2["cluster_id"]).isConnected())
  87. active_machine_mock.setMetaDataEntry.assert_called_with("um_cloud_cluster_id", cluster2["cluster_id"])
  88. @patch.object(CloudOutputDeviceManager, "Message")
  89. def test_api_error(self, message_mock):
  90. self.clusters_response = {
  91. "errors": [{"id": "notFound", "title": "Not found!", "http_status": "404", "code": "notFound"}]
  92. }
  93. self.network.prepareReply("GET", self.URL, 200, self.clusters_response)
  94. self._loadData()
  95. message_mock.return_value.show.assert_called_once_with()