Skip to content

Commit 8755f10

Browse files
committed
PRT-289 Creates Portal 404 page; fixes tests
1 parent 5735aa8 commit 8755f10

File tree

6 files changed

+70
-44
lines changed

6 files changed

+70
-44
lines changed

Diff for: experiments/tests/test_views.py

+5
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,11 @@ def test_access_experiment_detail_with_wrong_url_returns_404_http_status_code(se
447447
slug = str(Experiment.objects.first().slug)[:-3]
448448
response = self.client.get('/experiments/' + slug + '/')
449449
self.assertEqual(response.status_code, 404)
450+
self.assertIn('404 - Not Found', response.content.decode())
451+
self.assertIn(
452+
'Neuroscience Experiments Database', response.content.decode(),
453+
)
454+
self.assertIn('Related Projects', response.content.decode())
450455

451456
def test_uses_detail_template(self):
452457
slug = str(Experiment.objects.first().slug)

Diff for: experiments/views.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
from django.core.exceptions import PermissionDenied
1111
from django.core.mail import send_mail
1212
from django.db.models import Count
13-
from django.http import HttpResponseRedirect, HttpResponse
14-
from django.shortcuts import render, get_object_or_404
13+
from django.http import HttpResponseRedirect, HttpResponse, Http404
14+
from django.shortcuts import render
1515
from django.template.loader import render_to_string
1616
from django.utils.text import slugify
1717
from django.utils.translation import activate, LANGUAGE_SESSION_KEY, \
@@ -214,7 +214,10 @@ def experiment_detail(request, slug):
214214
to_be_analysed_count = all_experiments.filter(
215215
status=Experiment.TO_BE_ANALYSED).count()
216216

217-
experiment = get_object_or_404(Experiment, slug=slug)
217+
try:
218+
experiment = Experiment.objects.get(slug=slug)
219+
except Experiment.DoesNotExist:
220+
raise Http404('404 - Not Found')
218221

219222
gender_grouping = {}
220223
age_grouping = {}

Diff for: functional_tests/test_experiment_detail.py

+12-37
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,10 @@ def test_can_view_detail_page(self):
133133
study_start_date = self.browser.find_element_by_id(
134134
'study_startdate').text
135135
self.assertIn('Start date:', study_start_date)
136-
##
137-
# To conform to study_start_date format in template. For month
138-
# September (9) we have to do something more.
139-
# TODO: search for better solution
140-
##
141-
if self.experiment.study.start_date.month == 9:
142-
strdate = self.experiment.study.start_date.strftime(
143-
"%b. %d, %Y"
144-
).lstrip("0").replace(". ", "t. ")
145-
else:
146-
strdate = self.experiment.study.start_date.strftime(
147-
"%b. %d, %Y"
148-
).lstrip("0").replace(" 0", " ")
149-
self.assertIn(strdate, study_start_date)
136+
self.assertIn(
137+
self.experiment.study.start_date.strftime("%B %d, %Y"),
138+
study_start_date
139+
)
150140
study_end_date = self.browser.find_element_by_id(
151141
'study_enddate').text
152142
self.assertIn('End date:', study_end_date)
@@ -202,7 +192,8 @@ def test_can_view_detail_page(self):
202192
# expands displaying textual representation of the experimental
203193
# protocol.
204194
group = self.experiment.groups.first()
205-
link_details = self.browser.find_element_by_link_text('Details')
195+
link_details = self.browser.find_element_by_xpath(
196+
"//a[@href='#collapse_group" + str(group.id) + "']")
206197
link_details.click()
207198

208199
self.wait_for(lambda: self.assertIn(
@@ -1478,13 +1469,6 @@ def test_clicking_in_download_all_experiment_data_link_pops_up_a_modal_with_lice
14781469
# In the modal there is also how to cite that experiment in her own
14791470
# work
14801471
self.assertIn('How to cite this experiment:', license_modal.text)
1481-
##
1482-
# this is to mimic how datetime is displayed in template by default
1483-
##
1484-
sent_date = \
1485-
self.experiment.sent_date.strftime("%b. %d, %Y").lstrip("0").replace(
1486-
" 0", " "
1487-
)
14881472
self.assertIn(
14891473
'das Dores'.upper() + ', Anibal', license_modal.text
14901474
)
@@ -1495,7 +1479,8 @@ def test_clicking_in_download_all_experiment_data_link_pops_up_a_modal_with_lice
14951479
'Farias'.upper() + ', Antônio', license_modal.text
14961480
)
14971481
self.assertIn(
1498-
self.experiment.title + '. Sent date: ' + str(sent_date),
1482+
self.experiment.title + '. Sent date: ' + str(
1483+
self.experiment.sent_date.strftime('%B %d, %Y')),
14991484
license_modal.text
15001485
)
15011486
self.assertNotIn(
@@ -1532,20 +1517,14 @@ def test_clicking_in_download_all_experiment_data_link_pops_up_a_modal_with_lice
15321517
# In the modal there is also how to cite that experiment in her own
15331518
# work
15341519
self.assertIn('How to cite this experiment:', license_modal.text)
1535-
##
1536-
# this is to mimic how datetime is displayed in template by default
1537-
##
1538-
sent_date = \
1539-
self.experiment.sent_date.strftime("%b. %d, %Y").lstrip("0").replace(
1540-
" 0", " ")
1541-
15421520
self.assertIn(
15431521
self.experiment.study.researcher.last_name.upper() + ', ' +
15441522
self.experiment.study.researcher.first_name,
15451523
license_modal.text
15461524
)
15471525
self.assertIn(
1548-
self.experiment.title + '. Sent date: ' + str(sent_date) + '.',
1526+
self.experiment.title + '. Sent date: ' +
1527+
self.experiment.sent_date.strftime('%B %d, %Y') + '.',
15491528
license_modal.text
15501529
)
15511530

@@ -1613,9 +1592,7 @@ def test_how_to_cite_in_license_modal_is_equal_to_how_to_cite_in_citation_file_1
16131592
self.assertIn(
16141593
'SORIANO, Valdick. ' + self.experiment.title
16151594
+ '. Sent date: '
1616-
+ str(self.experiment.sent_date.strftime('%b. %d, %Y').lstrip('0').replace(
1617-
' 0', ' '
1618-
)),
1595+
+ self.experiment.sent_date.strftime('%B %d, %Y'),
16191596
license_modal.text
16201597
)
16211598

@@ -1672,9 +1649,7 @@ def test_how_to_cite_in_license_modal_is_equal_to_how_to_cite_in_citation_file_2
16721649
self.assertIn(
16731650
'COSTA, Edimilson; ROSS B., Diana; BOULOS, Guilherme. '
16741651
+ self.experiment.title + '. Sent date: '
1675-
+ str(self.experiment.sent_date.strftime('%b. %d, %Y').lstrip('0').replace(
1676-
' 0', ' '
1677-
)),
1652+
+ self.experiment.sent_date.strftime('%B %d, %Y'),
16781653
license_modal.text
16791654
)
16801655

Diff for: functional_tests/test_list_last_version_experiments.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,13 @@ def test_can_view_home_page_when_there_is_new_version_not_approved(self):
172172
str(self.experiment.version) for row in rows)
173173
)
174174

175-
def test_view_404_http_status_code_when_trying_access_not_existent_page(self):
175+
def test_when_trying_to_access_not_existent_detail_page_display_404_not_found_page(self):
176176
# Josenilda is trying to access her experiment that she recently
177177
# sent to Portal, but she types the URL address incorrectly in
178-
# Browser, so she sees a 404 error page
178+
# Browser, so she sees the 404 error page
179179
broken_url = self.experiment.slug[:-3]
180180
self.browser.get(self.live_server_url + '/experiments/' + broken_url)
181181
body = self.browser.find_element_by_tag_name('body')
182-
self.assertIn('Not Found', body.text)
182+
self.assertIn('404 - Not Found', body.text)
183+
self.assertIn('Neuroscience Experiments Database', body.text)
184+
self.assertIn('RELATED PROJECTS', body.text)

Diff for: nep/settings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
TEMPLATES = [
8787
{
8888
'BACKEND': 'django.template.backends.django.DjangoTemplates',
89-
'DIRS': [],
89+
'DIRS': [os.path.join(BASE_DIR, 'templates')],
9090
'APP_DIRS': True,
9191
'OPTIONS': {
9292
'context_processors': [

Diff for: templates/404.html

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{% extends 'experiments/base.html' %}
2+
3+
{% block style %}
4+
{% load static %}
5+
{# define id to functional tests #}
6+
<link id="id_bs_select_css" rel="stylesheet" href="{% static 'bootstrap-select-1.12.4-dist/css/bootstrap-select.min.css' %}">
7+
{% endblock %}
8+
9+
{% block header %}
10+
{% load i18n %}
11+
{% include 'experiments/header_home.html' %}
12+
{% endblock %}
13+
14+
{% block content %}
15+
<section class="container nep-content">
16+
{% if messages %}
17+
{% include 'experiments/messages.html' %}
18+
{% endif %}
19+
<h1 align="center">404 - Not Found</h1>
20+
</section>
21+
{% endblock %}
22+
23+
{% block script %}
24+
<script src="{% url 'javascript-catalog' %}"></script>
25+
{##}
26+
{## Scripts related to experiments/header_home.html #}
27+
{##}
28+
{% load static %}
29+
{# define id for functional tests #}
30+
<script id="id_bs_select_js" src="{% static 'bootstrap-select-1.12.4-dist/js/bootstrap-select.js' %}"></script>
31+
{##}
32+
{## Script related to experiments/top.html #}
33+
{##}
34+
<script>
35+
let to_be_analysed_count = {{ to_be_analysed_count }};
36+
if (to_be_analysed_count) {
37+
$('head').append('<style>.badger::after{content:"' +
38+
to_be_analysed_count + '"}</style>');
39+
}
40+
</script>
41+
{% endblock %}

0 commit comments

Comments
 (0)