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 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 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 setUp(self): super().setUp() self.domain = d = Domain(name='example.com', catchall='catchall') DBSession.add(d) DBSession.flush() self.mailbox = Address(domain_id=d.id, local_part='mb') self.mailbox.set_password('pw') self.redirect = Address(domain_id=d.id, local_part='alias', redirect='redirect@test') self.redirect.set_password('pw') DBSession.add_all([self.mailbox, self.redirect]) DBSession.flush()
def test_init(self): u = User(username='******') DBSession.add(u) DBSession.flush() l = Log(type='test', target=u, data=dict(value=True)) DBSession.add(l) DBSession.flush() self.assertEqual(l.type, 'test') self.assertEqual(l.target, u) self.assertEqual(l.user, u) self.assertEqual(l.user_id, u.id) self.assertIn('value', l.data) self.assertEqual(l.data['value'], True)
def setUp(self): super().setUp() d = Domain(name="example.com") DBSession.add(d) DBSession.flush() self.address = Address(domain_id=d.id, local_part="test") self.address.set_password("pw") DBSession.add(self.address) DBSession.flush() self.valid_token = PwResetToken(address_id=self.address.id) self.expired_token = PwResetToken(address_id=self.address.id) self.expired_token.create_date = datetime.now() - timedelta(days=10) DBSession.add_all([self.valid_token, self.expired_token]) 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('csvfile', type=str, action='store', help="CSV file") parser.add_argument('-q', dest='quiet', action='store_true', help="Dont print every address") commit = parser.add_mutually_exclusive_group() commit.add_argument('-n', dest='commit_never', action='store_true', help="Dont commit changes and dont ask") commit.add_argument('-y', dest='commit_always', action='store_true', help="Commit changes without asking") 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) transaction.begin() known_domains = {} counter = 0 with open(args.csvfile, 'r') as csvfile: reader = csv.reader(csvfile) for row in reader: local, password, redirect, dname = row if dname in known_domains: domain = known_domains[dname] else: try: domain = Domain.query(name=dname).one() except NoResultFound: domain = Domain(name=dname) DBSession.add(domain) known_domains[domain.name] = domain if not local: if not redirect: print('Invalid address: catch-all without redirect') return 1 domain.catchall = redirect print('*@%s -> %s' % (domain.name, domain.catchall)) continue address = Address() address.local_part = local address.password = password or None address.redirect = redirect or None address.domain = domain address.domain_id = domain.id DBSession.add(address) if not args.quiet: print(address.address, end='') if address.password: print(' [...%s]' % address.password[-8:], end='') if address.redirect: print(' -> %s' % address.redirect, end='') print() counter += 1 if args.commit_always: transaction.commit() print('saved.') elif args.commit_never: transaction.abort() print('aborted.') else: r = input('Commit %d addresses? ' % counter) if r.strip().lower() == 'y': transaction.commit() print('saved.') else: transaction.abort() print('aborted.')
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')