Skip to content

Commit 957aa0a

Browse files
committed
Improved logging for the research_assistant app.
1 parent bf56b88 commit 957aa0a

File tree

6 files changed

+250
-149
lines changed

6 files changed

+250
-149
lines changed

src/encryption_compendium/settings.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,47 @@
248248
EMAIL_USE_TLS = False
249249
else:
250250
raise Exception("EMAIL_USE_TLS should be 'yes' or 'no'.")
251+
252+
### Logging configuration
253+
LOGGING = {
254+
"version": 1,
255+
"disable_existing_loggers": False,
256+
"formatters": {
257+
"default": {
258+
"format": "[{asctime}] [{process:d}] [{levelname}] {message}",
259+
"style": "{",
260+
},
261+
"verbose": {
262+
"format": "[{asctime}] {module} {process:d} {thread:d} [{levelname}] {message}",
263+
"style": "{",
264+
},
265+
},
266+
"handlers": {
267+
"console": {"class": "logging.StreamHandler", "formatter": "default",},
268+
"console_debug": {"class": "logging.StreamHandler", "formatter": "verbose",},
269+
},
270+
"root": {"handlers": ["console"], "level": "INFO" if DEBUG else "WARNING"},
271+
"loggers": {
272+
# Search loggers
273+
"search": {
274+
"handlers": ["console"],
275+
"level": os.getenv("DJANGO_SEARCH_LOGLEVEL", "INFO"),
276+
},
277+
"search.solr": {
278+
"handlers": ["console"],
279+
"level": os.getenv("DJANGO_SEARCH_LOGLEVEL", "DEBUG" if DEBUG else "INFO"),
280+
"propagate": False,
281+
},
282+
# Authentication loggers
283+
"auth": {
284+
"handlers": ["console"],
285+
"level": os.getenv("DJANGO_AUTH_LOGLEVEL", "DEBUG" if DEBUG else "INFO"),
286+
},
287+
"auth.login": {"handlers": ["console"], "level": "INFO",},
288+
# Compendium loggers
289+
"compendium": {
290+
"handlers": ["console"],
291+
"level": os.getenv("DJANGO_COMPENDIUM_LOGLEVEL", "INFO"),
292+
},
293+
},
294+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
"""
2+
Tests for views that are used to modify the compendium.
3+
"""
4+
5+
import os
6+
from django.urls import reverse
7+
from django.test import tag
8+
from entries.models import CompendiumEntry, CompendiumEntryTag, Author
9+
from utils.test_utils import UnitTest
10+
11+
12+
@tag("compendium-modification")
13+
class AddNewCompendiumEntryTestCase(UnitTest):
14+
"""
15+
Test the view that presents a form for manually creating a new compendium
16+
entry.
17+
"""
18+
19+
def setUp(self):
20+
super().setUp(preauth=True)
21+
self.new_entry_page = reverse("research new article")
22+
23+
# Add three tags for testing purposes
24+
for tagname in ("test_tag_A", "test_tag_B", "test_tag_C"):
25+
if not CompendiumEntryTag.objects.filter(tagname=tagname).exists():
26+
CompendiumEntryTag.objects.create(tagname=tagname)
27+
28+
def test_page_templates(self):
29+
# Ensure that the page uses the correct templates in its response
30+
response = self.client.get(self.new_entry_page)
31+
self.assertTemplateUsed("dashboard_base.html")
32+
self.assertTemplateUsed("new_article.html")
33+
34+
@tag("tags")
35+
def test_add_new_compendium_entry(self):
36+
self.assertEqual(len(CompendiumEntry.objects.all()), 0)
37+
38+
# Retrieve the tag ID for "test_tag_B"
39+
tag_id = CompendiumEntryTag.objects.get(tagname="test_tag_B").id
40+
41+
# publisher information
42+
publisher = random_username(self.rd)
43+
44+
# published date
45+
year = random.randrange(1900, datetime.date.today().year)
46+
month = random.randrange(1, 12)
47+
day = random.randrange(1, 31)
48+
49+
# Send POST data to the URL to create a new entry and ensure that
50+
# the entry was created correctly.
51+
data = {
52+
"title": "New compendium entry",
53+
"abstract": "Abstract for new entry",
54+
"url": "https://example.com",
55+
"tags": [tag_id],
56+
"publisher_text": publisher,
57+
"year": year,
58+
"month": month,
59+
"day": day,
60+
"edit-entry": "",
61+
}
62+
response = self.client.post(self.new_entry_page, data)
63+
self.assertTrue(len(CompendiumEntry.objects.all()), 1)
64+
65+
entry = CompendiumEntry.objects.get(title="New compendium entry")
66+
self.assertEqual(entry.owner, self.user)
67+
self.assertEqual(entry.title, "New compendium entry")
68+
self.assertEqual(entry.abstract, "Abstract for new entry")
69+
self.assertEqual(entry.url, "https://example.com")
70+
self.assertEqual(len(entry.tags.all()), 1)
71+
self.assertEqual(entry.tags.get().tagname, "test_tag_B")
72+
self.assertEqual(entry.publisher.publishername, publisher)
73+
self.assertEqual(entry.year, year)
74+
self.assertEqual(entry.month, month)
75+
self.assertEqual(entry.day, day)
76+
77+
@tag("tags")
78+
def test_add_compendium_entry_with_multiple_tags(self):
79+
"""Create a CompendiumEntry with multiple tags"""
80+
self.assertEqual(len(CompendiumEntry.objects.all()), 0)
81+
82+
# Retrieve tag IDs for "test_tag_B" and "test_tag_C"
83+
id_A = CompendiumEntryTag.objects.get(tagname="test_tag_A").id
84+
id_C = CompendiumEntryTag.objects.get(tagname="test_tag_C").id
85+
86+
# Send POST data with multiple tag IDs
87+
data = {
88+
"title": "New compendium entry",
89+
"tags": [id_A, id_C],
90+
"edit-entry": "",
91+
}
92+
self.client.post(self.new_entry_page, data)
93+
self.assertEqual(len(CompendiumEntry.objects.all()), 1)
94+
95+
entry = CompendiumEntry.objects.get(title="New compendium entry")
96+
self.assertEqual(len(entry.tags.all()), 2)
97+
self.assertTrue(entry.tags.filter(tagname="test_tag_A").exists())
98+
self.assertFalse(entry.tags.filter(tagname="test_tag_B").exists())
99+
self.assertTrue(entry.tags.filter(tagname="test_tag_C").exists())
100+
101+
def test_attempt_to_create_entry_with_empty_title(self):
102+
"""New entries must have a title"""
103+
self.assertEqual(len(CompendiumEntry.objects.all()), 0)
104+
tag_id = CompendiumEntryTag.objects.get(tagname="test_tag_A").id
105+
106+
### Try to create an entry without a title
107+
data = {
108+
"tags": [tag_id],
109+
}
110+
response = self.client.post(self.new_entry_page, data)
111+
self.assertEqual(len(CompendiumEntry.objects.all()), 0)
112+
113+
### Try to create an entry with an empty title
114+
data = {
115+
"title": "",
116+
"tags": [tag_id],
117+
}
118+
response = self.client.post(self.new_entry_page, data)
119+
self.assertEqual(len(CompendiumEntry.objects.all()), 0)
120+
121+
def test_attempt_to_create_entry_without_tags(self):
122+
"""New entries must have _at least_ one tag"""
123+
self.assertEqual(len(CompendiumEntry.objects.all()), 0)
124+
125+
data = {
126+
"title": "New compendium entry",
127+
}
128+
self.client.post(self.new_entry_page, data)
129+
self.assertEqual(len(CompendiumEntry.objects.all()), 0)
130+
131+
132+
### TODO
133+
134+
135+
@tag("compendium-modification")
136+
class UploadBibTexViewTestCase(UnitTest):
137+
"""
138+
Test the view that's involved in handling BibTeX uploads to the site for
139+
adding new compendium entries.
140+
"""
141+
142+
def setUp(self):
143+
super().setUp(preauth=True)
144+
145+
# Load in a test .bib file
146+
# self.test_filename = os.

src/research_assistant/tests/test_views.py

Lines changed: 8 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,11 @@ def test_no_redirect_login_to_logout(self):
244244
self.assertTrue(get_user(self.client).is_authenticated)
245245

246246

247-
"""
248-
---------------------------------------------------
249-
Tests for dashboard view
250-
---------------------------------------------------
251-
"""
252-
253-
254247
class DashboardTestCase(UnitTest):
248+
"""
249+
Tests for the dashboard homepage.
250+
"""
251+
255252
def setUp(self):
256253
super().setUp(create_user=True)
257254

@@ -304,130 +301,12 @@ def test_all_compendium_entries_visible_on_dashboard(self):
304301
self.assertEqual(entry["owner"], self.user)
305302

306303

307-
"""
308-
---------------------------------------------------
309-
Tests for adding new tags and compendium entries
310-
---------------------------------------------------
311-
"""
312-
313-
314-
@tag("compendium-entries")
315-
class AddNewCompendiumEntryTestCase(UnitTest):
316-
def setUp(self):
317-
super().setUp(preauth=True)
318-
self.new_entry_page = reverse("research new article")
319-
320-
# Add three tags for testing purposes
321-
for tagname in ("test_tag_A", "test_tag_B", "test_tag_C"):
322-
if not CompendiumEntryTag.objects.filter(tagname=tagname).exists():
323-
CompendiumEntryTag.objects.create(tagname=tagname)
324-
325-
def test_page_templates(self):
326-
# Ensure that the page uses the correct templates in its response
327-
response = self.client.get(self.new_entry_page)
328-
self.assertTemplateUsed("dashboard_base.html")
329-
self.assertTemplateUsed("new_article.html")
330-
331-
@tag("tags")
332-
def test_add_new_compendium_entry(self):
333-
self.assertEqual(len(CompendiumEntry.objects.all()), 0)
334-
335-
# Retrieve the tag ID for "test_tag_B"
336-
tag_id = CompendiumEntryTag.objects.get(tagname="test_tag_B").id
337-
338-
# publisher information
339-
publisher = random_username(self.rd)
340-
341-
# published date
342-
year = random.randrange(1900, datetime.date.today().year)
343-
month = random.randrange(1, 12)
344-
day = random.randrange(1, 31)
345-
346-
# Send POST data to the URL to create a new entry and ensure that
347-
# the entry was created correctly.
348-
data = {
349-
"title": "New compendium entry",
350-
"abstract": "Abstract for new entry",
351-
"url": "https://example.com",
352-
"tags": [tag_id],
353-
"publisher_text": publisher,
354-
"year": year,
355-
"month": month,
356-
"day": day,
357-
"edit-entry": "",
358-
}
359-
response = self.client.post(self.new_entry_page, data)
360-
self.assertTrue(len(CompendiumEntry.objects.all()), 1)
361-
362-
entry = CompendiumEntry.objects.get(title="New compendium entry")
363-
self.assertEqual(entry.owner, self.user)
364-
self.assertEqual(entry.title, "New compendium entry")
365-
self.assertEqual(entry.abstract, "Abstract for new entry")
366-
self.assertEqual(entry.url, "https://example.com")
367-
self.assertEqual(len(entry.tags.all()), 1)
368-
self.assertEqual(entry.tags.get().tagname, "test_tag_B")
369-
self.assertEqual(entry.publisher.publishername, publisher)
370-
self.assertEqual(entry.year, year)
371-
self.assertEqual(entry.month, month)
372-
self.assertEqual(entry.day, day)
373-
374-
@tag("tags")
375-
def test_add_compendium_entry_with_multiple_tags(self):
376-
"""Create a CompendiumEntry with multiple tags"""
377-
self.assertEqual(len(CompendiumEntry.objects.all()), 0)
378-
379-
# Retrieve tag IDs for "test_tag_B" and "test_tag_C"
380-
id_A = CompendiumEntryTag.objects.get(tagname="test_tag_A").id
381-
id_C = CompendiumEntryTag.objects.get(tagname="test_tag_C").id
382-
383-
# Send POST data with multiple tag IDs
384-
data = {
385-
"title": "New compendium entry",
386-
"tags": [id_A, id_C],
387-
"edit-entry": "",
388-
}
389-
self.client.post(self.new_entry_page, data)
390-
self.assertEqual(len(CompendiumEntry.objects.all()), 1)
391-
392-
entry = CompendiumEntry.objects.get(title="New compendium entry")
393-
self.assertEqual(len(entry.tags.all()), 2)
394-
self.assertTrue(entry.tags.filter(tagname="test_tag_A").exists())
395-
self.assertFalse(entry.tags.filter(tagname="test_tag_B").exists())
396-
self.assertTrue(entry.tags.filter(tagname="test_tag_C").exists())
397-
398-
def test_attempt_to_create_entry_with_empty_title(self):
399-
"""New entries must have a title"""
400-
self.assertEqual(len(CompendiumEntry.objects.all()), 0)
401-
tag_id = CompendiumEntryTag.objects.get(tagname="test_tag_A").id
402-
403-
### Try to create an entry without a title
404-
data = {
405-
"tags": [tag_id],
406-
}
407-
response = self.client.post(self.new_entry_page, data)
408-
self.assertEqual(len(CompendiumEntry.objects.all()), 0)
409-
410-
### Try to create an entry with an empty title
411-
data = {
412-
"title": "",
413-
"tags": [tag_id],
414-
}
415-
response = self.client.post(self.new_entry_page, data)
416-
self.assertEqual(len(CompendiumEntry.objects.all()), 0)
417-
418-
def test_attempt_to_create_entry_without_tags(self):
419-
"""New entries must have _at least_ one tag"""
420-
self.assertEqual(len(CompendiumEntry.objects.all()), 0)
421-
422-
data = {
423-
"title": "New compendium entry",
424-
}
425-
self.client.post(self.new_entry_page, data)
426-
self.assertEqual(len(CompendiumEntry.objects.all()), 0)
427-
428-
429304
@tag("compendium-entries", "tags")
430305
class NewTagTestCase(UnitTest):
306+
"""
307+
Tests for the view to add a new tag to the compendium.
308+
"""
309+
431310
def setUp(self):
432311
super().setUp(preauth=True)
433312
self.new_tag_page = reverse("research add tag")

src/research_assistant/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
url(r"^add-user", add_new_user, name="add new user"),
88
url(r"^sign-up", sign_up, name="sign up"),
99
url(r"^dashboard", research_dashboard, name="research dashboard"),
10-
url(r"^login", research_login, name="research login"),
10+
url(r"^login", LoginView.as_view(), name="research login"),
1111
url(r"^logout", research_logout, name="research logout"),
1212
url(r"^new-article", NewCompendiumEntryView.as_view(), name="research new article"),
1313
url(r"^new-tag", research_add_tag, name="research add tag"),

0 commit comments

Comments
 (0)