class TestPwm(TestCase): def setUp(self): self.db_path = os.path.join(os.path.dirname(__file__), "tmp.dat") self.p = PWM(key='123456', db_path=self.db_path) def tearDown(self): if os.path.exists(self.db_path): os.unlink(os.path.join(os.path.dirname(__file__), "tmp.dat")) def test_gen_passwd(self): passwd = self.p.gen_passwd('1234') self.assertTrue(re.search(r"[0-9]", passwd) is not None) self.assertTrue(re.search(r"[a-z]", passwd) is not None) self.assertTrue(re.search(r"[A-Z]", passwd) is not None) def test_insert(self): self.p.insert(domain='github.com', account='lovedboy', batch='') res = self.p._query_account('github.com') self.assertEqual(res, [(1, 'github.com', 'lovedboy', '')]) def test_search(self): self.p.insert(domain='github.com', account='lovedboy', batch='') self.p.search('*') def test_delete(self): self.p.insert(domain='github.com', account='lovedboy', batch='') res = self.p._query_account('github.com') self.assertEqual(res, [(1, 'github.com', 'lovedboy', '')]) self.p.delete(1) res = self.p._query_account('github.com') self.assertEqual(res, []) def test_gen_account_password(self): p = self.p.gen_account_passwd('github.com', 'lovedboy', '') self.assertEqual(p, 'lcrv5vttjqOk7c3')
class PWMCoreTest(unittest.TestCase): def setUp(self): self.tmp_db = tempfile.NamedTemporaryFile(delete=False) self.tmp_db.close() db = sa.create_engine('sqlite:///%s' % self.tmp_db.name) Base.metadata.create_all(db) DBSession = sessionmaker(bind=db) self.session = DBSession() self.session.add(Domain(name='example.com', salt=b'NaCl')) self.session.add(Domain(name='otherexample.com', salt=b'supersalty')) self.session.add(Domain(name='facebook.com', salt=b'notsomuch')) self.session.commit() self.pwm = PWM() self.pwm.bootstrap(self.tmp_db.name) def tearDown(self): os.remove(self.tmp_db.name) def test_get_domain(self): # test getting existing domain domain = self.pwm.get_domain('example.com') self.assertEqual(domain.salt, b'NaCl') self.assertEqual(domain.name, 'example.com') # test nonexisting domain self.assertRaises(NoSuchDomainException, self.pwm.get_domain, 'neverheardofthis') def test_add_domain(self): new_domain = self.pwm.create_domain('othersite.com') key = new_domain.derive_key('secret') # should now get the same key on second attempt fetched_domain = self.pwm.get_domain('othersite.com') self.assertEqual(fetched_domain.derive_key('secret'), key) def test_domain_search(self): results = self.pwm.search('example') self.assertEqual(len(results), 2) results = self.pwm.search('bank') self.assertEqual(len(results), 0) def test_no_duplicates(self): # PY26: If we drop support for python 2.6, this can be rewritten to use assertRaises as a # context manager, which is better for readability self.assertRaises(DuplicateDomainException, self.pwm.create_domain, 'example.com') def test_modify_domain(self): domain = self.pwm.get_domain('example.com') old_key = domain.derive_key('secret') modified_domain = self.pwm.modify_domain('example.com', new_salt=True, username='******') self.assertNotEqual(old_key, modified_domain.derive_key('secret')) self.assertEqual(modified_domain.username, '*****@*****.**') def test_modify_nonexistent_domain(self): self.assertRaises(NoSuchDomainException, self.pwm.modify_domain, 'neverheardofthis')