def initialize_db(): Base.metadata.create_all(DBSession.bind.engine) with transaction.manager: admin = User(username='******') admin.set_password('root') DBSession.add(admin) DBSession.flush()
def setUp(self): super().setUp() d = Domain(name="example.com") DBSession.add(d) DBSession.flush() self.user = User(username="******") self.user.set_password("pw") DBSession.add(self.user) self.duser = User(username="******", enabled=False) self.duser.set_password("pw") DBSession.add(self.duser) self.luser = User(username="******") self.luser.password = False DBSession.add(self.luser) self.address = Address(domain_id=d.id, local_part="test") self.address.set_password("pw") DBSession.add(self.address) DBSession.flush()
def main(argv=sys.argv): parser = ArgumentParser(description=__doc__) parser.add_argument('config') parser.add_argument('username', type=str, action='store', help="Username") command = parser.add_mutually_exclusive_group() command.add_argument('-P', '--set-password', action='store_true', help="Change user's password (prompt)") command.add_argument('-L', '--lock', action='store_true', help="Lock account") command.add_argument('-U', '--unlock', action='store_true', help="Unlock account") command.add_argument('-I', '--info', action='store_true', help="Account details (default)") command.add_argument('-A', '--add', action='store_true', help="Add account") args = parser.parse_args() config_uri = args.config setup_logging(config_uri) settings = get_appsettings(config_uri) engine = engine_from_config(settings, 'sqlalchemy.') DBSession.configure(bind=engine) with transaction.manager: if args.add: u = User(username=args.username) p = getpass.getpass() if not p: print("No password supplied, user not created.") return 1 u.set_password(p) DBSession.add(u) DBSession.flush() log('add_user', user=u.id) else: user = User.query(username=args.username).first() if not user: print("User not found.") return 1 if args.set_password: p = getpass.getpass() if not p: print("No password supplied, user not created.") return 1 user.set_password(p) log('set_password', target=user) print("Password changed") elif args.lock: user.enabled = False log('disable', target=user) print("Account locked") elif args.unlock: user.enabled = True log('enable', target=user) print("Account unlocked") else: print("Account #%d" % user.id) print("Username: %s" % user.username) print("Status: %s" % 'enabled' if user.enabled else 'disabled')
class TestAuthView(TestCase): def setUp(self): super().setUp() d = Domain(name="example.com") DBSession.add(d) DBSession.flush() self.user = User(username="******") self.user.set_password("pw") DBSession.add(self.user) self.duser = User(username="******", enabled=False) self.duser.set_password("pw") DBSession.add(self.duser) self.luser = User(username="******") self.luser.password = False DBSession.add(self.luser) self.address = Address(domain_id=d.id, local_part="test") self.address.set_password("pw") DBSession.add(self.address) DBSession.flush() def test_auth_get(self): r = views.auth(DummyRequest()) self.assertEqual(r, {}) def test_auth_redirect(self): r = views.auth(DummyRequest(user=self.user)) self.assertIsInstance(r, HTTPSeeOther) def test_auth_as_user(self): request = DummyRequest(post=dict(username="******", password="******")) r = views.auth_post(request) self.assertIsInstance(r, HTTPSeeOther) self.assertEqual(request.session["auth_type"], "User") self.assertEqual(request.session["auth_id"], self.user.id) request = DummyRequest(session=dict(auth_type="User", auth_id=42, keep="me")) r = views.logout(request) self.assertIsInstance(r, HTTPSeeOther) self.assertNotIn("auth_type", request.session) self.assertNotIn("auth_id", request.session) self.assertIn("keep", request.session) self.assertEqual(request.session["keep"], "me") def test_auth_as_address(self): request = DummyRequest(post=dict(username="******", password="******")) r = views.auth_post(request) self.assertIsInstance(r, HTTPSeeOther) self.assertEqual(request.session["auth_type"], "Address") self.assertEqual(request.session["auth_id"], self.address.id) def test_auth_fail(self): request = DummyRequest(post=dict(username="******", password="******")) r = views.auth_post(request) self.assertIsInstance(r, HTTPSeeOther) self.assertNotIn("auth_type", request.session) self.assertNotIn("auth_id", request.session) def test_auth_disabled(self): request = DummyRequest(post=dict(username="******", password="******")) r = views.auth_post(request) self.assertIsInstance(r, HTTPSeeOther) self.assertNotIn("auth_type", request.session) self.assertNotIn("auth_id", request.session) request = DummyRequest(post=dict(username="******", password="")) r = views.auth_post(request) self.assertIsInstance(r, HTTPSeeOther) self.assertNotIn("auth_type", request.session) self.assertNotIn("auth_id", request.session)