import deform import pytest from pyramid.httpexceptions import HTTPFound from pyramid.testing import DummyRequest, DummyResource from .. import ( # noqa: F401 app_config, dbsession, get_example_user, get_post_request ) def test_forgotten_password_form(): ''' test the view for the forgotten password form ''' from ordr.resources.account import PasswordResetResource from ordr.views.forgotten_password import forgotten_password_form request = DummyRequest() parent = DummyResource(request=request) context = PasswordResetResource(name=None, parent=parent) result = forgotten_password_form(context, None) assert result == {'formerror': False} @pytest.mark.parametrize( # noqa: F811 'identifier', ['TerryGilliam', 'gilliam@example.com', 'Gilliam@Example.com'] ) def test_forgotten_password_processing_ok(dbsession, identifier): ''' test the processing of the forgotten password form ''' from ordr.models.account import Role, TokenSubject from ordr.resources.account import PasswordResetResource from ordr.views.forgotten_password import ( forgotten_password_form_processing ) user = get_example_user(Role.USER) dbsession.add(user) dbsession.flush() post_data = { 'identifier': identifier, 'send_mail': 'send_mail', } request = DummyRequest(dbsession=dbsession, POST=post_data) parent = DummyResource(request=request) context = PasswordResetResource(name=None, parent=parent) result = forgotten_password_form_processing(context, request) assert isinstance(result, HTTPFound) assert result.location == 'http://example.com//verify' # a token should be created token = user.tokens[0] assert token.subject == TokenSubject.RESET_PASSWORD # a verification email should be sent # this is tested in the functional test since request.registry.notify # doesn't know about event subscribers in the unittest @pytest.mark.parametrize( # noqa: F811 'identifier', ['', 'GrahamChapman', 'unknown@example.com'] ) def test_forgotten_password_processing_not_ok(dbsession, identifier): ''' test error processing of the forgotten password form ''' from ordr.models.account import Role, Token from ordr.resources.account import PasswordResetResource from ordr.views.forgotten_password import ( forgotten_password_form_processing ) user = get_example_user(Role.UNVALIDATED) dbsession.add(user) dbsession.flush() post_data = { 'identifier': identifier, 'send_mail': 'send_mail', } request = DummyRequest(dbsession=dbsession, POST=post_data) parent = DummyResource(request=request) context = PasswordResetResource(name=None, parent=parent) result = forgotten_password_form_processing(context, request) assert result == {'formerror': True} assert dbsession.query(Token).count() == 0 def test_forgotten_password_processing_cancel(dbsession): ''' test the canceling of the forgotten password form ''' from ordr.models.account import Token from ordr.resources.account import PasswordResetResource from ordr.views.forgotten_password import ( forgotten_password_form_processing ) post_data = { 'identifier': 'TerryGilliam', 'cancel': 'cancel', } request = DummyRequest(dbsession=dbsession, POST=post_data) parent = DummyResource(request=request) context = PasswordResetResource(name=None, parent=parent) result = forgotten_password_form_processing(context, request) assert isinstance(result, HTTPFound) assert result.location == 'http://example.com//' assert dbsession.query(Token).count() == 0 def test_verify(): from ordr.views.forgotten_password import verify result = verify(None, None) assert result == {}