django rest framweork test:post request在get test中破解了身份验证

时间:2016-01-12 01:01:09

标签: python django unit-testing authentication django-rest-framework

我使用django-rest-framework测试库定义了两个测试:

def test_read_site_API(self):
    """
    Read a Site trough API
    """
    self.client.login(email='test@mail.org', password='testing' )
    response = self.client.get('/xos/sites/', format='json')
    parsed = json.loads(response.content)
    self.assertEqual(response.status_code, status.HTTP_200_OK)
    self.assertEqual(len(parsed), 1)
    self.assertEqual(parsed[0]['login_base'], 'test_')

def test_create_site_API(self):
    """
    Create a Site trough API
    """
    data = {
        'name': "Another Test Site",
        'login_base': "another_test_",
        'location': [10, 20],
        'abbreviated_name': 'test'
    }
    self.client.login(email='test@mail.org', password='testing' )
    response = self.client.post('/xos/sites/', data, format='json')
    print(response.content)
    self.assertEqual(response.status_code, status.HTTP_201_CREATED)
    self.assertEqual(Site.objects.count(), 2)
    self.assertEqual(Site.objects.get(name="Another Test Site").count(), 1)

如果我只运行第一次测试就行了。 如果我运行两个测试,结果是:

======================================================================
ERROR: test_read_site_API (core.tests.SiteTestAPI)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/opt/xos/core/tests.py", line 75, in test_read_site_API
    self.client.login(email='test@mail.org', password='testing' )
  File "/usr/local/lib/python2.7/site-packages/django/test/client.py", line 563, in login
    login(request, user)
  File "/usr/local/lib/python2.7/site-    packages/django/contrib/auth/__init__.py", line 102, in login
    user_logged_in.send(sender=user.__class__, request=request, user=user)
  File "/usr/local/lib/python2.7/site-    packages/django/dispatch/dispatcher.py", line 198, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/opt/xos/core/admin.py", line 1908, in cache_credentials
    auth = {'username': request.POST['username'],
KeyError: 'username'

有关可能发生的事情的任何想法吗?

提前致谢

0 个答案:

没有答案