登录后,Django 2.1 Testcase Client遭到未经授权的

时间:2018-09-19 09:36:45

标签: django unit-testing pytest-django django-2.1

我正在尝试将应用程序从Django 1.11升级到Django 2.1,尽管我能够解决大多数迁移问题,但API测试在迁移过程中停止了工作。检查它们后,我看到我收到状态码为401(未授权)的响应。我从外壳上手动尝试过,这就是我得到的:

In [46]: from django.test import Client

In [47]: c = Client()

In [48]: user = User.objects.create(username='foobar')

In [49]: user.set_password('foobar')

In [50]: user.save()

In [51]: c.login(username=user.username, password='foobar')
Out[51]: True

In [52]: c.get('/me/')
Unauthorized: /me/
Out[52]: <Response status_code=401, "application/json">

In [53]: c.get('/me/').content
Unauthorized: /me/
Out[53]: b'{"detail":"Authentication credentials were not provided."}'

因此,显然是出于某种原因,我的 Client 正在登录,但未获得授权,尽管在Django 1.11中它没有任何更改也可以正常工作。 API和应用程序仍可以正常工作。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

在django 2.1中,settings.py中的MIDDLEWARE_CLASSES更改为MIDDLEWARE。尝试更改它,它应该可以工作。

如果您正在寻找django 2.1的其他变化,我发现此博客非常有用:https://www.codingforentrepreneurs.com/blog/django-version-20-a-few-key-features/