def test_forgot_password_invalid_password(self): from horus.views import ForgotPasswordController from pyramid_mailer.interfaces import IMailer from pyramid_mailer.mailer import DummyMailer from horus.interfaces import IUserClass from horus.tests.models import User self.config.registry.registerUtility(User, IUserClass) self.config.add_route('index', '/') self.config.include('horus') self.config.registry.registerUtility(DummyMailer(), IMailer) user = User(username='******', password='******', email='*****@*****.**') user.password = '******' self.session.add(user) self.session.flush() request = self.get_csrf_request(post={ 'email': 'sontek' }, request_method='POST') request.user = None view = ForgotPasswordController(request) response = view.forgot_password() assert len(response['errors']) == 1
def test_forgot_password_valid_user(self): from horus.views import ForgotPasswordController from pyramid_mailer.interfaces import IMailer from pyramid_mailer.mailer import DummyMailer from horus.interfaces import IUserClass from horus.tests.models import User self.config.registry.registerUtility(User, IUserClass) self.config.add_route('index', '/') self.config.include('horus') self.config.registry.registerUtility(DummyMailer(), IMailer) user = User(username='******', password='******', email='*****@*****.**') user.password = '******' self.session.add(user) self.session.flush() request = self.get_csrf_request(post={ 'email': '*****@*****.**' }, request_method='POST') request.user = None flash = Mock() request.session.flash = flash view = ForgotPasswordController(request) response = view.forgot_password() flash.assert_called_with( 'Please check your e-mail to reset your password.', 'success') assert response.status_int == 302
def test_forgot_password_valid_user(self): from horus.views import ForgotPasswordController from pyramid_mailer.interfaces import IMailer from pyramid_mailer.mailer import DummyMailer from horus.interfaces import IUserClass from horus.tests.models import User self.config.registry.registerUtility(User, IUserClass) self.config.add_route('index', '/') self.config.include('horus') self.config.registry.registerUtility(DummyMailer(), IMailer) user = User(username='******', password='******', email='*****@*****.**') user.password = '******' self.session.add(user) self.session.flush() request = self.get_csrf_request(post={ 'email': '*****@*****.**' }, request_method='POST') request.user = None view = ForgotPasswordController(request) with patch('horus.views.FlashMessage') as FlashMessage: response = view.forgot_password() FlashMessage.assert_called_with(request, view.Str.reset_password_email_sent, kind="success") assert response.status_int == 302
def test_reset_password_valid_user(self): from horus.views import ForgotPasswordController from hem.interfaces import IDBSession from horus.events import PasswordResetEvent from pyramid_mailer.interfaces import IMailer from pyramid_mailer.mailer import DummyMailer from horus.models import crypt from horus.interfaces import IHorusUserClass from horus.interfaces import IHorusActivationClass from horus.tests.models import User from horus.tests.models import Activation self.config.registry.registerUtility(User, IHorusUserClass) self.config.registry.registerUtility(Activation, IHorusActivationClass) self.config.add_route('index', '/') self.config.include('horus') self.config.registry.registerUtility(DummyMailer(), IMailer) user = User(username='******', email='*****@*****.**') user.set_password('foo') user.activation = Activation() self.session.add(user) self.session.flush() request = self.get_csrf_request(post={ 'Password': { 'Password': '******', 'Password-confirm': 'test123', }, }, request_method='POST') request.matchdict = Mock() get = Mock() get.return_value = user.activation.code request.matchdict.get = get request.user = None flash = Mock() request.session.flash = flash def handle_password_reset(event): request = event.request session = request.registry.getUtility(IDBSession) session.commit() self.config.add_subscriber(handle_password_reset, PasswordResetEvent) view = ForgotPasswordController(request) response = view.reset_password() assert not crypt.check(user.password, 'temp' + user.salt) assert response.status_int == 302
def test_reset_password_valid_user(self): from horus.views import ForgotPasswordController from hem.interfaces import IDBSession from horus.events import PasswordResetEvent from pyramid_mailer.interfaces import IMailer from pyramid_mailer.mailer import DummyMailer from horus.models import crypt from horus.interfaces import IUserClass from horus.interfaces import IActivationClass from horus.tests.models import User from horus.tests.models import Activation self.config.registry.registerUtility(User, IUserClass) self.config.registry.registerUtility(Activation, IActivationClass) self.config.add_route('index', '/') self.config.include('horus') self.config.registry.registerUtility(DummyMailer(), IMailer) user = User(username='******', email='*****@*****.**') user.password = '******' user.activation = Activation() self.session.add(user) self.session.flush() request = self.get_csrf_request(post={ 'password': { 'password': '******', 'password-confirm': 'test123', }, }, request_method='POST') request.matchdict = Mock() get = Mock() get.return_value = user.activation.code request.matchdict.get = get request.user = None def handle_password_reset(event): request = event.request session = request.registry.getUtility(IDBSession) session.commit() self.config.add_subscriber(handle_password_reset, PasswordResetEvent) view = ForgotPasswordController(request) response = view.reset_password() assert not crypt.check(user.password, 'temp' + user.salt) assert response.status_int == 302
def test_forgot_password_logged_in_redirects(self): from horus.views import ForgotPasswordController from horus.interfaces import IUserClass from horus.tests.models import User self.config.registry.registerUtility(User, IUserClass) self.config.add_route('index', '/') self.config.include('horus') request = testing.DummyRequest() request.user = Mock() view = ForgotPasswordController(request) response = view.forgot_password() assert response.status_int == 302
def test_reset_password_invalid_password(self): from horus.views import ForgotPasswordController from pyramid_mailer.interfaces import IMailer from pyramid_mailer.mailer import DummyMailer from horus.interfaces import IUserClass from horus.interfaces import IActivationClass from horus.tests.models import User from horus.tests.models import Activation self.config.registry.registerUtility(User, IUserClass) self.config.registry.registerUtility(Activation, IActivationClass) self.config.add_route('index', '/') self.config.include('horus') self.config.registry.registerUtility(DummyMailer(), IMailer) user = User(username='******', password='******', email='*****@*****.**') user.password = '******' user.activation = Activation() self.session.add(user) self.session.flush() request = self.get_csrf_request(post={ 'Password': { 'Password': '******', 'Password-confirm': 't', }, }, request_method='POST') request.matchdict = Mock() get = Mock() get.return_value = user.activation.code request.matchdict.get = get request.user = None flash = Mock() request.session.flash = flash view = ForgotPasswordController(request) response = view.reset_password() assert len(response['errors']) == 1
def test_forgot_password_loads(self): from horus.views import ForgotPasswordController from horus.interfaces import IUserClass from horus.tests.models import User from horus.interfaces import IActivationClass from horus.tests.models import Activation self.config.registry.registerUtility(Activation, IActivationClass) self.config.registry.registerUtility(User, IUserClass) self.config.add_route('index', '/') self.config.include('horus') request = testing.DummyRequest() request.user = None view = ForgotPasswordController(request) response = view.forgot_password() assert response.get('form', None)
def test_reset_password_invalid_password(self): from horus.views import ForgotPasswordController from pyramid_mailer.interfaces import IMailer from pyramid_mailer.mailer import DummyMailer from horus.interfaces import IUserClass from horus.interfaces import IActivationClass from horus.tests.models import User from horus.tests.models import Activation self.config.registry.registerUtility(User, IUserClass) self.config.registry.registerUtility(Activation, IActivationClass) self.config.add_route('index', '/') self.config.include('horus') self.config.registry.registerUtility(DummyMailer(), IMailer) user = User(username='******', password='******', email='*****@*****.**') user.password = '******' user.activation = Activation() self.session.add(user) self.session.flush() request = self.get_csrf_request(post={ 'Password': { 'Password': '******', 'Password-confirm': 't', }, }, request_method='POST') request.matchdict = Mock() get = Mock() get.return_value = user.activation.code request.matchdict.get = get request.user = None view = ForgotPasswordController(request) response = view.reset_password() assert len(response['errors']) == 1
def test_reset_password_loads(self): from horus.views import ForgotPasswordController from pyramid_mailer.interfaces import IMailer from pyramid_mailer.mailer import DummyMailer from horus.interfaces import IUserClass from horus.tests.models import User from horus.tests.models import Activation from horus.interfaces import IActivationClass self.config.registry.registerUtility(User, IUserClass) self.config.registry.registerUtility(Activation, IActivationClass) self.config.add_route('index', '/') self.config.include('horus') self.config.registry.registerUtility(DummyMailer(), IMailer) user = User(username='******', password='******', email='*****@*****.**') user.password = '******' user.activation = Activation() self.session.add(user) self.session.flush() request = testing.DummyRequest() request.matchdict = Mock() get = Mock() get.return_value = user.activation.code request.matchdict.get = get request.user = None view = ForgotPasswordController(request) response = view.reset_password() assert response.get('form', None) assert 'sontek' in response['form']
def forgot(self): request = self.request controller = ForgotPasswordController(request) form = self.forgot_form result = controller.forgot_password() if isinstance(result, dict): if 'errors' in result: error = colander.Invalid( form.schema, messages.INVALID_FORM ) error.children = result.pop('errors') form.widget.handle_error(form, error) result = { 'form': { 'forgot': form.render() } } else: # TODO: take care of flash success message return None return result
def password(self): request = self.request action = request.params.get('action', 'forgot') if action == 'forgot': schema = request.registry.getUtility(IHorusForgotPasswordSchema) form = request.registry.getUtility(IHorusForgotPasswordForm) elif action == 'reset': schema = request.registry.getUtility(IHorusResetPasswordSchema) form = request.registry.getUtility(IHorusResetPasswordForm) else: raise HTTPBadRequest() controller = ForgotPasswordController(request) schema = schema().bind(request=self.request) form = form(schema) form.formid = 'password' form.use_ajax = True form.ajax_options = self.ajax_options if request.POST.get('__formid__', '') == 'password': result = getattr(controller, '%s_password' % action)() error = request.session.pop_flash('error') if isinstance(result, dict): if error: form.error = colander.Invalid(form.schema, error[0]) result = {'form': form.render()} else: return result return dict(password=result) lm = request.layout_manager lm.layout.add_form(form) return dict(password={'form': form.render()})
def forgot(self): result = ForgotPasswordController(self.request).forgot_password() return self.respond(result)