Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#27184 closed Bug (fixed)

Test client crashes when uploading TemporaryFile on Unix

Reported by: Joonsik,Jang Owned by: Thomas Scrace
Component: Testing framework Version: dev
Severity: Normal Keywords: test TemporaryFile django client file upload
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by Tim Graham)

below code is file upload test method
This code was generated error message when I test only in Mac.
This code was ok when I test in Windows 10.
if I change TemporaryFile to NamedTemporaryFile, then It work correctly.


def test_ChunkedSampleFile(self):
        """
        ChunkedSampleFileList, ChunkedSampleFileDetail View Test
        """
        with TemporaryFile() as test_file:
            for count in range(100000):
                test_file.write(('test_data %s' % count).encode())
            test_file.seek(0)
            hash_md5 = hashlib.md5()
            for chunk in iter(lambda: test_file.read(4096), b""):
                hash_md5.update(chunk)
            etag = hash_md5.hexdigest()
            test_file.seek(0)
            
            #post
            response = self.client.post('%s/chunked_sample_files' % URL_PREFIX,
                        data={'file': test_file, 'name': 'test.vcf',
                              'sample_file_meta': self.sample_file_meta_id,
                              'file_seq': 1, 'etag': etag},                        
                        **{AUTH_TOKEN_HEADER_NAME: 'Token %s' % self.testuserfastqc1_token} )
            self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.content)
            file_id = response.json()['id']

Error Message

ERROR: test_ChunkedSampleFile (analysis_files.tests.AnalysisFilesTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/joonsik/git/ngenebio_webapi/ngenebio_webapi/analysis_files/tests.py", line 174, in test_ChunkedSampleFile
    **{AUTH_TOKEN_HEADER_NAME: 'Token %s' % self.testuserfastqc1_token} )
  File "/Users/joonsik/venv_ngenebio_webapi/lib/python3.4/site-packages/django/test/client.py", line 515, in post
    secure=secure, **extra)
  File "/Users/joonsik/venv_ngenebio_webapi/lib/python3.4/site-packages/django/test/client.py", line 311, in post
    post_data = self._encode_data(data, content_type)
  File "/Users/joonsik/venv_ngenebio_webapi/lib/python3.4/site-packages/django/test/client.py", line 275, in _encode_data
    return encode_multipart(BOUNDARY, data)
  File "/Users/joonsik/venv_ngenebio_webapi/lib/python3.4/site-packages/django/test/client.py", line 171, in encode_multipart
    lines.extend(encode_file(boundary, key, value))
  File "/Users/joonsik/venv_ngenebio_webapi/lib/python3.4/site-packages/django/test/client.py", line 200, in encode_file
    filename = os.path.basename(file.name) if hasattr(file, 'name') else ''
  File "/Users/joonsik/venv_ngenebio_webapi/lib/python3.4/posixpath.py", line 139, in basename
    i = p.rfind(sep) + 1
AttributeError: 'int' object has no attribute 'rfind'

Change History (9)

comment:1 by Tim Graham, 8 years ago

Description: modified (diff)

comment:2 by Tim Graham, 8 years ago

Summary: django test client bug when use TemporaryFile in MacTest client crashes when uploading TemporaryFile on Unix
Triage Stage: UnreviewedAccepted

I'm not sure if this should/can work, but if not, it would be nice to have a friendlier message rather than a crash.

comment:3 by Claude Paroz, 8 years ago

Easy pickings: set
Version: 1.9master

This should be relatively easy to fix by testing the type of file.name.

comment:4 by Thomas Scrace, 8 years ago

Owner: changed from nobody to Thomas Scrace
Status: newassigned

Fixed with https://github.com/django/django/pull/7230

This is my first django contribution. I've tried to follow the guidelines, but please let me know if anything is wrong!

Thanks,
Tom

comment:5 by Claude Paroz, 8 years ago

Patch needs improvement: set

Thanks! Comments added in the PR. Uncheck patch needs improvement after you update the patch.

comment:6 by Tim Graham, 8 years ago

Has patch: set

comment:7 by Federico Capoano, 8 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

Improved patch is available at https://github.com/django/django/pull/7500 and should be ready to be merged.

Federico

Last edited 8 years ago by Federico Capoano (previous) (diff)

comment:8 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In 5549e89b:

Fixed #27184 -- Allowed uploading TemporaryFile with the test client.

Thanks Federico Capoano for finishing the patch.

comment:9 by Tim Graham <timograham@…>, 8 years ago

In 8db6a6c:

Refs #27184 -- Fixed unclosed file ResourceWarning in test_client test.

Note: See TracTickets for help on using tickets.
Back to Top