def test_post_redirects_when_logged_in(self, pyramid_config, pyramid_request): pyramid_config.testing_securitypolicy("acct:[email protected]") pyramid_request.user = mock.Mock(username='******') with pytest.raises(httpexceptions.HTTPFound): views.AuthController(pyramid_request).post()
def test_logout_invalidates_session(self, pyramid_config, pyramid_request): pyramid_request.session["foo"] = "bar" pyramid_config.testing_securitypolicy("acct:[email protected]") views.AuthController(pyramid_request).logout() assert "foo" not in pyramid_request.session
def test_logout_event(self, logoutevent, notify, pyramid_config, pyramid_request): pyramid_config.testing_securitypolicy("acct:[email protected]") views.AuthController(pyramid_request).logout() logoutevent.assert_called_with(pyramid_request) notify.assert_called_with(logoutevent.return_value)
def test_logout_response_has_forget_headers(self, pyramid_config, pyramid_request): pyramid_config.testing_securitypolicy( forget_result={'x-erase-fingerprints': 'on the hob'}) result = views.AuthController(pyramid_request).logout() assert result.headers['x-erase-fingerprints'] == 'on the hob'
def test_post_redirects_to_search_page_when_logged_in(self, pyramid_config, pyramid_request): pyramid_config.testing_securitypolicy("acct:[email protected]") pyramid_request.user = mock.Mock(username='******') with pytest.raises(httpexceptions.HTTPFound) as exc: views.AuthController(pyramid_request).post() assert exc.value.location == 'http://example.com/users/janedoe'
def test_post_redirects_to_next_param_when_logged_in(self, pyramid_config, pyramid_request): pyramid_request.params = {'next': '/foo/bar'} pyramid_config.testing_securitypolicy("acct:[email protected]") with pytest.raises(httpexceptions.HTTPFound) as e: views.AuthController(pyramid_request).post() assert e.value.location == '/foo/bar'
def test_post_redirects_to_search_page_when_logged_in(self, pyramid_config, pyramid_request): pyramid_config.testing_securitypolicy("acct:[email protected]") pyramid_request.feature.flags['search_page'] = True with pytest.raises(httpexceptions.HTTPFound) as exc: views.AuthController(pyramid_request).post() assert exc.value.location == 'http://example.com/search'
def test_post_returns_form_when_validation_fails(self, invalid_form, pyramid_config, pyramid_request): pyramid_config.testing_securitypolicy(None) # Logged out controller = views.AuthController(pyramid_request) controller.form = invalid_form() result = controller.post() assert result == {'form': 'invalid form'}
def test_post_no_event_when_validation_fails( self, loginevent, invalid_form, notify, pyramid_config, pyramid_request ): pyramid_config.testing_securitypolicy(None) # Logged out controller = views.AuthController(pyramid_request) controller.form = invalid_form() controller.post() assert not loginevent.called assert not notify.called
def test_post_redirects_when_validation_succeeds(self, factories, form_validating_to, pyramid_config, pyramid_request): pyramid_config.testing_securitypolicy(None) # Logged out controller = views.AuthController(pyramid_request) controller.form = form_validating_to( {"user": factories.User(username='******')}) result = controller.post() assert isinstance(result, httpexceptions.HTTPFound)
def test_post_event_when_validation_succeeds(self, loginevent, factories, form_validating_to, notify, pyramid_config, pyramid_request): pyramid_config.testing_securitypolicy(None) # Logged out elephant = factories.User(username='******') controller = views.AuthController(pyramid_request) controller.form = form_validating_to({"user": elephant}) controller.post() loginevent.assert_called_with(pyramid_request, elephant) notify.assert_called_with(loginevent.return_value)
def test_post_redirects_to_next_param_when_validation_succeeds( self, factories, form_validating_to, pyramid_config, pyramid_request): pyramid_request.params = {'next': '/foo/bar'} pyramid_config.testing_securitypolicy(None) # Logged out controller = views.AuthController(pyramid_request) user = factories.User(username='******') pyramid_request.user = user controller.form = form_validating_to({"user": user}) result = controller.post() assert isinstance(result, httpexceptions.HTTPFound) assert result.location == '/foo/bar'
def test_get(self, pyramid_request, LoginSchema): template_vars = views.AuthController(pyramid_request).get() # It initializes and binds the schema. LoginSchema.assert_called_once_with() schema = LoginSchema.return_value schema.bind.assert_called_once_with(request=pyramid_request) bound_schema = schema.bind.return_value # It initializes the form. pyramid_request.create_form.assert_called_once_with( bound_schema, buttons=("Log in", ), footer=Any.string.containing("Forgot your password?"), show_cancel_button=False, ) form = pyramid_request.create_form.return_value # It returns the rendered form. LoginSchema.default_values.assert_called_once_with(pyramid_request) form.render.assert_called_once_with( LoginSchema.default_values.return_value) assert template_vars == {"form": form.render.return_value}
def test_logout_redirects(self, pyramid_request): result = views.AuthController(pyramid_request).logout() assert isinstance(result, httpexceptions.HTTPFound)