Debian Codegraph @

tests/test_account.py

raw
1 # SPDX-License-Identifier: LGPL-2.1-or-later
2 #
3 # This file is formatted with Python Black
4
5 import tests.xdp_utils as xdp
6
7 import pytest
8 import os
9 from pathlib import Path
10
11
12 ACCOUNT_DATA = {
13 "id": "test",
14 "name": "Test Name",
15 "image": "FILLED OUT LATER",
16 }
17
18
19 @pytest.fixture
20 def required_templates():
21 image = Path(os.environ["XDG_DATA_HOME"]) / "account-image.png"
22 image.write_text("image contents")
23 ACCOUNT_DATA["image"] = f"file://{image.absolute().as_posix()}"
24
25 return {
26 "account": {
27 "results": ACCOUNT_DATA,
28 },
29 }
30
31
32 class TestAccount:
33 def test_version(self, portals, dbus_con):
34 xdp.check_version(dbus_con, "Account", 1)
35
36 def test_basic1(self, xdg_document_portal, portals, dbus_con, xdp_app_info):
37 app_id = xdp_app_info.app_id
38 account_intf = xdp.get_portal_iface(dbus_con, "Account")
39 mock_intf = xdp.get_mock_iface(dbus_con)
40
41 reason = "reason"
42
43 request = xdp.Request(dbus_con, account_intf)
44 options = {
45 "reason": reason,
46 }
47 response = request.call(
48 "GetUserInformation",
49 window="",
50 options=options,
51 )
52
53 assert response
54 assert response.response == 0
55 assert response.results["id"] == ACCOUNT_DATA["id"]
56 assert response.results["name"] == ACCOUNT_DATA["name"]
57 assert response.results["image"]
58 assert xdp.uri_same_file(ACCOUNT_DATA["image"], response.results["image"])
59
60 # Check the impl portal was called with the right args
61 method_calls = mock_intf.GetMethodCalls("GetUserInformation")
62 assert len(method_calls) > 0
63 _, args = method_calls[-1]
64 assert args[1] == app_id
65 assert args[2] == "" # window
66 assert args[3]["reason"] == reason
67
68 def test_reason(self, portals, dbus_con):
69 account_intf = xdp.get_portal_iface(dbus_con, "Account")
70 mock_intf = xdp.get_mock_iface(dbus_con)
71
72 reason = """This reason is unreasonably long, it stretches over
73 more than twohundredfiftysix characters, which is really quite
74 long. Excessively so. The portal frontend will silently drop
75 reasons of this magnitude. If you can't express your reasons
76 concisely, you probably have no good reason in the first place
77 and are just waffling around."""
78
79 assert len(reason) - 1 > 256
80
81 request = xdp.Request(dbus_con, account_intf)
82 options = {
83 "reason": reason,
84 }
85 response = request.call(
86 "GetUserInformation",
87 window="",
88 options=options,
89 )
90
91 assert response
92 assert response.response == 0
93
94 # Check the impl portal was called with the right args
95 method_calls = mock_intf.GetMethodCalls("GetUserInformation")
96 assert len(method_calls) > 0
97 _, args = method_calls[-1]
98 assert "reason" not in args[3]
99
100 @pytest.mark.parametrize("template_params", ({"account": {"expect-close": True}},))
101 def test_close(self, portals, dbus_con):
102 account_intf = xdp.get_portal_iface(dbus_con, "Account")
103
104 reason = "reason"
105
106 request = xdp.Request(dbus_con, account_intf)
107 request.schedule_close(1000)
108 options = {
109 "reason": reason,
110 }
111 request.call(
112 "GetUserInformation",
113 window="",
114 options=options,
115 )
116
117 # Only true if the impl.Request was closed too
118 assert request.closed