Refs #29138 -- Added extra tests for autocomplete FK to_field variations.

This commit is contained in:
Johannes Maron 2021-02-22 12:54:33 +01:00 committed by Carlton Gibson
parent ceb4b9ee68
commit 0b120f5d64
2 changed files with 25 additions and 0 deletions

View File

@ -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)

View File

@ -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