Exemplo n.º 1
0
 def test_newrepo(self):
     nrepo = Repository('testrepo', 'mypass')
     self.assertFalse(nrepo.is_decrypted())
     nrepo.store()
     import os
     self.assertTrue(os.path.exists('repositories/testrepo.repo'))
     os.remove('repositories/testrepo.repo')
     self.assertFalse(os.path.exists('repositories/testrepo.repo'))
Exemplo n.º 2
0
 def test_loadstorerepo(self):
     erepo = Repository('testrepo', '1212')
     self.assertFalse(erepo.is_decrypted())
     erepo.load()
     self.assertTrue(erepo.is_decrypted())
     erepo.store()
     self.assertFalse(erepo.is_decrypted())
     import os
     self.assertTrue(os.path.exists('repositories/testrepo.repo'))
     os.remove('repositories/testrepo.repo')
     self.assertFalse(os.path.exists('repositories/testrepo.repo'))
Exemplo n.º 3
0
class RepoFTest(unittest.TestCase):
    """
    Class for testing repo class methods
    """

    # Set up for testing
    def setUp(self):
        #Set up a repository for testing
        self.repo = Repository('testrepo', '1212')  #New repo
        self.repo.load()

    def test_wrongkeytype(self):
        with self.assertRaises(AssertionError):
            self.repo[1212] = {"Password": "******"}

    # Test len function of repo
    def test_lenrepo(self):
        l = len(self.repo)
        self.assertEqual(l, 0)
        self.repo['google'] = {'Password': '******'}
        l = len(self.repo)
        self.assertEqual(l, 1)

    # Test getting item from repo
    def test_getitem(self):
        self.repo['mozilla'] = {'Password': '******'}
        self.repo['writelatex'] = {'Password': '******'}
        ret1 = self.repo['mozilla']
        ret2 = self.repo['writelatex']
        self.assertIn(member='mozilla', container=self.repo)
        self.assertIn(member='writelatex', container=self.repo)
        self.assertEqual(ret1, {'Password': '******'})
        self.assertEqual(ret2, {'Password': '******'})
        # Check if Non existant key is checked
        with self.assertRaises(KeyError):
            ret = self.repo['github']

    # Test setting item in repo
    def test_setitem(self):
        l = len(self.repo)
        self.repo['twitter'] = {'Password': '******'}
        self.assertIn(member='twitter', container=self.repo)
        self.assertEqual(self.repo['twitter'], {'Password': '******'})
        self.assertEqual(len(self.repo), l + 1)
        # Check if ValueError is raised
        with self.assertRaises(ValueError):
            self.repo['atom'] = None
        # Check for duplicate key
        with self.assertRaises(KeyError):
            self.repo['twitter'] = {'Password': '******'}

    # Test deleting item from repo
    def test_delitem(self):
        # Existing item deletion
        self.repo['ubuntu'] = {'Password': '******'}
        l = len(self.repo)
        del self.repo['ubuntu']
        self.assertNotIn(member='ubuntu', container=self.repo)
        self.assertEqual(len(self.repo), l - 1)
        # Non-existing item deletion
        with self.assertRaises(KeyError):
            del self.repo['whatsapp']

    # Test writing too many accounts to repository
    def test_overinsert(self):
        oldlen = int(Repository.config.get('accounts.max', 100))
        Repository.config['accounts.max'] = len(self.repo)
        try:
            self.repo['hooli'] = {"Password": "******"}
        except ValueError:
            pass
        else:
            self.fail(
                "Did not raise a ValueError when writing too many accounts")
        finally:
            Repository.config['accounts.max'] = oldlen

    # Test keys function
    def test_keyset(self):
        self.repo['a'] = {'Password': '******'}
        self.repo['b'] = {'Password': '******'}
        self.repo['c'] = {'Password': '******'}
        self.repo['d'] = {'Password': '******'}

        keys = self.repo.keys()
        self.assertEqual(keys, set(['a', 'b', 'c', 'd']))

        del self.repo['a']
        del self.repo['b']
        del self.repo['c']
        del self.repo['d']

    # Test values() function
    def test_valueset(self):
        with self.assertRaises(NotImplementedError):
            values = self.repo.values()

    # Test items() function
    def test_itemset(self):
        self.repo['a'] = {'Password': '******'}
        self.repo['b'] = {'Password': '******'}

        items = self.repo.items()
        self.assertEqual(sorted(list(items)), [('a', {
            'Password': '******'
        }), ('b', {
            'Password': '******'
        })])

        del self.repo['a']
        del self.repo['b']

    # Test representaion and stringing
    def test_strrepr(self):
        self.assertEqual(str(self.repo), 'testrepo')
        self.assertEqual(
            repr(self.repo),
            'Encrypted repository: testrepo loaded from repositories/testrepo.repo'
        )

    # Check clearing
    def test_clear(self):
        self.repo.clear()
        self.assertEqual(len(self.repo), 0)

    # Test iterator
    def test_iter(self):
        self.repo['a'] = {'Password': '******'}
        self.repo['b'] = {'Password': '******'}
        self.repo['c'] = {'Password': '******'}
        self.repo['d'] = {'Password': '******'}
        keys = set()
        for x in self.repo:
            keys.add(x)
        self.assertEqual(keys, set(['a', 'b', 'c', 'd']))
        del self.repo['a']
        del self.repo['b']
        del self.repo['c']
        del self.repo['d']

    # Done with testing, destroy repo
    def tearDown(self):
        self.repo.store()
        import os
        os.remove('repositories/testrepo.repo')
Exemplo n.º 4
0
 def setUp(self):
     repo = Repository('myrepo', 'rounak')
     repo.load()
     repo['mylife'] = {'Password': '******'}
     repo.store()