无法在django_webtest案例中重定向到@login_required网址

时间:2012-02-11 14:04:15

标签: django webtest django-testing

我正在使用 django_webtest 来测试我的应用程序。我在尝试测试注册页面时遇到了问题。此页面应创建用户并进行其他初始操作,对新创建的用户进行身份验证,并将其重定向到GET请求的 next 参数中指定的页面。

以下是视图方法的代码:

def _redirect_path(referrer, restricted_paths=()):
if not referrer or referrer in (reverse(name) for name in restricted_paths):
    return reverse('home')
else:
    return referrer

...

def sign_up(request):
if request.user.is_authenticated():
    return redirect(reverse('home'))
if request.method == 'POST':
    form = forms.SignUpForm(request.POST)
    if form.is_valid():
        with transaction.commit_on_success():
            user = form.save()
            profile = Profile.objects.create(user=user)
            tokens.create_token(profile, 'unsubscribe')
            mail.send_welcome_letter(request, user)
        messages.success(request, _('You have successfully signed up. Welcome!'))
        authenticated_user = auth.authenticate(username=user.username, password=form.cleaned_data['password'])
        if authenticated_user:
            auth.login(request, authenticated_user)
            remember_user(request, authenticated_user)
            redirect_path = _redirect_path(form.clean_referrer(), ('password_reset', 'sign_up'))
            return redirect(redirect_path)
        else:
            raise Exception("Newly created user couldn't be authenticated.")
else:
    referrer = request.GET.get('next')
    form = forms.SignUpForm(initial={'referrer': referrer})

return render_to_response('sign_up.html', {'form': form}, context_instance=RequestContext(request))

现在,当用户在浏览器中输入example.com/sign_up/?next=/settings/时,我会尝试测试其行为,正确填写表单的所有字段并提交。

查看处理/ settings /有装饰器@login_required,但是在用户成功注册后,他应该进行身份验证,所以我希望在提交用户之后会转到example.com/settings/(当我测试时他会去它手动)。

但是当我进行测试时:

def test_user_creation_redirect_to_settings(self):
    form = self.app.get('/sign_up/', {'next': '/settings/'}).form
    self.fill_sign_up_form(form)
    submit_response = form.submit()
    self.assertRedirects(submit_response, '/settings/')

它返回“AssertionError:无法检索重定向页面'/ settings /':响应代码为302(预期为200)”。当我调试时,我已经看到* submit_response *实际上是302找到了位置路径/ settings /。但是当方法 assertRedirects 尝试获取目标页面时,它再次面临重定向 - example.com/settings/重定向到example.com/login/?next=/settings/。因此用户在提交后没有登录。

好的,我尝试使用测试客户端的登录方法登录他:

def test_user_creation_redirect_to_settings(self):
    form = self.app.get('/sign_up/', {'next': '/settings/'}).form
    self.fill_sign_up_form(form)
    submit_response = form.submit()

    submit_response.client.login(username='User', password='secret')

    self.assertRedirects(submit_response, '/settings/')

但仍然如此。好像,这种方法不起作用:

def test_user_creation_redirect_to_settings(self):
    form = self.app.get('/sign_up/', {'next': '/settings/'}).form
    self.fill_sign_up_form(form)
    submit_response = form.submit()

    inside = self.client.login(username='User', password='secret')
    print inside and 'Login successful in self client'
    print 'Authenticated: %s' % bool('_auth_user_id' in self.client.session)

    inside = submit_response.client.login(username='User', password='secret')
    print inside and 'Login successful in response client'
    print 'Authenticated: %s' % bool('_auth_user_id' in submit_response.client.session)

打印

    Login successful in self client
    Authenticated: True
    Login successful in response client
    Authenticated: False

请您帮我理解为什么登录功能在测试用例中不起作用以及如何在重定向之前登录用户。

谢谢!

2 个答案:

答案 0 :(得分:4)

您是否尝试过在测试中使用follow方法?这样做是在重定向之后进行的。

form.submit().follow()

答案 1 :(得分:0)

这是在django-webtest 1.5.3中修复的django-webtest错误。