访问中间件值以测试Django DetailView

时间:2018-03-05 13:39:47

标签: django django-views django-testing

我正在编写一个DetailView测试,通过访问中间件中设置的值来查询get_object()。这适用于公司应用程序和公司模型。每个用户都在公司。

要在整个项目中访问公司,我通过自定义中间件在请求上设置当前用户的Company.uuid。

中间件

from django.utils.deprecation import MiddlewareMixin

class DynamicCompanyUUIDMiddleware(MiddlewareMixin):
    """ Adds the current organization's UUID from the current user."""

    def process_request(self, request):
        try:
            company_uuid = request.user.company_uuid
        except:
            company_uuid = None
        request.company_uuid = company_uuid

通过我用于其他公司视图的Mixin在CompanyDetailView的get_object()方法中使用它。

密新

class CompanyMixin(LoginRequiredMixin, SetHeadlineMixin):
    model = Company

    def get_object(self):
        return get_object_or_404(
            self.model,
            uuid=self.request.user.company_uuid)

测试

我正在尝试编写的测试是:

from django.test import RequestFactory
from django.urls import reverse, resolve
from test_plus.test import TestCase
from ..models import Company
from ..views import CompanyDetailView

class BaseCompanyTestCase(TestCase):

    def setUp(self):
        self.user = self.make_user()
        self.object = Company.objects.create(owner=self.user, name="testcompany")
        self.user.company_uuid = self.object.uuid
        self.factory = RequestFactory()

class TestCompanyDetailView(BaseCompanyTestCase):

    def setUp(self):
        super(TestCompanyDetailView, self).setUp()
        self.client.login(username="testuser", password="password")
        self.view = CompanyDetailView()
        self.view.object = self.object
        request = self.factory.get(reverse('companies:detail'))
        request.user = self.user
        request.company_uuid = self.user.company_uuid
        response = CompanyDetailView.as_view()(request)
        self.assertEqual(response.status_code, 200)

    def test_get_headline(self):
        self.assertEqual(
            self.view.get_headline(),
            '%s Members' % self.object.name

结果

这导致404找不到testuser的公司。

走过它:

  • 我创建了用户
  • 为此新testuser创建公司
  • 设置user.company_uuid
  • 这应该允许mixin访问company_uuid
  • 因此在请求中返回用户的公司

然而,我没有像404那样回归公司。

问题

我在哪里错了?在此先感谢您的帮助。

答案

我正在混合Django的客户端& RequestFactory。我已经纠正了上面的代码是正确的。

1 个答案:

答案 0 :(得分:0)

我正在混合Django的客户端& RequestFactory。走开后,我想出来 -

from django.test import RequestFactory
from django.urls import reverse, resolve
from test_plus.test import TestCase
from ..models import Company
from ..views import CompanyDetailView

class BaseCompanyTestCase(TestCase):

    def setUp(self):
        self.user = self.make_user()
        self.object = Company.objects.create(owner=self.user, name="testcompany")
        self.user.company_uuid = self.object.uuid
        self.factory = RequestFactory()

class TestCompanyDetailView(BaseCompanyTestCase):

    def setUp(self):
        super(TestCompanyDetailView, self).setUp()
        self.client.login(username="testuser", password="password")
        self.view = CompanyDetailView()
        self.view.object = self.object
        request = self.factory.get(reverse('companies:detail'))
        request.user = self.user
        request.company_uuid = self.user.company_uuid
        response = CompanyDetailView.as_view()(request)
        self.assertEqual(response.status_code, 200)

    def test_get_headline(self):
        self.assertEqual(
            self.view.get_headline(),
            '%s Members' % self.object.name