def test_job_ok(self, mock_get_uri): mock_get_uri.return_value.status_code = 200 mock_get_uri.return_value.json.return_value = { "job": { "file": { "display": "test-pouzdro-na-iphone.gcode" } }, "progress": { "completion": 12, "printTimeLeft": 35, "printTime": 10 }, "state": "Printing", } printer = Octoprint("192.168.1.15", client=PrinterClientInfo(connected=True)) result = printer.job() self.assertEqual( result, { "name": "test-pouzdro-na-iphone.gcode", "completion": 12, "printTimeLeft": 35, "printTime": 10, }, )
def test_upload_job_path_ok(self, mock_post_uri, mock_get_uri): mock_get_uri.return_value.status_code = 200 mock_get_uri.return_value.json.return_value = { "state": { "text": "Operational" }, "temperature": {}, } mock_post_uri.return_value.status_code = 201 mock_post_uri.return_value.json.return_value = { "files": { "local": { "name": "20mm-umlaut-box", "origin": "local", "refs": { "resource": "http://example.com/api/files/local/whistle_v2.gcode", "download": "http://example.com/downloads/files/local/whistle_v2.gcode", }, } }, "done": True, } printer = Octoprint("192.168.1.15", client=PrinterClientInfo(connected=True)) result = printer.upload_and_start_job(self.file_mock.name, path="sub/path/on/disk") self.assertTrue(result) args, kwargs = mock_post_uri.call_args self.assertEqual(kwargs["data"], { "path": "karmen/sub/path/on/disk", "print": True }) self.assertEqual(kwargs["files"]["file"].name, self.file_mock.name)
def test_modify_job_disconnect(self, mock_post_uri): printer = Octoprint("192.168.1.15", client=PrinterClientInfo(connected=True)) self.assertTrue(printer.client.connected) result = printer.modify_current_job("toggle") self.assertFalse(result) self.assertFalse(printer.client.connected)
def test_webcam_no_absolute_url(self, mock_get_uri): mock_get_uri.return_value.status_code = 200 mock_get_uri.return_value.json.return_value = { "webcam": { "bitrate": "5000k", "ffmpegPath": "/usr/bin/ffmpeg", "ffmpegThreads": 1, "flipH": True, "flipV": True, "rotate90": False, "snapshotSslValidation": True, "snapshotTimeout": 5, "snapshotUrl": "http://127.0.0.1:8080/?action=snapshot", "streamRatio": "4:3", "streamTimeout": 5, "streamUrl": "/webcam/?action=stream", "timelapseEnabled": True, "watermark": True, "webcamEnabled": True, } } printer = Octoprint("192.168.1.15", client=PrinterClientInfo(connected=True)) result = printer.webcam() self.assertEqual( result, { "message": "OK", "stream": "http://192.168.1.15/webcam/?action=stream", "flipHorizontal": True, "flipVertical": True, "rotate90": False, }, )
def test_unknown_actino(self): printer = Octoprint("192.168.1.15", client=PrinterClientInfo(connected=True)) with self.assertRaises(Exception) as ctx: printer.modify_current_job("random") self.assertTrue("random is not allowed" in str(ctx.exception))
def test_status_unreachable(self, mock_get_uri): printer = Octoprint("192.168.1.15", client=PrinterClientInfo(connected=True)) result = printer.status() self.assertEqual(result, { "state": "Printer is not responding", "temperature": {} })
def test_job_malformed_json(self, mock_get_uri): mock_get_uri.return_value.status_code = 200 mock_get_uri.return_value.json.side_effect = json.decoder.JSONDecodeError( "msg", "aa", 123) printer = Octoprint("192.168.1.15", client=PrinterClientInfo(connected=True)) result = printer.job() self.assertEqual(result, {})
def test_modify_job_cancel_ok(self, mock_post_uri): mock_post_uri.return_value.status_code = 204 printer = Octoprint("192.168.1.15", client=PrinterClientInfo(connected=True)) result = printer.modify_current_job("cancel") self.assertTrue(result) args, kwargs = mock_post_uri.call_args self.assertEqual(kwargs["json"], {"command": "cancel"})
def __init__( self, ip, hostname=None, name=None, client=PrinterClientInfo(), client_props=None, ): self.name = name self.hostname = hostname self.ip = ip if not client_props: self.client = (client if isinstance(client, PrinterClientInfo) else PrinterClientInfo()) else: self.client = PrinterClientInfo( client_props.get("version", None), client_props.get("connected", False), client_props.get("read_only", False), )
def sniff(self): request = get_uri(self.ip, endpoint="/api/version") if request is None: app.logger.debug( "%s is not responding on /api/version - not octoprint" % self.ip) self.client = PrinterClientInfo({}, False) return if request.status_code == 403: app.logger.debug( "%s is responding with %s on /api/version - might be access-protected octoprint" % (self.ip, request.status_code)) settings_req = get_uri(self.ip, endpoint="/api/settings") if settings_req and settings_req.status_code == 200: app.logger.debug( "%s is responding with 200 on /api/settings - probably access-protected octoprint" % self.ip) self.client = PrinterClientInfo({}, True, True) else: app.logger.debug( "%s is responding with %s on /api/settings - probably not octoprint" % (self.ip, settings_req.status_code)) self.client = PrinterClientInfo({}, False) return if request.status_code != 200: app.logger.debug( "%s is responding with %s on /api/version - not accessible" % (self.ip, request.status_code)) self.client = PrinterClientInfo({}, False) return try: data = request.json() if "text" not in data: app.logger.debug( "%s is responding with unfamiliar JSON %s on /api/version - probably not octoprint" % (self.ip, data)) self.client = PrinterClientInfo(data, False) return except json.decoder.JSONDecodeError: app.logger.debug( "%s is not responding with JSON on /api/version - probably not octoprint" % self.ip) self.client = PrinterClientInfo({}, False) return if re.match(r"^octoprint", data["text"], re.IGNORECASE) is None: app.logger.debug( "%s is responding with %s on /api/version - probably not octoprint" % (self.ip, data["text"])) self.client = PrinterClientInfo(data, False) return self.client = PrinterClientInfo(data, True)
def test_upload_job_no_response(self, mock_post_uri, mock_get_uri): mock_get_uri.return_value.status_code = 200 mock_get_uri.return_value.json.return_value = { "state": { "text": "Operational" }, "temperature": {}, } printer = Octoprint("192.168.1.15", client=PrinterClientInfo(connected=True)) result = printer.upload_and_start_job(self.file_mock.name) self.assertFalse(result)
def test_status_conflict(self, mock_get_uri): mock_get_uri.return_value.status_code = 409 printer = Octoprint("192.168.1.15", client=PrinterClientInfo(connected=True)) result = printer.status() self.assertEqual( result, { "state": "Printer is not connected to Octoprint", "temperature": {} }, )
def test_upload_job_printing(self, mock_post_uri, mock_get_uri): mock_get_uri.return_value.status_code = 200 mock_get_uri.return_value.json.return_value = { "state": { "text": "Printing" }, "temperature": {}, } with self.assertRaises(PrinterDriverException) as context: printer = Octoprint("192.168.1.15", client=PrinterClientInfo(connected=True)) printer.upload_and_start_job(self.file_mock.name) self.assertTrue("Printer is printing" in str(context.exception))
def test_status_malformed_json(self, mock_get_uri): mock_get_uri.return_value.status_code = 200 mock_get_uri.return_value.json.side_effect = json.decoder.JSONDecodeError( "msg", "aa", 123) printer = Octoprint("192.168.1.15", client=PrinterClientInfo(connected=True)) result = printer.status() self.assertEqual( result, { "state": "Printer is responding with invalid data", "temperature": {} }, )
def test_webcam_disabled(self, mock_get_uri): mock_get_uri.return_value.status_code = 200 mock_get_uri.return_value.json.return_value = { "webcam": { "flipH": True, "flipV": True, "rotate90": False, "streamUrl": "http://1.2.3.4/webcam/?action=stream", "webcamEnabled": False, } } printer = Octoprint("192.168.1.15", client=PrinterClientInfo(connected=True)) result = printer.webcam() self.assertEqual(result, {"message": "Stream disabled in octoprint"})
def test_already_connected_printer(self, mock_get_uri): class Response: def __init__(self, status_code, contents): self.status_code = status_code self.contents = contents def json(self): return {"text": "something"} def mock_call(uri, **kwargs): return Response(200, "") mock_get_uri.side_effect = mock_call printer = Octoprint("192.168.1.15", client=PrinterClientInfo(connected=True)) self.assertTrue(printer.client.connected) self.assertTrue(printer.is_alive()) self.assertTrue(printer.client.connected) self.assertEqual(mock_get_uri.call_count, 1)
def test_job_offline_printer(self, mock_get_uri): mock_get_uri.return_value.status_code = 200 mock_get_uri.return_value.json.return_value = { "job": { "file": { "display": "test-pouzdro-na-iphone.gcode" } }, "progress": { "completion": 100, "printTimeLeft": 0, "printTime": 10 }, "state": "Offline (Error: Too many consecutive timeouts, printer still connected and alive?)", } printer = Octoprint("192.168.1.15", client=PrinterClientInfo(connected=True)) result = printer.job() self.assertEqual(result, {})
def test_status_ok(self, mock_get_uri): mock_get_uri.return_value.status_code = 200 mock_get_uri.return_value.json.return_value = { "state": { "text": "Printing" }, "temperature": { "bed": { "actual": 49.8, "offset": 0, "target": 50.0 }, "tool0": { "actual": 240.4, "offset": 0, "target": 240.0 }, }, } printer = Octoprint("192.168.1.15", client=PrinterClientInfo(connected=True)) result = printer.status() self.assertEqual( result, { "state": "Printing", "temperature": { "bed": { "actual": 49.8, "offset": 0, "target": 50.0 }, "tool0": { "actual": 240.4, "offset": 0, "target": 240.0 }, }, }, )
def test_modify_job_disconnected(self, mock_post_uri): printer = Octoprint("192.168.1.15", client=PrinterClientInfo(connected=False)) result = printer.modify_current_job("toggle") self.assertEqual(mock_post_uri.call_count, 0) self.assertFalse(result)
def test_job_no_response(self, mock_get_uri): printer = Octoprint("192.168.1.15", client=PrinterClientInfo(connected=True)) result = printer.job() self.assertEqual(result, {})
def test_job_disconnected(self, mock_get_uri): printer = Octoprint("192.168.1.15", client=PrinterClientInfo(connected=False)) printer.job() self.assertEqual(mock_get_uri.call_count, 0)
def test_job_disconnect(self, mock_get_uri): printer = Octoprint("192.168.1.15", client=PrinterClientInfo(connected=True)) self.assertTrue(printer.client.connected) printer.job() self.assertFalse(printer.client.connected)
def test_webcam_no_response(self, mock_get_uri): printer = Octoprint("192.168.1.15", client=PrinterClientInfo(connected=True)) result = printer.webcam() self.assertEqual(result, {"message": "Stream not accessible"})