mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-08-12 00:19:48 +00:00
Fix: backwards-compatible versioned API response for custom field select fields (#8912)
This commit is contained in:
@@ -43,10 +43,13 @@ class TestCustomFieldsAPI(DirectoriesMixin, APITestCase):
|
||||
]:
|
||||
resp = self.client.post(
|
||||
self.ENDPOINT,
|
||||
data={
|
||||
"data_type": field_type,
|
||||
"name": name,
|
||||
},
|
||||
data=json.dumps(
|
||||
{
|
||||
"data_type": field_type,
|
||||
"name": name,
|
||||
},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
self.assertEqual(resp.status_code, status.HTTP_201_CREATED)
|
||||
|
||||
@@ -148,7 +151,6 @@ class TestCustomFieldsAPI(DirectoriesMixin, APITestCase):
|
||||
def test_custom_field_select_unique_ids(self):
|
||||
"""
|
||||
GIVEN:
|
||||
- Nothing
|
||||
- Existing custom field
|
||||
WHEN:
|
||||
- API request to create custom field with select options without id
|
||||
@@ -245,7 +247,7 @@ class TestCustomFieldsAPI(DirectoriesMixin, APITestCase):
|
||||
|
||||
resp = self.client.patch(
|
||||
f"{self.ENDPOINT}{custom_field_select.id}/",
|
||||
json.dumps(
|
||||
data=json.dumps(
|
||||
{
|
||||
"extra_data": {
|
||||
"select_options": [
|
||||
@@ -272,6 +274,113 @@ class TestCustomFieldsAPI(DirectoriesMixin, APITestCase):
|
||||
doc.refresh_from_db()
|
||||
self.assertEqual(doc.custom_fields.first().value, None)
|
||||
|
||||
def test_custom_field_select_old_version(self):
|
||||
"""
|
||||
GIVEN:
|
||||
- Nothing
|
||||
WHEN:
|
||||
- API post request is made for custom fields with api version header < 7
|
||||
- API get request is made for custom fields with api version header < 7
|
||||
THEN:
|
||||
- The select options are created with unique ids
|
||||
- The select options are returned in the old format
|
||||
"""
|
||||
resp = self.client.post(
|
||||
self.ENDPOINT,
|
||||
headers={"Accept": "application/json; version=6"},
|
||||
data=json.dumps(
|
||||
{
|
||||
"data_type": "select",
|
||||
"name": "Select Field",
|
||||
"extra_data": {
|
||||
"select_options": [
|
||||
"Option 1",
|
||||
"Option 2",
|
||||
],
|
||||
},
|
||||
},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
self.assertEqual(resp.status_code, status.HTTP_201_CREATED)
|
||||
|
||||
field = CustomField.objects.get(name="Select Field")
|
||||
self.assertEqual(
|
||||
field.extra_data["select_options"],
|
||||
[
|
||||
{"label": "Option 1", "id": ANY},
|
||||
{"label": "Option 2", "id": ANY},
|
||||
],
|
||||
)
|
||||
|
||||
resp = self.client.get(
|
||||
f"{self.ENDPOINT}{field.id}/",
|
||||
headers={"Accept": "application/json; version=6"},
|
||||
)
|
||||
self.assertEqual(resp.status_code, status.HTTP_200_OK)
|
||||
|
||||
data = resp.json()
|
||||
self.assertEqual(
|
||||
data["extra_data"]["select_options"],
|
||||
[
|
||||
"Option 1",
|
||||
"Option 2",
|
||||
],
|
||||
)
|
||||
|
||||
def test_custom_field_select_value_old_version(self):
|
||||
"""
|
||||
GIVEN:
|
||||
- Existing document with custom field select
|
||||
WHEN:
|
||||
- API post request is made to add the field for document with api version header < 7
|
||||
- API get request is made for document with api version header < 7
|
||||
THEN:
|
||||
- The select value is returned in the old format, the index of the option
|
||||
"""
|
||||
custom_field_select = CustomField.objects.create(
|
||||
name="Select Field",
|
||||
data_type=CustomField.FieldDataType.SELECT,
|
||||
extra_data={
|
||||
"select_options": [
|
||||
{"label": "Option 1", "id": "abc-123"},
|
||||
{"label": "Option 2", "id": "def-456"},
|
||||
],
|
||||
},
|
||||
)
|
||||
|
||||
doc = Document.objects.create(
|
||||
title="WOW",
|
||||
content="the content",
|
||||
checksum="123",
|
||||
mime_type="application/pdf",
|
||||
)
|
||||
|
||||
resp = self.client.patch(
|
||||
f"/api/documents/{doc.id}/",
|
||||
headers={"Accept": "application/json; version=6"},
|
||||
data=json.dumps(
|
||||
{
|
||||
"custom_fields": [
|
||||
{"field": custom_field_select.id, "value": 1},
|
||||
],
|
||||
},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
self.assertEqual(resp.status_code, status.HTTP_200_OK)
|
||||
doc.refresh_from_db()
|
||||
self.assertEqual(doc.custom_fields.first().value, "def-456")
|
||||
|
||||
resp = self.client.get(
|
||||
f"/api/documents/{doc.id}/",
|
||||
headers={"Accept": "application/json; version=6"},
|
||||
)
|
||||
self.assertEqual(resp.status_code, status.HTTP_200_OK)
|
||||
|
||||
data = resp.json()
|
||||
self.assertEqual(data["custom_fields"][0]["value"], 1)
|
||||
|
||||
def test_create_custom_field_monetary_validation(self):
|
||||
"""
|
||||
GIVEN:
|
||||
|
@@ -2029,31 +2029,37 @@ class TestDocumentApi(DirectoriesMixin, DocumentConsumeDelayMixin, APITestCase):
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
self.assertEqual(Tag.objects.get(id=response.data["id"]).color, "#a6cee3")
|
||||
self.assertEqual(
|
||||
self.client.get(f"/api/tags/{response.data['id']}/", format="json").data[
|
||||
"colour"
|
||||
],
|
||||
self.client.get(
|
||||
f"/api/tags/{response.data['id']}/",
|
||||
headers={"Accept": "application/json; version=1"},
|
||||
format="json",
|
||||
).data["colour"],
|
||||
1,
|
||||
)
|
||||
|
||||
def test_tag_color(self):
|
||||
response = self.client.post(
|
||||
"/api/tags/",
|
||||
{"name": "tag", "colour": 3},
|
||||
data={"name": "tag", "colour": 3},
|
||||
headers={"Accept": "application/json; version=1"},
|
||||
format="json",
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
|
||||
self.assertEqual(Tag.objects.get(id=response.data["id"]).color, "#b2df8a")
|
||||
self.assertEqual(
|
||||
self.client.get(f"/api/tags/{response.data['id']}/", format="json").data[
|
||||
"colour"
|
||||
],
|
||||
self.client.get(
|
||||
f"/api/tags/{response.data['id']}/",
|
||||
headers={"Accept": "application/json; version=1"},
|
||||
format="json",
|
||||
).data["colour"],
|
||||
3,
|
||||
)
|
||||
|
||||
def test_tag_color_invalid(self):
|
||||
response = self.client.post(
|
||||
"/api/tags/",
|
||||
{"name": "tag", "colour": 34},
|
||||
data={"name": "tag", "colour": 34},
|
||||
headers={"Accept": "application/json; version=1"},
|
||||
format="json",
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
@@ -2061,7 +2067,11 @@ class TestDocumentApi(DirectoriesMixin, DocumentConsumeDelayMixin, APITestCase):
|
||||
def test_tag_color_custom(self):
|
||||
tag = Tag.objects.create(name="test", color="#abcdef")
|
||||
self.assertEqual(
|
||||
self.client.get(f"/api/tags/{tag.id}/", format="json").data["colour"],
|
||||
self.client.get(
|
||||
f"/api/tags/{tag.id}/",
|
||||
headers={"Accept": "application/json; version=1"},
|
||||
format="json",
|
||||
).data["colour"],
|
||||
1,
|
||||
)
|
||||
|
||||
|
@@ -1,7 +1,7 @@
|
||||
import json
|
||||
import types
|
||||
from collections.abc import Callable
|
||||
from datetime import date
|
||||
from unittest.mock import Mock
|
||||
from urllib.parse import quote
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
@@ -149,7 +149,12 @@ class TestCustomFieldsSearch(DirectoriesMixin, APITestCase):
|
||||
document,
|
||||
data=data,
|
||||
partial=True,
|
||||
context={"request": Mock()},
|
||||
context={
|
||||
"request": types.SimpleNamespace(
|
||||
method="GET",
|
||||
version="7",
|
||||
),
|
||||
},
|
||||
)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
serializer.save()
|
||||
|
Reference in New Issue
Block a user