def serve(): """ Entry point for the CLI. Bootstrap a new database first if necessary, then start the webserver. """ args = parse_args() _init_logging(verbose=args.verbose, log_file=args.log_file) app = create_app(args.config_file) with app.app_context(): pwm = PWM() pwm.bootstrap(app.config['SQLALCHEMY_DATABASE_URI']) app.run(debug=args.debug, host=args.host, port=args.port)
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')
from pwm_server import create_app from os import path from pwm import PWM dev_config = path.abspath(path.join(path.dirname(__file__), 'dev_config.py')) app = create_app(dev_config) with app.app_context(): pwm = PWM() pwm.bootstrap(app.config['SQLALCHEMY_DATABASE_URI']) app.run(port=8848, debug=True)