简体   繁体   English

Django - 测试登录视图 - AttributeError: 'HttpRequest' 对象没有属性 'user'

[英]Django - Testing Login View - AttributeError: 'HttpRequest' object has no attribute 'user'

I'm trying to test Django Login View with TestCase but I'm getting the following error -我正在尝试使用TestCase测试 Django 登录视图,但出现以下错误 -

ERROR: test_login_form (accounts.tests.LoginPageTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/farhan/Documents/_playground_new/django_playground/cbv-dfb/accounts/tests.py", line 53, in test_login_form
    logged_in = self.client.force_login(new_user)
  File "/home/farhan/Documents/_playground_new/django_playground/cbv-dfb/venv/lib/python3.8/site-packages/django/test/client.py", line 619, in force_login
    self._login(user, backend)
  File "/home/farhan/Documents/_playground_new/django_playground/cbv-dfb/venv/lib/python3.8/site-packages/django/test/client.py", line 631, in _login
    login(request, user, backend)
  File "/home/farhan/Documents/_playground_new/django_playground/cbv-dfb/venv/lib/python3.8/site-packages/django/contrib/auth/__init__.py", line 135, in login
    user_logged_in.send(sender=user.__class__, request=request, user=user)
  File "/home/farhan/Documents/_playground_new/django_playground/cbv-dfb/venv/lib/python3.8/site-packages/django/dispatch/dispatcher.py", line 180, in send
    return [
  File "/home/farhan/Documents/_playground_new/django_playground/cbv-dfb/venv/lib/python3.8/site-packages/django/dispatch/dispatcher.py", line 181, in <listcomp>
    (receiver, receiver(signal=self, sender=sender, **named))
  File "/home/farhan/Documents/_playground_new/django_playground/cbv-dfb/accounts/views.py", line 29, in on_user_logged_out
    f"{request.user.username} successfully logged in!",
AttributeError: 'HttpRequest' object has no attribute 'user'

I have a user_logged_in signal that adds a message by Django messages framework upon logging in. Here is the code -我有一个 user_logged_in 信号,它在登录时通过 Django 消息框架添加一条消息。这是代码 -


@receiver(user_logged_in)
def on_user_logged_in(sender, request, **kwargs):
    messages.add_message(
        request,
        messages.INFO,
        f"{request.user.username} successfully logged in!",
    )

And here is the code for the unit test -这是单元测试的代码 -

class LoginPageTests(TestCase):
    username = "newuser"
    email = "newuser@email.com"
    password = "averydeifficultpasswordtobreak"

    def test_login_page_status_code(self):
        response = self.client.get("/accounts/login/")
        self.assertEqual(response.status_code, 200)

    def test_view_url_by_name(self):
        response = self.client.get(reverse("login"))
        self.assertEqual(response.status_code, 200)

    def test_view_uses_correct_template(self):
        response = self.client.get(reverse("login"))
        self.assertTemplateUsed(response, "registration/login.html")

    def test_login_form(self):
        new_user = get_user_model().objects.create_user(self.username, self.email)
        new_user.set_password(self.password)
        new_user.save()

        logged_in = self.client.login(username=self.username, password=self.password)
        self.assertEqual(logged_in, True)

If I just simply comment out the signal code, the test runs fine.如果我只是简单地注释掉信号代码,测试运行良好。 How can I solve this?我该如何解决这个问题?

The signal sends user as an argument, so use that instead of request.user .信号将 user 作为参数发送,因此请使用它而不是request.user

@receiver(user_logged_in)
def on_user_logged_in(sender, request, user, **kwargs):
    messages.add_message(
        request,
        messages.INFO,
        f"{user.username} successfully logged in!",
    )

Note that self.client.login() is using the Django test client, it isn't testing the login form like the test name suggests.请注意, self.client.login()使用的是 Django 测试客户端,它不像测试名称所暗示的那样测试登录表单。 To test your login form, you could either instantate the login form and assert it behaves as expected, or you might be happy with an integration test that posts login details and asserts that the view logs in the user when the details are correct.要测试您的登录表单,您可以实例化登录表单并断言它的行为符合预期,或者您可能会对发布登录详细信息并断言当详细信息正确时视图登录用户的集成测试感到满意。

I am working on tests and have encountered a similar problem.我正在进行测试并遇到了类似的问题。 You keep getting the MessageFailure error because you are still using the HttpRequest in the signal .您不断收到MessageFailure error因为您仍在signal使用HttpRequest The culprit is a Client() that handles the backend but has trouble with the message argument ( HttpRequest ).罪魁祸首是Client()处理后端但在message参数 ( HttpRequest ) 上有问题。

You can take a look at my topic where I describe such experiences.您可以查看我描述此类经历的主题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django AttributeError: &#39;WSGIRequest&#39; 对象没有属性 &#39;user&#39; - Django AttributeError: 'WSGIRequest' object has no attribute 'user' Django: AttributeError: 'User' object 没有属性 'get' - Django: AttributeError: 'User' object has no attribute 'get' / accounts / login /上的AttributeError&#39;用户&#39;对象没有属性&#39;user&#39; - AttributeError at /accounts/login/ 'User' object has no attribute 'user' / login /&#39;bool&#39;对象的AttributeError没有属性&#39;rindex&#39;-Django 1.5问题 - AttributeError at /login/ 'bool' object has no attribute 'rindex' - Django 1.5 issue Flask-Login: AttributeError: type object 'User' 没有属性 'get' - Flask-Login: AttributeError: type object 'User' has no attribute 'get' Flask-login AttributeError: &#39;User&#39; 对象没有属性 &#39;is_active&#39; - Flask-login AttributeError: 'User' object has no attribute 'is_active' 在 /login/authenticated 处获取 'AttributeError','User' object 没有属性 'backend' - Getting a 'AttributeError at /login/authenticated', 'User' object has no attribute 'backend' Django:AttributeError-对象没有属性 - Django: AttributeError - Object has no attribute Django:AttributeError:“对象没有属性” - Django: AttributeError: “Object has no attribute” Django 查看 AttributeError: 'str' object 没有属性 'get' - Django view AttributeError: 'str' object has no attribute 'get'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM