Skip to content

AttributeError when using APITestCase.client.patch with 'follow=True' on redirects with 307 and 308 #8406

@realsuayip

Description

@realsuayip

Discussed in #8357

Originally posted by realsuayip February 8, 2022
Case:

class TestStuff(APITestCase):
    def test_stuff(self):
        user = UserFactory(username="janet", display_name="Janet Doe")
        self.client.force_login(user)
        self.client.patch("/api/users/me/", follow=True)

Error raised:

Traceback (most recent call last):
  File "/code/app/auth/tests.py", line 77, in test_stuff
    self.client.patch("/api/users/me/", follow=True)
  File "/usr/local/lib/python3.10/site-packages/rest_framework/test.py", line 315, in patch
    response = self._handle_redirects(response, **extra)
  File "/usr/local/lib/python3.10/site-packages/django/test/client.py", line 858, in _handle_redirects
    response = request_method(path, data=data, content_type=content_type, follow=False, **extra)
  File "/usr/local/lib/python3.10/site-packages/rest_framework/test.py", line 312, in patch
    response = super().patch(
  File "/usr/local/lib/python3.10/site-packages/rest_framework/test.py", line 217, in patch
    data, content_type = self._encode_data(data, format, content_type)
  File "/usr/local/lib/python3.10/site-packages/rest_framework/test.py", line 180, in _encode_data
    ret = renderer.render(data)
  File "/usr/local/lib/python3.10/site-packages/rest_framework/renderers.py", line 914, in render
    return encode_multipart(self.BOUNDARY, data)
  File "/usr/local/lib/python3.10/site-packages/django/test/client.py", line 245, in encode_multipart
    for (key, value) in data.items():
AttributeError: 'str' object has no attribute 'items'

Am I missing something? Presence of 'data' won't do anything.

When a view returns HttpResponseRedirect with 307 or 308, follow=True will fail (at least in patch method, haven't tried others). Steps to reproduce:

  1. Clone: https://github.com/realsuayip/zeynep/tree/problematic-test
  2. Run: python3 docker.py up
  3. Run tests while the container is running: python3 docker.py test

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions