Skip to content

Commit c2f1b64

Browse files
authored
+tests as application (#13)
added run during installation to coverage
1 parent 83fbfdb commit c2f1b64

File tree

6 files changed

+82
-20
lines changed

6 files changed

+82
-20
lines changed

.github/workflows/analysis-coverage.yml

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,18 +129,21 @@ jobs:
129129
run: |
130130
patch -p 1 -i apps/app_ecosystem_v2/base_php.patch
131131
php occ app:enable app_ecosystem_v2
132-
python3 nc_py_api/tests/_install.py &
132+
cd nc_py_api
133+
coverage run --data-file=.coverage.ci_install tests/_install.py &
133134
echo $! > /tmp/_install.pid
135+
cd ..
134136
sleep 5s
135137
php occ app_ecosystem_v2:app:register $app_name $app_version "NcPyApi" \
136138
--host 127.0.0.1 --port 9002 \
137139
--secret $app_secret \
138140
-e --force-scopes --system-app
139-
kill -9 $(cat /tmp/_install.pid)
141+
kill -15 $(cat /tmp/_install.pid)
142+
timeout 3m tail --pid=$(cat /tmp/_install.pid) -f /dev/null
140143
141144
- name: Generate coverage report
142145
working-directory: nc_py_api
143-
run: coverage run -m pytest && coverage xml && coverage html
146+
run: coverage run --data-file=.coverage.ci -m pytest && coverage combine && coverage xml && coverage html
144147

145148
- name: HTML coverage to artifacts
146149
uses: actions/upload-artifact@v3
@@ -258,18 +261,21 @@ jobs:
258261
run: |
259262
patch -p 1 -i apps/app_ecosystem_v2/base_php.patch
260263
php occ app:enable app_ecosystem_v2
261-
python3 nc_py_api/tests/_install.py &
264+
cd nc_py_api
265+
coverage run --data-file=.coverage.ci_install tests/_install.py &
262266
echo $! > /tmp/_install.pid
267+
cd ..
263268
sleep 5s
264269
php occ app_ecosystem_v2:app:register $app_name $app_version "NcPyApi" \
265270
--host 127.0.0.1 --port 9002 \
266271
--secret $app_secret \
267272
-e --force-scopes --system-app
268-
kill -9 $(cat /tmp/_install.pid)
273+
kill -15 $(cat /tmp/_install.pid)
274+
timeout 3m tail --pid=$(cat /tmp/_install.pid) -f /dev/null
269275
270276
- name: Generate coverage report
271277
working-directory: nc_py_api
272-
run: coverage run -m pytest && coverage xml && coverage html
278+
run: coverage run --data-file=.coverage.ci -m pytest && coverage combine && coverage xml && coverage html
273279

274280
- name: HTML coverage to artifacts
275281
uses: actions/upload-artifact@v3
@@ -373,28 +379,29 @@ jobs:
373379

374380
- name: Checkout AppEcosystemV2
375381
uses: actions/checkout@v3
376-
if: ${{ !startsWith(matrix.nextcloud, '26.') }}
377382
with:
378383
path: apps/app_ecosystem_v2
379384
repository: cloud-py-api/app_ecosystem_v2
380385

381386
- name: Install AppEcosystemV2
382-
if: ${{ !startsWith(matrix.nextcloud, '26.') }}
383387
run: |
384388
patch -p 1 -i apps/app_ecosystem_v2/base_php.patch
385389
php occ app:enable app_ecosystem_v2
386-
python3 nc_py_api/tests/_install.py &
390+
cd nc_py_api
391+
coverage run --data-file=.coverage.ci_install tests/_install.py &
387392
echo $! > /tmp/_install.pid
393+
cd ..
388394
sleep 5s
389395
php occ app_ecosystem_v2:app:register $app_name $app_version "NcPyApi" \
390396
--host 127.0.0.1 --port 9002 \
391397
--secret $app_secret \
392398
-e --force-scopes --system-app
393-
kill -9 $(cat /tmp/_install.pid)
399+
kill -15 $(cat /tmp/_install.pid)
400+
timeout 3m tail --pid=$(cat /tmp/_install.pid) -f /dev/null
394401
395402
- name: Generate coverage report
396403
working-directory: nc_py_api
397-
run: coverage run -m pytest && coverage xml && coverage html
404+
run: coverage run --data-file=.coverage.ci -m pytest && coverage combine && coverage xml && coverage html
398405
env:
399406
SKIP_NC_WO_AE: 1
400407

nc_py_api/_session.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -311,15 +311,9 @@ def sign_check(self, request: Request):
311311
data = asyncio.run(request.body())
312312
if data:
313313
data_hash.update(data)
314-
# try:
315-
# data_hash.update(
316-
# dumps(loads(data, parse_int=str, parse_float=str), separators=(",", ":")).encode("UTF-8")
317-
# )
318-
# except (JSONDecodeError, TypeError):
319-
# data_hash.update(data)
320314
ae_data_hash = data_hash.hexdigest()
321315
if ae_data_hash != headers["AE-DATA-HASH"]:
322316
raise ValueError(f"Invalid AE-DATA-HASH:{ae_data_hash} !={headers['AE-DATA-HASH']}")
323-
# if headers["EX-APP-ID"] != self.cfg.app_name:
324-
# raise ValueError(f"Invalid EX-APP-ID:{headers['EX-APP-ID']} != {self.cfg.app_name}")
317+
if headers["EX-APP-ID"] != self.cfg.app_name:
318+
raise ValueError(f"Invalid EX-APP-ID:{headers['EX-APP-ID']} != {self.cfg.app_name}")
325319
return True

nc_py_api/nextcloud.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ def srv_version(self) -> ServerVersion:
5151
def check_capabilities(self, capabilities: Union[str, list[str]]) -> list[str]:
5252
return check_capabilities(capabilities, self.capabilities)
5353

54+
def update_server_info(self) -> None:
55+
self._session.update_server_info()
56+
5457
@property
5558
def theme(self) -> Optional[ThemingInfo]:
5659
return get_parsed_theme(self.capabilities["theming"]) if "theming" in self.capabilities else None

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ cover_pylib = true
2828
include = ["*/nc_py_api/*"]
2929
omit = ["*/tests/*"]
3030

31+
[tool.coverage.paths]
32+
source = [
33+
"nc_py_api/",
34+
"*/site-packages/nc_py_api/"
35+
]
36+
3137
[tool.coverage.report]
3238
exclude_lines = [
3339
"pragma: no cover",

tests/_install.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ def initialization():
2424

2525
if __name__ == "__main__":
2626
urllib3.disable_warnings()
27-
uvicorn.run("_install:APP", host="0.0.0.0", port=9002, log_level='trace', reload=False)
27+
uvicorn.run("_install:APP", host="0.0.0.0", port=9002, log_level='trace')

tests/nc_app_test.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import pytest
2+
3+
from gfixture import NC_APP, NC
4+
5+
from nc_py_api import ApiScope
6+
7+
8+
if NC_APP is None:
9+
pytest.skip("Only for Nextcloud App mode", allow_module_level=True)
10+
11+
12+
def test_get_users_list():
13+
users = NC_APP.users_list()
14+
assert users
15+
assert NC_APP.user in users
16+
17+
18+
def test_scope_allowed():
19+
for i in ApiScope:
20+
assert NC_APP.scope_allowed(i)
21+
assert not NC_APP.scope_allowed(0)
22+
assert not NC_APP.scope_allowed(999999999)
23+
24+
25+
@pytest.mark.skipif(NC is None, reason="Usual Nextcloud mode required for the test")
26+
def test_scope_allow_app_ecosystem_disabled():
27+
NC.apps.disable("app_ecosystem_v2")
28+
try:
29+
assert NC_APP.scope_allowed(ApiScope.DAV)
30+
NC_APP.update_server_info()
31+
assert not NC_APP.scope_allowed(ApiScope.DAV)
32+
finally:
33+
NC.apps.enable("app_ecosystem_v2")
34+
NC_APP.update_server_info()
35+
36+
37+
def test_change_user():
38+
orig_user = NC_APP.user
39+
try:
40+
orig_capabilities = NC_APP.capabilities
41+
assert NC_APP.users_status.available
42+
NC_APP.user = ""
43+
assert not NC_APP.users_status.available
44+
assert orig_capabilities != NC_APP.capabilities
45+
finally:
46+
NC_APP.user = orig_user
47+
assert orig_capabilities == NC_APP.capabilities
48+
49+
50+
@pytest.mark.skipif(NC is None, reason="Usual Nextcloud mode required for the test")
51+
def test_register_new_app():
52+
pass

0 commit comments

Comments
 (0)