67 lines
4.5 KiB
Python
67 lines
4.5 KiB
Python
import json, tempfile, time, unittest, sqlite3
|
|
from pathlib import Path
|
|
import fuc_navigation_domains as d
|
|
import fuc_navigation_stats as stats
|
|
|
|
class NavigationTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp=tempfile.TemporaryDirectory();self.addCleanup(self.tmp.cleanup);self.path=Path(self.tmp.name)/'state.json'
|
|
def entry(code,eligible,access):return dict(code=code,url='https://'+code+'.example.com/',hostname=code+'.example.com',role='direct_access',eligible=eligible,access=access,reason=None if eligible else access)
|
|
self.entries=[entry('good',True,'available'),entry('bad',False,'blocked')]
|
|
self.state={'digest':'a'*64,'checked_at':int(time.time()),'manifest':{'schema':'domainhub.public.v1','project':'fuc','environment':'production','instance':'navigation','version':1,'providers':{'fuc':{'entries':self.entries,'selected':self.entries[0]}}}}
|
|
self.save()
|
|
def save(self):
|
|
self.path.write_text(json.dumps(self.state))
|
|
Path(str(self.path)+'.watermark').write_text(json.dumps({'version':1,'digest':'a'*64}))
|
|
def test_redirect_rechecks_current_status_and_retains_only_valid_invitation(self):
|
|
self.assertEqual('https://good.example.com/#/register?code=abc_1',d.destination(self.path,'good','abc_1'))
|
|
self.assertEqual('https://bad.example.com/#/register',d.destination(self.path,'bad'))
|
|
self.assertIsNone(d.destination(self.path,'https://evil.example.com'))
|
|
self.assertEqual('https://good.example.com/#/register',d.destination(self.path,'good','https://evil'))
|
|
self.entries[0].update(eligible=False,access='blocked',reason='blocked');self.state['manifest']['providers']['fuc']['selected']=None;self.save()
|
|
self.assertEqual('https://good.example.com/#/register',d.destination(self.path,'good'))
|
|
values=d.entries(self.path)[1]
|
|
self.assertTrue(all(e['selectable'] for e in values))
|
|
self.assertTrue(all(not e['probe_enabled'] for e in values))
|
|
from public_client import available
|
|
self.assertIsNone(available(self.state,'fuc'))
|
|
def test_access_warnings_allow_manual_navigation_without_probes(self):
|
|
from public_client import available
|
|
for access in ['requires_proxy','suspected_blocked','blocked','unknown']:
|
|
self.entries[1].update(access=access,reason=access)
|
|
self.save()
|
|
entry=d.entries(self.path)[1][1]
|
|
self.assertTrue(entry['selectable'])
|
|
self.assertFalse(entry['probe_enabled'])
|
|
self.assertFalse(entry['eligible'])
|
|
self.assertEqual('https://bad.example.com/#/register',d.destination(self.path,'bad'))
|
|
self.assertEqual('https://good.example.com/',available(self.state,'fuc')['url'])
|
|
self.assertNotIn('selectable',self.state['manifest']['providers']['fuc']['entries'][1])
|
|
def test_registration_revocation_still_prevents_navigation(self):
|
|
for reason in ['disabled','registration_changed','not_active_yet','future_policy']:
|
|
self.entries[1].update(reason=reason)
|
|
self.save()
|
|
self.assertIsNone(d.destination(self.path,'bad'))
|
|
self.assertFalse(d.entries(self.path)[1][1]['selectable'])
|
|
def test_expired_reachability_evidence_is_not_probed(self):
|
|
self.entries[0]['valid_until']='2020-01-01 00:00:00'
|
|
self.save()
|
|
entry=d.entries(self.path)[1][0]
|
|
self.assertTrue(entry['selectable'])
|
|
self.assertFalse(entry['probe_enabled'])
|
|
self.assertEqual('evidence_expired',entry['reason'])
|
|
def test_expired_or_wrong_scope_snapshot_is_not_used(self):
|
|
self.state['checked_at']-=86401;self.save()
|
|
with self.assertRaises(ValueError):d.entries(self.path)
|
|
self.state['checked_at']=int(time.time());self.state['manifest']['project']='other';self.save()
|
|
with self.assertRaises(ValueError):d.entries(self.path)
|
|
def test_stats_accept_subset_without_mutating_domain_access(self):
|
|
db=sqlite3.connect(':memory:');now=stats.datetime.now(stats.SHANGHAI);stats.initialize(db,now)
|
|
stats.count_round(db,d.catalog(self.path),'https://fu-nav3.com',{'good':'success'},now)
|
|
self.assertEqual(1,db.execute('select success from totals').fetchone()[0])
|
|
self.assertEqual('https://bad.example.com/#/register',d.destination(self.path,'bad'))
|
|
with self.assertRaises(ValueError):stats.count_round(db,d.catalog(self.path),'https://fu-nav3.com',{'unknown':'success'},now)
|
|
with self.assertRaises(ValueError):stats.count_round(db,d.catalog(self.path),'https://fu-nav3.com',{},now)
|
|
|
|
if __name__=='__main__':unittest.main()
|