Django测试-测试登录用户是否是userdetails的所有者

时间:2019-05-27 10:09:14

标签: django unit-testing

因此,我制作了这个小应用程序,用户可以在其中拥有一个帐户页面,以便他们查看和更新​​自己的详细信息。

现在,我正在为DetailView和UpdateView编写一些测试。我想验证登录用户确实是用户详细信息的所有者。

我用不同的用户创建了一个测试数据库,并添加了三个测试。

  • 检查用户是否已登录
  • 用户登录后是否获得正确的模板
  • 用户登录后只能看到自己的详细信息

问题出在上次测试中,我试图检索已登录的用户并将其与当前用户详细信息进行比较,但这不起作用。有什么想法吗?

编辑:测试现在成功通过,因此用户数据属于已登录的用户。

但是,这感觉不是有效的测试方法。即使用户匹配了详细信息的所有者,我仍在寻找一种方法来验证用户是否可以访问其他人的详细信息。

之前,我会在url中使用用户ID,例如:

urls.py

path('account/<int:pk>/', views.AccountDetailView.as_view(), name='account_detail'),

因此,如果未添加LoginRequiredMixin,则某人将能够编辑urlpath并访问其他人的详细信息。

通过使用get_object(self):,这不再可能,测试这种可能性的最佳方法是什么?

views.py

class AccountDetailView(LoginRequiredMixin, DetailView):
    model = User
    template_name = 'account.html'

    '''
    Retrieve user id from "self.request.user" instead of retrieving the user_id
    from the URL. This way we can eliminate the user id in the URL.
    '''
    def get_object(self):
        return self.request.user

test_views.py

class LoggedInTestCase(TestCase):
    '''
    Setup LoginInTestCase 
    '''
    def setUp(self):
        guest = User.objects.create_user(username='john.doe', email='john@doe.com', password='1234')
        guest_other = User.objects.create_user(username='david.doe', email='david@doe.com', password='5678')


class AccountDetailViewTests(LoggedInTestCase):
    '''
    Test the UserDetailView which shows the user details
    '''
    def test_login_required_redirection(self):
        '''
        Test if only logged in users can view the user detail page
        '''
        self.url = reverse('account_detail')
        login_url = reverse('account_login')
        response = self.client.get(self.url)
        self.assertRedirects(response, '{login_url}?next={url}'.format(login_url=login_url, url=self.url))

    def test_logged_in_uses_correct_template(self):
        '''
        Test if logged in user gets to see the correct template
        '''
        login = self.client.login(username='john.doe', password='1234')
        response = self.client.get(reverse('account_detail'))
        # Check if our guest is logged in
        self.assertEqual(str(response.context['user']), 'john.doe')
        # Check for response "succes"
        self.assertEqual(response.status_code, 200)
        # Check if we get the correct template
        self.assertTemplateUsed(response, 'account.html')

    def test_accountdetails_belong_to_logged_in_user(self):
        '''
        Test if logged in user can only see the details that belong to him
        '''
        login = self.client.login(username='john.doe', password='1234')
        response = self.client.get(reverse('account_detail'))
        # Check if our guest is logged in matches the
        user = User.objects.get(username='john.doe') #edited
        self.assertEqual(response.context['user'], user)
        # Check for response "success"
        self.assertEqual(response.status_code, 200)

1 个答案:

答案 0 :(得分:1)

如果登录成功,client.login方法将返回True,但不会返回用户。

您可以从数据库中获取用户。

user = User.objects.get(username='john.doe')
self.assertEqual(response.context['user'], user)

或者像其他测试中那样比较字符串。

self.assertEqual(str(response.context['user']), 'john.doe')