From 0b120f5d649fb21bb898a8b834b6b33e4cdd9504 Mon Sep 17 00:00:00 2001 From: Johannes Maron Date: Mon, 22 Feb 2021 12:54:33 +0100 Subject: [PATCH] Refs #29138 -- Added extra tests for autocomplete FK to_field variations. --- tests/admin_views/models.py | 1 + tests/admin_views/test_autocomplete_view.py | 24 +++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/tests/admin_views/models.py b/tests/admin_views/models.py index 81948e05a2..4ab2bc8eee 100644 --- a/tests/admin_views/models.py +++ b/tests/admin_views/models.py @@ -622,6 +622,7 @@ class WorkHour(models.Model): class Question(models.Model): + big_id = models.BigAutoField(primary_key=True) question = models.CharField(max_length=20) posted = models.DateField(default=datetime.date.today) expires = models.DateTimeField(null=True, blank=True) diff --git a/tests/admin_views/test_autocomplete_view.py b/tests/admin_views/test_autocomplete_view.py index 1ba8fb146d..c57fb106ed 100644 --- a/tests/admin_views/test_autocomplete_view.py +++ b/tests/admin_views/test_autocomplete_view.py @@ -94,6 +94,30 @@ class AutocompleteJsonViewTests(AdminViewBasicTestCase): 'pagination': {'more': False}, }) + def test_custom_to_field_permission_denied(self): + Question.objects.create(question='Is this a question?') + request = self.factory.get(self.url, {'term': 'is', **self.opts, 'field_name': 'question_with_to_field'}) + request.user = self.user + with self.assertRaises(PermissionDenied): + AutocompleteJsonView.as_view(**self.as_view_args)(request) + + def test_custom_to_field_custom_pk(self): + q = Question.objects.create(question='Is this a question?') + opts = { + 'app_label': Question._meta.app_label, + 'model_name': Question._meta.model_name, + 'field_name': 'related_questions', + } + request = self.factory.get(self.url, {'term': 'is', **opts}) + request.user = self.superuser + response = AutocompleteJsonView.as_view(**self.as_view_args)(request) + self.assertEqual(response.status_code, 200) + data = json.loads(response.content.decode('utf-8')) + self.assertEqual(data, { + 'results': [{'id': str(q.big_id), 'text': q.question}], + 'pagination': {'more': False}, + }) + def test_field_does_not_exist(self): request = self.factory.get(self.url, {'term': 'is', **self.opts, 'field_name': 'does_not_exist'}) request.user = self.superuser