Exemplo n.º 1
0
    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()
Exemplo n.º 2
0
class TestGroup(unittest.TestCase):
    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 tearDown(self):
        cleanup(self.unittest_dir)

    def test_group_iterates_over_contact_objects(self):
        for c in self.group:
            self.assertIsInstance(c, Contact)

    def test_len_returns_the_number_of_members(self):
        self.assertEqual(len(self.group), len(self.nicks))

    def test_group_serialization_length_and_type(self):
        serialized = self.group.serialize_g()
        self.assertIsInstance(serialized, bytes)
        self.assertEqual(
            len(serialized),
            GROUP_STATIC_LENGTH + (self.settings.max_number_of_group_members *
                                   ONION_SERVICE_PUBLIC_KEY_LENGTH))

    def test_add_members(self):
        # Test members to be added are not already in group
        self.assertFalse(self.group.has_member(nick_to_pub_key('David')))
        self.assertFalse(self.group.has_member(nick_to_pub_key('Eric')))

        self.assertIsNone(
            self.group.add_members(
                list(map(create_contact, ['Alice', 'David', 'Eric']))))

        # Test new members were added
        self.assertTrue(self.group.has_member(nick_to_pub_key('David')))
        self.assertTrue(self.group.has_member(nick_to_pub_key('Eric')))

        # Test Alice was not added twice
        self.assertEqual(len(self.group),
                         len(['Alice', 'Bob', 'Charlie', 'David', 'Eric']))

    def test_remove_members(self):
        # Test members to be removed are part of group
        self.assertTrue(self.group.has_member(nick_to_pub_key('Alice')))
        self.assertTrue(self.group.has_member(nick_to_pub_key('Bob')))
        self.assertTrue(self.group.has_member(nick_to_pub_key('Charlie')))

        # Test first attempt to remove returns True (because Charlie was removed)
        self.assertTrue(
            self.group.remove_members(
                [nick_to_pub_key('Charlie'),
                 nick_to_pub_key('Unknown')]))

        # Test second attempt to remove returns False (because no-one was removed)
        self.assertFalse(
            self.group.remove_members(
                [nick_to_pub_key('Charlie'),
                 nick_to_pub_key('Unknown')]))

        # Test Charlie was removed
        self.assertFalse(self.group.has_member(nick_to_pub_key('Charlie')))

        # Test no other members were removed
        self.assertTrue(self.group.has_member(nick_to_pub_key('Alice')))
        self.assertTrue(self.group.has_member(nick_to_pub_key('Bob')))

    def test_get_list_of_member_pub_keys(self):
        self.assertEqual(first=self.group.get_list_of_member_pub_keys(),
                         second=[
                             nick_to_pub_key('Alice'),
                             nick_to_pub_key('Bob'),
                             nick_to_pub_key('Charlie')
                         ])

    def test_has_member(self):
        self.assertTrue(self.group.has_member(nick_to_pub_key('Charlie')))
        self.assertFalse(self.group.has_member(nick_to_pub_key('David')))

    def test_has_members(self):
        self.assertFalse(self.group.empty())
        self.group.members = []
        self.assertTrue(self.group.empty())
Exemplo n.º 3
0
class TestGroup(unittest.TestCase):

    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 tearDown(self):
        cleanup()

    def test_group_iterates_over_contact_objects(self):
        for c in self.group:
            self.assertIsInstance(c, Contact)

    def test_len_returns_number_of_members(self):
        self.assertEqual(len(self.group), 3)

    def test_serialize_g(self):
        serialized = self.group.serialize_g()
        self.assertIsInstance(serialized, bytes)
        self.assertEqual(len(serialized),
                         PADDED_UTF32_STR_LEN
                         + (2 * BOOLEAN_SETTING_LEN)
                         + (self.settings.max_number_of_group_members * PADDED_UTF32_STR_LEN))

    def test_add_members(self):
        self.group.members = []
        self.assertFalse(self.group.has_member('*****@*****.**'))
        self.assertFalse(self.group.has_member('*****@*****.**'))

        self.group.add_members([create_contact(n) for n in ['David', 'Eric']])
        self.assertTrue(self.group.has_member('*****@*****.**'))
        self.assertTrue(self.group.has_member('*****@*****.**'))

    def test_remove_members(self):
        self.assertTrue(self.group.has_member('*****@*****.**'))
        self.assertTrue(self.group.has_member('*****@*****.**'))
        self.assertTrue(self.group.has_member('*****@*****.**'))

        self.assertTrue(self.group.remove_members(['*****@*****.**', '*****@*****.**']))
        self.assertFalse(self.group.remove_members(['*****@*****.**', '*****@*****.**']))

        self.assertTrue(self.group.has_member('*****@*****.**'))
        self.assertTrue(self.group.has_member('*****@*****.**'))
        self.assertFalse(self.group.has_member('*****@*****.**'))

    def test_get_list_of_member_accounts(self):
        self.assertEqual(self.group.get_list_of_member_accounts(),
                         ['*****@*****.**', '*****@*****.**', '*****@*****.**'])

    def test_get_list_of_member_nicks(self):
        self.assertEqual(self.group.get_list_of_member_nicks(), ['Alice', 'Bob', 'Charlie'])

    def test_has_member(self):
        self.assertTrue(self.group.has_member('*****@*****.**'))
        self.assertFalse(self.group.has_member('*****@*****.**'))

    def test_has_members(self):
        self.assertTrue(self.group.has_members())
        self.group.members = []
        self.assertFalse(self.group.has_members())