def test_upload_file_with_taxonomy(self): """Test if we can upload a file with taxonomy.""" self.client.credentials(HTTP_AUTHORIZATION="Token " + self.admin_user_permitted_token.key) url = reverse("file-api:upload") pdf_file = generate_pdf_file() data = {"f": pdf_file, "taxonomy": "gallery"} response = self.client.post(url, data, format="multipart") self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.data["name"], "test.pdf") self.assertEqual(response.data["taxonomy"], "gallery") self.assertEqual(response.data["mimetype"], "application/pdf") if self.display_doc: payload = data.copy() payload["f"] = "test.pdf" content = { "title": "Upload file with taxonomy", "url": url, "http_method": "POST", "payload": payload, "response": response.data, } self.doc.display_section(content)
def test_upload_file_with_generic_relation(self): """Test if we can upload a file with content type.""" self.client.credentials(HTTP_AUTHORIZATION="Token " + self.admin_user_permitted_token.key) url = reverse("file-api:upload") pdf_file = generate_pdf_file() content_type_id = ContentType.objects.get_for_model(self.item).id data = { "f": pdf_file, "content_type": content_type_id, "object_id": self.item.id, } response = self.client.post(url, data, format="multipart") self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.data["name"], "test.pdf") self.assertEqual(response.data["mimetype"], "application/pdf") self.assertEqual(response.data["content_type"], content_type_id) self.assertEqual(response.data["object_id"], self.item.id) if self.display_doc: payload = data.copy() payload["f"] = "test.pdf" content = { "title": "Upload file with generic relation", "url": url, "http_method": "POST", "payload": payload, "response": response.data, } self.doc.display_section(content)
def test_upload_file_permission_user_permitted(self): """Test that an admin user with the right permission can upload.""" url = reverse("file-api:upload") pdf_file = generate_pdf_file() data = {"f": pdf_file} self.client.credentials(HTTP_AUTHORIZATION="Token " + self.admin_user_permitted_token.key) response = self.client.post(url, data, format="multipart") self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_upload_file_public_no_public_storage_setup(self): """Test if we can upload public without public storage setup.""" del settings.PUBLIC_FILE_STORAGE self.client.credentials(HTTP_AUTHORIZATION="Token " + self.admin_user_permitted_token.key) url = reverse("file-api:upload") pdf_file = generate_pdf_file() data = {"f": pdf_file, "public": True} response = self.client.post(url, data, format="multipart") self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_upload_file_public(self): """Test a sub class of the upload view with no permissions required. The view will also handle the content_type itself. """ self.client.credentials(HTTP_AUTHORIZATION="Token " + self.admin_user_permitted_token.key) url = reverse("file-upload-public") pdf_file = generate_pdf_file() content_type_id = ContentType.objects.get_for_model(self.item).id data = { "f": pdf_file, "object_id": self.item.id, "taxonomy": "floorplan" } response = self.client.post(url, data, format="multipart") self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.data["name"], "test.pdf") self.assertEqual(response.data["taxonomy"], "floorplan") self.assertEqual(response.data["mimetype"], "application/pdf") self.assertEqual(response.data["content_type"], content_type_id) self.assertEqual(response.data["object_id"], self.item.id) if self.display_doc: payload = data.copy() payload["f"] = "test.pdf" content = { "title": "Upload file without permission required", "url": url, "http_method": "POST", "payload": payload, "response": response.data, } self.doc.display_section(content)
def test_upload_file_permission_restricted(self): """Test that a user without the necessary permissions is restricted.""" url = reverse("file-api:upload") pdf_file = generate_pdf_file() data = {"f": pdf_file} response = self.client.post(url, data, format="multipart") self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) self.client.credentials(HTTP_AUTHORIZATION="Token " + self.normal_user_token.key) response = self.client.post(url, data, format="multipart") self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.client.credentials(HTTP_AUTHORIZATION="Token " + self.admin_user_token.key) response = self.client.post(url, data, format="multipart") self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_file_filesize_validation(self): """Test if we get a validation error for large files.""" self.client.credentials(HTTP_AUTHORIZATION="Token " + self.admin_user_permitted_token.key) url = reverse("file-api:upload") # Make a larger image so it exceeds the file size limit on purpose pdf_file = generate_pdf_file(content="Some text " * 5000) data = {"f": pdf_file} max_upload_size_fmt = filesizeformat(settings.FILE_MAX_UPLOAD_SIZE) response = self.client.post(url, data, format="multipart") self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual( response.data["f"], [ "File size too large. It must be less than {}".format( max_upload_size_fmt) ], )