Exemplo n.º 1
0
async def test_prepare_get_local_size_context_error(plugin, read, write):
    request_id = "31415"
    error_details = {"Details": "Unexpected syntax"}
    error_message, error_code = FailedParsingManifest(
    ).message, FailedParsingManifest().code
    plugin.prepare_local_size_context.side_effect = FailedParsingManifest(
        data=error_details)
    request = {
        "jsonrpc": "2.0",
        "id": request_id,
        "method": "start_local_size_import",
        "params": {
            "game_ids": ["6"]
        }
    }
    read.side_effect = [
        async_return_value(create_message(request)),
        async_return_value(b"", 10)
    ]
    await plugin.run()
    assert get_messages(write) == [{
        "jsonrpc": "2.0",
        "id": request_id,
        "error": {
            "code": error_code,
            "message": error_message,
            "data": {
                "internal_type": "FailedParsingManifest",
                "Details": "Unexpected syntax"
            }
        }
    }]
async def test_get_subscriptions_success(plugin, read, write):
    request = {"jsonrpc": "2.0", "id": "3", "method": "import_subscriptions"}
    read.side_effect = [
        async_return_value(create_message(request)),
        async_return_value(b"", 10)
    ]

    plugin.get_subscriptions.return_value = async_return_value([
        Subscription("1"),
        Subscription("2", False),
        Subscription("3", True, 1580899100)
    ])
    await plugin.run()
    plugin.get_subscriptions.assert_called_with()

    assert get_messages(write) == [{
        "jsonrpc": "2.0",
        "id": "3",
        "result": {
            "subscriptions": [{
                "subscription_name": "1"
            }, {
                "subscription_name": "2",
                "owned": False
            }, {
                "subscription_name": "3",
                "owned": True,
                "end_time": 1580899100
            }]
        }
    }]
Exemplo n.º 3
0
async def test_prepare_get_unlocked_achievements_context_error(
        plugin, read, write):
    plugin.prepare_achievements_context.side_effect = BackendError()
    request = {
        "jsonrpc": "2.0",
        "id": "3",
        "method": "start_achievements_import",
        "params": {
            "game_ids": ["14"]
        }
    }
    read.side_effect = [
        async_return_value(create_message(request)),
        async_return_value(b"", 10)
    ]

    await plugin.run()

    assert get_messages(write) == [{
        "jsonrpc": "2.0",
        "id": "3",
        "error": {
            "code": 4,
            "message": "Backend error"
        }
    }]
async def test_get_friends_success(plugin, read, write):
    request = {
        "jsonrpc": "2.0",
        "id": "3",
        "method": "import_friends"
    }

    read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
    plugin.get_friends.return_value = async_return_value([
        FriendInfo("3", "Jan"),
        FriendInfo("5", "Ola")
    ])
    await plugin.run()
    plugin.get_friends.assert_called_with()

    assert get_messages(write) == [
        {
            "jsonrpc": "2.0",
            "id": "3",
            "result": {
                "friend_info_list": [
                    {"user_id": "3", "user_name": "Jan"},
                    {"user_id": "5", "user_name": "Ola"}
                ]
            }
        }
    ]
async def test_get_capabilities(reader, writer, read, write):
    class PluginImpl(Plugin): #pylint: disable=abstract-method
        async def get_owned_games(self):
            pass

    request = {
        "jsonrpc": "2.0",
        "id": "3",
        "method": "get_capabilities"
    }
    token = "token"
    plugin = PluginImpl(Platform.Generic, "0.1", reader, writer, token)
    read.side_effect = [async_return_value(create_message(request)), async_return_value(b"")]
    await plugin.run()
    assert get_messages(write) == [
        {
            "jsonrpc": "2.0",
            "id": "3",
            "result": {
                "platform_name": "generic",
                "features": [
                    "ImportOwnedGames"
                ],
                "token": token
            }
        }
    ]
Exemplo n.º 6
0
async def test_prepare_get_user_presence_context_error(plugin, read, write):
    request_id = "31415"
    plugin.prepare_user_presence_context.side_effect = BackendError()
    request = {
        "jsonrpc": "2.0",
        "id": request_id,
        "method": "start_user_presence_import",
        "params": {
            "user_id_list": ["6"]
        }
    }
    read.side_effect = [
        async_return_value(create_message(request)),
        async_return_value(b"", 10)
    ]
    await plugin.run()

    assert get_messages(write) == [{
        "jsonrpc": "2.0",
        "id": request_id,
        "error": {
            "code": 4,
            "message": "Backend error",
            "data": {
                "internal_type": "BackendError"
            }
        }
    }]
Exemplo n.º 7
0
async def test_get_friends_success(plugin, read, write):
    request = {
        "jsonrpc": "2.0",
        "id": "3",
        "method": "import_friends"
    }

    read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
    plugin.get_friends.return_value = async_return_value([
        UserInfo("3", "Jan", "https://avatar.url/u3", None),
        UserInfo("5", "Ola", None, "https://profile.url/u5"),
        UserInfo("6", "Ola2", None),
        UserInfo("7", "Ola3"),
    ])
    await plugin.run()
    plugin.get_friends.assert_called_with()

    assert get_messages(write) == [
        {
            "jsonrpc": "2.0",
            "id": "3",
            "result": {
                "friend_info_list": [
                    {"user_id": "3", "user_name": "Jan", "avatar_url": "https://avatar.url/u3"},
                    {"user_id": "5", "user_name": "Ola", "profile_url": "https://profile.url/u5"},
                    {"user_id": "6", "user_name": "Ola2"},
                    {"user_id": "7", "user_name": "Ola3"},
                ]
            }
        }
    ]
async def test_prepare_get_os_compatibility_context_error(plugin, read, write):
    request_id = "31415"
    plugin.prepare_os_compatibility_context.side_effect = BackendError()
    request = {
        "jsonrpc": "2.0",
        "id": request_id,
        "method": "start_os_compatibility_import",
        "params": {
            "game_ids": ["6"]
        }
    }
    read.side_effect = [
        async_return_value(create_message(request)),
        async_return_value(b"", 10)
    ]
    await plugin.run()

    assert get_messages(write) == [{
        "jsonrpc": "2.0",
        "id": request_id,
        "error": {
            "code": 4,
            "message": "Backend error"
        }
    }]
async def test_prepare_get_subscription_games_context_error(
        plugin, read, write):
    request_id = "31415"
    error_details = "Unexpected backend error"
    error_message, error_code = BackendError().message, BackendError().code
    plugin.prepare_subscription_games_context.side_effect = BackendError(
        error_details)
    request = {
        "jsonrpc": "2.0",
        "id": request_id,
        "method": "start_subscription_games_import",
        "params": {
            "subscription_names": ["sub_a", "sub_b"]
        }
    }
    read.side_effect = [
        async_return_value(create_message(request)),
        async_return_value(b"", 10)
    ]
    await plugin.run()

    assert get_messages(write) == [{
        "jsonrpc": "2.0",
        "id": request_id,
        "error": {
            "code": error_code,
            "message": error_message,
            "data": error_details
        }
    }]
Exemplo n.º 10
0
async def test_import_already_in_progress_error(plugin, read, write):
    plugin.prepare_local_size_context.return_value = async_return_value(None)
    requests = [{
        "jsonrpc": "2.0",
        "id": "3",
        "method": "start_local_size_import",
        "params": {
            "game_ids": ["42"]
        }
    }, {
        "jsonrpc": "2.0",
        "id": "4",
        "method": "start_local_size_import",
        "params": {
            "game_ids": ["13"]
        }
    }]
    read.side_effect = [
        async_return_value(create_message(requests[0])),
        async_return_value(create_message(requests[1])),
        async_return_value(b"", 10)
    ]

    await plugin.run()

    responses = get_messages(write)
    assert {"jsonrpc": "2.0", "id": "3", "result": None} in responses
    assert {
        "jsonrpc": "2.0",
        "id": "4",
        "error": {
            "code": 600,
            "message": "Import already in progress"
        }
    } in responses
Exemplo n.º 11
0
async def test_success(plugin, read, write):
    request = {"jsonrpc": "2.0", "id": "3", "method": "import_local_games"}
    read.side_effect = [
        async_return_value(create_message(request)),
        async_return_value(b"")
    ]

    plugin.get_local_games.return_value = async_return_value([
        LocalGame("1", LocalGameState.Running),
        LocalGame("2", LocalGameState.Installed),
        LocalGame("3", LocalGameState.Installed | LocalGameState.Running)
    ])
    await plugin.run()
    plugin.get_local_games.assert_called_with()

    assert get_messages(write) == [{
        "jsonrpc": "2.0",
        "id": "3",
        "result": {
            "local_games": [{
                "game_id": "1",
                "local_game_state": LocalGameState.Running.value
            }, {
                "game_id": "2",
                "local_game_state": LocalGameState.Installed.value
            }, {
                "game_id":
                "3",
                "local_game_state":
                (LocalGameState.Installed | LocalGameState.Running).value
            }]
        }
    }]
Exemplo n.º 12
0
def assert_rpc_response(write, response_id, result=None):
    assert get_messages(write) == [
        {
            "jsonrpc": "2.0",
            "id": str(response_id),
            "result": result
        }
    ]
Exemplo n.º 13
0
def assert_rpc_request(write, method, params=None):
    assert get_messages(write) == [{
        "jsonrpc": "2.0",
        "method": method,
        "params": {
            "data": params
        }
    }]
Exemplo n.º 14
0
async def test_get_local_size_success(plugin, read, write):
    context = {'abc': 'def'}
    plugin.prepare_local_size_context.return_value = async_return_value(
        context)
    request = {
        "jsonrpc": "2.0",
        "id": "11",
        "method": "start_local_size_import",
        "params": {
            "game_ids": ["777", "13", "42"]
        }
    }
    read.side_effect = [
        async_return_value(create_message(request)),
        async_return_value(b"", 10)
    ]
    plugin.get_local_size.side_effect = [
        async_return_value(100000000000, 1),
        async_return_value(None),
        async_return_value(3333333)
    ]
    await plugin.run()
    plugin.get_local_size.assert_has_calls(
        [call("777", context),
         call("13", context),
         call("42", context)])
    plugin.local_size_import_complete.assert_called_once_with()

    assert get_messages(write) == [{
        "jsonrpc": "2.0",
        "id": "11",
        "result": None
    }, {
        "jsonrpc": "2.0",
        "method": "local_size_import_success",
        "params": {
            "game_id": "777",
            "local_size": 100000000000
        }
    }, {
        "jsonrpc": "2.0",
        "method": "local_size_import_success",
        "params": {
            "game_id": "13",
            "local_size": None
        }
    }, {
        "jsonrpc": "2.0",
        "method": "local_size_import_success",
        "params": {
            "game_id": "42",
            "local_size": 3333333
        }
    }, {
        "jsonrpc": "2.0",
        "method": "local_size_import_finished",
        "params": None
    }]
Exemplo n.º 15
0
async def test_remove_game(plugin, write):
    plugin.remove_game("5")
    assert get_messages(write) == [{
        "jsonrpc": "2.0",
        "method": "owned_game_removed",
        "params": {
            "game_id": "5"
        }
    }]
async def test_lost_authentication(plugin, write):
    plugin.lost_authentication()
    await skip_loop()

    assert get_messages(write) == [{
        "jsonrpc": "2.0",
        "method": "authentication_lost",
        "params": None
    }]
Exemplo n.º 17
0
async def test_remove_friend(plugin, write):
    plugin.remove_friend("5")

    assert get_messages(write) == [{
        "jsonrpc": "2.0",
        "method": "friend_removed",
        "params": {
            "user_id": "5"
        }
    }]
async def test_store_credentials(plugin, write):
    credentials = {"token": "ABC"}
    plugin.store_credentials(credentials)
    await skip_loop()

    assert get_messages(write) == [{
        "jsonrpc": "2.0",
        "method": "store_credentials",
        "params": credentials
    }]
async def test_shutdown(plugin, read, write):
    request = {"jsonrpc": "2.0", "id": "5", "method": "shutdown"}
    read.side_effect = [async_return_value(create_message(request))]
    await plugin.run()
    plugin.shutdown.assert_called_with()
    assert get_messages(write) == [{
        "jsonrpc": "2.0",
        "id": "5",
        "result": None
    }]
async def test_get_unlocked_achievements_success(plugin, read, write):
    plugin.prepare_achievements_context.return_value = async_return_value(5)
    request = {
        "jsonrpc": "2.0",
        "id": "3",
        "method": "start_achievements_import",
        "params": {
            "game_ids": ["14"]
        }
    }
    read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
    plugin.get_unlocked_achievements.return_value = async_return_value([
        Achievement(achievement_id="lvl10", unlock_time=1548421241),
        Achievement(achievement_name="Got level 20", unlock_time=1548422395),
        Achievement(achievement_id="lvl30", achievement_name="Got level 30", unlock_time=1548495633)
    ])
    await plugin.run()
    plugin.prepare_achievements_context.assert_called_with(["14"])
    plugin.get_unlocked_achievements.assert_called_with("14", 5)
    plugin.achievements_import_complete.asert_called_with()

    assert get_messages(write) == [
        {
            "jsonrpc": "2.0",
            "id": "3",
            "result": None
        },
        {
            "jsonrpc": "2.0",
            "method": "game_achievements_import_success",
            "params": {
                "game_id": "14",
                "unlocked_achievements": [
                    {
                        "achievement_id": "lvl10",
                        "unlock_time": 1548421241
                    },
                    {
                        "achievement_name": "Got level 20",
                        "unlock_time": 1548422395
                    },
                    {
                        "achievement_id": "lvl30",
                        "achievement_name": "Got level 30",
                        "unlock_time": 1548495633
                    }
                ]
            }
        },
        {
            "jsonrpc": "2.0",
            "method": "achievements_import_finished",
            "params": None
        }
    ]
async def test_ping(plugin, read, write):
    request = {"jsonrpc": "2.0", "id": "7", "method": "ping"}
    read.side_effect = [
        async_return_value(create_message(request)),
        async_return_value(b"")
    ]
    await plugin.run()
    assert get_messages(write) == [{
        "jsonrpc": "2.0",
        "id": "7",
        "result": None
    }]
Exemplo n.º 22
0
async def test_success(plugin, read, write):
    request = {"jsonrpc": "2.0", "id": "3", "method": "import_owned_games"}
    read.side_effect = [
        async_return_value(create_message(request)),
        async_return_value(b"", 10)
    ]

    plugin.get_owned_games.return_value = async_return_value([
        Game("3", "Doom", None, LicenseInfo(LicenseType.SinglePurchase, None)),
        Game("5", "Witcher 3", [
            Dlc("7", "Hearts of Stone",
                LicenseInfo(LicenseType.SinglePurchase, None)),
            Dlc("8", "Temerian Armor Set",
                LicenseInfo(LicenseType.FreeToPlay, None)),
        ], LicenseInfo(LicenseType.SinglePurchase, None))
    ])
    await plugin.run()
    plugin.get_owned_games.assert_called_with()
    assert get_messages(write) == [{
        "jsonrpc": "2.0",
        "id": "3",
        "result": {
            "owned_games": [{
                "game_id": "3",
                "game_title": "Doom",
                "license_info": {
                    "license_type": "SinglePurchase"
                }
            }, {
                "game_id":
                "5",
                "game_title":
                "Witcher 3",
                "dlcs": [{
                    "dlc_id": "7",
                    "dlc_title": "Hearts of Stone",
                    "license_info": {
                        "license_type": "SinglePurchase"
                    }
                }, {
                    "dlc_id": "8",
                    "dlc_title": "Temerian Armor Set",
                    "license_info": {
                        "license_type": "FreeToPlay"
                    }
                }],
                "license_info": {
                    "license_type": "SinglePurchase"
                }
            }]
        }
    }]
async def test_add_friend(plugin, write):
    friend = FriendInfo("7", "Kuba")

    plugin.add_friend(friend)

    assert get_messages(write) == [
        {
            "jsonrpc": "2.0",
            "method": "friend_added",
            "params": {
                "friend_info": {"user_id": "7", "user_name": "Kuba"}
            }
        }
    ]
Exemplo n.º 24
0
async def test_local_game_state_update(plugin, write):
    game = LocalGame("1", LocalGameState.Running)
    plugin.update_local_game_status(game)

    assert get_messages(write) == [{
        "jsonrpc": "2.0",
        "method": "local_game_status_changed",
        "params": {
            "local_game": {
                "game_id": "1",
                "local_game_state": LocalGameState.Running.value
            }
        }
    }]
async def test_get_subscription_games_success_empty(plugin, read, write):
    plugin.prepare_subscription_games_context.return_value = async_return_value(5)
    request = {
        "jsonrpc": "2.0",
        "id": "3",
        "method": "start_subscription_games_import",
        "params": {
            "subscription_names": ["sub_a"]
        }
    }
    read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]

    async def sub_games():
        yield None

    plugin.get_subscription_games.return_value = sub_games()
    await plugin.run()
    plugin.prepare_subscription_games_context.assert_called_with(["sub_a"])
    plugin.get_subscription_games.assert_called_with("sub_a", 5)
    plugin.subscription_games_import_complete.asert_called_with()

    assert get_messages(write) == [
        {
            "jsonrpc": "2.0",
            "id": "3",
            "result": None
        },
        {
            "jsonrpc": "2.0",
            "method": "subscription_games_import_success",
            "params": {
                "subscription_name": "sub_a",
                "subscription_games": None
            }
        },
        {
            'jsonrpc': '2.0',
            'method':
                'subscription_games_partial_import_finished',
                'params': {
                    "subscription_name": "sub_a"
                }
        },
        {
            "jsonrpc": "2.0",
            "method": "subscription_games_import_finished",
            "params": None
        }
    ]
async def test_get_subscription_games_error(exception, code, message, internal_type, plugin, read, write):
    plugin.prepare_subscription_games_context.return_value = async_return_value(None)
    request = {
        "jsonrpc": "2.0",
        "id": "3",
        "method": "start_subscription_games_import",
        "params": {
            "subscription_names": ["sub_a"]
        }
    }

    read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
    plugin.get_subscription_games.side_effect = exception
    await plugin.run()
    plugin.get_subscription_games.assert_called()
    plugin.subscription_games_import_complete.asert_called_with()

    assert get_messages(write) == [
        {
            "jsonrpc": "2.0",
            "id": "3",
            "result": None
        },
        {
            "jsonrpc": "2.0",
            "method": "subscription_games_import_failure",
            "params": {
                "subscription_name": "sub_a",
                "error": {
                    "code": code,
                    "message": message,
                    "data": {"internal_type": internal_type}
                }
            }
        },
        {
            'jsonrpc': '2.0',
            'method':
                'subscription_games_partial_import_finished',
                'params': {
                    "subscription_name": "sub_a"
                }
        },
        {
            "jsonrpc": "2.0",
            "method": "subscription_games_import_finished",
            "params": None
        }
    ]
async def test_update_game(plugin, write):
    game_time = GameTime("3", 60, 1549550504)
    plugin.update_game_time(game_time)

    assert get_messages(write) == [{
        "jsonrpc": "2.0",
        "method": "game_time_updated",
        "params": {
            "game_time": {
                "game_id": "3",
                "time_played": 60,
                "last_played_time": 1549550504
            }
        }
    }]
Exemplo n.º 28
0
async def test_add_game(plugin, write):
    game = Game("3", "Doom", None, LicenseInfo(LicenseType.SinglePurchase,
                                               None))
    plugin.add_game(game)
    assert get_messages(write) == [{
        "jsonrpc": "2.0",
        "method": "owned_game_added",
        "params": {
            "owned_game": {
                "game_id": "3",
                "game_title": "Doom",
                "license_info": {
                    "license_type": "SinglePurchase"
                }
            }
        }
    }]
Exemplo n.º 29
0
async def test_get_user_presence_error(exception, code, message, internal_type,
                                       plugin, read, write):
    user_id = "69"
    request_id = "55"
    plugin.prepare_user_presence_context.return_value = async_return_value(
        None)
    request = {
        "jsonrpc": "2.0",
        "id": request_id,
        "method": "start_user_presence_import",
        "params": {
            "user_id_list": [user_id]
        }
    }
    read.side_effect = [
        async_return_value(create_message(request)),
        async_return_value(b"", 10)
    ]
    plugin.get_user_presence.side_effect = exception
    await plugin.run()
    plugin.get_user_presence.assert_called()
    plugin.user_presence_import_complete.assert_called_once_with()

    assert get_messages(write) == [{
        "jsonrpc": "2.0",
        "id": request_id,
        "result": None
    }, {
        "jsonrpc": "2.0",
        "method": "user_presence_import_failure",
        "params": {
            "user_id": user_id,
            "error": {
                "code": code,
                "message": message,
                "data": {
                    "internal_type": internal_type
                }
            }
        }
    }, {
        "jsonrpc": "2.0",
        "method": "user_presence_import_finished",
        "params": None
    }]
Exemplo n.º 30
0
async def test_failure(plugin, read, write):
    request = {"jsonrpc": "2.0", "id": "3", "method": "import_owned_games"}

    read.side_effect = [
        async_return_value(create_message(request)),
        async_return_value(b"", 10)
    ]
    plugin.get_owned_games.side_effect = UnknownError()
    await plugin.run()
    plugin.get_owned_games.assert_called_with()
    assert get_messages(write) == [{
        "jsonrpc": "2.0",
        "id": "3",
        "error": {
            "code": 0,
            "message": "Unknown error"
        }
    }]