anki/qt/tests/test_addons.py

105 lines
2.8 KiB
Python
Raw Normal View History

2021-04-13 10:45:05 +02:00
# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
2019-04-24 21:44:11 +02:00
import os.path
from tempfile import TemporaryDirectory
from zipfile import ZipFile
from mock import MagicMock
2019-04-24 21:44:11 +02:00
from aqt.addons import AddonManager, extract_update_info, package_name_valid
2019-04-24 21:44:11 +02:00
def test_readMinimalManifest():
assertReadManifest(
2019-12-25 05:18:34 +01:00
'{"package": "yes", "name": "no"}', {"package": "yes", "name": "no"}
2019-04-24 21:44:11 +02:00
)
def test_readExtraKeys():
assertReadManifest(
'{"package": "a", "name": "b", "mod": 3, "conflicts": ["d", "e"]}',
2019-12-25 05:18:34 +01:00
{"package": "a", "name": "b", "mod": 3, "conflicts": ["d", "e"]},
2019-04-24 21:44:11 +02:00
)
def test_invalidManifest():
2019-12-25 05:18:34 +01:00
assertReadManifest('{"one": 1}', {})
2019-04-24 21:44:11 +02:00
def test_mustHaveName():
2019-12-25 05:18:34 +01:00
assertReadManifest('{"package": "something"}', {})
2019-04-24 21:44:11 +02:00
def test_mustHavePackage():
2019-12-25 05:18:34 +01:00
assertReadManifest('{"name": "something"}', {})
2019-04-24 21:44:11 +02:00
def test_invalidJson():
2019-12-25 05:18:34 +01:00
assertReadManifest("this is not a JSON dictionary", {})
2019-04-24 21:44:11 +02:00
def test_missingManifest():
assertReadManifest(
2019-12-25 05:18:34 +01:00
'{"package": "what", "name": "ever"}', {}, nameInZip="not-manifest.bin"
2019-04-24 21:44:11 +02:00
)
def test_ignoreExtraKeys():
assertReadManifest(
2019-12-25 05:18:34 +01:00
'{"package": "a", "name": "b", "game": "c"}', {"package": "a", "name": "b"}
2019-04-24 21:44:11 +02:00
)
def test_conflictsMustBeStrings():
assertReadManifest(
2019-12-25 05:18:34 +01:00
'{"package": "a", "name": "b", "conflicts": ["c", 4, {"d": "e"}]}', {}
2019-04-24 21:44:11 +02:00
)
def assertReadManifest(contents, expectedManifest, nameInZip="manifest.json"):
with TemporaryDirectory() as td:
zfn = os.path.join(td, "addon.zip")
with ZipFile(zfn, "w") as zfile:
zfile.writestr(nameInZip, contents)
adm = AddonManager(MagicMock())
with ZipFile(zfn, "r") as zfile:
assert adm.readManifestFile(zfile) == expectedManifest
def test_update_info():
json_info = dict(
id=999,
branches=[
{"minpt": 0, "maxpt": -15, "fmod": 222},
{"minpt": 20, "maxpt": -25, "fmod": 333},
{"minpt": 30, "maxpt": 35, "fmod": 444},
],
)
r = extract_update_info(5, 0, json_info)
assert r.current_branch_max_point_ver == -15
assert r.suitable_branch_last_modified == 222
r = extract_update_info(5, 1, json_info)
assert r.current_branch_max_point_ver == -25
assert r.suitable_branch_last_modified == 222
r = extract_update_info(19, 1, json_info)
assert r.current_branch_max_point_ver == -25
assert r.suitable_branch_last_modified == 0
r = extract_update_info(20, 1, json_info)
assert r.current_branch_max_point_ver == -25
assert r.suitable_branch_last_modified == 333
def test_package_name_validation():
assert not package_name_valid("")
assert not package_name_valid("/")
assert not package_name_valid("a/b")
assert not package_name_valid("..")
assert package_name_valid("ab")