"""Release channel isolation and full update validation regression tests."""
import base64
from pathlib import Path
import plistlib
import tempfile
import unittest
from unittest.mock import patch
from macos_updates import configure, validate_url, publish, SPARKLE
class UpdateConfigurationTests(unittest.TestCase):
def test_production_rejects_local_feed(self):
with self.assertRaises(ValueError):
validate_url('http://localhost:8765/appcast.xml')
with self.assertRaises(ValueError):
validate_url('http://example.com/appcast.xml', local=True)
with self.assertRaises(ValueError):
validate_url('https://user:password@example.com/appcast.xml')
def test_local_configuration_is_scoped_to_development(self):
with tempfile.TemporaryDirectory() as directory:
app = Path(directory) / 'cutdex.app'
(app / 'Contents').mkdir(parents=True)
path = app / 'Contents/Info.plist'
key = base64.b64encode(bytes(range(32))).decode()
original = {'CFBundleIdentifier': 'app.cutdex.desktop'}
path.write_bytes(plistlib.dumps(original))
with self.assertRaises(ValueError):
configure(app, 'http://localhost:8765/appcast.xml', key, local=True)
self.assertEqual(plistlib.loads(path.read_bytes()), original)
original['CFBundleIdentifier'] += '.dev'
path.write_bytes(plistlib.dumps(original))
configure(app, 'http://localhost:8765/appcast.xml', key, local=True)
info = plistlib.loads(path.read_bytes())
self.assertFalse(info['SUAllowsAutomaticUpdates'])
self.assertFalse(info['SUEnableAutomaticChecks'])
self.assertNotIn('NSAllowsArbitraryLoads', info['NSAppTransportSecurity'])
self.assertEqual(set(info['NSAppTransportSecurity']['NSExceptionDomains']), {'localhost'})
def test_invalid_public_key_does_not_change_app(self):
with tempfile.TemporaryDirectory() as directory:
app = Path(directory)
(app / 'Contents').mkdir()
path = app / 'Contents/Info.plist'
original = plistlib.dumps({'CFBundleIdentifier': 'app.cutdex.desktop'})
path.write_bytes(original)
with self.assertRaises(ValueError):
configure(app, 'https://updates.example.com/appcast.xml', 'YQ==')
self.assertEqual(path.read_bytes(), original)
class PublicationVersionTests(unittest.TestCase):
def test_older_or_equivalent_build_cannot_replace_current_feed(self):
for current, candidate in [('900002', '900001'), ('2', '2.0'), ('2.10', '2.9')]:
with self.subTest(current=current, candidate=candidate), tempfile.TemporaryDirectory() as directory:
output = Path(directory)
feed = output / 'appcast.xml'
original = (f'- '
f'{current}'
'
').encode()
feed.write_bytes(original)
def generate(command, **kwargs):
if command[0] == 'ditto':
Path(command[-1]).write_bytes(b'archive')
else:
Path(command[command.index('-o') + 1]).write_text(
f'- '
f'{candidate}'
''
'
')
with patch('macos_updates.validate_app', return_value={'CFBundleVersion': candidate}), \
patch('macos_updates.subprocess.run', side_effect=generate) as run:
with self.assertRaisesRegex(ValueError, 'newer'):
publish(Path('app'), output, 'https://example.com/', Path('tools'))
run.assert_not_called()
self.assertEqual(feed.read_bytes(), original)
self.assertEqual(list(output.glob('*.zip')), [])
if __name__ == '__main__':
unittest.main()