def test_remove_contact(self): # Setup contact1 = Contact('*****@*****.**', '*****@*****.**', 'Alice', 32 * b'\x01', 32 * b'\x02', True, True, True) contact2 = Contact('*****@*****.**', '*****@*****.**', 'Charlie', 32 * b'\x01', 32 * b'\x02', True, True, True) contact_l = ContactList(MasterKey(), Settings()) contact_l.contacts = [contact1, contact2] # Test self.assertTrue(contact_l.has_contacts()) self.assertTrue(contact_l.has_contact('Alice')) self.assertTrue(contact_l.has_contact('*****@*****.**')) contact_l.remove_contact('*****@*****.**') self.assertFalse(contact_l.has_contact('Alice')) self.assertFalse(contact_l.has_contact('*****@*****.**')) contact_l.remove_contact('Charlie') self.assertEqual(len(contact_l.contacts), 0) self.assertFalse(contact_l.has_contacts())
class TestContactList(TFCTestCase): def setUp(self): self.master_key = MasterKey() self.settings = Settings() self.contact_list = ContactList(self.master_key, self.settings) self.contact_list.contacts = list( map(create_contact, ['Alice', 'Benny', 'Charlie', 'David', 'Eric'])) def tearDown(self): cleanup() def test_contact_list_iterates_over_contact_objects(self): for c in self.contact_list: self.assertIsInstance(c, Contact) def test_len_returns_number_of_contacts(self): self.assertEqual(len(self.contact_list), 5) def test_storing_and_loading_of_contacts(self): # Test store self.contact_list.store_contacts() self.assertTrue(os.path.isfile(f'{DIR_USER_DATA}ut_contacts')) self.assertEqual( os.path.getsize(f'{DIR_USER_DATA}ut_contacts'), XSALSA20_NONCE_LEN + self.settings.max_number_of_contacts * CONTACT_LENGTH + POLY1305_TAG_LEN) # Test load contact_list2 = ContactList(self.master_key, self.settings) self.assertEqual(len(contact_list2), 5) for c in contact_list2: self.assertIsInstance(c, Contact) def test_generate_dummy_contact(self): dummy_contact = ContactList.generate_dummy_contact() self.assertIsInstance(dummy_contact, Contact) self.assertEqual(len(dummy_contact.serialize_c()), CONTACT_LENGTH) def test_add_contact(self): self.assertIsNone( self.contact_list.add_contact(f'*****@*****.**', '*****@*****.**', f'Faye', FINGERPRINT_LEN * b'\x03', FINGERPRINT_LEN * b'\x04', True, True, True)) contact_list2 = ContactList(MasterKey(), Settings()) c_alice = contact_list2.get_contact('Alice') c_faye = contact_list2.get_contact('Faye') self.assertEqual(len(self.contact_list), 6) self.assertIsInstance(c_alice, Contact) self.assertEqual(c_alice.tx_fingerprint, FINGERPRINT_LEN * b'\x01') self.assertEqual(c_faye.tx_fingerprint, FINGERPRINT_LEN * b'\x03') def test_replace_existing_contact(self): c_alice = self.contact_list.get_contact('Alice') self.assertEqual(c_alice.tx_fingerprint, FINGERPRINT_LEN * b'\x01') self.assertIsNone( self.contact_list.add_contact(f'*****@*****.**', '*****@*****.**', f'Alice', FINGERPRINT_LEN * b'\x03', FINGERPRINT_LEN * b'\x04', True, True, True)) contact_list2 = ContactList(MasterKey(), Settings()) c_alice = contact_list2.get_contact('Alice') self.assertEqual(len(self.contact_list), 5) self.assertIsInstance(c_alice, Contact) self.assertEqual(c_alice.tx_fingerprint, FINGERPRINT_LEN * b'\x03') def test_remove_contact(self): self.assertTrue(self.contact_list.has_contact('Benny')) self.assertTrue(self.contact_list.has_contact('Charlie')) self.contact_list.remove_contact('*****@*****.**') self.assertFalse(self.contact_list.has_contact('Benny')) self.contact_list.remove_contact('Charlie') self.assertFalse(self.contact_list.has_contact('Charlie')) def test_get_contact(self): for selector in ['*****@*****.**', 'Benny']: contact = self.contact_list.get_contact(selector) self.assertIsInstance(contact, Contact) self.assertEqual(contact.rx_account, '*****@*****.**') def test_get_list_of_contacts(self): for c in self.contact_list.get_list_of_contacts(): self.assertIsInstance(c, Contact) def test_get_list_of_accounts(self): self.assertEqual(self.contact_list.get_list_of_accounts(), [ '*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**' ]) def test_get_list_of_nicks(self): self.assertEqual(self.contact_list.get_list_of_nicks(), ['Alice', 'Benny', 'Charlie', 'David', 'Eric']) def test_get_list_of_users_accounts(self): self.assertEqual(self.contact_list.get_list_of_users_accounts(), ['*****@*****.**']) def test_contact_selectors(self): self.assertEqual(self.contact_list.contact_selectors(), [ '*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**', '*****@*****.**', 'Alice', 'Benny', 'Charlie', 'David', 'Eric' ]) def test_has_contacts(self): self.assertTrue(self.contact_list.has_contacts()) self.contact_list.contacts = [] self.assertFalse(self.contact_list.has_contacts()) def test_has_contact(self): self.contact_list.contacts = [] self.assertFalse(self.contact_list.has_contact('Benny')) self.assertFalse(self.contact_list.has_contact('*****@*****.**')) self.contact_list.contacts = list( map(create_contact, ['Bob', 'Charlie'])) self.assertTrue(self.contact_list.has_contact('Bob')) self.assertTrue(self.contact_list.has_contact('*****@*****.**')) def test_has_local_contact(self): self.assertFalse(self.contact_list.has_local_contact()) self.contact_list.contacts.append(create_contact(LOCAL_ID)) self.assertTrue(self.contact_list.has_local_contact()) def test_contact_printing(self): self.contact_list.contacts.append(create_contact(LOCAL_ID)) self.contact_list.get_contact('Alice').log_messages = False self.contact_list.get_contact('Benny').notifications = False self.contact_list.get_contact('Charlie').file_reception = False self.contact_list.get_contact('David').tx_fingerprint = bytes( FINGERPRINT_LEN) self.assertPrints( CLEAR_ENTIRE_SCREEN + CURSOR_LEFT_UP_CORNER + """\ Contact Logging Notify Files Key Ex Account ──────────────────────────────────────────────────────────────────────────────── Alice No Yes Accept X25519 [email protected] Benny Yes No Accept X25519 [email protected] Charlie Yes Yes Reject X25519 [email protected] David Yes Yes Accept PSK [email protected] Eric Yes Yes Accept X25519 [email protected] """, self.contact_list.print_contacts)