| 1 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
# |
| 3 |
# This file is formatted with Python Black |
| 4 |
|
| 5 |
import tests as xdp |
| 6 |
|
| 7 |
import dbus |
| 8 |
import pytest |
| 9 |
import time |
| 10 |
|
| 11 |
|
| 12 |
@pytest.fixture |
| 13 |
def required_templates(): |
| 14 |
return {"email": {}} |
| 15 |
|
| 16 |
|
| 17 |
class TestEmail: |
| 18 |
def test_version(self, portals, dbus_con): |
| 19 |
"""tests the version of the interface""" |
| 20 |
|
| 21 |
xdp.check_version(dbus_con, "Email", 4) |
| 22 |
|
| 23 |
def test_basic(self, portals, dbus_con): |
| 24 |
"""test that the backend receives the expected data""" |
| 25 |
|
| 26 |
email_intf = xdp.get_portal_iface(dbus_con, "Email") |
| 27 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 28 |
|
| 29 |
addresses = ["[email protected]"] |
| 30 |
subject = "Re: portal tests" |
| 31 |
body = "You have to see this" |
| 32 |
|
| 33 |
request = xdp.Request(dbus_con, email_intf) |
| 34 |
options = { |
| 35 |
"addresses": addresses, |
| 36 |
"subject": subject, |
| 37 |
"body": body, |
| 38 |
} |
| 39 |
response = request.call( |
| 40 |
"ComposeEmail", |
| 41 |
parent_window="", |
| 42 |
options=options, |
| 43 |
) |
| 44 |
|
| 45 |
assert response |
| 46 |
assert response.response == 0 |
| 47 |
|
| 48 |
# Check the impl portal was called with the right args |
| 49 |
method_calls = mock_intf.GetMethodCalls("ComposeEmail") |
| 50 |
assert len(method_calls) > 0 |
| 51 |
_, args = method_calls[-1] |
| 52 |
assert args[2] == "" # parent window |
| 53 |
assert args[3]["addresses"] == addresses |
| 54 |
assert args[3]["subject"] == subject |
| 55 |
assert args[3]["body"] == body |
| 56 |
|
| 57 |
def test_address(self, portals, dbus_con): |
| 58 |
"""test that an invalid address triggers an error""" |
| 59 |
|
| 60 |
email_intf = xdp.get_portal_iface(dbus_con, "Email") |
| 61 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 62 |
|
| 63 |
addresses = ["gibberish! not an email address\n%Q"] |
| 64 |
|
| 65 |
request = xdp.Request(dbus_con, email_intf) |
| 66 |
options = { |
| 67 |
"addresses": addresses, |
| 68 |
} |
| 69 |
with pytest.raises(dbus.exceptions.DBusException) as excinfo: |
| 70 |
request.call( |
| 71 |
"ComposeEmail", |
| 72 |
parent_window="", |
| 73 |
options=options, |
| 74 |
) |
| 75 |
assert ( |
| 76 |
excinfo.value.get_dbus_name() |
| 77 |
== "org.freedesktop.portal.Error.InvalidArgument" |
| 78 |
) |
| 79 |
|
| 80 |
# Check the impl portal was never called |
| 81 |
method_calls = mock_intf.GetMethodCalls("ComposeEmail") |
| 82 |
assert len(method_calls) == 0 |
| 83 |
|
| 84 |
def test_punycode_address(self, portals, dbus_con): |
| 85 |
"""test email address containing punycode""" |
| 86 |
|
| 87 |
email_intf = xdp.get_portal_iface(dbus_con, "Email") |
| 88 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 89 |
|
| 90 |
addresses = ["[email protected]"] |
| 91 |
subject = "Re: portal tests" |
| 92 |
body = "To ASCII and beyond" |
| 93 |
|
| 94 |
request = xdp.Request(dbus_con, email_intf) |
| 95 |
options = { |
| 96 |
"addresses": addresses, |
| 97 |
"subject": subject, |
| 98 |
"body": body, |
| 99 |
} |
| 100 |
response = request.call( |
| 101 |
"ComposeEmail", |
| 102 |
parent_window="", |
| 103 |
options=options, |
| 104 |
) |
| 105 |
|
| 106 |
assert response |
| 107 |
assert response.response == 0 |
| 108 |
|
| 109 |
# Check the impl portal was called with the right args |
| 110 |
method_calls = mock_intf.GetMethodCalls("ComposeEmail") |
| 111 |
assert len(method_calls) > 0 |
| 112 |
_, args = method_calls[-1] |
| 113 |
assert args[2] == "" # parent window |
| 114 |
assert args[3]["addresses"] == addresses |
| 115 |
assert args[3]["subject"] == subject |
| 116 |
assert args[3]["body"] == body |
| 117 |
|
| 118 |
def test_subject_multiline(self, portals, dbus_con): |
| 119 |
"""test that an multiline subject triggers an error""" |
| 120 |
|
| 121 |
email_intf = xdp.get_portal_iface(dbus_con, "Email") |
| 122 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 123 |
|
| 124 |
subject = "not\na\nvalid\nsubject line" |
| 125 |
|
| 126 |
request = xdp.Request(dbus_con, email_intf) |
| 127 |
options = { |
| 128 |
"subject": subject, |
| 129 |
} |
| 130 |
with pytest.raises(dbus.exceptions.DBusException) as excinfo: |
| 131 |
request.call( |
| 132 |
"ComposeEmail", |
| 133 |
parent_window="", |
| 134 |
options=options, |
| 135 |
) |
| 136 |
assert ( |
| 137 |
excinfo.value.get_dbus_name() |
| 138 |
== "org.freedesktop.portal.Error.InvalidArgument" |
| 139 |
) |
| 140 |
|
| 141 |
# Check the impl portal was never called |
| 142 |
method_calls = mock_intf.GetMethodCalls("ComposeEmail") |
| 143 |
assert len(method_calls) == 0 |
| 144 |
|
| 145 |
def test_subject_too_long(self, portals, dbus_con): |
| 146 |
"""test that a subject line over 200 chars triggers an error""" |
| 147 |
|
| 148 |
email_intf = xdp.get_portal_iface(dbus_con, "Email") |
| 149 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 150 |
|
| 151 |
subject = "This subject line is too long" + "abc" * 60 |
| 152 |
|
| 153 |
assert len(subject) > 200 |
| 154 |
|
| 155 |
request = xdp.Request(dbus_con, email_intf) |
| 156 |
options = { |
| 157 |
"subject": subject, |
| 158 |
} |
| 159 |
with pytest.raises(dbus.exceptions.DBusException) as excinfo: |
| 160 |
request.call( |
| 161 |
"ComposeEmail", |
| 162 |
parent_window="", |
| 163 |
options=options, |
| 164 |
) |
| 165 |
assert ( |
| 166 |
excinfo.value.get_dbus_name() |
| 167 |
== "org.freedesktop.portal.Error.InvalidArgument" |
| 168 |
) |
| 169 |
|
| 170 |
# Check the impl portal was never called |
| 171 |
method_calls = mock_intf.GetMethodCalls("ComposeEmail") |
| 172 |
assert len(method_calls) == 0 |
| 173 |
|
| 174 |
@pytest.mark.parametrize("template_params", ({"email": {"delay": 2000}},)) |
| 175 |
def test_delay(self, portals, dbus_con): |
| 176 |
""" |
| 177 |
Test that everything works as expected when the backend takes some |
| 178 |
time to send its response, as * is to be expected from a real backend |
| 179 |
that presents dialogs to the user. |
| 180 |
""" |
| 181 |
|
| 182 |
email_intf = xdp.get_portal_iface(dbus_con, "Email") |
| 183 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 184 |
|
| 185 |
subject = "delay test" |
| 186 |
addresses = ["[email protected]"] |
| 187 |
|
| 188 |
request = xdp.Request(dbus_con, email_intf) |
| 189 |
options = { |
| 190 |
"addresses": addresses, |
| 191 |
"subject": subject, |
| 192 |
} |
| 193 |
|
| 194 |
start_time = time.perf_counter() |
| 195 |
|
| 196 |
response = request.call( |
| 197 |
"ComposeEmail", |
| 198 |
parent_window="", |
| 199 |
options=options, |
| 200 |
) |
| 201 |
|
| 202 |
assert response |
| 203 |
assert response.response == 0 |
| 204 |
|
| 205 |
end_time = time.perf_counter() |
| 206 |
|
| 207 |
assert end_time - start_time > 2 |
| 208 |
|
| 209 |
# Check the impl portal was called with the right args |
| 210 |
method_calls = mock_intf.GetMethodCalls("ComposeEmail") |
| 211 |
assert len(method_calls) > 0 |
| 212 |
_, args = method_calls[-1] |
| 213 |
assert args[2] == "" # parent window |
| 214 |
assert args[3]["addresses"] == addresses |
| 215 |
assert args[3]["subject"] == subject |
| 216 |
|
| 217 |
@pytest.mark.parametrize("template_params", ({"email": {"response": 1}},)) |
| 218 |
def test_cancel(self, portals, dbus_con): |
| 219 |
""" |
| 220 |
Test that user cancellation works as expected. |
| 221 |
We simulate that the user cancels a hypothetical dialog, |
| 222 |
by telling the backend to return 1 as response code. |
| 223 |
And we check that we get the expected G_IO_ERROR_CANCELLED. |
| 224 |
""" |
| 225 |
|
| 226 |
email_intf = xdp.get_portal_iface(dbus_con, "Email") |
| 227 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 228 |
|
| 229 |
subject = "cancel test" |
| 230 |
addresses = ["[email protected]"] |
| 231 |
|
| 232 |
request = xdp.Request(dbus_con, email_intf) |
| 233 |
options = { |
| 234 |
"addresses": addresses, |
| 235 |
"subject": subject, |
| 236 |
} |
| 237 |
|
| 238 |
response = request.call( |
| 239 |
"ComposeEmail", |
| 240 |
parent_window="", |
| 241 |
options=options, |
| 242 |
) |
| 243 |
|
| 244 |
assert response |
| 245 |
assert response.response == 1 |
| 246 |
|
| 247 |
# Check the impl portal was called with the right args |
| 248 |
method_calls = mock_intf.GetMethodCalls("ComposeEmail") |
| 249 |
assert len(method_calls) > 0 |
| 250 |
_, args = method_calls[-1] |
| 251 |
assert args[2] == "" # parent window |
| 252 |
assert args[3]["addresses"] == addresses |
| 253 |
assert args[3]["subject"] == subject |
| 254 |
|
| 255 |
@pytest.mark.parametrize("template_params", ({"email": {"expect-close": True}},)) |
| 256 |
def test_close(self, portals, dbus_con): |
| 257 |
""" |
| 258 |
Test that app-side cancellation works as expected. |
| 259 |
We cancel the cancellable while while the hypothetical |
| 260 |
dialog is up, and tell the backend that it should |
| 261 |
expect a Close call. We rely on the backend to |
| 262 |
verify that that call actually happened. |
| 263 |
""" |
| 264 |
|
| 265 |
email_intf = xdp.get_portal_iface(dbus_con, "Email") |
| 266 |
mock_intf = xdp.get_mock_iface(dbus_con) |
| 267 |
|
| 268 |
subject = "close test" |
| 269 |
addresses = ["[email protected]"] |
| 270 |
|
| 271 |
request = xdp.Request(dbus_con, email_intf) |
| 272 |
request.schedule_close(1000) |
| 273 |
options = { |
| 274 |
"addresses": addresses, |
| 275 |
"subject": subject, |
| 276 |
} |
| 277 |
|
| 278 |
request.call( |
| 279 |
"ComposeEmail", |
| 280 |
parent_window="", |
| 281 |
options=options, |
| 282 |
) |
| 283 |
|
| 284 |
# Only true if the impl.Request was closed too |
| 285 |
assert request.closed |
| 286 |
|
| 287 |
# Check the impl portal was called with the right args |
| 288 |
method_calls = mock_intf.GetMethodCalls("ComposeEmail") |
| 289 |
assert len(method_calls) > 0 |
| 290 |
_, args = method_calls[-1] |
| 291 |
assert args[2] == "" # parent window |
| 292 |
assert args[3]["addresses"] == addresses |
| 293 |
assert args[3]["subject"] == subject |