mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-04-02 13:45:10 -05:00
api validation, more tests
This commit is contained in:
parent
544e8db722
commit
e9b5f8d9f8
@ -246,8 +246,55 @@ class BulkEditSerializer(serializers.Serializer):
|
|||||||
else:
|
else:
|
||||||
raise serializers.ValidationError("Unsupported method.")
|
raise serializers.ValidationError("Unsupported method.")
|
||||||
|
|
||||||
|
def _validate_parameters_tags(self, parameters):
|
||||||
|
if 'tag' in parameters:
|
||||||
|
tag_id = parameters['tag']
|
||||||
|
try:
|
||||||
|
Tag.objects.get(id=tag_id)
|
||||||
|
except Tag.DoesNotExist:
|
||||||
|
raise serializers.ValidationError("Tag does not exist")
|
||||||
|
else:
|
||||||
|
raise serializers.ValidationError("tag not specified")
|
||||||
|
|
||||||
|
def _validate_parameters_document_type(self, parameters):
|
||||||
|
if 'document_type' in parameters:
|
||||||
|
document_type_id = parameters['document_type']
|
||||||
|
if document_type_id is None:
|
||||||
|
# None is ok
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
DocumentType.objects.get(id=document_type_id)
|
||||||
|
except DocumentType.DoesNotExist:
|
||||||
|
raise serializers.ValidationError(
|
||||||
|
"Document type does not exist")
|
||||||
|
else:
|
||||||
|
raise serializers.ValidationError("document_type not specified")
|
||||||
|
|
||||||
|
def _validate_parameters_correspondent(self, parameters):
|
||||||
|
if 'correspondent' in parameters:
|
||||||
|
correspondent_id = parameters['correspondent']
|
||||||
|
if correspondent_id is None:
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
Correspondent.objects.get(id=correspondent_id)
|
||||||
|
except Correspondent.DoesNotExist:
|
||||||
|
raise serializers.ValidationError(
|
||||||
|
"Correspondent does not exist")
|
||||||
|
else:
|
||||||
|
raise serializers.ValidationError("correspondent not specified")
|
||||||
|
|
||||||
def validate(self, attrs):
|
def validate(self, attrs):
|
||||||
|
|
||||||
|
method = attrs['method']
|
||||||
|
parameters = attrs['parameters']
|
||||||
|
|
||||||
|
if method == bulk_edit.set_correspondent:
|
||||||
|
self._validate_parameters_correspondent(parameters)
|
||||||
|
elif method == bulk_edit.set_document_type:
|
||||||
|
self._validate_parameters_document_type(parameters)
|
||||||
|
elif method == bulk_edit.add_tag or method == bulk_edit.remove_tag:
|
||||||
|
self._validate_parameters_tags(parameters)
|
||||||
|
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
|
|
||||||
|
@ -669,6 +669,7 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
|
|||||||
self.assertEqual(v1.filter_rules.count(), 0)
|
self.assertEqual(v1.filter_rules.count(), 0)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class TestBulkEdit(DirectoriesMixin, APITestCase):
|
class TestBulkEdit(DirectoriesMixin, APITestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
@ -891,7 +892,7 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
|||||||
doc2 = Document.objects.get(id=self.doc2.id)
|
doc2 = Document.objects.get(id=self.doc2.id)
|
||||||
self.assertEqual(doc2.document_type, self.dt1)
|
self.assertEqual(doc2.document_type, self.dt1)
|
||||||
|
|
||||||
def test_api_invalid_tag(self):
|
def test_api_add_invalid_tag(self):
|
||||||
self.assertEqual(list(self.doc2.tags.all()), [self.t1])
|
self.assertEqual(list(self.doc2.tags.all()), [self.t1])
|
||||||
response = self.client.post("/api/documents/bulk_edit/", json.dumps({
|
response = self.client.post("/api/documents/bulk_edit/", json.dumps({
|
||||||
"documents": [self.doc2.id],
|
"documents": [self.doc2.id],
|
||||||
@ -901,3 +902,14 @@ class TestBulkEdit(DirectoriesMixin, APITestCase):
|
|||||||
self.assertEqual(response.status_code, 400)
|
self.assertEqual(response.status_code, 400)
|
||||||
|
|
||||||
self.assertEqual(list(self.doc2.tags.all()), [self.t1])
|
self.assertEqual(list(self.doc2.tags.all()), [self.t1])
|
||||||
|
|
||||||
|
def test_api_delete_invalid_tag(self):
|
||||||
|
self.assertEqual(list(self.doc2.tags.all()), [self.t1])
|
||||||
|
response = self.client.post("/api/documents/bulk_edit/", json.dumps({
|
||||||
|
"documents": [self.doc2.id],
|
||||||
|
"method": "remove_tag",
|
||||||
|
"parameters": {'tag': 345657}
|
||||||
|
}), content_type='application/json')
|
||||||
|
self.assertEqual(response.status_code, 400)
|
||||||
|
|
||||||
|
self.assertEqual(list(self.doc2.tags.all()), [self.t1])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user