89 lines
5.4 KiB
Python
89 lines
5.4 KiB
Python
import json
|
|
import time
|
|
import unittest
|
|
import test_households as fixtures
|
|
app=fixtures.app
|
|
|
|
class LocationTests(unittest.TestCase):
|
|
setUp=fixtures.HouseholdTests.setUp
|
|
stop_server=fixtures.HouseholdTests.stop_server
|
|
|
|
def setup_member(self):
|
|
self.m=app.MEMBERS.save(self.owner,dict(name='没有账号的妈妈',label='妈妈'))
|
|
return self.m
|
|
|
|
def code(self,**extra):
|
|
if not hasattr(self,'m'):self.setup_member()
|
|
return app.LOCATIONS.manage(self.owner,dict(action='pair',memberId=self.m['id'],viewerIds=[self.members[self.aid]['id']],days=1,**extra))['code']
|
|
|
|
def paired(self,**extra):
|
|
grant=app.LOCATIONS.pair(dict(code=self.code(**extra),consentProtocol=2),'fixture')
|
|
self.grant=grant
|
|
app.LOCATIONS.native(grant['token'],dict(action='start',consent=True,receipt=grant['receipt']))
|
|
self.sid=app.DB.execute('SELECT id FROM member_location_shares').fetchone()[0]
|
|
app.LOCATIONS.latest[self.sid]['seen']=0
|
|
return grant['token']
|
|
|
|
def point(self,token):
|
|
app.LOCATIONS.native(token,dict(action='update',signal='available',position=dict(latitude=36.61,longitude=117.1,accuracy=9,at=time.time())))
|
|
|
|
def test_explicit_consent_names_viewers_and_no_login(self):
|
|
code=self.code();grant=app.LOCATIONS.pair(dict(code=code,consentProtocol=2),'fixture')
|
|
self.assertEqual(grant['memberName'],'没有账号的妈妈')
|
|
self.assertEqual(grant['viewers'][0]['id'],self.members[self.aid]['id'])
|
|
self.assertEqual(app.LOCATIONS.listing(self.aid,user=self.members[self.aid]),[])
|
|
for body in (dict(action='start'),dict(action='start',consent=True),dict(action='update')):
|
|
with self.assertRaises(app.Problem):app.LOCATIONS.native(grant['token'],body)
|
|
with self.assertRaises(app.Problem):app.LOCATIONS.pair(dict(code=code,consentProtocol=2),'fixture')
|
|
self.assertFalse(any(u.get('personId')==self.m['personId'] for u in app.objects('users')))
|
|
|
|
def test_scope_explicit_viewer_admin_no_bypass(self):
|
|
token=self.paired();self.point(token)
|
|
self.assertEqual(app.LOCATIONS.listing(self.aid,user=self.members[self.aid])[0]['status'],'live')
|
|
for viewer in (self.owner,self.root,self.other):self.assertEqual(app.LOCATIONS.listing(self.aid,user=viewer),[])
|
|
self.assertEqual(app.LOCATIONS.listing(self.aid),[])
|
|
self.assertEqual(app.LOCATIONS.listing(self.bid,user=self.members[self.aid]),[])
|
|
raw='\n'.join(app.DB.iterdump());self.assertNotIn('36.61',raw);self.assertNotIn(token,raw)
|
|
|
|
def test_stop_pause_resume_and_remote_revoke(self):
|
|
token=self.paired();self.point(token)
|
|
app.LOCATIONS.native(token,dict(action='stop'));self.assertNotIn(self.sid,app.LOCATIONS.latest)
|
|
with self.assertRaises(app.Problem):self.point(token)
|
|
app.LOCATIONS.native(token,dict(action='start',consent=True,receipt=self.grant['receipt']))
|
|
app.LOCATIONS.manage(self.owner,dict(action='stop',memberId=self.m['id']))
|
|
with self.assertRaises(app.Problem):self.point(token)
|
|
|
|
def test_stale_and_account_revocation(self):
|
|
token=self.paired();self.point(token)
|
|
app.LOCATIONS.latest[self.sid]['seen']=time.time()-901
|
|
row=app.LOCATIONS.listing(self.aid,user=self.members[self.aid])[0]
|
|
self.assertEqual(row['status'],'stale');self.assertIsNone(row['position'])
|
|
viewer=app.get_object('users',self.members[self.aid]['id']);viewer['disabled']=True;app.save_object('users',viewer)
|
|
self.assertEqual(app.LOCATIONS.listing(self.aid,user=self.members[self.aid]),[])
|
|
|
|
def test_member_inactive_and_old_app_are_denied(self):
|
|
token=self.paired();app.MEMBERS.save(self.owner,dict(self.m,status='historical'))
|
|
with self.assertRaises(app.Problem):self.point(token)
|
|
self.assertEqual(app.LOCATIONS.listing(self.aid,user=self.members[self.aid]),[])
|
|
with self.assertRaises(app.Problem):app.LOCATIONS.pair(dict(code='12345678'),'legacy')
|
|
|
|
def test_invalid_fix_and_cross_family_recipients_rejected(self):
|
|
token=self.paired()
|
|
for lat in (True,float('nan'),91):
|
|
with self.assertRaises(app.Problem):app.LOCATIONS.native(token,dict(action='update',signal='available',position=dict(latitude=lat,longitude=1,accuracy=1,at=time.time())))
|
|
with self.assertRaises(app.Problem):app.LOCATIONS.manage(self.owner,dict(action='pair',memberId=self.m['id'],viewerIds=[self.other['id']]))
|
|
with self.assertRaises(app.Problem):app.LOCATIONS.manage(self.other,dict(action='pair',memberId=self.m['id'],viewerIds=[self.other['id']]))
|
|
|
|
def test_display_requires_specific_current_pairing(self):
|
|
device=app.INVENTORY.save(dict(name='公共屏',deviceType='显示终端'),self.owner)
|
|
display=app.DISPLAYS.manage(self.owner,dict(action='create',deviceId=device['id']))
|
|
token=self.paired();self.point(token)
|
|
self.assertEqual(app.LOCATIONS.listing(self.aid,display_id=display['id']),[])
|
|
token=self.paired(displayIds=[display['id']]);self.point(token)
|
|
self.assertEqual(len(app.LOCATIONS.listing(self.aid,display_id=display['id'])),1)
|
|
replacement=app.DISPLAYS.manage(self.owner,dict(action='create',deviceId=device['id']))
|
|
self.assertEqual(app.LOCATIONS.listing(self.aid,display_id=replacement['id']),[])
|
|
self.assertEqual(app.LOCATIONS.listing(self.aid,display_id=display['id']),[])
|
|
|
|
if __name__=='__main__':unittest.main()
|