def test__create_service__miner__invalid_request(self):
     self.assertEqual(
         invalid_request,
         essentials.create_service("miner", {"device_uuid": "my-device"},
                                   "user"))
     self.assertEqual(
         invalid_request,
         essentials.create_service("miner", {
             "device_uuid": "my-device",
             "wallet_uuid": ["not-a-string"]
         }, "user"),
     )
Example #2
0
def device_restart(data: dict, microservice: str) -> dict:
    service: Optional[Service] = wrapper.session.query(Service).filter_by(
        device=data["device_uuid"], name="ssh"
    ).first()
    if service is not None:
        if register_service(service.device, service.uuid, service.name, service.owner) == -1:
            return could_not_start_service
        service.running = True
        wrapper.session.commit()
    else:
        create_service("ssh", data, data["user"])

    return success_scheme
    def test__create_service__miner__successful(self, service_patch,
                                                miner_patch, wallet_patch):
        mock_service = mock.MagicMock()
        wallet_patch.return_value = True

        def service_create(uuid, dev, user, name, speed, running):
            miner_patch.assert_called_with(uuid, "wallet")
            self.assertRegex(uuid,
                             r"[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}")
            self.assertEqual("my-device", dev)
            self.assertEqual("user", user)
            self.assertEqual("miner", name)
            self.assertEqual(0, speed)
            self.assertEqual(False, running)

            return mock_service

        service_patch.side_effect = service_create

        expected_result = mock_service.serialize
        actual_result = essentials.create_service("miner", {
            "device_uuid": "my-device",
            "wallet_uuid": "wallet"
        }, "user")

        self.assertEqual(expected_result, actual_result)
        wallet_patch.assert_called_with("wallet")
        service_patch.assert_called_once()
    def test__create_service__bruteforce(self, service_patch,
                                         bruteforce_patch):
        mock_service = mock.MagicMock()

        def service_create(uuid, dev, user, name, speed, running):
            bruteforce_patch.assert_called_with(uuid)
            self.assertRegex(uuid,
                             r"[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}")
            self.assertEqual("my-device", dev)
            self.assertEqual("user", user)
            self.assertEqual("bruteforce", name)
            self.assertEqual(0, speed)
            self.assertEqual(False, running)

            return mock_service

        service_patch.side_effect = service_create

        expected_result = mock_service.serialize
        actual_result = essentials.create_service("bruteforce",
                                                  {"device_uuid": "my-device"},
                                                  "user")

        self.assertEqual(expected_result, actual_result)
        service_patch.assert_called_once()
    def test__create_service__default__with_auto_start(self, service_patch,
                                                       register_patch):
        mock_service = mock.MagicMock()

        def service_create(uuid, dev, user, name, speed, running):
            register_patch.assert_called_with("my-device", uuid, "ssh", "user")
            self.assertRegex(uuid,
                             r"[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}")
            self.assertEqual("my-device", dev)
            self.assertEqual("user", user)
            self.assertEqual("ssh", name)
            self.assertEqual(register_patch(), speed)
            self.assertEqual(True, running)

            return mock_service

        service_patch.side_effect = service_create

        expected_result = mock_service.serialize
        actual_result = essentials.create_service("ssh",
                                                  {"device_uuid": "my-device"},
                                                  "user")

        self.assertEqual(expected_result, actual_result)
        service_patch.assert_called_once()
    def test__create_service__miner__wallet_not_found(self, wallet_patch):
        wallet_patch.return_value = False

        expected_result = wallet_not_found
        actual_result = essentials.create_service("miner", {
            "device_uuid": "my-device",
            "wallet_uuid": "wallet"
        }, "user")

        self.assertEqual(expected_result, actual_result)
        wallet_patch.assert_called_with("wallet")
Example #7
0
def create(data: dict, user: str) -> dict:
    device_uuid: str = data["device_uuid"]
    name: str = data["name"]

    if name not in config["services"]:
        return service_not_supported

    device_owner: str = get_device_owner(device_uuid)
    service_count: int = wrapper.session.query(func.count(Service.name)).filter_by(
        owner=device_owner, device=device_uuid, name=name
    ).scalar()
    if service_count != 0:
        return already_own_this_service

    return create_service(name, data, device_owner)
Example #8
0
def device_init(data: dict, microservice: str) -> dict:
    create_service("ssh", data, data["user"])
    return success_scheme