-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_chromewebstoreprovider.py
More file actions
250 lines (216 loc) · 9.52 KB
/
Copy pathtest_chromewebstoreprovider.py
File metadata and controls
250 lines (216 loc) · 9.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import json
import subprocess
import tempfile
from pathlib import Path
from abxpkg import Binary, ChromeWebstoreProvider
PACKAGED_CHROMEWEBSTORE_UTILS_PATH = (
Path(__file__).resolve().parent.parent / "abxpkg" / "chromewebstore_utils.js"
)
UBLOCK_WEBSTORE_ID = "ddkjiahejlhfcafbddmgiahcphecmpfh"
def assert_extension_binary_loaded(loaded) -> None:
assert loaded is not None
assert loaded.is_valid
assert loaded.loaded_binprovider is not None
assert loaded.loaded_binprovider.name == "chromewebstore"
assert loaded.loaded_abspath is not None
assert loaded.loaded_abspath.name.endswith(".extension.json")
assert loaded.loaded_abspath.exists()
assert loaded.loaded_version is not None
assert loaded.loaded_sha256 is not None
metadata = json.loads(loaded.loaded_abspath.read_text(encoding="utf-8"))
unpacked_path = Path(metadata["unpacked_path"])
assert unpacked_path.exists()
assert not (unpacked_path / "_metadata").exists()
manifest = json.loads((unpacked_path / "manifest.json").read_text(encoding="utf-8"))
assert manifest["version"] == str(loaded.loaded_version)
def restore_signed_store_metadata_from_real_crx(
unzip: str,
crx_path: Path,
unpacked_path: Path,
) -> None:
assert crx_path.exists(), crx_path
proc = subprocess.run(
[unzip, "-q", "-o", str(crx_path), "-d", str(unpacked_path)],
capture_output=True,
text=True,
)
assert (unpacked_path / "manifest.json").exists(), proc.stderr or proc.stdout
assert (unpacked_path / "_metadata").exists(), (
"The real Chrome Web Store CRX did not restore signed-store metadata; "
f"stdout={proc.stdout} stderr={proc.stderr}"
)
class TestChromeWebstoreProvider:
def test_install_root_alias_without_explicit_bin_dir_uses_root_extensions(
self,
test_machine,
):
test_machine.require_tool("node")
assert PACKAGED_CHROMEWEBSTORE_UTILS_PATH.exists(), (
PACKAGED_CHROMEWEBSTORE_UTILS_PATH
)
with tempfile.TemporaryDirectory() as temp_dir:
install_root = Path(temp_dir) / "chromewebstore-root"
provider = ChromeWebstoreProvider.model_validate(
{
"install_root": install_root,
"postinstall_scripts": True,
"min_release_age": 0,
},
).get_provider_with_overrides(
overrides={
"ublock": {
"install_args": [UBLOCK_WEBSTORE_ID, "--name=ublock"],
},
},
)
installed = provider.install("ublock")
assert_extension_binary_loaded(installed)
assert installed is not None
bin_dir = provider.bin_dir
assert bin_dir is not None
assert provider.install_root == install_root
assert bin_dir == install_root / "extensions"
assert installed.loaded_abspath is not None
assert installed.loaded_abspath.is_relative_to(bin_dir)
def test_install_root_and_bin_dir_aliases_install_into_the_requested_paths(
self,
test_machine,
):
test_machine.require_tool("node")
assert PACKAGED_CHROMEWEBSTORE_UTILS_PATH.exists(), (
PACKAGED_CHROMEWEBSTORE_UTILS_PATH
)
with tempfile.TemporaryDirectory() as temp_dir:
install_root = Path(temp_dir) / "chromewebstore-root"
bin_dir = Path(temp_dir) / "custom-extensions"
provider = ChromeWebstoreProvider.model_validate(
{
"install_root": install_root,
"bin_dir": bin_dir,
"postinstall_scripts": True,
"min_release_age": 0,
},
).get_provider_with_overrides(
overrides={
"ublock": {
"install_args": [UBLOCK_WEBSTORE_ID, "--name=ublock"],
},
},
)
installed = provider.install("ublock")
assert_extension_binary_loaded(installed)
assert installed is not None
assert bin_dir is not None
assert provider.install_root == install_root
assert provider.bin_dir == bin_dir
assert installed.loaded_abspath is not None
assert installed.loaded_abspath.is_relative_to(bin_dir)
def test_provider_direct_methods_exercise_real_lifecycle(self, test_machine):
test_machine.require_tool("node")
unzip = test_machine.require_tool("unzip")
assert PACKAGED_CHROMEWEBSTORE_UTILS_PATH.exists(), (
PACKAGED_CHROMEWEBSTORE_UTILS_PATH
)
with tempfile.TemporaryDirectory() as temp_dir:
provider = ChromeWebstoreProvider(
install_root=Path(temp_dir) / "chromewebstore-root",
bin_dir=Path(temp_dir) / "chromewebstore-root/extensions",
postinstall_scripts=True,
min_release_age=3,
).get_provider_with_overrides(
overrides={
"ublock": {
"install_args": [UBLOCK_WEBSTORE_ID, "--name=ublock"],
},
},
)
provider.setup(
postinstall_scripts=True,
min_release_age=3,
min_version=None,
)
assert provider.load("ublock", quiet=True, no_cache=True) is None
installed = provider.install("ublock", no_cache=True)
assert_extension_binary_loaded(installed)
assert installed is not None
assert installed.loaded_abspath is not None
crx_path = provider.bin_dir / f"{UBLOCK_WEBSTORE_ID}__ublock.crx"
metadata = json.loads(installed.loaded_abspath.read_text(encoding="utf-8"))
restore_signed_store_metadata_from_real_crx(
unzip,
crx_path,
Path(metadata["unpacked_path"]),
)
loaded = provider.load("ublock", no_cache=True)
assert_extension_binary_loaded(loaded)
loaded_or_installed = provider.install("ublock", no_cache=True)
assert_extension_binary_loaded(loaded_or_installed)
updated = provider.update("ublock", no_cache=True)
assert_extension_binary_loaded(updated)
assert provider.uninstall("ublock", no_cache=True) is True
assert provider.load("ublock", quiet=True, no_cache=True) is None
def test_binary_direct_methods_exercise_real_lifecycle(self, test_machine):
test_machine.require_tool("node")
assert PACKAGED_CHROMEWEBSTORE_UTILS_PATH.exists(), (
PACKAGED_CHROMEWEBSTORE_UTILS_PATH
)
with tempfile.TemporaryDirectory() as temp_dir:
binary = Binary(
name="ublock",
binproviders=[
ChromeWebstoreProvider(
install_root=Path(temp_dir) / "chromewebstore-root",
bin_dir=Path(temp_dir) / "chromewebstore-root/extensions",
postinstall_scripts=True,
min_release_age=3,
),
],
overrides={
"chromewebstore": {
"install_args": [UBLOCK_WEBSTORE_ID, "--name=ublock"],
},
},
postinstall_scripts=True,
min_release_age=3,
)
fresh = test_machine.unloaded_binary(binary)
test_machine.assert_binary_missing(fresh)
installed = fresh.install()
assert_extension_binary_loaded(installed)
loaded = test_machine.unloaded_binary(binary).load(no_cache=True)
assert_extension_binary_loaded(loaded)
loaded_or_installed = test_machine.unloaded_binary(binary).install(
no_cache=True,
)
assert_extension_binary_loaded(loaded_or_installed)
updated = installed.update()
assert_extension_binary_loaded(updated)
removed = updated.uninstall()
assert not removed.is_valid
assert removed.loaded_abspath is None
assert removed.loaded_version is None
assert removed.loaded_sha256 is None
test_machine.assert_binary_missing(binary)
def test_search_finds_real_chromewebstore_extension_by_id(self):
# Search resolves a 32-char Chrome Web Store extension ID into
# its canonical name by scraping the public detail page's
# ``<title>`` tag. The Binary's ``.name`` is the stable webstore
# ID (BinName-safe); the human extension name lives in
# ``description`` and the ``--name=`` install_arg. Non-ID
# queries return [] because the Web Store doesn't expose a JSON
# search API for keyword lookups.
results = ChromeWebstoreProvider().search(UBLOCK_WEBSTORE_ID)
assert results, "chromewebstore search by id should resolve uBlock Origin Lite"
assert len(results) == 1
match = results[0]
assert match.name == UBLOCK_WEBSTORE_ID
assert "ublock" in match.description.lower()
assert match.overrides == {
"chromewebstore": {
"install_args": [
UBLOCK_WEBSTORE_ID,
f"--name={match.description.split(' (')[0]}",
],
},
}
assert ChromeWebstoreProvider().search("not-an-id") == []