Update the dynamic_search app API tests to conform with the new BaseAPITestCase class interface.
Signed-off-by: Michael Price <loneviking72@gmail.com>
This commit is contained in:
committed by
Roberto Rosario
parent
57bb282dbc
commit
c556e095e0
@@ -1,18 +1,17 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from json import loads
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.test import override_settings
|
||||
from django.urls import reverse
|
||||
|
||||
from rest_framework import status
|
||||
|
||||
from documents.models import DocumentType
|
||||
from documents.search import document_search
|
||||
from documents.tests import TEST_DOCUMENT_TYPE_LABEL, TEST_SMALL_DOCUMENT_PATH
|
||||
from rest_api.tests import BaseAPITestCase
|
||||
from user_management.tests import (
|
||||
TEST_ADMIN_EMAIL, TEST_ADMIN_PASSWORD, TEST_ADMIN_USERNAME
|
||||
from documents.permissions import permission_document_view
|
||||
from documents.tests import (
|
||||
TEST_DOCUMENT_TYPE_LABEL, TEST_SMALL_DOCUMENT_PATH
|
||||
)
|
||||
from rest_api.tests import BaseAPITestCase
|
||||
|
||||
from ..classes import SearchModel
|
||||
|
||||
@@ -21,44 +20,53 @@ from ..classes import SearchModel
|
||||
class SearchAPITestCase(BaseAPITestCase):
|
||||
def setUp(self):
|
||||
super(SearchAPITestCase, self).setUp()
|
||||
self.login_user()
|
||||
|
||||
self.admin_user = get_user_model().objects.create_superuser(
|
||||
username=TEST_ADMIN_USERNAME, email=TEST_ADMIN_EMAIL,
|
||||
password=TEST_ADMIN_PASSWORD
|
||||
)
|
||||
|
||||
self.client.login(
|
||||
username=TEST_ADMIN_USERNAME, password=TEST_ADMIN_PASSWORD
|
||||
)
|
||||
|
||||
def test_search(self):
|
||||
document_type = DocumentType.objects.create(
|
||||
label=TEST_DOCUMENT_TYPE_LABEL
|
||||
)
|
||||
|
||||
with open(TEST_SMALL_DOCUMENT_PATH) as file_object:
|
||||
document = document_type.new_document(
|
||||
file_object=file_object,
|
||||
)
|
||||
|
||||
response = self.client.get(
|
||||
'{}?q={}'.format(
|
||||
def _request_search_view(self):
|
||||
return self.get(
|
||||
path='{}?q={}'.format(
|
||||
reverse(
|
||||
'rest_api:search-view', args=(
|
||||
document_search.get_full_name(),
|
||||
)
|
||||
), document.label
|
||||
), self.document.label
|
||||
)
|
||||
)
|
||||
|
||||
content = loads(response.content)
|
||||
self.assertEqual(content['results'][0]['label'], document.label)
|
||||
self.assertEqual(content['count'], 1)
|
||||
def _create_document(self):
|
||||
self.document_type = DocumentType.objects.create(
|
||||
label=TEST_DOCUMENT_TYPE_LABEL
|
||||
)
|
||||
|
||||
with open(TEST_SMALL_DOCUMENT_PATH) as file_object:
|
||||
self.document = self.document_type.new_document(
|
||||
file_object=file_object,
|
||||
)
|
||||
|
||||
def test_search_no_access(self):
|
||||
self._create_document()
|
||||
response = self._request_search_view()
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
self.assertEqual(response.data['count'], 0)
|
||||
|
||||
def test_search_with_access(self):
|
||||
self._create_document()
|
||||
self.grant_access(
|
||||
permission=permission_document_view, obj=self.document
|
||||
)
|
||||
response = self._request_search_view()
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
self.assertEqual(
|
||||
response.data['results'][0]['label'], self.document.label
|
||||
)
|
||||
self.assertEqual(response.data['count'], 1)
|
||||
|
||||
def test_search_models_view(self):
|
||||
response = self.client.get(
|
||||
reverse('rest_api:searchmodel-list')
|
||||
response = self.get(
|
||||
viewname='rest_api:searchmodel-list'
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
|
||||
self.assertEqual(
|
||||
[search_model['pk'] for search_model in response.data['results']],
|
||||
|
||||
Reference in New Issue
Block a user