I wrote a login_required
decorator for the pyramid web framework. In a pyramid test server it works well.
But in the Pyramid unit tests for the @view_config
decorator do not work for all configurations (not only the decorator parameter).
This is the code:
class MyViews(object):
@view_config(decorator=login_required(login_url=LOGIN_URL),
match_param="action=change_password", request_method="GET",
renderer="accounts/change_password.jinja2")
def change_password(self):
form = ChangePwdForm()
return {'form': form}
Here is the test code:
def test_change_password_fail(self):
from .views import AccountsViews
aviews = AccountsViews(testing.DummyRequest(path='/accounts/forget_password'))
response = aviews.forget_password()
self.assertEqual(response.status_code, 307) #HTTPTemporaryRedirect
What i excepted was that the not-logined-user
will be redirected to login url.
All paramenters in @view_config
such as renderer
and 'match_param' do not work.
How can I solve this problem?
References:
Mocking render to response with Pyramid
Unit and Integration Testing:Pyramid official guide,but not refer to class-based view's decorator problem
@view_config()
does not get applied until you run aconfig.scan()
.When you are doing testing, in general you want to test a single unit, in this case the view without worrying about the rest of the framework.
You'd test your view and your decorator separately.
Once you get to the higher level, and want to test that Pyramid is the doing the right thing for your views, you will want to do integration testing. With integration testing you will set up the full configurator object, and the full app, it is more heavy handed, but allows you to test that Pyramid applies the decorator.
The last tests you would want are full end 2 end tests that emulate the full application. The latest documentation is available at: http://docs.pylonsproject.org/docs/pyramid/en/latest/narr/testing.html