def test_no_update_ip_if_resolve_fails( self, mock_datetime, mock_get_data, mock_address, mock_hostname, mock_update_network_client, mock_update_printer, mock_get_network_client, mock_get_printer, ): date = datetime.strptime("06 Mar 2020", "%d %b %Y") mock_datetime.now.return_value = date.replace(minute=29) check_printer("298819f5-0119-4e9b-8191-350d931f7ecf") self.assertEqual(mock_update_network_client.call_count, 0)
def test_call_sniff_periodically( self, mock_octoprint_redis, mock_datetime, mock_get_data, mock_address, mock_hostname, mock_update_network_client, mock_update_printer, mock_get_network_client, mock_get_printer, ): date = datetime.strptime("06 Mar 2020", "%d %b %Y") mock_datetime.now.return_value = date.replace(minute=30) mock_octoprint_redis.get.return_value = None def mock_call(uri, **kwargs): if "/api/settings" in uri: return Response(200, {"plugins": {"aaa": {},}},) return Response(200, {"text": "octoprint"}) mock_get_data.side_effect = mock_call check_printer("298819f5-0119-4e9b-8191-350d931f7ecf") self.assertEqual(mock_hostname.call_count, 1) self.assertEqual(mock_address.call_count, 0) self.assertEqual(mock_get_printer.call_count, 1) mock_get_data.assert_any_call("https://1234/api/version", timeout=2) # sniff mock_get_data.assert_any_call("https://1234/api/settings", timeout=2) # webcam, TODO this is not ideal mock_get_data.assert_any_call("https://1234/api/settings", timeout=2) self.assertEqual(mock_get_data.call_count, 3) self.assertEqual(mock_update_printer.call_count, 1) mock_update_printer.assert_any_call( **{ "uuid": "298819f5-0119-4e9b-8191-350d931f7ecf", "client_props": { "connected": True, "version": {"text": "octoprint"}, "access_level": PrinterClientAccessLevel.UNLOCKED, "api_key": None, "webcam": {"message": "Webcam disabled in octoprint"}, "plugins": ["aaa"], }, } )
def test_deactivate_no_data_responding_printer( self, mock_datetime, mock_get_data, mock_address, mock_hostname, mock_update_network_client, mock_update_printer, mock_get_network_client, mock_get_printer, ): date = datetime.strptime("06 Mar 2020", "%d %b %Y") mock_datetime.now.return_value = date.replace(minute=29) check_printer("298819f5-0119-4e9b-8191-350d931f7ecf") self.assertEqual(mock_get_printer.call_count, 1) self.assertEqual(mock_get_network_client.call_count, 1) self.assertEqual(mock_get_data.call_count, 1) self.assertEqual(mock_update_printer.call_count, 1) self.assertEqual(mock_update_network_client.call_count, 1) self.assertEqual(mock_address.call_count, 1) self.assertEqual(mock_hostname.call_count, 1) mock_update_printer.assert_any_call( **{ "uuid": "298819f5-0119-4e9b-8191-350d931f7ecf", "client_props": { "connected": False, "version": {}, "access_level": PrinterClientAccessLevel.UNLOCKED, "api_key": None, "webcam": { "message": "Webcam not accessible" }, "plugins": [], "pill_info": None, }, }) mock_update_network_client.assert_any_call( **{ "uuid": "298819f5-0119-4e9b-8191-350d931f7ecf", "hostname": "router.asus.com", "ip": "1234", })
def test_update_hostname( self, mock_datetime, mock_get_data, mock_address, mock_hostname, mock_update_network_client, mock_update_printer, mock_get_network_client, mock_get_printer, ): date = datetime.strptime("06 Mar 2020", "%d %b %Y") mock_datetime.now.return_value = date.replace(minute=29) check_printer("298819f5-0119-4e9b-8191-350d931f7ecf") mock_update_network_client.assert_any_call( **{ "uuid": "298819f5-0119-4e9b-8191-350d931f7ecf", "hostname": "router.asus.com", "ip": "1234", })
def test_karmen_sniff_no_pill( self, mock_octoprint_redis, mock_datetime, mock_get_data, mock_address, mock_hostname, mock_update_network_client, mock_update_printer, mock_get_network_client, mock_get_printer, ): date = datetime.strptime("06 Mar 2020", "%d %b %Y") mock_datetime.now.return_value = date.replace(minute=29) mock_octoprint_redis.get.return_value = None def mock_call(uri, **kwargs): print("=======CALLING: ", uri) if "/api/settings" in uri: return Response( 200, {"plugins": { "aaa": {}, }}, ) if "/karmen-pill-info/get" in uri: return Response(502, ) return Response(200, {"text": "octoprint"}) mock_get_data.side_effect = mock_call check_printer("298819f5-0119-4e9b-8191-350d931f7ecf") self.assertEqual(mock_hostname.call_count, 1) self.assertEqual(mock_address.call_count, 0) self.assertEqual(mock_get_printer.call_count, 1) self.assertEqual(mock_get_data.call_count, 1) self.assertEqual(mock_update_printer.call_count, 1)
def test_activate_responding_printer( self, mock_datetime, mock_octoprint_redis, mock_address, mock_hostname, mock_get_data, mock_update_network_client, mock_update_printer, mock_get_network_client, mock_get_printer, ): date = datetime.strptime("06 Mar 2020", "%d %b %Y") mock_datetime.now.return_value = date.replace(minute=29) settings_response = Response( 200, { "webcam": { "webcamEnabled": True, "streamUrl": "/webcam/?action=stream", "flipH": False, "flipV": True, "rotate90": False, } }, ) def mock_call(uri, **kwargs): if "5678" in uri and "/api/settings" in uri: return settings_response return Response(200, {"text": "octoprint"}) mock_octoprint_redis.get.return_value = pickle.dumps(settings_response) mock_get_data.side_effect = mock_call check_printer("b2732ff8-605b-4d56-87f3-5a590d672912") self.assertEqual(mock_get_printer.call_count, 1) self.assertEqual(mock_address.call_count, 1) self.assertEqual(mock_hostname.call_count, 1) self.assertEqual(mock_get_data.call_count, 3) self.assertEqual(mock_update_printer.call_count, 1) self.assertEqual(mock_update_network_client.call_count, 0) mock_update_printer.assert_any_call( **{ "uuid": "b2732ff8-605b-4d56-87f3-5a590d672912", "client_props": { "connected": True, "version": {"text": "octoprint"}, "access_level": PrinterClientAccessLevel.UNLOCKED, "api_key": None, "webcam": { "message": "OK", "stream": "http://5678/webcam/?action=stream", "snapshot": None, "flipHorizontal": False, "flipVertical": True, "rotate90": False, }, "plugins": [], }, } )
def test_task_does_not_fail_when_printer_disappeared( self, mock_get_printer): "situation when printer was scheduled for update but was removed in the meantime" result = check_printer("298819f5-0119-4e9b-8191-350d931f7ecf") self.assertIs(result, None)
def test_call_sniff_periodically( self, mock_octoprint_redis, mock_datetime, mock_get_data, mock_address, mock_hostname, mock_update_network_client, mock_update_printer, mock_get_network_client, mock_get_printer, ): date = datetime.strptime("06 Mar 2020", "%d %b %Y") mock_datetime.now.return_value = date.replace(minute=30) mock_octoprint_redis.get.return_value = None def mock_call(uri, **kwargs): print("=======CALLING: ", uri) if "/api/settings" in uri: return Response( 200, {"plugins": { "aaa": {}, }}, ) if "/karmen-pill-info/get" in uri: return Response( 200, { "system": { "karmen_version": "fb89a94ed5e0bf3b4e30a50e41acc1a19fcc90ee 0.1.0-alpha", } }, ) return Response(200, {"text": "octoprint"}) mock_get_data.side_effect = mock_call check_printer("298819f5-0119-4e9b-8191-350d931f7ecf") self.assertEqual(mock_hostname.call_count, 1) self.assertEqual(mock_address.call_count, 0) self.assertEqual(mock_get_printer.call_count, 1) any_http_kwargs = {'timeout': mock.ANY} mock_get_data.assert_any_call("https://1234/api/version", **any_http_kwargs) # sniff mock_get_data.assert_any_call("https://1234/api/settings", **any_http_kwargs) # webcam, TODO this is not ideal mock_get_data.assert_any_call("https://1234/api/settings", **any_http_kwargs) self.assertEqual(mock_get_data.call_count, 4) self.assertEqual(mock_update_printer.call_count, 1) mock_update_printer.assert_any_call( **{ "uuid": "298819f5-0119-4e9b-8191-350d931f7ecf", "client_props": { "connected": True, "version": { "text": "octoprint" }, "access_level": PrinterClientAccessLevel.UNLOCKED, "api_key": None, "webcam": { "message": "Webcam disabled in octoprint" }, "plugins": ["aaa"], "pill_info": { "karmen_version": "fb89a94ed5e0bf3b4e30a50e41acc1a19fcc90ee 0.1.0-alpha", "version_number": "0.1.0-alpha", "update_available": None, "update_status": None, }, }, })