Skip to content

Commit

Permalink
Merge pull request #1775 from laws-africa/admin-tests
Browse files Browse the repository at this point in the history
Admin tests
  • Loading branch information
longhotsummer authored Mar 18, 2024
2 parents e574834 + 80d6a8a commit 4103b05
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 0 deletions.
Binary file not shown.
Binary file not shown.
17 changes: 17 additions & 0 deletions peachjam/fixtures/tests/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,22 @@
"password": "pbkdf2_sha256$12000$NkxvaoXfb4p2$A6u0ez3trvm9VyIT606HB5G3NKRLl4znE4ZlsZS5T7U=",
"date_joined": "2012-01-13 00:14:00+00:00"
}
},
{
"pk": 2,
"model": "auth.user",
"fields": {
"username": "[email protected]",
"first_name": "admin_user",
"last_name": "",
"is_active": true,
"is_superuser": true,
"is_staff": true,
"last_login": "2021-01-13 00:14:00+00:00",
"groups": [],
"user_permissions": [],
"password": "pbkdf2_sha256$12000$NkxvaoXfb4p2$A6u0ez3trvm9VyIT606HB5G3NKRLl4znE4ZlsZS5T7U=",
"date_joined": "2012-01-13 00:14:00+00:00"
}
}
]
155 changes: 155 additions & 0 deletions peachjam/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import os

from django.contrib.auth.models import User
from django.urls import reverse
from django_webtest import WebTest
from webtest import Upload

from peachjam.models import Judgment


class TestJudgmentAdmin(WebTest):
fixtures = ["tests/users", "tests/countries", "tests/courts", "tests/languages"]

def setUp(self):
self.app.set_user(User.objects.get(username="[email protected]"))

def test_add_judgment_docx_swap_pdf(self):
# add judgment
judgment_add_url = reverse("admin:peachjam_judgment_add")
judgment_list_url = reverse("admin:peachjam_judgment_changelist")

form = self.app.get(judgment_add_url).form

form["jurisdiction"] = "ZA"
form["court"] = "1"
form["language"] = "en"
form["case_name"] = "test case"

# date uses a multi-field widget
form["date_0"] = "21"
form["date_1"] = "2"
form["date_2"] = "2000"

with open(
os.path.abspath("peachjam/fixtures/source_files/zagpjhc_judgment.docx"),
"rb",
) as docx_file:
docx_file_content = docx_file.read()

# upload file
form["upload_file"] = Upload(
"file.docx",
docx_file_content,
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
)

response = form.submit()
self.assertRedirects(response, judgment_list_url)

judgment = Judgment.objects.filter(
expression_frbr_uri="/akn/za/judgment/eacj/2000/1/eng@2000-02-21"
).first()
self.assertIsNotNone(judgment)

# check if content_html has been extracted
self.assertIn(
"The second count is robbery, in that on or about near the place mentioned in count",
judgment.content_html,
)
self.assertEqual(
"test-case-2000-eacj-1-21-february-2000.docx",
judgment.source_file.filename,
)

# swap source file with pdf
judgment_change_url = reverse(
"admin:peachjam_judgment_change", kwargs={"object_id": judgment.pk}
)
form2 = self.app.get(judgment_change_url).form

with open(
os.path.abspath("peachjam/fixtures/source_files/gauteng_judgment.pdf"), "rb"
) as pdf_file:
pdf_file_content = pdf_file.read()
form2["source_file-0-file"] = Upload(
"upload_pdf.pdf", pdf_file_content, "application/pdf"
)
response2 = form2.submit()
self.assertRedirects(response2, judgment_list_url)

judgment.refresh_from_db()
self.assertEqual(
"test-case-2000-eacj-1-21-february-2000.pdf", judgment.source_file.filename
)

def test_add_judgment_pdf_swap_docx(self):
# add judgment
judgment_add_url = reverse("admin:peachjam_judgment_add")
judgment_list_url = reverse("admin:peachjam_judgment_changelist")

form = self.app.get(judgment_add_url).form

form["jurisdiction"] = "ZA"
form["court"] = "1"
form["language"] = "en"
form["case_name"] = "test case"

# date uses a multi-field widget
form["date_0"] = "25"
form["date_1"] = "3"
form["date_2"] = "1999"

# upload file
with open(
os.path.abspath("peachjam/fixtures/source_files/gauteng_judgment.pdf"), "rb"
) as pdf_file:
pdf_file_content = pdf_file.read()

form["upload_file"] = Upload(
"upload_pdf.pdf", pdf_file_content, "application/pdf"
)

response = form.submit()
self.assertRedirects(response, judgment_list_url)

judgment = Judgment.objects.filter(
expression_frbr_uri="/akn/za/judgment/eacj/1999/1/eng@1999-03-25"
).first()
self.assertIsNotNone(judgment)

# check if content_html has been extracted
self.assertEqual(
"test-case-1999-eacj-1-25-march-1999.pdf",
judgment.source_file.filename,
)

# swap source file with pdf
judgment_change_url = reverse(
"admin:peachjam_judgment_change", kwargs={"object_id": judgment.pk}
)

with open(
os.path.abspath("peachjam/fixtures/source_files/zagpjhc_judgment.docx"),
"rb",
) as docx_file:
docx_file_content = docx_file.read()

form2 = self.app.get(judgment_change_url).form
form2["source_file-0-file"] = Upload(
"file.docx",
docx_file_content,
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
)

response2 = form2.submit()
self.assertRedirects(response2, judgment_list_url)

judgment.refresh_from_db()
self.assertIn(
"The second count is robbery, in that on or about near the place mentioned in count",
judgment.content_html,
)
self.assertEqual(
"test-case-1999-eacj-1-25-march-1999.docx", judgment.source_file.filename
)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ dependencies = [
"django-sass-processor-dart-sass>=0.1.0",
"django-storages>=1.12.3",
"django-treebeard>=4.5.1",
"django-webtest>=1.9.11",
"djangorestframework>=3.13.1",
"drf-spectacular>=0.26.0",
"docpipe @ git+https://github.com/laws-africa/docpipe.git@dadd9683eb946066816ad1250c098ff96686eb5f",
Expand Down

0 comments on commit 4103b05

Please sign in to comment.