117 lines
6.8 KiB
Python
117 lines
6.8 KiB
Python
import base64
|
|
import hashlib
|
|
import http.client
|
|
import json
|
|
import time
|
|
import unittest
|
|
from unittest.mock import patch
|
|
import test_households as fixtures
|
|
import test_phone_locations as phone_fixtures
|
|
|
|
app=fixtures.app
|
|
|
|
def audio(frames=10):
|
|
# 16 kHz ADTS frames; payload fixture is for transport validation, not playback.
|
|
return base64.b64encode(bytes([255,241,96,64,1,31,252,0])*frames).decode()
|
|
|
|
class VoiceTests(unittest.TestCase):
|
|
setUp=fixtures.HouseholdTests.setUp
|
|
stop_server=fixtures.HouseholdTests.stop_server
|
|
phone=phone_fixtures.PhoneLocationTests.phone
|
|
|
|
def call(self,phone,action,**data):return app.VOICE.native(phone['token'],dict(kind='phone',action=action,**data))
|
|
def receive(self,receiver,sender):
|
|
self.call(receiver,'configure',enabled=True,allowed=['phone:'+sender['sid']],consent=True)
|
|
self.call(receiver,'status')
|
|
def send(self,sender,receiver,**data):
|
|
body=dict(target='phone:'+receiver['sid'],audio=audio(),id='f'*32);body.update(data)
|
|
return self.call(sender,'send',**body)
|
|
|
|
def test_default_deny_and_explicit_two_way_consents(self):
|
|
a,b=self.phone('甲'),self.phone('乙')
|
|
with self.assertRaises(app.Problem):self.send(a,b)
|
|
self.receive(b,a)
|
|
result=self.send(a,b)
|
|
self.assertEqual(result['status'],'queued')
|
|
with self.assertRaises(app.Problem):self.send(b,a)
|
|
incoming=self.call(b,'status')['inbox'];self.assertEqual(len(incoming),1)
|
|
clip=self.call(b,'take',id=incoming[0]['id']);self.assertEqual(clip['audio'],audio())
|
|
self.assertEqual(len(app.VOICE.messages),0)
|
|
self.call(b,'ack',id=clip['id'],status='played')
|
|
self.assertEqual(self.call(a,'status')['sent'][0]['status'],'played')
|
|
with self.assertRaises(app.Problem):self.call(b,'take',id=clip['id'])
|
|
|
|
def test_idempotency_no_duplicate_delivery(self):
|
|
a,b=self.phone('甲'),self.phone('乙');self.receive(b,a)
|
|
result=self.send(a,b);self.assertEqual(self.send(a,b)['id'],result['id']);self.assertEqual(len(app.VOICE.messages),1)
|
|
self.call(b,'take',id=result['id']);self.send(a,b);self.assertEqual(len(app.VOICE.messages),0)
|
|
|
|
def test_delivery_requires_device_confirmation_and_is_separate_from_cloud(self):
|
|
a,b=self.phone('甲'),self.phone('乙');self.receive(b,a)
|
|
key=self.send(a,b)['id'];self.call(b,'take',id=key)
|
|
row=app.VOICE.history.existing(key);self.assertEqual(row['delivery'],'transferring');self.assertEqual(row['state'],'pending')
|
|
self.call(b,'ack',id=key,status='received')
|
|
self.assertEqual(app.VOICE.history.existing(key)['delivery'],'received')
|
|
self.assertEqual(app.VOICE.history.existing(key)['state'],'pending')
|
|
with self.assertRaises(app.Problem):self.call(a,'ack',id=key,status='received')
|
|
self.call(b,'ack',id=key,status='played')
|
|
self.assertEqual(app.VOICE.history.existing(key)['delivery'],'played')
|
|
with self.assertRaises(app.Problem):self.call(b,'ack',id=key,status='received')
|
|
|
|
def test_cross_family_and_foreign_inbox_rejected(self):
|
|
a,b,c=self.phone('甲'),self.phone('乙'),self.phone('其他',self.other)
|
|
with self.assertRaises(app.Problem):self.receive(b,c)
|
|
self.receive(b,a);msg=self.send(a,b)
|
|
with self.assertRaises(app.Problem):self.call(c,'take',id=msg['id'])
|
|
with self.assertRaises(app.Problem):self.send(c,b)
|
|
with self.assertRaises(app.Problem):self.call(a,'ack',id=msg['id'],status='played')
|
|
|
|
def test_mute_expiry_and_repair_clear_audio(self):
|
|
a,b=self.phone('甲'),self.phone('乙');self.receive(b,a);self.send(a,b)
|
|
self.call(b,'configure',enabled=False,allowed=[]);self.assertEqual(app.VOICE.messages,{})
|
|
self.receive(b,a);self.send(a,b,id='a'*32)
|
|
with patch('family_voice.time.time',return_value=time.time()+91):app.VOICE.sweep()
|
|
self.assertEqual(app.VOICE.messages,{})
|
|
self.receive(b,a);self.send(a,b,id='b'*32)
|
|
app.LOCATIONS.native(a['token'],{'action':'revoke'});app.VOICE.sweep();self.assertEqual(app.VOICE.messages,{})
|
|
with self.assertRaises(app.Problem):self.call(a,'status')
|
|
|
|
def test_pairing_not_location_consent_and_offline_no_delivery(self):
|
|
a,b=self.phone('甲'),self.phone('乙');self.receive(b,a)
|
|
self.assertEqual(app.LOCATIONS.latest,{})
|
|
self.send(a,b);self.call(b,'offline');self.assertEqual(app.VOICE.messages,{})
|
|
with self.assertRaises(app.Problem):self.send(a,b,id='a'*32)
|
|
|
|
def test_bad_audio_bounds_and_queue_limit(self):
|
|
a,b=self.phone('甲'),self.phone('乙');self.receive(b,a)
|
|
for content in ('invalid',base64.b64encode(b'not audio').decode(),audio(500),audio(1),'A'*200001):
|
|
with self.subTest(content=content[:15]),self.assertRaises(app.Problem):self.send(a,b,audio=content)
|
|
for i in range(3):self.send(a,b,id=str(i)*32)
|
|
with self.assertRaises(app.Problem):self.send(a,b,id='a'*32)
|
|
with self.assertRaises(app.Problem):self.call(b,'take',id=[])
|
|
|
|
def test_display_uses_native_token_and_manager_scope(self):
|
|
phone=self.phone('家人')
|
|
device=app.INVENTORY.save(dict(name='屏幕',deviceType='显示终端'),self.roots[self.aid])
|
|
grant=app.DISPLAYS.manage(self.roots[self.aid],dict(action='create',deviceId=device['id']))
|
|
paired=app.DISPLAYS.pair({'code':grant['pairingCode']},'voice-test')
|
|
with self.assertRaises(app.Problem):app.VOICE.native(paired['token'],dict(kind='display',action='status'))
|
|
data=dict(deviceId=device['id'],action='configure',enabled=True,allowed=['phone:'+phone['sid']],consent=True)
|
|
for actor in (self.other,self.members[self.aid]):
|
|
with self.assertRaises(app.Problem):app.VOICE.manage_display(actor,data)
|
|
app.VOICE.manage_display(self.owner,data)
|
|
app.VOICE.native(paired['nativeToken'],dict(kind='display',action='status'))
|
|
self.assertEqual(self.call(phone,'status')['targets'][0]['name'],'屏幕')
|
|
app.INVENTORY.transfer(dict(source='device',id=device['id'],targetFamilyId=self.bid),self.root)
|
|
self.assertEqual(self.call(phone,'status')['targets'],[])
|
|
|
|
def test_http_auth_and_audio_not_persisted(self):
|
|
a,b=self.phone('甲'),self.phone('乙');self.receive(b,a);self.send(a,b)
|
|
raw='\n'.join(app.DB.iterdump());self.assertNotIn(audio(),raw);self.assertNotIn(a['token'],raw)
|
|
for token,expected in ((a['token'],200),(self.tokens[self.root['id']],401),('',401)):
|
|
connection=http.client.HTTPConnection(*self.server.server_address,timeout=3)
|
|
connection.request('POST','/api/voice',json.dumps(dict(kind='phone',action='status')),{'Authorization':'Bearer '+token,'Content-Type':'application/json'})
|
|
response=connection.getresponse();response.read();connection.close();self.assertEqual(response.status,expected)
|
|
|
|
if __name__=='__main__':unittest.main()
|