Files

164 lines
11 KiB
Python
Raw Permalink Normal View History

import secrets
import unittest
import test_households as fixtures
app=fixtures.app
class MemberTests(unittest.TestCase):
setUp=fixtures.HouseholdTests.setUp
stop_server=fixtures.HouseholdTests.stop_server
request=fixtures.HouseholdTests.request
def test_generation_directory_uses_confirmed_links_and_keeps_unknown_branches(self):
people = [app.MEMBERS.save(self.owner, dict(name=name)) for name in ('祖父', '父亲', '本人', '待考亲人')]
for first, second in zip(people[:2], people[1:3]):
app.save_object('family_links', dict(id=secrets.token_hex(8), familyId=self.aid,
kind='parent', active=True, fromId=first['personId'], toId=second['personId']))
app.MEMBERS.save(self.owner, dict(people[0], status='historical'))
app.MEMBERS.save(self.owner, dict(people[1], status='away'))
directory = app.MEMBERS.directory(self.owner)
rows = {p['name']:p for tree in directory['trees'] for branch in tree['branches'] for p in branch['people']}
self.assertEqual([rows[name]['generation'] for name in ('祖父', '父亲', '本人')], [1, 2, 3])
self.assertEqual(rows['祖父']['homes'], []) # Genealogy survives leaving a household.
self.assertEqual(rows['父亲']['homes'][0]['status'], 'away')
self.assertIsNone(rows['待考亲人']['generation'])
self.assertFalse(any(h['id'] == self.bid for tree in directory['trees'] for h in tree['homes']))
def test_generation_directory_shares_person_without_duplicate_or_foreign_home(self):
member = app.MEMBERS.save(self.owner, dict(name='跨住所成员'))
app.MEMBERS.share_genealogy(self.root, dict(targetFamilyId=self.bid, confirm=True))
app.MEMBERS.save(self.other, dict(personId=member['personId']))
def rows(actor):
return [p for tree in app.MEMBERS.directory(actor)['trees'] for b in tree['branches'] for p in b['people']]
self.assertEqual(len(rows(self.root)), 1)
self.assertEqual({h['id'] for h in rows(self.root)[0]['homes']}, {self.aid, self.bid})
self.assertEqual([h['id'] for h in rows(self.owner)[0]['homes']], [self.aid])
account = self.members[self.aid]
app.ACCOUNTS.save(dict(account, familyAccess='none'), self.root)
self.assertEqual(app.MEMBERS.directory(app.get_object('users', account['id']))['trees'], [])
def test_generation_directory_spouses_only_and_conflicts_stay_unknown(self):
a, b = [app.MEMBERS.save(self.owner, dict(name=n)) for n in ('一人', '另一人')]
edge = dict(id=secrets.token_hex(8), familyId=self.aid, kind='spouse', active=True,
fromId=a['personId'], toId=b['personId'])
app.save_object('family_links', edge)
def unknown():
return all(p['generation'] is None for t in app.MEMBERS.directory(self.owner)['trees'] for b in t['branches'] for p in b['people'])
self.assertTrue(unknown())
app.save_object('family_links', dict(edge, id=secrets.token_hex(8), kind='parent'))
self.assertTrue(unknown())
def test_global_account_credentials_cannot_be_changed_by_household_manager(self):
account = self.members[self.aid]
for actor in (self.owner, self.backup):
with self.assertRaises(app.Problem) as error:
app.ACCOUNTS.save(dict(account, password='attempt-change'), actor)
self.assertEqual(error.exception.status, 403)
def test_people_directory_needs_no_hardware_and_links_historical_experience(self):
member = app.MEMBERS.save(self.owner, dict(name='无账号长辈'))
app.MEMBERS.save(self.owner, dict(member, status='historical'))
with app.DB:
for table in ('cameras', 'assets', 'recorders', 'devices', 'sites'):
app.DB.execute('DELETE FROM ' + table)
tree = app.MEMBERS.directory(self.owner)['trees'][0]
person = tree['branches'][0]['people'][0]
self.assertEqual(tree['familyId'], self.aid)
self.assertEqual(person['id'], member['personId'])
self.assertFalse(person['self'])
self.assertEqual(person['homes'], [])
self.assertEqual([h['id'] for h in person['journalHomes']], [self.aid])
self.assertFalse(app.MEMBERS.snapshot(self.owner)['items'][0]['hasAccount'])
def test_directory_profile_context_requires_genealogy_access_and_marks_self(self):
member = app.MEMBERS.save(self.owner, dict(name='跨家庭亲人'))
app.MEMBERS.share_genealogy(self.root, dict(targetFamilyId=self.bid, confirm=True))
app.MEMBERS.save(self.other, dict(personId=member['personId']))
account = app.ACCOUNTS.save(dict(self.members[self.aid], personId=member['personId'], familyAccess='none'), self.root)
app.ACCOUNTS.grant_household(self.root, dict(userId=account['id'], familyId=self.bid, role='member', familyAccess='read'))
actor = app.get_object('users', account['id'])
tree = app.MEMBERS.directory(actor)['trees'][0]
self.assertEqual(tree['familyId'], self.bid)
person = tree['branches'][0]['people'][0]
self.assertTrue(person['self'])
self.assertEqual({h['id'] for h in person['journalHomes']}, {self.aid, self.bid})
self.assertFalse(app.MEMBERS.snapshot(actor)['canViewGenealogy'])
self.assertTrue(app.MEMBERS.snapshot(app.HOUSEHOLDS.context(actor, self.bid))['canViewGenealogy'])
# Removing the other grant hides its name and all of its history entry points.
app.ACCOUNTS.grant_household(self.root, dict(userId=account['id'], familyId=self.bid, role='none'))
app.ACCOUNTS.save(dict(app.get_object('users', account['id']), familyAccess='read'), self.root)
tree = app.MEMBERS.directory(app.get_object('users', account['id']))['trees'][0]
self.assertEqual(tree['familyId'], self.aid)
self.assertEqual([h['id'] for h in tree['branches'][0]['people'][0]['journalHomes']], [self.aid])
def test_additional_household_leader_promotion_does_not_promote_primary_home(self):
account = self.members[self.aid]
app.ACCOUNTS.grant_household(self.root, dict(userId=account['id'], familyId=self.bid, role='member'))
data = dict(id=self.bid, ownerId=account['id'], successorId='', revision=0, reason='指定已授权账号')
with self.assertRaises(app.Problem):
app.HOUSEHOLDS.leadership(data, self.roots[self.bid])
app.HOUSEHOLDS.leadership(dict(data, grantManagement=True), self.roots[self.bid])
stored = app.get_object('users', account['id'])
self.assertEqual(stored['role'], 'member')
self.assertEqual(app.HOUSEHOLDS.context(stored, self.bid)['role'], 'family_admin')
secondary = app.HOUSEHOLDS.context(stored, self.bid)
self.assertEqual(app.HOUSEHOLDS.context(secondary, self.aid)['role'], 'member')
for role in ('none', 'member'):
with self.assertRaises(app.Problem):
app.ACCOUNTS.grant_household(self.root, dict(userId=account['id'], familyId=self.bid, role=role))
with self.assertRaises(app.Problem):
app.ACCOUNTS.save(dict(stored, disabled=True), self.root)
def test_person_without_account_and_membership_counts(self):
before=len(app.objects('users'))
m=app.MEMBERS.save(self.owner,dict(name='妈妈',label='母亲'))
self.assertEqual(len(app.objects('users')),before)
view=app.MEMBERS.snapshot(self.members[self.aid])
self.assertFalse(view['items'][0]['hasAccount'])
self.assertEqual(view['items'][0]['personId'],m['personId'])
self.assertEqual(app.HOUSEHOLDS.list(self.owner)[0]['members'],1)
with self.assertRaises(app.Problem):app.MEMBERS.save(self.other,dict(m,label='越界'))
with self.assertRaises(app.Problem):app.MEMBERS.save(self.members[self.aid],dict(name='未授权'))
def test_shared_genealogy_keeps_devices_and_memberships_separate(self):
m=app.MEMBERS.save(self.owner,dict(name='同一位母亲'))
with self.assertRaises(app.Problem):app.MEMBERS.save(self.other,dict(personId=m['personId']))
app.MEMBERS.share_genealogy(self.root,dict(targetFamilyId=self.bid,confirm=True))
second=app.MEMBERS.save(self.other,dict(personId=m['personId']))
self.assertEqual(second['personId'],m['personId'])
self.assertEqual(len(app.objects('people')),1)
self.assertEqual(app.FAMILY.snapshot(self.other)['people'][0]['id'],m['personId'])
self.assertNotEqual(app.MEMBERS.rows(self.aid)[0]['id'],app.MEMBERS.rows(self.bid)[0]['id'])
self.assertEqual(len(app.HOUSEHOLDS.objects(self.other,'assets')),1)
self.assertEqual(app.HOUSEHOLDS.revision_key(self.owner),app.HOUSEHOLDS.revision_key(self.other))
with self.assertRaises(app.Problem):app.MEMBERS.share_genealogy(self.owner,dict(targetFamilyId=self.bid,confirm=True))
def test_membership_edit_cannot_change_person_or_bypass_genealogy(self):
m=app.MEMBERS.save(self.owner,dict(name='人物'))
old=app.get_object('people',m['personId'])
changed=app.MEMBERS.save(self.owner,dict(m,name='试图改名',status='historical'))
self.assertEqual(changed['status'],'historical')
self.assertEqual(app.get_object('people',m['personId']),old)
with self.assertRaises(app.Problem):app.FAMILY.save('person',dict(old,name='越过范围',revision=app.setting(app.HOUSEHOLDS.revision_key(self.owner),0)),self.owner)
def test_multiple_household_access_does_not_create_members(self):
u=self.members[self.aid]
app.ACCOUNTS.grant_household(self.root,dict(userId=u['id'],familyId=self.bid,role='member',familyAccess='read'))
stored=app.get_object('users',u['id']);ctx=app.HOUSEHOLDS.context(stored,self.bid)
self.assertEqual(ctx['role'],'member')
self.assertEqual(ctx['familyAccess'],'read')
self.assertEqual(ctx['siteIds'],[])
self.assertEqual({h['id'] for h in app.HOUSEHOLDS.list(ctx)},{self.aid,self.bid})
self.assertEqual(app.MEMBERS.rows(self.bid),[])
with self.assertRaises(app.Problem):app.ACCOUNTS.camera(ctx,self.cameras[self.bid])
with self.assertRaises(app.Problem):app.ACCOUNTS.grant_household(self.owner,dict(userId=u['id'],familyId=self.bid,role='family_admin'))
app.ACCOUNTS.grant_household(self.root,dict(userId=u['id'],familyId=self.bid,role='none'))
with self.assertRaises(app.Problem):app.HOUSEHOLDS.context(app.get_object('users',u['id']),self.bid)
def test_primary_account_edit_keeps_extra_grants(self):
u=self.members[self.aid]
app.ACCOUNTS.grant_household(self.root,dict(userId=u['id'],familyId=self.bid,role='member'))
old=app.get_object('users',u['id'])
saved=app.ACCOUNTS.save(dict(old,name='新显示名称'),self.root)
self.assertIn(self.bid,saved['familyGrants'])