def test__service_exists__device_not_required__service_not_found(self): self.query_service.filter_by().first.return_value = None with self.assertRaises(MicroserviceException) as context: errors.service_exists(None, False)({"service_uuid": "s"}, "") self.assertEqual(service_not_found, context.exception.error) self.query_service.filter_by.assert_called_with(uuid="s")
def test__service_exists__device_not_required__name_required__service_not_found( self): mock_service = self.query_service.filter_by( ).first.return_value = mock.MagicMock() mock_service.name = "not-the-name" with self.assertRaises(MicroserviceException) as context: errors.service_exists("name", False)({"service_uuid": "s"}, "") self.assertEqual(service_not_found, context.exception.error) self.query_service.filter_by.assert_called_with(uuid="s")
def test__service_exists__device_not_required__name_not_required__successful( self): mock_service = self.query_service.filter_by( ).first.return_value = mock.MagicMock() self.assertEqual( mock_service, errors.service_exists(None, False)({ "service_uuid": "s" }, "")) self.query_service.filter_by.assert_called_with(uuid="s")
from resources.essentials import register_service, stop_service from resources.game_content import calculate_pos from schemes import ( attack_scheme, service_not_found, service_not_running, success_scheme, attack_not_running, standard_scheme, attack_already_running, could_not_start_service, ) @m.user_endpoint(path=["bruteforce", "attack"], requires=attack_scheme) @register_errors(service_exists("bruteforce"), device_online, device_accessible) def attack(data: dict, user: str, service: Service): if service.running: return attack_already_running target_device: str = data["target_device"] target_service_uuid: str = data["target_service"] target_service: Service = wrapper.session.query(Service).filter_by( uuid=target_service_uuid, device=target_device).first() if target_service is None: return service_not_found if not target_service.running: return service_not_running
check_device_online, get_wallet_owner, ) from schemes import ( wallet_not_found, service_scheme, wallet_scheme, miner_set_wallet_scheme, miner_set_power_scheme, could_not_start_service, success_scheme, ) @m.user_endpoint(path=["miner", "get"], requires=service_scheme) @register_errors(service_exists("miner", device_required=False), device_online, device_accessible) def get(data: dict, user: str, service: Service) -> dict: return wrapper.session.query(Miner).get(service.uuid).serialize @m.user_endpoint(path=["miner", "list"], requires=wallet_scheme) def list_miners(data: dict, user: str) -> dict: miners: List[dict] = [] for miner in wrapper.session.query(Miner).filter_by( wallet=data["wallet_uuid"]): service: Service = wrapper.session.query(Service).get(miner.uuid) if not check_device_online(service.device): continue miners.append({"miner": miner.serialize, "service": service.serialize}) return {"miners": miners}
def test__endpoints_available(self): main = import_main("__main__") elements = [getattr(main, element_name) for element_name in dir(main)] registered_user_endpoints = mock.user_endpoints.copy() registered_ms_endpoints = mock.ms_endpoints.copy() service_errors = (service_exists(), device_online, device_accessible) bruteforce_errors = (service_exists("bruteforce"), device_online, device_accessible) miner_errors = ( service_exists("miner", device_required=False), device_online, device_accessible, ) expected_user_endpoints = [ (["public_info"], standard_scheme, service.public_info, service_exists(), device_online), (["use"], None, service.use, has_service_and_device, *service_errors), (["private_info"], standard_scheme, service.private_info, *service_errors), (["toggle"], standard_scheme, service.toggle, *service_errors), (["delete"], standard_scheme, service.delete_service, *service_errors), (["list"], device_scheme, service.list_services, device_online, device_accessible), (["create"], None, service.create, device_online, device_accessible), (["part_owner"], device_scheme, service.part_owner, device_online), (["list_part_owner"], {}, service.list_part_owner), (["bruteforce", "attack"], attack_scheme, bruteforce.attack, *bruteforce_errors), (["bruteforce", "status"], standard_scheme, bruteforce.status, *bruteforce_errors), (["bruteforce", "stop"], standard_scheme, bruteforce.stop, *bruteforce_errors), (["miner", "get"], service_scheme, miner.get, *miner_errors), (["miner", "list"], wallet_scheme, miner.list_miners), (["miner", "wallet"], miner_set_wallet_scheme, miner.set_wallet, *miner_errors), (["miner", "power"], miner_set_power_scheme, miner.set_power, *miner_errors), ] expected_ms_endpoints = [ (["device_init"], service.device_init), (["device_restart"], service.device_restart), (["check_part_owner"], service.check_part_owner), (["hardware", "scale"], service.hardware_scale), (["hardware", "stop"], service.hardware_stop), (["hardware", "delete"], service.hardware_delete), (["miner", "stop"], miner.miner_stop), (["miner", "collect"], miner.collect), (["delete_user"], service.delete_user), ] for path, requires, func, *errors in expected_user_endpoints: self.assertIn((path, requires), registered_user_endpoints) endpoint_handler = mock.user_endpoint_handlers[tuple(path)] registered_user_endpoints.remove((path, requires)) self.assertIn(endpoint_handler, elements) self.assertEqual(func, endpoint_handler) if errors: self.assertEqual(len(errors), len(endpoint_handler.__errors__)) for a, b in zip(errors, endpoint_handler.__errors__): if a == b: continue self.assertEqual(a.__qualname__, b.__qualname__) self.assertEqual(a.__closure__, b.__closure__) else: self.assertNotIn("__errors__", dir(endpoint_handler)) for path, func, *errors in expected_ms_endpoints: self.assertIn(path, registered_ms_endpoints) endpoint_handler = mock.ms_endpoint_handlers[tuple(path)] registered_ms_endpoints.remove(path) self.assertIn(endpoint_handler, elements) self.assertEqual(func, endpoint_handler) if errors: self.assertEqual(len(errors), len(endpoint_handler.__errors__)) for a, b in zip(errors, endpoint_handler.__errors__): if a == b: continue self.assertEqual(a.__qualname__, b.__qualname__) self.assertEqual(a.__closure__, b.__closure__) else: self.assertNotIn("__errors__", dir(endpoint_handler)) self.assertFalse(registered_user_endpoints) self.assertFalse(registered_ms_endpoints)
success_scheme, standard_scheme, cannot_toggle_directly, device_scheme, could_not_start_service, cannot_delete_enforced_service, ) from vars import config switch: dict = { # this is just for tools, its a more smooth way of a "switch" statement "portscan": game_content.portscan } @m.user_endpoint(path=["public_info"], requires=standard_scheme) @register_errors(service_exists(), device_online) def public_info(data: dict, user: str, service: Service) -> dict: if service.running_port is None or not service.running: return service_not_found return service.public_data() @m.user_endpoint(path=["use"], requires=None) @register_errors(has_service_and_device, service_exists(), device_online, device_accessible) def use(data: dict, user: str, service: Service) -> dict: if service.name not in switch: return service_cannot_be_used return switch[service.name](data, user)