TestNetworkedPrinterOutputDevice.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import time
  2. from unittest.mock import MagicMock, patch
  3. from PyQt5.QtNetwork import QNetworkAccessManager
  4. from PyQt5.QtCore import QUrl
  5. from cura.PrinterOutput.NetworkedPrinterOutputDevice import NetworkedPrinterOutputDevice, AuthState
  6. from cura.PrinterOutput.PrinterOutputDevice import ConnectionState
  7. def test_properties():
  8. properties = { b"firmware_version": b"12", b"printer_type": b"BHDHAHHADAD", b"address": b"ZOMG", b"name": b":(", b"testProp": b"zomg"}
  9. with patch("UM.Qt.QtApplication.QtApplication.getInstance"):
  10. output_device = NetworkedPrinterOutputDevice(device_id = "test", address = "127.0.0.1", properties = properties)
  11. assert output_device.address == "ZOMG"
  12. assert output_device.firmwareVersion == "12"
  13. assert output_device.printerType == "BHDHAHHADAD"
  14. assert output_device.ipAddress == "127.0.0.1"
  15. assert output_device.name == ":("
  16. assert output_device.key == "test"
  17. assert output_device.getProperties() == properties
  18. assert output_device.getProperty("testProp") == "zomg"
  19. assert output_device.getProperty("whateverr") == ""
  20. def test_authenticationState():
  21. with patch("UM.Qt.QtApplication.QtApplication.getInstance"):
  22. output_device = NetworkedPrinterOutputDevice(device_id="test", address="127.0.0.1", properties={})
  23. output_device.setAuthenticationState(AuthState.Authenticated)
  24. assert output_device.authenticationState == AuthState.Authenticated
  25. def test_post():
  26. with patch("UM.Qt.QtApplication.QtApplication.getInstance"):
  27. output_device = NetworkedPrinterOutputDevice(device_id="test", address="127.0.0.1", properties={})
  28. mocked_network_manager = MagicMock()
  29. output_device._manager = mocked_network_manager
  30. # Create a fake reply (we cant use a QReply, since those are abstract C++)
  31. reply = MagicMock()
  32. reply.operation = MagicMock(return_value=QNetworkAccessManager.PostOperation)
  33. reply.url = MagicMock(return_value=QUrl("127.0.0.1"))
  34. mocked_network_manager.post = MagicMock(return_value = reply)
  35. mocked_callback_handler = MagicMock()
  36. output_device.post("whatever", "omgzomg", on_finished = mocked_callback_handler.onFinished)
  37. # So we now fake that the request was sucesful.
  38. output_device._handleOnFinished(reply)
  39. # We expect to get a callback regarding this.
  40. mocked_callback_handler.onFinished.assert_called_once_with(reply)
  41. def test_get():
  42. with patch("UM.Qt.QtApplication.QtApplication.getInstance"):
  43. output_device = NetworkedPrinterOutputDevice(device_id="test", address="127.0.0.1", properties={})
  44. mocked_network_manager = MagicMock()
  45. output_device._manager = mocked_network_manager
  46. # Create a fake reply (we cant use a QReply, since those are abstract C++)
  47. reply = MagicMock()
  48. reply.operation = MagicMock(return_value=QNetworkAccessManager.PostOperation)
  49. reply.url = MagicMock(return_value=QUrl("127.0.0.1"))
  50. mocked_network_manager.get = MagicMock(return_value=reply)
  51. mocked_callback_handler = MagicMock()
  52. output_device.get("whatever", on_finished=mocked_callback_handler.onFinished)
  53. # So we now fake that the request was sucesful.
  54. output_device._handleOnFinished(reply)
  55. # We expect to get a callback regarding this.
  56. mocked_callback_handler.onFinished.assert_called_once_with(reply)
  57. def test_delete():
  58. with patch("UM.Qt.QtApplication.QtApplication.getInstance"):
  59. output_device = NetworkedPrinterOutputDevice(device_id="test", address="127.0.0.1", properties={})
  60. mocked_network_manager = MagicMock()
  61. output_device._manager = mocked_network_manager
  62. # Create a fake reply (we cant use a QReply, since those are abstract C++)
  63. reply = MagicMock()
  64. reply.operation = MagicMock(return_value=QNetworkAccessManager.PostOperation)
  65. reply.url = MagicMock(return_value=QUrl("127.0.0.1"))
  66. mocked_network_manager.deleteResource = MagicMock(return_value=reply)
  67. mocked_callback_handler = MagicMock()
  68. output_device.delete("whatever", on_finished=mocked_callback_handler.onFinished)
  69. # So we now fake that the request was sucesful.
  70. output_device._handleOnFinished(reply)
  71. # We expect to get a callback regarding this.
  72. mocked_callback_handler.onFinished.assert_called_once_with(reply)
  73. def test_put():
  74. with patch("UM.Qt.QtApplication.QtApplication.getInstance"):
  75. output_device = NetworkedPrinterOutputDevice(device_id="test", address="127.0.0.1", properties={})
  76. mocked_network_manager = MagicMock()
  77. output_device._manager = mocked_network_manager
  78. # Create a fake reply (we cant use a QReply, since those are abstract C++)
  79. reply = MagicMock()
  80. reply.operation = MagicMock(return_value=QNetworkAccessManager.PostOperation)
  81. reply.url = MagicMock(return_value=QUrl("127.0.0.1"))
  82. mocked_network_manager.put = MagicMock(return_value = reply)
  83. mocked_callback_handler = MagicMock()
  84. output_device.put("whatever", "omgzomg", on_finished = mocked_callback_handler.onFinished)
  85. # So we now fake that the request was sucesful.
  86. output_device._handleOnFinished(reply)
  87. # We expect to get a callback regarding this.
  88. mocked_callback_handler.onFinished.assert_called_once_with(reply)
  89. def test_timeout():
  90. with patch("UM.Qt.QtApplication.QtApplication.getInstance"):
  91. output_device = NetworkedPrinterOutputDevice(device_id="test", address="127.0.0.1", properties={})
  92. output_device.setConnectionState(ConnectionState.Connected)
  93. assert output_device.connectionState == ConnectionState.Connected
  94. output_device._update()
  95. # Pretend we didn't get any response for 15 seconds
  96. output_device._last_response_time = time.time() - 15
  97. # But we did recently ask for a response!
  98. output_device._last_request_time = time.time() - 5
  99. output_device._update()
  100. # The connection should now be closed, since it went into timeout.
  101. assert output_device.connectionState == ConnectionState.Closed