Files
ucvl-home-vision/test_displays.py
T

102 lines
5.7 KiB
Python
Raw Permalink Normal View History

import json
import time
import unittest
from unittest.mock import patch
import test_households as fixtures
app=fixtures.app
class DisplayTests(unittest.TestCase):
setUp = fixtures.HouseholdTests.setUp
stop_server = fixtures.HouseholdTests.stop_server
def tv(self, family=None):
return app.INVENTORY.save(dict(name='Fixture TV',deviceType='安卓电视',managementHost='192.168.8.223'),self.roots[family or self.aid])
def pair(self, device):
app.DISPLAYS.state_cache=(0,{})
return app.DISPLAYS.manage(self.roots[self.aid],dict(action='create',deviceId=device['id'],city='Fixture'))
def test_token_hash_only_and_revocation(self):
tv=self.tv();first=self.pair(tv)
row=dict(app.DB.execute('SELECT * FROM household_displays').fetchone())
self.assertNotIn(first['token'],json.dumps(row))
self.assertEqual(app.DISPLAYS.authorize(first['token'])['family_id'],self.aid)
self.pair(tv)
with self.assertRaises(app.Problem):app.DISPLAYS.authorize(first['token'])
def test_scoped_projection_does_not_expose_secrets(self):
tv=self.tv();other=self.tv(self.bid);grant=app.DISPLAYS.authorize(self.pair(tv)['token'])
with patch.object(app.DISPLAYS,'weather',return_value={'available':False}):snap=app.DISPLAYS.snapshot(grant)
raw=json.dumps(snap)
self.assertNotIn(other['id'],raw)
for key in ('password','username','managementHost','host','token','ownerName','serial'):
self.assertNotIn('"'+key+'"',raw)
self.assertEqual(snap['home']['deviceCount'],2)
def test_cross_household_admin_context_is_enforced(self):
tv=self.tv()
for actor in (self.other,self.members[self.aid],self.roots[self.bid]):
with self.assertRaises(app.Problem):app.DISPLAYS.listing(actor,tv['id'])
def test_expired_token_is_denied(self):
token=self.pair(self.tv())['token']
app.DB.execute('UPDATE household_displays SET expires_at=?',(time.time()-1,));app.DB.commit()
with self.assertRaises(app.Problem):app.DISPLAYS.authorize(token)
def test_pair_code_is_single_use_and_native_camera_access_is_separate(self):
grant=self.pair(self.tv())
with self.assertRaises(app.Problem):app.DISPLAYS.authorize_native(grant['token'])
paired=app.DISPLAYS.pair({'code':grant['pairingCode']},'fixture-pair')
native=app.DISPLAYS.authorize_native(paired['nativeToken'])
items=app.DISPLAYS.cameras(native)['items']
self.assertEqual([c['id'] for c in items],[self.cameras[self.aid]['id']])
self.assertTrue(items[0]['url'].startswith('rtsp://192.168.3.10:554/'))
with self.assertRaises(app.Problem):app.DISPLAYS.pair({'code':grant['pairingCode']},'fixture-pair')
self.pair(app.get_object('devices',native['device_id']))
with self.assertRaises(app.Problem):app.DISPLAYS.authorize_native(paired['nativeToken'])
def test_pair_attempt_limit_and_expiry(self):
grant=self.pair(self.tv())
for item in app.DISPLAYS.pair_codes.values():item['expires']=0
with self.assertRaises(app.Problem):app.DISPLAYS.pair({'code':grant['pairingCode']},'expired-fixture')
for _ in range(5):
with self.assertRaises(app.Problem):app.DISPLAYS.pair({'code':'wrong'},'blocked-fixture')
with self.assertRaises(app.Problem) as caught:app.DISPLAYS.pair({'code':'00000000'},'blocked-fixture')
self.assertEqual(caught.exception.status,429)
def test_telemetry_alert_opens_and_recovers(self):
tv=self.tv();grant=app.DISPLAYS.authorize(self.pair(tv)['token'])
app.DISPLAYS.heartbeat(grant,{'temperatureC':86,'temperatureSource':'soc-thermal','foreground':True})
status=app.DISPLAYS.hardware(self.root,tv['id'])
self.assertTrue(status['online']);self.assertEqual(status['alerts'][0]['severity'],'critical')
app.DB.execute('UPDATE display_telemetry SET at=at-10');app.DB.commit()
app.DISPLAYS.heartbeat(grant,{'temperatureC':55,'foreground':True})
self.assertTrue(app.DISPLAYS.hardware(self.root,tv['id'])['alerts'][0]['cleared'])
def test_invalid_telemetry_rejected(self):
grant=app.DISPLAYS.authorize(self.pair(self.tv())['token'])
for value in (float('nan'),151,True,'86'):
with self.assertRaises(app.Problem):app.DISPLAYS.heartbeat(grant,{'temperatureC':value})
def test_transfer_revokes_previous_family_access(self):
tv=self.tv();token=self.pair(tv)['token']
with self.assertRaises(app.Problem):app.INVENTORY.transfer(dict(source='device',id=tv['id'],targetFamilyId=self.bid),self.owner)
app.INVENTORY.transfer(dict(source='device',id=tv['id'],targetFamilyId=self.bid),self.root)
self.assertEqual(app.get_object('devices',tv['id'])['familyId'],self.bid)
with self.assertRaises(app.Problem):app.DISPLAYS.authorize(token)
with self.assertRaises(app.Problem):app.DISPLAYS.hardware(self.owner,tv['id'])
self.assertFalse(app.DISPLAYS.hardware(self.roots[self.bid],tv['id'])['paired'])
def test_preview_is_requested_scoped_and_ephemeral(self):
tv=self.tv();grant=app.DISPLAYS.authorize(self.pair(tv)['token'])
with self.assertRaises(app.Problem):app.DISPLAYS.accept_preview(grant,{'jpeg':''})
app.DISPLAYS.manage(self.root,dict(action='preview',deviceId=tv['id']))
self.assertTrue(app.DISPLAYS.heartbeat(grant,{'foreground':True})['capturePreview'])
with self.assertRaises(app.Problem):app.DISPLAYS.accept_preview(grant,{'jpeg':'PHN2Zz4='})
app.DISPLAYS.previews[grant['id']]=(time.time()-91,b'old')
with self.assertRaises(app.Problem):app.DISPLAYS.preview(self.root,tv['id'])
if __name__=='__main__':unittest.main()