第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何為受身份驗證保護的 REST API 編寫 Django 單元測試?

如何為受身份驗證保護的 REST API 編寫 Django 單元測試?

回首憶惘然 2021-08-05 14:49:29
我已經(jīng)看過關(guān)于 SO 的兩個答案,它們可能已經(jīng)有了我的答案,但坦率地說,我只是不理解它們兩個 SO 問題都使用不同的方法。我試圖使用的方法是這樣的。我創(chuàng)建了這個類:from rest_framework.test import APIClientfrom django.test import testcasesfrom django.contrib.auth.models import Userclass RemoteAuthenticatedTest(testcases.TestCase):    client_class = APIClient    def setUp(self):        self.username = 'mister_neutron'        self.user = User.objects.create_user(username='mister_neutron',                                          email='mister_neutron@example.com',                                             password='F4kePaSs0d')        super(RemoteAuthenticatedTest, self).setUp()我的單元測試如下所示:class InfoViewTestCase(RemoteAuthenticatedTest):    def create_info_record(self):        from random import randint        blade = 'test-blade-host-name-%s' % (randint(0, 100))        breachs = randint(0,100)        dimm = 'test dimm slot %s' % (randint(0,100))        url = reverse('info:info_creat')        data = {            'blade_hostname': blade,            'breach_count': breachs,            'dimm_slot': dimm,        }        response = self.client.post(url,                                     data,                                     format='json',                                     REMOTE_USER=self.username)        self.assertEqual(response.status_code, status.HTTP_201_CREATED)        self.assertEqual(Info.objects.count(), 1)        self.assertEqual(Info.objects.get().blade_hostname, blade)我的 settings.py 文件中有這個:#AuthenticationsREST_FRAMEWORK = {    'DEFAULT_AUTHENTICATION_CLASSES': (        'rest_framework.authentication.BasicAuthentication',        'rest_framework.authentication.SessionAuthentication',        'rest_framework.authentication.TokenAuthentication',    ),    'DEFAULT_PERMISSION_CLASSES': (        'rest_framework.permissions.IsAuthenticated',    ),}
查看完整描述

1 回答

  • 1 回答
  • 0 關(guān)注
  • 117 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號