|
| 1 | +""" |
| 2 | +Tests for Documents API endpoint in impress's core app: list |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +import responses |
| 7 | +from faker import Faker |
| 8 | +from rest_framework.test import APIClient |
| 9 | + |
| 10 | +from core import factories, models |
| 11 | + |
| 12 | +fake = Faker() |
| 13 | +pytestmark = pytest.mark.django_db |
| 14 | + |
| 15 | + |
| 16 | +@pytest.mark.parametrize("role", models.LinkRoleChoices.values) |
| 17 | +@pytest.mark.parametrize("reach", models.LinkReachChoices.values) |
| 18 | +@responses.activate |
| 19 | +def test_api_documents_search_anonymous(reach, role, settings): |
| 20 | + """ |
| 21 | + Anonymous users should not be allowed to search documents whatever the |
| 22 | + link reach and link role |
| 23 | + """ |
| 24 | + factories.DocumentFactory(link_reach=reach, link_role=role) |
| 25 | + settings.SEARCH_INDEXER_QUERY_URL = "http://find/api/v1.0/search" |
| 26 | + |
| 27 | + factories.DocumentFactory(link_reach=reach, link_role=role) |
| 28 | + |
| 29 | + # Find response |
| 30 | + responses.add( |
| 31 | + responses.POST, |
| 32 | + "http://find/api/v1.0/search", |
| 33 | + json=[], |
| 34 | + status=200, |
| 35 | + ) |
| 36 | + |
| 37 | + response = APIClient().get("/api/v1.0/documents/search/", data={"q": "alpha"}) |
| 38 | + |
| 39 | + assert response.status_code == 200 |
| 40 | + assert response.json() == { |
| 41 | + "count": 0, |
| 42 | + "next": None, |
| 43 | + "previous": None, |
| 44 | + "results": [], |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | +def test_api_documents_search_endpoint_is_none(settings): |
| 49 | + """Missing SEARCH_INDEXER_QUERY_URL should throw an error""" |
| 50 | + settings.SEARCH_INDEXER_QUERY_URL = None |
| 51 | + |
| 52 | + user = factories.UserFactory() |
| 53 | + |
| 54 | + client = APIClient() |
| 55 | + client.force_login(user) |
| 56 | + |
| 57 | + response = APIClient().get("/api/v1.0/documents/search/", data={"q": "alpha"}) |
| 58 | + |
| 59 | + assert response.status_code == 401 |
| 60 | + assert response.json() == {"detail": "The service is not configured properly."} |
| 61 | + |
| 62 | + |
| 63 | +@responses.activate |
| 64 | +def test_api_documents_search_invalid_params(settings): |
| 65 | + """Validate the format of documents as returned by the search view.""" |
| 66 | + settings.SEARCH_INDEXER_QUERY_URL = "http://find/api/v1.0/search" |
| 67 | + |
| 68 | + user = factories.UserFactory() |
| 69 | + |
| 70 | + client = APIClient() |
| 71 | + client.force_login(user) |
| 72 | + |
| 73 | + response = APIClient().get("/api/v1.0/documents/search/") |
| 74 | + |
| 75 | + assert response.status_code == 400 |
| 76 | + assert response.json() == {"q": ["This field is required."]} |
| 77 | + |
| 78 | + |
| 79 | +@responses.activate |
| 80 | +def test_api_documents_search_format(settings): |
| 81 | + """Validate the format of documents as returned by the search view.""" |
| 82 | + settings.SEARCH_INDEXER_QUERY_URL = "http://find/api/v1.0/search" |
| 83 | + |
| 84 | + user = factories.UserFactory() |
| 85 | + |
| 86 | + client = APIClient() |
| 87 | + client.force_login(user) |
| 88 | + |
| 89 | + user_a, user_b, user_c = factories.UserFactory.create_batch(3) |
| 90 | + document = factories.DocumentFactory( |
| 91 | + title="alpha", |
| 92 | + users=(user_a, user_c), |
| 93 | + link_traces=(user, user_b), |
| 94 | + ) |
| 95 | + access = factories.UserDocumentAccessFactory(document=document, user=user) |
| 96 | + |
| 97 | + # Find response |
| 98 | + responses.add( |
| 99 | + responses.POST, |
| 100 | + "http://find/api/v1.0/search", |
| 101 | + json=[ |
| 102 | + {"_id": str(document.pk)}, |
| 103 | + ], |
| 104 | + status=200, |
| 105 | + ) |
| 106 | + response = client.get("/api/v1.0/documents/search/", data={"q": "alpha"}) |
| 107 | + |
| 108 | + assert response.status_code == 200 |
| 109 | + content = response.json() |
| 110 | + results = content.pop("results") |
| 111 | + assert content == { |
| 112 | + "count": 1, |
| 113 | + "next": None, |
| 114 | + "previous": None, |
| 115 | + } |
| 116 | + assert len(results) == 1 |
| 117 | + assert results[0] == { |
| 118 | + "id": str(document.id), |
| 119 | + "abilities": document.get_abilities(user), |
| 120 | + "ancestors_link_reach": None, |
| 121 | + "ancestors_link_role": None, |
| 122 | + "computed_link_reach": document.computed_link_reach, |
| 123 | + "computed_link_role": document.computed_link_role, |
| 124 | + "created_at": document.created_at.isoformat().replace("+00:00", "Z"), |
| 125 | + "creator": str(document.creator.id), |
| 126 | + "depth": 1, |
| 127 | + "excerpt": document.excerpt, |
| 128 | + "link_reach": document.link_reach, |
| 129 | + "link_role": document.link_role, |
| 130 | + "nb_accesses_ancestors": 3, |
| 131 | + "nb_accesses_direct": 3, |
| 132 | + "numchild": 0, |
| 133 | + "path": document.path, |
| 134 | + "title": document.title, |
| 135 | + "updated_at": document.updated_at.isoformat().replace("+00:00", "Z"), |
| 136 | + "user_role": access.role, |
| 137 | + } |
0 commit comments