Skip to content

Commit

Permalink
Convert test to pytest style, add basic frontend testing
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanw committed Oct 27, 2022
1 parent 3e2f19f commit 52905d1
Show file tree
Hide file tree
Showing 21 changed files with 154 additions and 117 deletions.
10 changes: 7 additions & 3 deletions fc_project/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from django.contrib import admin
from django.contrib.admin.sites import AlreadyRegistered

from fcdocs_annotate.annotation.admin import predict_feature
try:
from fcdocs_annotate.annotation.admin import predict_feature
except ImportError:
predict_feature = None

from filingcabinet import get_document_model, get_documentcollection_model
from filingcabinet.admin import (
Expand Down Expand Up @@ -40,5 +43,6 @@ def try_rgegister(model, model_admin):
try_rgegister(DocumentPortal, DocumentPortalAdmin)
try_rgegister(CollectionDirectory, CollectionDirectoryAdmin)

DocumentBaseAdmin.predict_feature = predict_feature
DocumentBaseAdmin.actions += ["predict_feature"]
if predict_feature:
DocumentBaseAdmin.predict_feature = predict_feature
DocumentBaseAdmin.actions += ["predict_feature"]
17 changes: 16 additions & 1 deletion test_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
FILINGCABINET_MEDIA_PUBLIC_PREFIX = "docs"
FILINGCABINET_MEDIA_PRIVATE_PREFIX = "docs-private"

MEDIA_URL = "/media/"
MEDIA_ROOT = BASE_DIR / "tests" / "testdata"
STATIC_ROOT = BASE_DIR / "static"

MIDDLEWARE = [
"django.contrib.sessions.middleware.SessionMiddleware",
Expand All @@ -51,6 +54,7 @@
"APP_DIRS": True,
"DIRS": [BASE_DIR / "fc_project" / "templates"],
"OPTIONS": {
"debug": True,
"context_processors": [
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
Expand All @@ -66,8 +70,14 @@
USE_TZ = True
TIME_ZONE = "UTC"

# Required for django-webtest to work
STATIC_URL = "/static/"
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
)

FRONTEND_BUILD_DIR = BASE_DIR / "build"
STATICFILES_DIRS = [FRONTEND_BUILD_DIR]

# Random secret key

Expand Down Expand Up @@ -103,4 +113,9 @@
"rest_framework.renderers.JSONRenderer",
"rest_framework.renderers.BrowsableAPIRenderer",
),
"DEFAULT_PAGINATION_CLASS": "filingcabinet.api_utils.CustomLimitOffsetPagination",
"PAGE_SIZE": 50,
}

CELERY_TASK_ALWAYS_EAGER = True
CELERY_TASK_EAGER_PROPAGATES = CELERY_TASK_ALWAYS_EAGER
239 changes: 126 additions & 113 deletions tests/test_unlisted.py
Original file line number Diff line number Diff line change
@@ -1,115 +1,128 @@
from django.test import TestCase
from django.urls import reverse

from . import DocumentCollectionFactory, DocumentFactory, UserFactory


class DocumentAccessTest(TestCase):
def setUp(self):
self.user = UserFactory.create()

def test_unlisted_document_needs_slug(self):
doc = DocumentFactory.create(user=self.user, public=True, listed=False)

url = reverse(
"filingcabinet:document-detail_short",
kwargs={
"pk": doc.pk,
},
)
response = self.client.get(url)
self.assertEqual(response.status_code, 404)

url = reverse(
"filingcabinet:document-detail", kwargs={"pk": doc.pk, "slug": doc.slug}
)
response = self.client.get(url)
self.assertEqual(response.status_code, 200)

self.client.force_login(self.user)
url = reverse(
"filingcabinet:document-detail_short",
kwargs={
"pk": doc.pk,
},
)
response = self.client.get(url)
self.assertEqual(response.status_code, 302)

def test_list_unlisted_document_api(self):
DocumentFactory.create(user=self.user, public=True, listed=False)
url = reverse("api:document-list")
response = self.client.get(url)
self.assertEqual(len(response.json()["objects"]), 0)
self.client.force_login(self.user)
response = self.client.get(url)
self.assertEqual(len(response.json()["objects"]), 1)

def test_retrieve_unlisted_document_api(self):
doc = DocumentFactory.create(user=self.user, public=True, listed=False)
url = reverse("api:document-detail", kwargs={"pk": doc.pk})
response = self.client.get(url)
self.assertEqual(response.status_code, 403)

url = reverse("api:document-detail", kwargs={"pk": doc.pk})
response = self.client.get(url + "?uid=" + str(doc.uid))
self.assertEqual(response.status_code, 200)

self.client.force_login(self.user)
response = self.client.get(url)
self.assertEqual(response.status_code, 200)

def test_unlisted_documentcollection_needs_slug(self):
collection = DocumentCollectionFactory.create(
user=self.user, public=True, listed=False
)

url = reverse(
"filingcabinet:document-collection_short",
kwargs={
"pk": collection.pk,
},
)
response = self.client.get(url)
self.assertEqual(response.status_code, 404)

url = reverse(
"filingcabinet:document-collection",
kwargs={"pk": collection.pk, "slug": collection.slug},
)
response = self.client.get(url)
self.assertEqual(response.status_code, 200)

self.client.force_login(self.user)
url = reverse(
"filingcabinet:document-collection_short",
kwargs={
"pk": collection.pk,
},
)
response = self.client.get(url)
self.assertEqual(response.status_code, 302)

def test_list_unlisted_documentcollection_api(self):
DocumentCollectionFactory.create(user=self.user, public=True, listed=False)
url = reverse("api:documentcollection-list")
response = self.client.get(url)
self.assertEqual(len(response.json()["objects"]), 0)
self.client.force_login(self.user)
response = self.client.get(url)
self.assertEqual(len(response.json()["objects"]), 1)

def test_retrieve_unlisted_documentcollection_api(self):
collection = DocumentCollectionFactory.create(
user=self.user, public=True, listed=False
)
url = reverse("api:documentcollection-detail", kwargs={"pk": collection.pk})
response = self.client.get(url)
self.assertEqual(response.status_code, 403)

response = self.client.get(url + "?uid=" + str(collection.uid))
self.assertEqual(response.status_code, 200)

self.client.force_login(self.user)
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
import pytest

from filingcabinet import get_document_model, get_documentcollection_model

from .factories import DocumentCollectionFactory, DocumentFactory

Document = get_document_model()
DocumentCollection = get_documentcollection_model()


@pytest.mark.django_db
def test_unlisted_document_needs_slug(client, dummy_user):
doc = DocumentFactory.create(user=dummy_user, public=True, listed=False)

url = reverse(
"filingcabinet:document-detail_short",
kwargs={
"pk": doc.pk,
},
)
response = client.get(url)
assert response.status_code == 404

url = reverse(
"filingcabinet:document-detail", kwargs={"pk": doc.pk, "slug": doc.slug}
)
response = client.get(url)
assert response.status_code == 200

client.force_login(dummy_user)
url = reverse(
"filingcabinet:document-detail_short",
kwargs={
"pk": doc.pk,
},
)
response = client.get(url)
assert response.status_code == 302


@pytest.mark.django_db
def test_list_unlisted_document_api(client, dummy_user):
DocumentFactory.create(user=dummy_user, public=True, listed=False)
url = reverse("api:document-list")
response = client.get(url)
assert len(response.json()["objects"]) == 0
client.force_login(dummy_user)
response = client.get(url)
assert len(response.json()["objects"]) == 1


@pytest.mark.django_db
def test_retrieve_unlisted_document_api(client, dummy_user):
doc = DocumentFactory.create(user=dummy_user, public=True, listed=False)
url = reverse("api:document-detail", kwargs={"pk": doc.pk})
response = client.get(url)
assert response.status_code == 403

url = reverse("api:document-detail", kwargs={"pk": doc.pk})
response = client.get(url + "?uid=" + str(doc.uid))
assert response.status_code == 200

client.force_login(dummy_user)
response = client.get(url)
assert response.status_code == 200


@pytest.mark.django_db
def test_unlisted_documentcollection_needs_slug(client, dummy_user):
collection = DocumentCollectionFactory.create(
user=dummy_user, public=True, listed=False
)

url = reverse(
"filingcabinet:document-collection_short",
kwargs={
"pk": collection.pk,
},
)
response = client.get(url)
assert response.status_code == 404

url = reverse(
"filingcabinet:document-collection",
kwargs={"pk": collection.pk, "slug": collection.slug},
)
response = client.get(url)
assert response.status_code == 200

client.force_login(dummy_user)
url = reverse(
"filingcabinet:document-collection_short",
kwargs={
"pk": collection.pk,
},
)
response = client.get(url)
assert response.status_code == 302


@pytest.mark.django_db
def test_list_unlisted_documentcollection_api(client, dummy_user):
DocumentCollectionFactory.create(user=dummy_user, public=True, listed=False)
url = reverse("api:documentcollection-list")
response = client.get(url)
assert len(response.json()["objects"]) == 0
client.force_login(dummy_user)
response = client.get(url)
assert len(response.json()["objects"]) == 1


@pytest.mark.django_db
def test_retrieve_unlisted_documentcollection_api(client, dummy_user):
collection = DocumentCollectionFactory.create(
user=dummy_user, public=True, listed=False
)
url = reverse("api:documentcollection-detail", kwargs={"pk": collection.pk})
response = client.get(url)
assert response.status_code == 403

response = client.get(url + "?uid=" + str(collection.uid))
assert response.status_code == 200

client.force_login(dummy_user)
response = client.get(url)
assert response.status_code == 200
5 changes: 5 additions & 0 deletions tests/test_viewer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def test_document_viewer(page, live_server, processed_document):
page.goto(live_server.url + processed_document.get_absolute_url())
assert processed_document.title in page.title()
assert page.query_selector("h2").text_content() == processed_document.title
page.pause()
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 52905d1

Please sign in to comment.