121 lines
9.0 KiB
Python
121 lines
9.0 KiB
Python
|
|
import io,json,time,unittest
|
||
|
|
from unittest.mock import patch
|
||
|
|
import test_phone_locations as fixtures
|
||
|
|
app=fixtures.app
|
||
|
|
|
||
|
|
class HistoryTests(unittest.TestCase):
|
||
|
|
setUp=fixtures.PhoneLocationTests.setUp
|
||
|
|
stop_server=fixtures.PhoneLocationTests.stop_server
|
||
|
|
phone=fixtures.PhoneLocationTests.phone
|
||
|
|
configure=fixtures.PhoneLocationTests.configure
|
||
|
|
start=fixtures.PhoneLocationTests.start
|
||
|
|
point=fixtures.PhoneLocationTests.point
|
||
|
|
def date(self):return time.strftime('%Y-%m-%d',time.gmtime(time.time()+8*3600))
|
||
|
|
def history(self,phone,source=None):
|
||
|
|
return app.LOCATIONS.native(phone['token'],dict(action='history-list',date=self.date(),memberId=(source or phone)['member']['id']))
|
||
|
|
def enabled(self):
|
||
|
|
source,viewer=self.phone('本人'),self.phone('查看人')
|
||
|
|
self.configure(source,phoneIds=[viewer['sid']],historyEnabled=True,historyConsent=True)
|
||
|
|
self.start(source);self.point(source)
|
||
|
|
return source,viewer
|
||
|
|
def package(self):
|
||
|
|
with patch('location_history.time.time',return_value=time.time()+3601):app.HISTORY.package()
|
||
|
|
return dict(app.DB.execute('SELECT * FROM location_history_files').fetchone())
|
||
|
|
def cloud(self,row):
|
||
|
|
files={}
|
||
|
|
def upload(key,file,remote):files[remote]=file.read_bytes();return dict(fs_id=5)
|
||
|
|
with patch.object(app.BAIDU,'upload',side_effect=upload),patch.object(app.BAIDU,'verify_file'):
|
||
|
|
app.HISTORY.process(row)
|
||
|
|
return files
|
||
|
|
def test_history_requires_separate_opt_in(self):
|
||
|
|
p=self.phone('本人');self.configure(p,viewerIds=[self.owner['id']]);self.start(p);self.point(p)
|
||
|
|
self.assertEqual(app.DB.execute('SELECT count(*) FROM location_history_points').fetchone()[0],0)
|
||
|
|
with self.assertRaises(app.Problem):self.configure(p,viewerIds=[self.owner['id']],historyEnabled=True)
|
||
|
|
def test_default_is_a_proposal_not_silent_collection(self):
|
||
|
|
p=self.phone('新成员');settings=app.LOCATIONS.native(p['token'],dict(action='settings'))
|
||
|
|
self.assertTrue(settings['historyDefault']);self.assertFalse(settings['historyEnabled'])
|
||
|
|
self.assertEqual(app.HISTORY.own_status(p['member']['id'])['pending'],0)
|
||
|
|
def test_day_week_month_year_life_and_custom_ranges(self):
|
||
|
|
from location_history import History
|
||
|
|
day=History.day_range('2026-10-18');week=History.day_range('2026-10-18','week');month=History.day_range('2026-10-18','month');year=History.day_range('2026-10-18','year')
|
||
|
|
self.assertEqual(day[1]-day[0],86400);self.assertEqual(week[1]-week[0],7*86400);self.assertEqual(month[1]-month[0],31*86400);self.assertEqual(year[1]-year[0],365*86400)
|
||
|
|
self.assertEqual(History.day_range('2026-10-18','life')[0],0)
|
||
|
|
self.assertEqual(History.day_range('2026-10-01','custom','2026-10-18')[1]-History.day_range('2026-10-01','custom','2026-10-18')[0],18*86400)
|
||
|
|
for period in ['bogus','custom']:
|
||
|
|
with self.assertRaises(ValueError):History.day_range('2026-10-18',period,'2026-10-01')
|
||
|
|
def test_ciphertext_and_explicit_recipient(self):
|
||
|
|
p,v=self.enabled();row=app.DB.execute('SELECT payload FROM location_history_points').fetchone()
|
||
|
|
self.assertNotIn(b'longitude',row[0]);self.assertEqual(len(self.history(v,p)['points']),1)
|
||
|
|
with self.assertRaises(app.Problem):app.HISTORY.query(self.aid,p['member']['id'],self.date(),user=self.root)
|
||
|
|
stranger=self.phone('无权查看人')
|
||
|
|
with self.assertRaises(app.Problem):self.history(stranger,p)
|
||
|
|
def test_new_viewer_does_not_get_old_points(self):
|
||
|
|
p,v=self.enabled();n=self.phone('新查看人')
|
||
|
|
self.configure(p,phoneIds=[v['sid'],n['sid']],historyEnabled=True,historyConsent=True);self.start(p)
|
||
|
|
self.assertEqual(self.history(n,p)['points'],[]);self.assertEqual(len(self.history(v,p)['points']),1)
|
||
|
|
def test_stop_revokes_read_but_owner_can_read(self):
|
||
|
|
p,v=self.enabled();app.LOCATIONS.native(p['token'],dict(action='stop'))
|
||
|
|
with self.assertRaises(app.Problem):self.history(v,p)
|
||
|
|
self.assertEqual(len(self.history(p)['points']),1)
|
||
|
|
def test_cloud_verify_before_local_release_and_restore(self):
|
||
|
|
p,v=self.enabled();row=self.package();files=self.cloud(row)
|
||
|
|
self.assertEqual(app.DB.execute('SELECT count(*) FROM location_history_points').fetchone()[0],0)
|
||
|
|
self.assertEqual(json.loads(app.DB.execute('SELECT body FROM location_history_files').fetchone()[0])['state'],'cloud')
|
||
|
|
with patch.object(app.BAIDU,'open_file',side_effect=lambda key,item:io.BytesIO(files[item['remotePath']])):
|
||
|
|
self.assertEqual(len(self.history(v,p)['points']),1)
|
||
|
|
def test_failed_upload_preserves_stable_ciphertext_for_retry(self):
|
||
|
|
p,v=self.enabled();row=self.package()
|
||
|
|
with patch.object(app.BAIDU,'upload',side_effect=OSError('failure')):app.HISTORY.process(row)
|
||
|
|
saved=json.loads(app.DB.execute('SELECT body FROM location_history_files').fetchone()[0]);self.assertIn('payload',saved)
|
||
|
|
self.assertEqual(app.DB.execute('SELECT count(*) FROM location_history_points').fetchone()[0],1)
|
||
|
|
row=dict(app.DB.execute('SELECT * FROM location_history_files').fetchone());files=self.cloud(row)
|
||
|
|
import base64
|
||
|
|
self.assertEqual(next(iter(files.values())),base64.b64decode(saved['payload']))
|
||
|
|
def test_failed_verification_does_not_delete_pending(self):
|
||
|
|
p,v=self.enabled();row=self.package()
|
||
|
|
with patch.object(app.BAIDU,'upload',return_value={'fs_id':5}),patch.object(app.BAIDU,'verify_file',side_effect=OSError()):app.HISTORY.process(row)
|
||
|
|
self.assertEqual(app.DB.execute('SELECT count(*) FROM location_history_points').fetchone()[0],1)
|
||
|
|
def test_only_self_can_delete_and_cloud_removal_is_queued(self):
|
||
|
|
p,v=self.enabled();row=self.package();self.cloud(row)
|
||
|
|
with self.assertRaises(app.Problem):app.LOCATIONS.native(v['token'],dict(action='history-delete',memberId=p['member']['id'],confirm=True))
|
||
|
|
app.LOCATIONS.native(p['token'],dict(action='history-delete',confirm=True))
|
||
|
|
with self.assertRaises(app.Problem):self.history(v,p)
|
||
|
|
row=dict(app.DB.execute('SELECT * FROM location_history_files').fetchone())
|
||
|
|
with patch.object(app.BAIDU,'delete_history_file') as remove:app.HISTORY.process(row);remove.assert_called_once()
|
||
|
|
self.assertEqual(app.DB.execute('SELECT count(*) FROM location_history_files').fetchone()[0],0)
|
||
|
|
def test_deleted_during_cloud_read_is_not_returned(self):
|
||
|
|
p,v=self.enabled();row=self.package();files=self.cloud(row)
|
||
|
|
def read(key,item):
|
||
|
|
app.LOCATIONS.native(p['token'],dict(action='history-delete',confirm=True));return io.BytesIO(files[item['remotePath']])
|
||
|
|
with patch.object(app.BAIDU,'open_file',side_effect=read),self.assertRaises(app.Problem):self.history(v,p)
|
||
|
|
def test_bounds_pause_new_history_without_stopping_live(self):
|
||
|
|
p,v=self.phone('本人'),self.phone('家人');self.configure(p,phoneIds=[v['sid']],historyEnabled=True,historyConsent=True);self.start(p)
|
||
|
|
with patch('location_history.LIMIT',0):self.point(p)
|
||
|
|
self.assertEqual(app.DB.execute('SELECT count(*) FROM location_history_points').fetchone()[0],0)
|
||
|
|
self.assertTrue(app.LOCATIONS.latest[p['sid']]['fix'])
|
||
|
|
def test_revoked_during_cloud_read_is_not_returned(self):
|
||
|
|
p,v=self.enabled();row=self.package();files=self.cloud(row)
|
||
|
|
def read(key,item):
|
||
|
|
app.LOCATIONS.native(p['token'],dict(action='stop'));return io.BytesIO(files[item['remotePath']])
|
||
|
|
with patch.object(app.BAIDU,'open_file',side_effect=read),self.assertRaises(app.Problem):self.history(v,p)
|
||
|
|
def test_cloud_delete_failure_stays_queued_and_hidden(self):
|
||
|
|
p,v=self.enabled();self.cloud(self.package())
|
||
|
|
app.LOCATIONS.native(p['token'],dict(action='history-delete',confirm=True))
|
||
|
|
row=dict(app.DB.execute('SELECT * FROM location_history_files').fetchone())
|
||
|
|
with patch.object(app.BAIDU,'delete_history_file',side_effect=OSError('offline')):app.HISTORY.process(row)
|
||
|
|
meta=json.loads(app.DB.execute('SELECT body FROM location_history_files').fetchone()[0])
|
||
|
|
self.assertEqual(meta['state'],'deleting');self.assertGreater(meta['nextTry'],time.time())
|
||
|
|
with self.assertRaises(app.Problem):self.history(p)
|
||
|
|
def test_pending_over_ten_thousand_samples_whole_interval(self):
|
||
|
|
p,v=self.enabled();member=p['member']['id'];base=time.time()-11000
|
||
|
|
with app.LOCK,app.DB:
|
||
|
|
app.DB.execute('DELETE FROM location_history_points')
|
||
|
|
app.DB.executemany('INSERT INTO location_history_points VALUES (?,?,?,?,?,NULL)',[(str(i),self.aid,member,base+i,str(i).encode()) for i in range(10005)])
|
||
|
|
def unseal(raw,key):return dict(position=dict(longitude=117,latitude=36,at=base+int(raw)))
|
||
|
|
with patch.object(app.HISTORY,'unseal',side_effect=unseal):
|
||
|
|
result=app.LOCATIONS.native(p['token'],dict(action='history-list',period='life',date=self.date()))
|
||
|
|
self.assertEqual(len(result['points']),4000);self.assertTrue(result['sampled'])
|
||
|
|
self.assertEqual(result['points'][0]['at'],base);self.assertEqual(result['points'][-1]['at'],base+10004)
|
||
|
|
|
||
|
|
if __name__=='__main__':unittest.main()
|