def boot(self, Environ, Request, ViewClass, SessionManager, SessionConfig): self.app.bind('Session', SessionManager.driver(SessionConfig.DRIVER)) Session = self.app.make('Session') Request.session = Session ViewClass.share({ 'session': Session.helper })
def setUp(self): wsgi_request = generate_wsgi() self.app = App() self.app.bind('Environ', wsgi_request) self.app.bind('Request', Request(wsgi_request)) self.app.bind('SessionConfig', session) self.app.bind('SessionCookieDriver', SessionCookieDriver) self.app.bind('SessionMemoryDriver', SessionMemoryDriver) self.app.bind('SessionManager', SessionManager(self.app)) self.app.bind('Application', self.app)
def login(self, request: Request, session: SessionManager, validate: Validator): tech = Technician.all() tech_emails = Technician.lists('pool_tech_email') pool_tech_user = tech.where('pool_tech_email', request.input('email')).first() pw = tech.where('password', request.input('password')).first() errors = request.validate( validate.required(['email', 'password']), validate.email('email'), validate.strong('password', length=8, special=1, uppercase=1)) #checks for errors in login inputs and redirects user back to login page. if errors: return request.back().with_errors(errors).with_input() #checks to see if admin enters correct email/password credentials and if no admin account exits and needs to register for one. if request.input('email') not in tech_emails: if not any( bcrypt.checkpw(bytes(request.input('password'), 'utf-8'), bytes(pw, 'utf-8')) for pw in Technician.lists('password')): return request.back().with_errors({ 'email': [ 'Credentials not found. Please register as a new pool technician.' ] }) else: return request.back().with_errors( {'email': ['Email is incorrect!']}) elif pool_tech_user and not bcrypt.checkpw( bytes(request.input('password'), 'utf-8'), bytes(pool_tech_user.password, 'utf-8')): return request.back().with_errors( {'email': ['Password is incorrect!']}) else: session.driver('cookie').set('key', 'value') return request.redirect('/tech/dashboard/')
def test_request_validation_redirects_back_with_session(self): wsgi = generate_wsgi() self.app.bind('Application', self.app) self.app.bind('SessionCookieDriver', SessionCookieDriver) self.app.bind('Environ', wsgi) request = self.app.make('Request') request.load_environ(wsgi) request.request_variables = {'id': 1, 'name': 'Joe'} errors = request.validate(required('user')) request.session = SessionManager(self.app).driver('cookie') request.key('UKLAdrye6pZG4psVRPZytukJo2-A_Zxbo0VaqR5oig8=') self.assertEqual( request.redirect('/login').with_errors(errors).redirect_url, '/login') self.assertEqual( request.redirect('/login').with_errors(errors).session.get( 'errors'), {'user': ['The user field is required.']})
def register(self): self.app.bind('SessionConfig', session) self.app.bind('SessionMemoryDriver', SessionMemoryDriver) self.app.bind('SessionCookieDriver', SessionCookieDriver) self.app.bind('SessionManager', SessionManager(self.app))