abxrcli 1.0.0__tar.gz → 1.0.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. {abxrcli-1.0.0 → abxrcli-1.0.2}/PKG-INFO +5 -1
  2. abxrcli-1.0.0/abxrcli.egg-info/PKG-INFO → abxrcli-1.0.2/README.md +4 -16
  3. {abxrcli-1.0.0 → abxrcli-1.0.2}/abxr/api_service.py +35 -9
  4. {abxrcli-1.0.0 → abxrcli-1.0.2}/abxr/app_bundles.py +60 -1
  5. abxrcli-1.0.0/README.md → abxrcli-1.0.2/abxrcli.egg-info/PKG-INFO +20 -0
  6. {abxrcli-1.0.0 → abxrcli-1.0.2}/pyproject.toml +1 -1
  7. {abxrcli-1.0.0 → abxrcli-1.0.2}/setup.py +1 -1
  8. abxrcli-1.0.2/tests/test_api_service.py +250 -0
  9. {abxrcli-1.0.0 → abxrcli-1.0.2}/tests/test_app_bundles.py +116 -0
  10. abxrcli-1.0.0/tests/test_api_service.py +0 -115
  11. {abxrcli-1.0.0 → abxrcli-1.0.2}/LICENSE +0 -0
  12. {abxrcli-1.0.0 → abxrcli-1.0.2}/abxr/apps.py +0 -0
  13. {abxrcli-1.0.0 → abxrcli-1.0.2}/abxr/audit_logs.py +0 -0
  14. {abxrcli-1.0.0 → abxrcli-1.0.2}/abxr/cli.py +0 -0
  15. {abxrcli-1.0.0 → abxrcli-1.0.2}/abxr/constants.py +0 -0
  16. {abxrcli-1.0.0 → abxrcli-1.0.2}/abxr/devices.py +0 -0
  17. {abxrcli-1.0.0 → abxrcli-1.0.2}/abxr/files.py +0 -0
  18. {abxrcli-1.0.0 → abxrcli-1.0.2}/abxr/formats.py +0 -0
  19. {abxrcli-1.0.0 → abxrcli-1.0.2}/abxr/groups.py +0 -0
  20. {abxrcli-1.0.0 → abxrcli-1.0.2}/abxr/multipart.py +0 -0
  21. {abxrcli-1.0.0 → abxrcli-1.0.2}/abxr/org.py +0 -0
  22. {abxrcli-1.0.0 → abxrcli-1.0.2}/abxr/output.py +0 -0
  23. {abxrcli-1.0.0 → abxrcli-1.0.2}/abxr/system_apps.py +0 -0
  24. {abxrcli-1.0.0 → abxrcli-1.0.2}/abxr/tags.py +0 -0
  25. {abxrcli-1.0.0 → abxrcli-1.0.2}/abxr/users.py +0 -0
  26. {abxrcli-1.0.0 → abxrcli-1.0.2}/abxr/version.py +0 -0
  27. {abxrcli-1.0.0 → abxrcli-1.0.2}/abxr/videos.py +0 -0
  28. {abxrcli-1.0.0 → abxrcli-1.0.2}/abxrcli.egg-info/SOURCES.txt +0 -0
  29. {abxrcli-1.0.0 → abxrcli-1.0.2}/abxrcli.egg-info/dependency_links.txt +0 -0
  30. {abxrcli-1.0.0 → abxrcli-1.0.2}/abxrcli.egg-info/entry_points.txt +0 -0
  31. {abxrcli-1.0.0 → abxrcli-1.0.2}/abxrcli.egg-info/requires.txt +0 -0
  32. {abxrcli-1.0.0 → abxrcli-1.0.2}/abxrcli.egg-info/top_level.txt +0 -0
  33. {abxrcli-1.0.0 → abxrcli-1.0.2}/setup.cfg +0 -0
  34. {abxrcli-1.0.0 → abxrcli-1.0.2}/tests/test_apps.py +0 -0
  35. {abxrcli-1.0.0 → abxrcli-1.0.2}/tests/test_devices.py +0 -0
  36. {abxrcli-1.0.0 → abxrcli-1.0.2}/tests/test_files.py +0 -0
  37. {abxrcli-1.0.0 → abxrcli-1.0.2}/tests/test_groups.py +0 -0
  38. {abxrcli-1.0.0 → abxrcli-1.0.2}/tests/test_multipart.py +0 -0
  39. {abxrcli-1.0.0 → abxrcli-1.0.2}/tests/test_org.py +0 -0
  40. {abxrcli-1.0.0 → abxrcli-1.0.2}/tests/test_output.py +0 -0
  41. {abxrcli-1.0.0 → abxrcli-1.0.2}/tests/test_system_apps.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: abxrcli
3
- Version: 1.0.0
3
+ Version: 1.0.2
4
4
  Summary: ArborXR CLI
5
5
  Author-email: Brian Rice <brian@arborxr.com>
6
6
  Description-Content-Type: text/markdown
@@ -56,6 +56,10 @@ pip install abxrcli
56
56
  `-s, --silent`
57
57
  * Description: Disable any terminal printing of progress bars for longer operations, like file uploads.
58
58
 
59
+ ## Retries
60
+
61
+ Every request the CLI makes (API calls and the file part uploads to storage) is retried automatically on transient failures: HTTP 429, 502, 503 and 504 responses, connection errors, and connections dropped before a response arrives. A request is attempted up to 6 times in total (the original plus 5 retries) with exponential backoff between attempts (0, 2, 4, 8 and 16 seconds, or the server's `Retry-After` value when present), so a request is abandoned after roughly 30 seconds of failures. Other errors, including 500, are reported immediately without retrying. No configuration is needed.
62
+
59
63
  ## Command Groups
60
64
 
61
65
  The CLI tool is divided into multiple groups including apps, app_bundles, files, and devices. Each group has its own set of subcommands.
@@ -1,19 +1,3 @@
1
- Metadata-Version: 2.4
2
- Name: abxrcli
3
- Version: 1.0.0
4
- Summary: ArborXR CLI
5
- Author-email: Brian Rice <brian@arborxr.com>
6
- Description-Content-Type: text/markdown
7
- License-File: LICENSE
8
- Requires-Dist: setuptools
9
- Requires-Dist: requests
10
- Requires-Dist: tqdm
11
- Requires-Dist: pyyaml
12
- Provides-Extra: test
13
- Requires-Dist: pytest; extra == "test"
14
- Requires-Dist: pytest-mock; extra == "test"
15
- Dynamic: license-file
16
-
17
1
  # ArborXR CLI
18
2
 
19
3
  This CLI tool provides commands for managing apps and files via the ArborXR API. The tool is organized into command groups: apps and files. Each group has several subcommands to perform specific operations through the command line.
@@ -56,6 +40,10 @@ pip install abxrcli
56
40
  `-s, --silent`
57
41
  * Description: Disable any terminal printing of progress bars for longer operations, like file uploads.
58
42
 
43
+ ## Retries
44
+
45
+ Every request the CLI makes (API calls and the file part uploads to storage) is retried automatically on transient failures: HTTP 429, 502, 503 and 504 responses, connection errors, and connections dropped before a response arrives. A request is attempted up to 6 times in total (the original plus 5 retries) with exponential backoff between attempts (0, 2, 4, 8 and 16 seconds, or the server's `Retry-After` value when present), so a request is abandoned after roughly 30 seconds of failures. Other errors, including 500, are reported immediately without retrying. No configuration is needed.
46
+
59
47
  ## Command Groups
60
48
 
61
49
  The CLI tool is divided into multiple groups including apps, app_bundles, files, and devices. Each group has its own set of subcommands.
@@ -5,9 +5,41 @@
5
5
 
6
6
  import re
7
7
  import requests
8
+ from requests.adapters import HTTPAdapter, Retry
8
9
 
9
10
  API_VERSION = 'v3'
10
11
 
12
+ RETRY_TOTAL = 5
13
+ RETRY_BACKOFF_FACTOR = 1
14
+ RETRY_STATUS_CODES = frozenset({429, 502, 503, 504})
15
+ RETRY_METHODS = frozenset({'GET', 'POST', 'PUT', 'PATCH', 'DELETE'})
16
+
17
+
18
+ def _build_client():
19
+ """A requests.Session that retries transient failures with exponential backoff.
20
+
21
+ Covers every v3 API call and the presigned part PUTs to storage. A bundle
22
+ upload makes thousands of requests, so a single 503 from a pod that is
23
+ being rolled during a deploy must not abort the whole run.
24
+
25
+ Retries exhaust to the last response rather than raising, so call sites
26
+ keep their existing raise_for_status() error handling.
27
+ """
28
+ retry = Retry(
29
+ total=RETRY_TOTAL,
30
+ backoff_factor=RETRY_BACKOFF_FACTOR,
31
+ status_forcelist=RETRY_STATUS_CODES,
32
+ allowed_methods=RETRY_METHODS,
33
+ raise_on_status=False,
34
+ respect_retry_after_header=True,
35
+ )
36
+ adapter = HTTPAdapter(max_retries=retry)
37
+
38
+ session = requests.Session()
39
+ session.mount('https://', adapter)
40
+ session.mount('http://', adapter)
41
+ return session
42
+
11
43
 
12
44
  class ApiService:
13
45
  def __init__(self, base_url, token):
@@ -29,19 +61,13 @@ class ApiService:
29
61
  'Accept': 'application/json'
30
62
  }
31
63
 
64
+ self.client = _build_client()
65
+
32
66
  if ".local" in self._raw_base_url:
33
67
  requests.packages.urllib3.disable_warnings(
34
68
  requests.packages.urllib3.exceptions.InsecureRequestWarning
35
69
  )
36
-
37
- old_request_method = requests.Session.request
38
- def new_request_method(self, *args, **kwargs):
39
- kwargs['verify'] = False
40
- return old_request_method(self, *args, **kwargs)
41
-
42
- requests.Session.request = new_request_method
43
-
44
- self.client = requests
70
+ self.client.verify = False
45
71
 
46
72
  @property
47
73
  def base_url(self):
@@ -6,6 +6,7 @@
6
6
 
7
7
  import os
8
8
  import hashlib
9
+ import re
9
10
  from tqdm import tqdm
10
11
  import time
11
12
  from pathlib import Path
@@ -20,6 +21,12 @@ from abxr.output import print_formatted
20
21
  from abxr.apps import AppsService
21
22
  from abxr.files import FilesService
22
23
 
24
+ # Android OBB filename convention: {main|patch}.{version_code}.{package_name}.obb
25
+ # Package name must have at least two segments separated by dots (e.g. com.example.app).
26
+ _OBB_FILENAME_PATTERN = re.compile(
27
+ r'^(?:main|patch)\.\d+\.([a-zA-Z][\w]*(?:\.[a-zA-Z][\w]*)+)\.obb$'
28
+ )
29
+
23
30
  class Commands(Enum):
24
31
  LIST = "list" # List app bundles for an app
25
32
  DETAILS = "details" # Get details of a specific app bundle
@@ -170,6 +177,22 @@ class AppBundlesService(ApiService):
170
177
  error_msg += "\nTo create a new bundle with these changes, use the upload command."
171
178
  raise ValueError(error_msg)
172
179
 
180
+ def _obb_device_path(self, file_path):
181
+ """Return the canonical /sdcard/Android/obb/{package_name} path for an OBB file.
182
+
183
+ Returns None (and prints a warning) when the filename does not match the
184
+ Android OBB naming convention.
185
+ """
186
+ match = _OBB_FILENAME_PATTERN.match(file_path.name)
187
+ if not match:
188
+ print(
189
+ f"Warning: OBB file '{file_path.name}' does not match the expected naming "
190
+ f"convention '{{main|patch}}.{{version_code}}.{{package_name}}.obb'. "
191
+ f"Skipping auto-correction; the bundle may fail with 'invalid-obb-path'."
192
+ )
193
+ return None
194
+ return f"/sdcard/Android/obb/{match.group(1)}"
195
+
173
196
  def _compute_device_path(self, file_path, folder, base_path=None):
174
197
  """Compute /sdcard device path for a file relative to folder root
175
198
 
@@ -179,8 +202,17 @@ class AppBundlesService(ApiService):
179
202
  base_path: Optional base path relative to /sdcard (e.g., "myapp/config")
180
203
 
181
204
  Returns:
182
- Device path string (e.g., "/sdcard" or "/sdcard/data/cache" or "/sdcard/myapp/config/data")
205
+ Device path string (e.g., "/sdcard" or "/sdcard/data/cache" or "/sdcard/myapp/config/data").
206
+ OBB files bypass `base_path` and folder layout and are routed to
207
+ "/sdcard/Android/obb/{package_name}" parsed from the filename.
183
208
  """
209
+ # Match the regex's case-sensitivity: Android's OBB loader only
210
+ # reads files with a lowercase .obb extension.
211
+ if file_path.suffix == '.obb':
212
+ obb_path = self._obb_device_path(file_path)
213
+ if obb_path is not None:
214
+ return obb_path
215
+
184
216
  rel_path = file_path.relative_to(folder)
185
217
  rel_dir = rel_path.parent
186
218
 
@@ -383,8 +415,35 @@ class AppBundlesService(ApiService):
383
415
  for file_path in all_files:
384
416
  file_hashes[file_path] = self.calculate_sha512(str(file_path))
385
417
 
418
+ self._warn_on_mixed_obb_packages(all_files)
419
+
386
420
  return folder, file_hashes
387
421
 
422
+ def _warn_on_mixed_obb_packages(self, all_files):
423
+ """Warn when bundle OBBs declare more than one package name.
424
+
425
+ Only OBBs matching the APK's package will load on-device, so a bundle
426
+ with OBBs from multiple packages is almost certainly a mistake.
427
+ """
428
+ packages = {}
429
+ for file_path in all_files:
430
+ if file_path.suffix != '.obb':
431
+ continue
432
+ match = _OBB_FILENAME_PATTERN.match(file_path.name)
433
+ if not match:
434
+ continue
435
+ packages.setdefault(match.group(1), []).append(file_path.name)
436
+
437
+ if len(packages) <= 1:
438
+ return
439
+
440
+ lines = [f" - {pkg}: {', '.join(files)}" for pkg, files in packages.items()]
441
+ print(
442
+ "Warning: bundle contains OBB files for multiple packages. Only OBBs "
443
+ "matching the APK's package name will load on-device:\n"
444
+ + "\n".join(lines)
445
+ )
446
+
388
447
  def create_app_bundle_from_existing(self, build_id, files=None, release_channel_id=None, new_release_channel_title=None):
389
448
  """Create an app bundle from existing build and files
390
449
 
@@ -1,3 +1,19 @@
1
+ Metadata-Version: 2.4
2
+ Name: abxrcli
3
+ Version: 1.0.2
4
+ Summary: ArborXR CLI
5
+ Author-email: Brian Rice <brian@arborxr.com>
6
+ Description-Content-Type: text/markdown
7
+ License-File: LICENSE
8
+ Requires-Dist: setuptools
9
+ Requires-Dist: requests
10
+ Requires-Dist: tqdm
11
+ Requires-Dist: pyyaml
12
+ Provides-Extra: test
13
+ Requires-Dist: pytest; extra == "test"
14
+ Requires-Dist: pytest-mock; extra == "test"
15
+ Dynamic: license-file
16
+
1
17
  # ArborXR CLI
2
18
 
3
19
  This CLI tool provides commands for managing apps and files via the ArborXR API. The tool is organized into command groups: apps and files. Each group has several subcommands to perform specific operations through the command line.
@@ -40,6 +56,10 @@ pip install abxrcli
40
56
  `-s, --silent`
41
57
  * Description: Disable any terminal printing of progress bars for longer operations, like file uploads.
42
58
 
59
+ ## Retries
60
+
61
+ Every request the CLI makes (API calls and the file part uploads to storage) is retried automatically on transient failures: HTTP 429, 502, 503 and 504 responses, connection errors, and connections dropped before a response arrives. A request is attempted up to 6 times in total (the original plus 5 retries) with exponential backoff between attempts (0, 2, 4, 8 and 16 seconds, or the server's `Retry-After` value when present), so a request is abandoned after roughly 30 seconds of failures. Other errors, including 500, are reported immediately without retrying. No configuration is needed.
62
+
43
63
  ## Command Groups
44
64
 
45
65
  The CLI tool is divided into multiple groups including apps, app_bundles, files, and devices. Each group has its own set of subcommands.
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "abxrcli"
7
- version = "1.0.0"
7
+ version = "1.0.2"
8
8
  description = "ArborXR CLI"
9
9
  readme = "README.md"
10
10
  authors = [{ name = "Brian Rice", email = "brian@arborxr.com" }]
@@ -6,7 +6,7 @@ from setuptools import setup
6
6
 
7
7
  setup(
8
8
  name = 'abxrcli',
9
- version = '1.0.0',
9
+ version = '1.0.2',
10
10
  packages = ['abxr'],
11
11
  entry_points = {
12
12
  'console_scripts': [
@@ -0,0 +1,250 @@
1
+ import threading
2
+ from http.server import BaseHTTPRequestHandler, HTTPServer
3
+
4
+ import pytest
5
+ import requests
6
+ from requests.adapters import Retry
7
+
8
+ from abxr.api_service import ApiService
9
+ from tests.conftest import make_response
10
+
11
+
12
+ class TestUrlNormalization:
13
+ def test_bare_origin(self):
14
+ svc = ApiService("https://api.xrdm.app", "tok")
15
+ assert svc._base_origin == "https://api.xrdm.app"
16
+
17
+ def test_trailing_slash_origin(self):
18
+ svc = ApiService("https://api.xrdm.app/", "tok")
19
+ assert svc._base_origin == "https://api.xrdm.app"
20
+
21
+ def test_strips_api(self):
22
+ svc = ApiService("https://api.xrdm.app/api", "tok")
23
+ assert svc._base_origin == "https://api.xrdm.app"
24
+
25
+ def test_strips_api_v3(self):
26
+ svc = ApiService("https://api.xrdm.app/api/v3", "tok")
27
+ assert svc._base_origin == "https://api.xrdm.app"
28
+
29
+ def test_strips_api_v3_with_trailing_slash(self):
30
+ svc = ApiService("https://api.xrdm.app/api/v3/", "tok")
31
+ assert svc._base_origin == "https://api.xrdm.app"
32
+
33
+ def test_strips_api_internal(self):
34
+ svc = ApiService("https://api.xrdm.app/api/internal", "tok")
35
+ assert svc._base_origin == "https://api.xrdm.app"
36
+
37
+
38
+ class TestV2Rejection:
39
+ def test_rejects_api_v2(self):
40
+ with pytest.raises(ValueError, match="v2"):
41
+ ApiService("https://api.xrdm.app/api/v2", "tok")
42
+
43
+ def test_rejects_api_v2_with_trailing_slash(self):
44
+ with pytest.raises(ValueError, match="v2"):
45
+ ApiService("https://api.xrdm.app/api/v2/", "tok")
46
+
47
+ def test_rejects_api_v2_with_extra_path(self):
48
+ with pytest.raises(ValueError, match="v2"):
49
+ ApiService("https://api.xrdm.app/api/v2/apps", "tok")
50
+
51
+ def test_error_message_mentions_v3(self):
52
+ with pytest.raises(ValueError, match=r"v3"):
53
+ ApiService("https://api.xrdm.app/api/v2", "tok")
54
+
55
+
56
+ class TestUrlBuilder:
57
+ def test_builds_v3_url(self):
58
+ svc = ApiService("https://api.xrdm.app", "tok")
59
+ assert svc._url("apps") == "https://api.xrdm.app/api/v3/apps"
60
+
61
+ def test_joins_multiple_segments(self):
62
+ svc = ApiService("https://api.xrdm.app", "tok")
63
+ assert svc._url("apps", "abc", "versions") == "https://api.xrdm.app/api/v3/apps/abc/versions"
64
+
65
+ def test_strips_segment_slashes(self):
66
+ svc = ApiService("https://api.xrdm.app", "tok")
67
+ assert svc._url("/apps/", "abc") == "https://api.xrdm.app/api/v3/apps/abc"
68
+
69
+
70
+ class TestAuthHeaders:
71
+ def test_bearer_token(self):
72
+ svc = ApiService("https://api.xrdm.app", "my-token")
73
+ assert svc.headers["Authorization"] == "Bearer my-token"
74
+ assert svc.headers["Content-Type"] == "application/json"
75
+ assert svc.headers["Accept"] == "application/json"
76
+
77
+
78
+ class TestPagination:
79
+ def test_single_page(self, mocker):
80
+ svc = ApiService("https://api.xrdm.app", "tok")
81
+ svc.client = mocker.MagicMock()
82
+ svc.client.get.return_value = make_response({"data": [{"id": 1}, {"id": 2}]})
83
+
84
+ result = svc._get_all_pages("https://api.xrdm.app/api/v3/apps")
85
+
86
+ assert result == [{"id": 1}, {"id": 2}]
87
+ assert svc.client.get.call_count == 1
88
+
89
+ def test_follows_links_next(self, mocker):
90
+ svc = ApiService("https://api.xrdm.app", "tok")
91
+ svc.client = mocker.MagicMock()
92
+ svc.client.get.side_effect = [
93
+ make_response({
94
+ "data": [{"id": 1}],
95
+ "links": {"next": "https://api.xrdm.app/api/v3/apps?page=2"}
96
+ }),
97
+ make_response({
98
+ "data": [{"id": 2}],
99
+ "links": {"next": "https://api.xrdm.app/api/v3/apps?page=3"}
100
+ }),
101
+ make_response({"data": [{"id": 3}], "links": {"next": None}}),
102
+ ]
103
+
104
+ result = svc._get_all_pages("https://api.xrdm.app/api/v3/apps")
105
+
106
+ assert result == [{"id": 1}, {"id": 2}, {"id": 3}]
107
+ assert svc.client.get.call_count == 3
108
+
109
+
110
+ class TestParseResponse:
111
+ def test_empty_content_returns_none(self, mocker):
112
+ svc = ApiService("https://api.xrdm.app", "tok")
113
+ response = mocker.MagicMock(content=b"")
114
+ assert svc._parse_response(response) is None
115
+
116
+ def test_non_empty_content_returns_json(self, mocker):
117
+ svc = ApiService("https://api.xrdm.app", "tok")
118
+ response = mocker.MagicMock(content=b'{"foo": "bar"}')
119
+ response.json.return_value = {"foo": "bar"}
120
+ assert svc._parse_response(response) == {"foo": "bar"}
121
+
122
+
123
+ class TestRetryClient:
124
+ def test_client_is_a_session(self):
125
+ svc = ApiService("https://api.xrdm.app", "tok")
126
+ assert isinstance(svc.client, requests.Session)
127
+
128
+ def test_verifies_tls_by_default(self):
129
+ svc = ApiService("https://api.xrdm.app", "tok")
130
+ assert svc.client.verify is True
131
+
132
+ def test_local_domain_skips_tls_verification(self):
133
+ svc = ApiService("https://portal.xrdm.local", "tok")
134
+ assert svc.client.verify is False
135
+
136
+ @pytest.mark.parametrize("url", ["https://api.xrdm.app/api/v3/apps", "http://portal.xrdm.local/api/v3/apps"])
137
+ def test_retry_adapter_mounted_for_both_schemes(self, url):
138
+ svc = ApiService("https://api.xrdm.app", "tok")
139
+ retry = svc.client.get_adapter(url).max_retries
140
+ assert isinstance(retry, Retry)
141
+ assert retry.total == 5
142
+ assert retry.backoff_factor == 1
143
+ assert retry.raise_on_status is False
144
+
145
+ @pytest.mark.parametrize("method", ["GET", "POST", "PUT", "PATCH", "DELETE"])
146
+ @pytest.mark.parametrize("status", [429, 502, 503, 504])
147
+ def test_retries_transient_statuses_for_every_method_used(self, method, status):
148
+ retry = ApiService("https://api.xrdm.app", "tok").client.get_adapter("https://x").max_retries
149
+ assert retry.is_retry(method, status)
150
+
151
+ @pytest.mark.parametrize("status", [400, 401, 403, 404, 409, 422, 500])
152
+ def test_does_not_retry_non_transient_statuses(self, status):
153
+ retry = ApiService("https://api.xrdm.app", "tok").client.get_adapter("https://x").max_retries
154
+ assert not retry.is_retry("GET", status)
155
+
156
+
157
+ class ScriptedHandler(BaseHTTPRequestHandler):
158
+ """Answers each request from server.script: a status code, or 'drop' to
159
+ close the connection without responding (a pod terminating mid-request)."""
160
+
161
+ def _handle(self):
162
+ self.server.hits += 1
163
+ self.rfile.read(int(self.headers.get('Content-Length') or 0))
164
+
165
+ step = self.server.script.pop(0) if self.server.script else 200
166
+ if step == 'drop':
167
+ return
168
+
169
+ body = b'{"ok": true}' if step == 200 else b''
170
+ self.send_response(step)
171
+ self.send_header('Content-Length', str(len(body)))
172
+ self.end_headers()
173
+ self.wfile.write(body)
174
+
175
+ do_GET = do_POST = do_PUT = _handle
176
+
177
+ def log_message(self, *args):
178
+ pass
179
+
180
+
181
+ @pytest.fixture
182
+ def scripted_server(mocker):
183
+ mocker.patch.object(Retry, "sleep")
184
+ server = HTTPServer(('127.0.0.1', 0), ScriptedHandler)
185
+ server.script = []
186
+ server.hits = 0
187
+ server.url = f"http://127.0.0.1:{server.server_address[1]}"
188
+ thread = threading.Thread(target=server.serve_forever, daemon=True)
189
+ thread.start()
190
+ yield server
191
+ server.shutdown()
192
+ server.server_close()
193
+
194
+
195
+ class TestRetryBehaviour:
196
+ def test_get_recovers_from_transient_503s(self, scripted_server):
197
+ scripted_server.script = [503, 503]
198
+ svc = ApiService(scripted_server.url, "tok")
199
+
200
+ response = svc.client.get(f"{scripted_server.url}/api/v3/apps", headers=svc.headers)
201
+
202
+ assert response.status_code == 200
203
+ assert scripted_server.hits == 3
204
+
205
+ def test_post_recovers_from_transient_503(self, scripted_server):
206
+ scripted_server.script = [503]
207
+ svc = ApiService(scripted_server.url, "tok")
208
+
209
+ response = svc.client.post(f"{scripted_server.url}/api/v3/files", json={"a": 1}, headers=svc.headers)
210
+
211
+ assert response.status_code == 200
212
+ assert scripted_server.hits == 2
213
+
214
+ def test_presigned_put_recovers_from_transient_503(self, scripted_server):
215
+ scripted_server.script = [503]
216
+ svc = ApiService(scripted_server.url, "tok")
217
+
218
+ response = svc.client.put(f"{scripted_server.url}/bucket/key?part=1", data=b"part-bytes")
219
+
220
+ assert response.status_code == 200
221
+ assert scripted_server.hits == 2
222
+
223
+ def test_recovers_from_connection_dropped_before_response(self, scripted_server):
224
+ scripted_server.script = ['drop', 'drop']
225
+ svc = ApiService(scripted_server.url, "tok")
226
+
227
+ response = svc.client.post(f"{scripted_server.url}/api/v3/files", json={"a": 1}, headers=svc.headers)
228
+
229
+ assert response.status_code == 200
230
+ assert scripted_server.hits == 3
231
+
232
+ def test_gives_up_after_retries_and_returns_last_response(self, scripted_server):
233
+ scripted_server.script = [503] * 10
234
+ svc = ApiService(scripted_server.url, "tok")
235
+
236
+ response = svc.client.get(f"{scripted_server.url}/api/v3/apps", headers=svc.headers)
237
+
238
+ assert response.status_code == 503
239
+ assert scripted_server.hits == 6
240
+ with pytest.raises(requests.HTTPError):
241
+ response.raise_for_status()
242
+
243
+ def test_does_not_retry_client_errors(self, scripted_server):
244
+ scripted_server.script = [404]
245
+ svc = ApiService(scripted_server.url, "tok")
246
+
247
+ response = svc.client.get(f"{scripted_server.url}/api/v3/apps/nope", headers=svc.headers)
248
+
249
+ assert response.status_code == 404
250
+ assert scripted_server.hits == 1
@@ -39,6 +39,122 @@ class TestDevicePathComputation:
39
39
  f.touch()
40
40
  assert svc._compute_device_path(f, tmp_path, "myapp/config") == "/sdcard/myapp/config/data"
41
41
 
42
+ def test_main_obb_routed_to_canonical_path(self, mocker, tmp_path):
43
+ svc = _service(mocker)
44
+ f = tmp_path / "main.123.com.example.testapp.obb"
45
+ f.touch()
46
+ assert svc._compute_device_path(f, tmp_path) == "/sdcard/Android/obb/com.example.testapp"
47
+
48
+ def test_patch_obb_routed_to_canonical_path(self, mocker, tmp_path):
49
+ svc = _service(mocker)
50
+ f = tmp_path / "patch.123.com.example.testapp.obb"
51
+ f.touch()
52
+ assert svc._compute_device_path(f, tmp_path) == "/sdcard/Android/obb/com.example.testapp"
53
+
54
+ def test_obb_in_subdir_ignores_folder_layout(self, mocker, tmp_path):
55
+ svc = _service(mocker)
56
+ (tmp_path / "extras").mkdir()
57
+ f = tmp_path / "extras" / "main.1.com.example.app.obb"
58
+ f.touch()
59
+ assert svc._compute_device_path(f, tmp_path) == "/sdcard/Android/obb/com.example.app"
60
+
61
+ def test_obb_bypasses_base_path(self, mocker, tmp_path):
62
+ svc = _service(mocker)
63
+ f = tmp_path / "main.7.com.foo.bar.obb"
64
+ f.touch()
65
+ assert svc._compute_device_path(f, tmp_path, "custom/path") == "/sdcard/Android/obb/com.foo.bar"
66
+
67
+ def test_misnamed_obb_falls_back_with_warning(self, mocker, tmp_path, capsys):
68
+ svc = _service(mocker)
69
+ f = tmp_path / "weird.obb"
70
+ f.touch()
71
+ assert svc._compute_device_path(f, tmp_path) == "/sdcard"
72
+ assert "does not match the expected naming convention" in capsys.readouterr().out
73
+
74
+ def test_obb_with_non_numeric_version_falls_back(self, mocker, tmp_path, capsys):
75
+ svc = _service(mocker)
76
+ f = tmp_path / "main.notanumber.com.foo.bar.obb"
77
+ f.touch()
78
+ assert svc._compute_device_path(f, tmp_path) == "/sdcard"
79
+ assert "does not match the expected naming convention" in capsys.readouterr().out
80
+
81
+ def test_obb_with_single_segment_package_falls_back(self, mocker, tmp_path, capsys):
82
+ svc = _service(mocker)
83
+ f = tmp_path / "main.1.foo.obb"
84
+ f.touch()
85
+ assert svc._compute_device_path(f, tmp_path) == "/sdcard"
86
+ assert "does not match the expected naming convention" in capsys.readouterr().out
87
+
88
+ def test_uppercase_extension_treated_as_non_obb(self, mocker, tmp_path, capsys):
89
+ # Android's OBB loader is case-sensitive; only lowercase .obb counts.
90
+ # A file with uppercase .OBB should get the default path with no warning.
91
+ svc = _service(mocker)
92
+ f = tmp_path / "main.1.com.example.app.OBB"
93
+ f.touch()
94
+ assert svc._compute_device_path(f, tmp_path) == "/sdcard"
95
+ assert capsys.readouterr().out == ""
96
+
97
+
98
+ class TestObbPackageConsistency:
99
+ def test_no_warning_for_single_package_with_main_and_patch(self, mocker, tmp_path, capsys):
100
+ svc = _service(mocker)
101
+ files = [
102
+ tmp_path / "main.1.com.example.app.obb",
103
+ tmp_path / "patch.1.com.example.app.obb",
104
+ ]
105
+ for f in files:
106
+ f.touch()
107
+ svc._warn_on_mixed_obb_packages(files)
108
+ out = capsys.readouterr().out
109
+ assert "multiple packages" not in out
110
+
111
+ def test_warns_when_obbs_declare_different_packages(self, mocker, tmp_path, capsys):
112
+ svc = _service(mocker)
113
+ files = [
114
+ tmp_path / "main.1.com.foo.app.obb",
115
+ tmp_path / "main.1.com.bar.app.obb",
116
+ ]
117
+ for f in files:
118
+ f.touch()
119
+ svc._warn_on_mixed_obb_packages(files)
120
+ out = capsys.readouterr().out
121
+ assert "multiple packages" in out
122
+ assert "com.foo.app" in out
123
+ assert "com.bar.app" in out
124
+ assert "main.1.com.foo.app.obb" in out
125
+ assert "main.1.com.bar.app.obb" in out
126
+
127
+ def test_misnamed_obb_does_not_count_as_separate_package(self, mocker, tmp_path, capsys):
128
+ svc = _service(mocker)
129
+ files = [
130
+ tmp_path / "main.1.com.example.app.obb",
131
+ tmp_path / "weird.obb",
132
+ ]
133
+ for f in files:
134
+ f.touch()
135
+ svc._warn_on_mixed_obb_packages(files)
136
+ out = capsys.readouterr().out
137
+ assert "multiple packages" not in out
138
+
139
+ def test_no_obbs_at_all(self, mocker, tmp_path, capsys):
140
+ svc = _service(mocker)
141
+ files = [tmp_path / "config.json", tmp_path / "data.bin"]
142
+ for f in files:
143
+ f.touch()
144
+ svc._warn_on_mixed_obb_packages(files)
145
+ assert capsys.readouterr().out == ""
146
+
147
+ def test_uppercase_obb_extension_does_not_count(self, mocker, tmp_path, capsys):
148
+ svc = _service(mocker)
149
+ files = [
150
+ tmp_path / "main.1.com.example.app.obb",
151
+ tmp_path / "main.1.com.other.app.OBB",
152
+ ]
153
+ for f in files:
154
+ f.touch()
155
+ svc._warn_on_mixed_obb_packages(files)
156
+ assert capsys.readouterr().out == ""
157
+
42
158
 
43
159
  class TestValidateBundleFilesMatch:
44
160
  def test_extracts_filename_and_checksum_from_v3_shape(self, mocker, tmp_path):
@@ -1,115 +0,0 @@
1
- import pytest
2
-
3
- from abxr.api_service import ApiService
4
- from tests.conftest import make_response
5
-
6
-
7
- class TestUrlNormalization:
8
- def test_bare_origin(self):
9
- svc = ApiService("https://api.xrdm.app", "tok")
10
- assert svc._base_origin == "https://api.xrdm.app"
11
-
12
- def test_trailing_slash_origin(self):
13
- svc = ApiService("https://api.xrdm.app/", "tok")
14
- assert svc._base_origin == "https://api.xrdm.app"
15
-
16
- def test_strips_api(self):
17
- svc = ApiService("https://api.xrdm.app/api", "tok")
18
- assert svc._base_origin == "https://api.xrdm.app"
19
-
20
- def test_strips_api_v3(self):
21
- svc = ApiService("https://api.xrdm.app/api/v3", "tok")
22
- assert svc._base_origin == "https://api.xrdm.app"
23
-
24
- def test_strips_api_v3_with_trailing_slash(self):
25
- svc = ApiService("https://api.xrdm.app/api/v3/", "tok")
26
- assert svc._base_origin == "https://api.xrdm.app"
27
-
28
- def test_strips_api_internal(self):
29
- svc = ApiService("https://api.xrdm.app/api/internal", "tok")
30
- assert svc._base_origin == "https://api.xrdm.app"
31
-
32
-
33
- class TestV2Rejection:
34
- def test_rejects_api_v2(self):
35
- with pytest.raises(ValueError, match="v2"):
36
- ApiService("https://api.xrdm.app/api/v2", "tok")
37
-
38
- def test_rejects_api_v2_with_trailing_slash(self):
39
- with pytest.raises(ValueError, match="v2"):
40
- ApiService("https://api.xrdm.app/api/v2/", "tok")
41
-
42
- def test_rejects_api_v2_with_extra_path(self):
43
- with pytest.raises(ValueError, match="v2"):
44
- ApiService("https://api.xrdm.app/api/v2/apps", "tok")
45
-
46
- def test_error_message_mentions_v3(self):
47
- with pytest.raises(ValueError, match=r"v3"):
48
- ApiService("https://api.xrdm.app/api/v2", "tok")
49
-
50
-
51
- class TestUrlBuilder:
52
- def test_builds_v3_url(self):
53
- svc = ApiService("https://api.xrdm.app", "tok")
54
- assert svc._url("apps") == "https://api.xrdm.app/api/v3/apps"
55
-
56
- def test_joins_multiple_segments(self):
57
- svc = ApiService("https://api.xrdm.app", "tok")
58
- assert svc._url("apps", "abc", "versions") == "https://api.xrdm.app/api/v3/apps/abc/versions"
59
-
60
- def test_strips_segment_slashes(self):
61
- svc = ApiService("https://api.xrdm.app", "tok")
62
- assert svc._url("/apps/", "abc") == "https://api.xrdm.app/api/v3/apps/abc"
63
-
64
-
65
- class TestAuthHeaders:
66
- def test_bearer_token(self):
67
- svc = ApiService("https://api.xrdm.app", "my-token")
68
- assert svc.headers["Authorization"] == "Bearer my-token"
69
- assert svc.headers["Content-Type"] == "application/json"
70
- assert svc.headers["Accept"] == "application/json"
71
-
72
-
73
- class TestPagination:
74
- def test_single_page(self, mocker):
75
- svc = ApiService("https://api.xrdm.app", "tok")
76
- svc.client = mocker.MagicMock()
77
- svc.client.get.return_value = make_response({"data": [{"id": 1}, {"id": 2}]})
78
-
79
- result = svc._get_all_pages("https://api.xrdm.app/api/v3/apps")
80
-
81
- assert result == [{"id": 1}, {"id": 2}]
82
- assert svc.client.get.call_count == 1
83
-
84
- def test_follows_links_next(self, mocker):
85
- svc = ApiService("https://api.xrdm.app", "tok")
86
- svc.client = mocker.MagicMock()
87
- svc.client.get.side_effect = [
88
- make_response({
89
- "data": [{"id": 1}],
90
- "links": {"next": "https://api.xrdm.app/api/v3/apps?page=2"}
91
- }),
92
- make_response({
93
- "data": [{"id": 2}],
94
- "links": {"next": "https://api.xrdm.app/api/v3/apps?page=3"}
95
- }),
96
- make_response({"data": [{"id": 3}], "links": {"next": None}}),
97
- ]
98
-
99
- result = svc._get_all_pages("https://api.xrdm.app/api/v3/apps")
100
-
101
- assert result == [{"id": 1}, {"id": 2}, {"id": 3}]
102
- assert svc.client.get.call_count == 3
103
-
104
-
105
- class TestParseResponse:
106
- def test_empty_content_returns_none(self, mocker):
107
- svc = ApiService("https://api.xrdm.app", "tok")
108
- response = mocker.MagicMock(content=b"")
109
- assert svc._parse_response(response) is None
110
-
111
- def test_non_empty_content_returns_json(self, mocker):
112
- svc = ApiService("https://api.xrdm.app", "tok")
113
- response = mocker.MagicMock(content=b'{"foo": "bar"}')
114
- response.json.return_value = {"foo": "bar"}
115
- assert svc._parse_response(response) == {"foo": "bar"}
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes