def test_log_by_user(self): e = log('test', target=User(id=42), by_user=42) self.assertIsInstance(e, Log) self.assertNotIn('by_user', e.data) e = log('test', by_user=42) self.assertIsInstance(e, Log) self.assertIn('by_user', e.data) self.assertEqual(e.data['by_user'], 42)
def test_log(self): self.assertIsInstance(log('test'), Log) e = log('test', value=42) self.assertIsInstance(e, Log) self.assertIn('value', e.data) self.assertEqual(e.data['value'], 42) with self.assertRaises(ValueError): log('test', value=User())
def test_address_query(self): addr = Address(local_part='test', domain=Domain(name='example.com')) DBSession.add(addr) DBSession.flush() e = log('test', target=addr) r = DummyRequest(user=addr) r.appconfig.address_log_access = 'address' self.assertIn(e, get_address_log(r)) self.assertIn(e, get_address_log(r, addr)) r.appconfig.address_log_access = 'user' self.assertIsNone(get_address_log(r)) self.assertIsNone(get_address_log(r, addr)) r = DummyRequest(user=User()) r.appconfig.address_log_access = 'address' self.assertNotIn(e, get_address_log(r)) self.assertIn(e, get_address_log(r, addr)) r.appconfig.address_log_access = 'user' self.assertNotIn(e, get_address_log(r)) self.assertIn(e, get_address_log(r, addr))
def test_user_query(self): user = User(username='******') DBSession.add(user) DBSession.flush() r = DummyRequest(user=user) e = log('test', target=user) self.assertIn(e, get_user_log(r))
def test_log_target(self): self.assertIsInstance(log('test', target=User()), Log) self.assertIsInstance(log('test', target=Address()), Log) with self.assertRaises(ValueError): log('test', target=Domain())
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')