【问题标题】:401 error for tastypie for api_clientapi_client 的 401 错误
【发布时间】:2016-09-27 14:05:46
【问题描述】:

您好,我正在尝试为我的应用编写测试用例。 URL = '/api/project/'。我已启用 get、post 和 put 方法以及身份验证。但我仍然收到发布请求的 401 错误

class EntryResourceTest(ResourceTestCaseMixin, TestCase):
    class Meta:
        queryset = Project.objects.all()
        resource_name = 'project'
        allowed_methods = ['get', 'post', 'put']
        authentication = Authentication()
        authorization = Authorization()

    def setUp(self):
        super(EntryResourceTest, self).setUp()
        # Create a user.
        self.username = 'daniel'
        self.password = 'pass'
        self.user = User.objects.create_user(self.username, 'daniel@example.com', self.password)

    def login(self):
         return self.api_client.client.login(
        username=self.username, password=self.password)

    def get_credentials(self):
    return self.create_basic(username=self.username, password=self.password)

    def test_post_list(self):
        self.login()
        req_get = self.api_client.get('/api/project/', format='json', authentication=self.get_credentials())      # -> get works and i get 200 status code
        req_post = self.api_client.post('/api/project/', format='json', data=self.post_data, authentication=self.get_credentials())

我使用以下命令运行测试用例,在这里得到工作正常但不是发布请求。即使我没有传递身份验证参数,获取请求也可以工作,因为它使用我在 self.login() 中定义的默认登录

django-admin test myapp.api.project.tests        

【问题讨论】:

    标签: python django tastypie django-unittest


    【解决方案1】:

    使用BasicAuthentication 而不是Authentication

    self.create_basic(...)BasicAuthentication 创建一个标头。

    def create_basic(self, username, password):
        """
        Creates & returns the HTTP ``Authorization`` header for use with BASIC
        Auth.
        """
    

    【讨论】:

      猜你喜欢
      • 2020-06-29
      • 2013-12-20
      • 2017-01-08
      • 1970-01-01
      • 1970-01-01
      • 2016-11-18
      • 1970-01-01
      • 2015-12-26
      • 2014-11-17
      相关资源
      最近更新 更多