def section_file_orphan(): section_file = SectionFile() with open(get_file_path(FILES['TXT']), 'rb') as fp: cf = ContentFile(fp.read()) section_file.file.save('test/file.txt', cf, save=False) section_file.save() return section_file
def test_ckeditor_file_upload_orphan(admin_api_client_logged_in): """ CKEditor file upload should create files protected by sendfile. The user can upload files when still being in the process of creating the Hearing, so the section id is not known yet. """ ckeditor_params = '?CKEditor=id_sections-0-content&CKEditorFuncNum=1&langCode=en' url = '/upload/' + ckeditor_params post_data = {} with open(get_file_path(FILES['TXT']), 'rb') as fp: post_data['upload'] = fp response = admin_api_client_logged_in.post(url, data=post_data, format='multipart') assert response.status_code == 200, 'expected status_code 200, received %s' % response.status_code assert SectionFile.objects.count() == 1 sectionfile_id = SectionFile.objects.first().pk expected = r"window.parent.CKEDITOR.tools.callFunction\(1, 'https?://.+/v1/download/sectionfile/%s/'\);" % sectionfile_id assert re.search(expected, response.content.decode('utf-8'))
def test_PUT_file_multipart_section(john_smith_api_client, default_hearing): """ File with empty section should be able to be updated with section data """ # Get some section data = get_data_from_response(john_smith_api_client.get(get_hearing_detail_url(default_hearing.id, 'sections'))) first_section = data[0] # POST new file to the section post_data = sectionfile_multipart_test_data() with open(get_file_path(FILES['TXT']), 'rb') as fp: post_data['file'] = fp data = get_data_from_response(john_smith_api_client.post('/v1/file/', data=post_data, format='multipart'), status_code=201) file_obj_id = data['id'] put_data = sectionfile_multipart_test_data() put_data['section'] = first_section['id'] data = get_data_from_response(john_smith_api_client.put('/v1/file/%s/' % file_obj_id, data=put_data, format='multipart'), status_code=200) assert data['section'] == first_section['id'] assert data['hearing'] == default_hearing.pk
def test_POST_first_file(john_smith_api_client, default_hearing): """ POST a file to hearing with no files """ SectionFile.objects.filter(section__in=default_hearing.sections.all()).delete() url = '/v1/file/' section = default_hearing.sections.first() # POST new file to the section post_data = sectionfile_multipart_test_data() post_data['section'] = section.pk with open(get_file_path(FILES['TXT']), 'rb') as fp: post_data['file'] = fp data = get_data_from_response(john_smith_api_client.post(url, data=post_data, format='multipart'), status_code=201) assert data['ordering'] == 1 assert data['section'] == section.pk assert data['hearing'] == default_hearing.pk # Make sure new file was created data = get_data_from_response(john_smith_api_client.get('/v1/file/')) assert len(data['results']) == 1
def test_POST_file_root_endpoint_empty_section(john_smith_api_client, default_hearing): """ File should be able to be uploaded without section data """ # Check original file count data = get_data_from_response(john_smith_api_client.get('/v1/file/')) assert len(data['results']) == 3 # Get some section data = get_data_from_response(john_smith_api_client.get(get_hearing_detail_url(default_hearing.id, 'sections'))) first_section = data[0] # POST new file to the section post_data = sectionfile_multipart_test_data() with open(get_file_path(FILES['TXT']), 'rb') as fp: post_data['file'] = fp data = get_data_from_response(john_smith_api_client.post('/v1/file/', data=post_data, format='multipart'), status_code=201) assert data['section'] is None assert data['hearing'] is None # Make sure new file was created data = get_data_from_response(john_smith_api_client.get('/v1/file/')) assert len(data['results']) == 4
def test_ckeditor_get_uploaded_orphan(admin_api_client_logged_in): """ CKEditor file upload should create files protected by sendfile. The user can upload files when still being in the process of creating the Hearing, so the section id is not known yet. Uploaded file should be accessible to the uploader immediately for CKEditor preview to work. """ ckeditor_params = '?CKEditor=id_sections-0-content&CKEditorFuncNum=1&langCode=en' url = '/upload/' + ckeditor_params post_data = {} with open(get_file_path(FILES['TXT']), 'rb') as fp: post_data['upload'] = fp response = admin_api_client_logged_in.post(url, data=post_data, format='multipart') assert response.status_code == 200, 'expected status_code 200, received %s' % response.status_code assert SectionFile.objects.count() == 1 section_file = SectionFile.objects.first() url = reverse('serve_file', kwargs={'filetype': 'sectionfile', 'pk': section_file.pk}) response = admin_api_client_logged_in.get(url) assert response.status_code == 200, 'expected status_code 200 when accessing uploaded file'