def setUp(self): self.unittest_dir = cd_unittest() self.master_key = MasterKey() self.settings = Settings() self.file_name = f'{DIR_USER_DATA}{self.settings.software_operation}_groups' self.contact_list = ContactList(self.master_key, self.settings) self.group_list = GroupList(self.master_key, self.settings, self.contact_list) self.nicks = [ 'Alice', 'Bob', 'Charlie', 'David', 'Eric', 'Fido', 'Guido', 'Heidi', 'Ivan', 'Joana', 'Karol' ] self.group_names = [ 'test_group_1', 'test_group_2', 'test_group_3', 'test_group_4', 'test_group_5', 'test_group_6', 'test_group_7', 'test_group_8', 'test_group_9', 'test_group_10', 'test_group_11' ] members = list(map(create_contact, self.nicks)) self.contact_list.contacts = members self.group_list.groups = \ [Group(name =name, group_id =group_name_to_group_id(name), log_messages =False, notifications=False, members =members, settings =self.settings, store_groups =self.group_list.store_groups) for name in self.group_names] self.single_member_data_len = ( GROUP_STATIC_LENGTH + self.settings.max_number_of_group_members * ONION_SERVICE_PUBLIC_KEY_LENGTH)
def test_class(self): # Setup settings = Settings() members = [create_contact(n) for n in ['Alice', 'Bob', 'Charlie']] sg_mock = lambda: None group = Group('testgroup', False, False, members, settings, sg_mock) # Test for c in group: self.assertIsInstance(c, Contact) self.assertEqual(len(group), 3) bytestring = group.dump_g() self.assertIsInstance(bytestring, bytes) self.assertEqual(len(bytestring), 1024 + 2 + (20 * 1024)) self.assertEqual( group.get_list_of_member_accounts(), ['*****@*****.**', '*****@*****.**', '*****@*****.**']) self.assertEqual(group.get_list_of_member_nicks(), ['Alice', 'Bob', 'Charlie']) self.assertTrue(group.has_members()) self.assertFalse(group.has_member('*****@*****.**')) group.add_members([create_contact(n) for n in ['David']]) self.assertTrue(group.has_member('*****@*****.**')) self.assertFalse(group.remove_members(['*****@*****.**'])) self.assertTrue(group.remove_members(['*****@*****.**'])) self.assertFalse(group.has_member('*****@*****.**')) # Teardown cleanup()
def create_group(name: str, nick_list: List[str] = None) -> Group: """Create a mock group object.""" if nick_list is None: nick_list = ['Alice', 'Bob'] settings = Settings() members = [create_contact(n) for n in nick_list] return Group(name, group_name_to_group_id(name), False, False, members, settings, lambda: None)
def create_group(name='testgroup', nick_list=None): """Create mock group object.""" if nick_list is None: nick_list = ['Alice', 'Bob'] settings = Settings() store_f = lambda: None contacts = [create_contact(n) for n in nick_list] return Group(name, False, False, contacts, settings, store_f)
def setUp(self): self.unittest_dir = cd_unittest() self.nicks = ['Alice', 'Bob', 'Charlie'] members = list(map(create_contact, self.nicks)) self.settings = Settings() self.group = Group(name='test_group', group_id=group_name_to_group_id('test_group'), log_messages=False, notifications=False, members=members, settings=self.settings, store_groups=lambda: None) ensure_dir(DIR_USER_DATA)
def setUp(self): self.master_key = MasterKey() self.settings = Settings() self.contact_list = ContactList(self.master_key, self.settings) self.group_list = GroupList(self.master_key, self.settings, self.contact_list) members = [create_contact(n) for n in ['Alice', 'Bob', 'Charlie', 'David', 'Eric', 'Fido', 'Guido', 'Heidi', 'Ivan', 'Joana', 'Karol']] self.contact_list.contacts = members groups = [Group(n, False, False, members, self.settings, self.group_list.store_groups) for n in ['testgroup_1', 'testgroup_2', 'testgroup_3', 'testgroup_4', 'testgroup_5', 'testgroup_6', 'testgroup_7', 'testgroup_8', 'testgroup_9', 'testgroup_10', 'testgroup_11']] self.group_list.groups = groups self.group_list.store_groups() self.single_member_data = (PADDED_UTF32_STR_LEN + (2 * BOOLEAN_SETTING_LEN) + (self.settings.max_number_of_group_members * PADDED_UTF32_STR_LEN))
def test_class(self): # Setup master_key = MasterKey() settings = Settings() contact_list = ContactList(master_key, settings) group_list = GroupList(master_key, settings, contact_list) members = [ create_contact(n) for n in [ 'Alice', 'Bob', 'Charlie', 'David', 'Eric', 'Fido', 'Gunter', 'Heidi', 'Ivan', 'Joana', 'Karol' ] ] contact_list.contacts = members groups = [ Group(n, False, False, members, settings, group_list.store_groups()) for n in [ 'testgroup_1', 'testgroup_2', 'testgroup3', 'testgroup_4', 'testgroup_5', 'testgroup_6', 'testgroup_7', 'testgroup8', 'testgroup_9', 'testgroup_10', 'testgroup_11' ] ] group_list.groups = groups group_list.store_groups() # Test for g in group_list: self.assertIsInstance(g, Group) self.assertEqual(len(group_list), 11) self.assertTrue(os.path.isfile(f'{DIR_USER_DATA}/ut_groups')) self.assertEqual(os.path.getsize(f'{DIR_USER_DATA}/ut_groups'), 24 + 32 + 20 * (1024 + 2 + (20 * 1024)) + 16) settings.m_number_of_groups = 10 settings.m_members_in_group = 10 group_list2 = GroupList(master_key, settings, contact_list) self.assertEqual(len(group_list2), 11) self.assertEqual(settings.m_number_of_groups, 20) self.assertEqual(settings.m_members_in_group, 20) bytestring = group_list2.generate_header() self.assertEqual(len(bytestring), 32) self.assertIsInstance(bytestring, bytes) dg_bytestring = group_list2.generate_dummy_group() self.assertEqual(len(dg_bytestring), (1024 + 2 + (20 * 1024))) self.assertIsInstance(dg_bytestring, bytes) members.append(create_contact('Laura')) group_list2.add_group('testgroup_12', False, False, members) group_list2.add_group('testgroup_12', False, True, members) self.assertTrue(group_list2.get_group('testgroup_12').notifications) self.assertEqual(len(group_list2), 12) self.assertEqual(group_list2.largest_group(), 12) g_names = [ 'testgroup_1', 'testgroup_2', 'testgroup3', 'testgroup_4', 'testgroup_5', 'testgroup_6', 'testgroup_7', 'testgroup8', 'testgroup_9', 'testgroup_10', 'testgroup_11', 'testgroup_12' ] self.assertEqual(group_list2.get_list_of_group_names(), g_names) g_o = group_list2.get_group('testgroup_1') self.assertIsInstance(g_o, Group) self.assertEqual(g_o.name, 'testgroup_1') self.assertTrue(group_list2.has_group('testgroup_12')) self.assertFalse(group_list2.has_group('testgroup_13')) self.assertTrue(group_list2.has_groups(), True) members = group_list2.get_group_members('testgroup_1') for c in members: self.assertIsInstance(c, Contact) self.assertEqual(len(group_list2), 12) group_list2.remove_group('testgroup_13') self.assertEqual(len(group_list2), 12) group_list2.remove_group('testgroup_12') self.assertEqual(len(group_list2), 11) self.assertIsNone(group_list2.print_groups()) # Teardown cleanup()
def setUp(self): members = list(map(create_contact, ['Alice', 'Bob', 'Charlie'])) self.settings = Settings() self.group = Group('testgroup', False, False, members, self.settings, lambda: None)
def add_group(self, name, logging, notifications, members): if self.has_group(name): self.remove_group(name) self.groups.append(Group(name, logging, notifications, members, self.settings, self.store_groups)) self.store_groups()