Using session object in Django unit test -
i writing login view , add together unit test view. view looks this:
def login(request): if request.post: usrname = request.post.get('username') password = request.post.get('password') user = authenticate(username=usrname, password=password) if user not none: auth_login(request, user) homecoming redirect('/core/home/') else: context = {'error_message': "invalid username or password"} homecoming render(request, 'core/login.html', context) else: c = {} c.update(csrf(request)) homecoming render_to_response('core/login.html',c) def home(request): if request.user.is_authenticated(): context = {'user' : request.user} homecoming render(request, 'core/home.html', context) else: homecoming render(request, 'core/login.html')
and unit test looks this:
class coreviewtests(testcase): def setup(self): self.factory = requestfactory() def test_login_view_with_valid_user(self): uf = userfactory() user = uf.make_user("validuser1", "12345", "user@abc.com") self.assertequal(user.username, "validuser1") request = self.factory.post('/core/login', {"username": "validuser1", "password": "12345"}) response = login(request) self.assertequal(response.status_code, 200)
the unit test crash because cannot find session object. follow couple tutorial on websites defining dummy session dictionary doesn't help.
can shed lite me how write unit test view need deal session object?
thanks.
from documentation requestfactory
object:
it not back upwards middleware. session , authentication attributes must supplied test if required view function properly.
you seek manually setting request.session
dictionary appropriate stuff in it, say. might turn out easier utilize old-fashioned django test client though.
django session
No comments:
Post a Comment