onionr/tests/test_onionrusers.py

103 lines
3.7 KiB
Python
Raw Normal View History

2019-02-17 20:39:00 +00:00
#!/usr/bin/env python3
import sys, os
sys.path.append(".")
2019-11-21 09:26:23 +00:00
sys.path.append("src/")
2019-02-19 22:14:06 +00:00
import unittest, uuid
2019-02-17 20:39:00 +00:00
import json
TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/'
print("Test directory:", TEST_DIR)
os.environ["ONIONR_HOME"] = TEST_DIR
2020-11-22 00:43:59 +00:00
from utils import createdirs
createdirs.create_dirs()
from onionrcrypto import getourkeypair
getourkeypair.get_keypair()
2019-02-17 20:39:00 +00:00
import onionrexceptions
from onionrusers import onionrusers
from onionrusers import contactmanager
2019-07-25 16:14:13 +00:00
import onionrcrypto as crypto
from coredb import keydb
2020-11-22 00:43:59 +00:00
from utils import identifyhome
2019-02-17 20:39:00 +00:00
class OnionrUserTests(unittest.TestCase):
'''
Tests both the onionrusers class and the contactmanager (which inherits it)
'''
2019-02-17 20:39:00 +00:00
def test_users(self):
2019-07-25 16:14:13 +00:00
keypair = crypto.generate()
onionrusers.OnionrUser(keypair[0])
2019-02-17 20:39:00 +00:00
def test_contact_init_no_save(self):
2019-07-25 16:14:13 +00:00
contact = crypto.generate()[0]
contact = contactmanager.ContactManager(contact)
self.assertFalse(contact.publicKey in keydb.listkeys.list_peers())
2019-02-17 20:39:00 +00:00
def test_contact_create(self):
2019-07-25 16:14:13 +00:00
contact = crypto.generate()[0]
contact = contactmanager.ContactManager(contact, saveUser=True)
self.assertTrue(contact.publicKey in keydb.listkeys.list_peers())
2019-02-17 20:39:00 +00:00
def test_contact_set_info(self):
2019-07-25 16:14:13 +00:00
contact = crypto.generate()[0]
contact = contactmanager.ContactManager(contact, saveUser=True)
fileLocation = '%s/contacts/%s.json' % (identifyhome.identify_home(), contact.publicKey)
2019-02-17 20:39:00 +00:00
contact.set_info('alias', 'bob')
self.assertTrue(os.path.exists(fileLocation))
with open(fileLocation, 'r') as data:
data = data.read()
2019-02-17 20:39:00 +00:00
data = json.loads(data)
self.assertEqual(data['alias'], 'bob')
2019-02-17 20:39:00 +00:00
def test_contact_get_info(self):
2019-07-25 16:14:13 +00:00
contact = crypto.generate()[0]
contact = contactmanager.ContactManager(contact, saveUser=True)
fileLocation = '%s/contacts/%s.json' % (identifyhome.identify_home(), contact.publicKey)
2019-02-17 20:39:00 +00:00
with open(fileLocation, 'w') as contactFile:
contactFile.write('{"alias": "bob"}')
self.assertEqual(contact.get_info('alias', forceReload=True), 'bob')
self.assertEqual(contact.get_info('fail', forceReload=True), None)
self.assertEqual(contact.get_info('fail'), None)
def test_is_friend(self):
contact = crypto.generate()[0]
contact = onionrusers.OnionrUser(contact, saveUser=True)
self.assertFalse(contact.isFriend())
contact.setTrust(1)
self.assertTrue(contact.isFriend())
def test_encrypt(self):
2019-07-25 16:14:13 +00:00
contactPair = crypto.generate()
contact = contactmanager.ContactManager(contactPair[0], saveUser=True)
encrypted = contact.encrypt('test')
2019-07-25 16:14:13 +00:00
decrypted = crypto.encryption.pub_key_decrypt(encrypted, privkey=contactPair[1], encodedData=True).decode()
self.assertEqual('test', decrypted)
2019-02-17 20:39:00 +00:00
def test_delete_contact(self):
2019-07-25 16:14:13 +00:00
contact = crypto.generate()[0]
contact = contactmanager.ContactManager(contact, saveUser=True)
fileLocation = '%s/contacts/%s.json' % (identifyhome.identify_home(), contact.publicKey)
2019-02-17 20:39:00 +00:00
self.assertFalse(os.path.exists(fileLocation))
with open(fileLocation, 'w') as contactFile:
contactFile.write('{"alias": "test"}')
self.assertTrue(os.path.exists(fileLocation))
contact.delete_contact()
self.assertFalse(os.path.exists(fileLocation))
try:
contact.get_info('alias')
except onionrexceptions.ContactDeleted:
pass
else:
self.assertTrue(False)
try:
contact.set_info('alias', 'test2')
except onionrexceptions.ContactDeleted:
pass
else:
self.assertTrue(False)
2019-09-13 02:22:25 +00:00
unittest.main()