From 3638db4895be145bedb550f3bf42b1a530037b52 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sun, 17 Feb 2019 14:39:00 -0600 Subject: [PATCH] added test for onionrusers --- onionr/__init__.py | 0 onionr/tests/test_onionrusers.py | 84 ++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 onionr/__init__.py create mode 100644 onionr/tests/test_onionrusers.py diff --git a/onionr/__init__.py b/onionr/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/onionr/tests/test_onionrusers.py b/onionr/tests/test_onionrusers.py new file mode 100644 index 00000000..152c5882 --- /dev/null +++ b/onionr/tests/test_onionrusers.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python3 +import sys, os +sys.path.append(".") +import unittest, uuid, hashlib +import json +TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/' +print("Test directory:", TEST_DIR) +os.environ["ONIONR_HOME"] = TEST_DIR +import core, onionr + +c = core.Core() +import onionrexceptions +from onionrusers import onionrusers +from onionrusers import contactmanager + +class OnionrUserTests(unittest.TestCase): + ''' + Tests both the onionrusers class and the contactmanager (which inherits it) + ''' + + def test_users(self): + keypair = c._crypto.generatePubKey() + onionrusers.OnionrUser(c, keypair[0]) + return + + def test_contact_init_no_save(self): + contact = c._crypto.generatePubKey()[0] + contact = contactmanager.ContactManager(c, contact) + self.assertFalse(contact.publicKey in c.listPeers()) + + def test_contact_create(self): + contact = c._crypto.generatePubKey()[0] + contact = contactmanager.ContactManager(c, contact, saveUser=True) + self.assertTrue(contact.publicKey in c.listPeers()) + + def test_contact_set_info(self): + contact = c._crypto.generatePubKey()[0] + contact = contactmanager.ContactManager(c, contact, saveUser=True) + fileLocation = '%s/contacts/%s.json' % (c.dataDir, contact.publicKey) + contact.set_info('alias', 'bob') + self.assertTrue(os.path.exists(fileLocation)) + + with open(fileLocation, 'r') as data: + data = data.read() + + data = json.loads(data) + self.assertTrue(data['alias'] == 'bob') + + def test_contact_get_info(self): + contact = c._crypto.generatePubKey()[0] + contact = contactmanager.ContactManager(c, contact, saveUser=True) + fileLocation = '%s/contacts/%s.json' % (c.dataDir, contact.publicKey) + + with open(fileLocation, 'w') as contactFile: + contactFile.write('{"alias": "bob"}') + + self.assertTrue(contact.get_info('alias', forceReload=True) == 'bob') + self.assertTrue(contact.get_info('fail', forceReload=True) == None) + self.assertTrue(contact.get_info('fail') == None) + + def test_delete_contact(self): + contact = c._crypto.generatePubKey()[0] + contact = contactmanager.ContactManager(c, contact, saveUser=True) + fileLocation = '%s/contacts/%s.json' % (c.dataDir, contact.publicKey) + 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) + +unittest.main() \ No newline at end of file