-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify the CCDB landing page view and template (#6684)
* Simplify the CCDB landing page view and template The CCDB legacy landing view contained a routine for ensuring that sample complaint narratives and data were available and up-to-date, and it handled conditions when different banner messages would be displayed. This can now be simplified. Complaint narrative samples are no longer used on the landing page and don't need to be checked. In addition, the check for stale data never worked, because of a bug. The code was checking for a nonexistent [response.status](https://github.com/cfpb/consumerfinance.gov/blob/main/cfgov/legacy/views/complaint.py#L48) value instead of `response.status_code`, and always returned blank json. Data for the page's high-charts map and metadata for staleness were moved to S3, where they are updated every morning. Those resources are called from the page. All this allows us to stop making view calls to Elasticsearch entirely. The view just needs to deliver the news when the CCDB_TECHNICAL_ISSUES flag is activated, and that's all we need to test now. Testing - Pull in the branch and check that the CCDB landing page renders locally at http://localhost:8000/data-research/consumer-complaints/ - in the local Wagtail admin, go to /admin/flags/ and enable the flag ` CCDB_TECHNICAL_ISSUES` - The landing page should now show a warning dialog below the hero: <img width="1223" alt="ccdb_warning" src="https://user-images.githubusercontent.com/515885/133856030-9ca0bbc1-b8fe-4ca5-a8d7-b51edd10fefa.png"> * remove unneeded classes and css * remove space after class * have test look for different class
- Loading branch information
Showing
4 changed files
with
25 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,116 +1,22 @@ | ||
from datetime import datetime, timedelta | ||
from unittest.mock import patch | ||
|
||
from django.test import RequestFactory, TestCase, override_settings | ||
|
||
from complaint_search import views as ComplaintViews | ||
from rest_framework.response import Response | ||
|
||
from legacy.views.complaint import ComplaintLandingView | ||
from django.test import TestCase, override_settings | ||
from django.urls import reverse | ||
|
||
|
||
class ComplaintLandingViewTests(TestCase): | ||
test_url = 'https://test.url/foo.json' | ||
|
||
def two_days_ago(self): | ||
return (datetime.now() - timedelta(2)).strftime("%Y-%m-%d") | ||
|
||
def setUp(self): | ||
self.request = RequestFactory().get('/') | ||
|
||
def assertNoBanner(self, response): | ||
self.assertNotContains(response, 'show-') | ||
|
||
@override_settings(COMPLAINT_LANDING_STATS_SOURCE=None) | ||
def test_no_stats_source(self): | ||
response = ComplaintLandingView.as_view()(self.request) | ||
self.assertNoBanner(response) | ||
|
||
@patch.object(ComplaintViews, 'search') | ||
def test_elasticsearch_down(self, mock_view): | ||
response = Response({}) | ||
response.status = 404 | ||
mock_view.return_value = response | ||
response = ComplaintLandingView.as_view()(self.request) | ||
self.assertTrue(mock_view.call_count == 1) | ||
self.assertNoBanner(response) | ||
|
||
@patch.object(ComplaintViews, 'search') | ||
def test_data_up_to_date(self, mock_view): | ||
data_json = { | ||
'_meta': { | ||
'last_indexed': self.two_days_ago(), | ||
'last_updated': self.two_days_ago(), | ||
}, | ||
} | ||
response = Response(data_json) | ||
mock_view.return_value = response | ||
response = ComplaintLandingView.as_view()(self.request) | ||
self.assertTrue(mock_view.call_count == 1) | ||
self.assertNoBanner(response) | ||
|
||
@patch.object(ComplaintViews, 'search') | ||
def test_data_out_of_date(self, mock_view): | ||
data_json = { | ||
'_meta': { | ||
'last_indexed': '2010-01-01', | ||
'last_updated': self.two_days_ago(), | ||
} | ||
} | ||
response = Response(data_json) | ||
response.status = 200 | ||
mock_view.return_value = response | ||
response = ComplaintLandingView.as_view()(self.request) | ||
self.assertTrue(mock_view.call_count == 1) | ||
self.assertContains(response, 'show-data-notification') | ||
|
||
@patch.object(ComplaintViews, 'search') | ||
def test_narratives_out_of_date(self, mock_view): | ||
data_json = { | ||
'_meta': { | ||
'last_indexed': self.two_days_ago(), | ||
'last_updated': '2010-01-01', | ||
}, | ||
} | ||
response = Response(data_json) | ||
mock_view.return_value = response | ||
response.status = 200 | ||
response = ComplaintLandingView.as_view()(self.request) | ||
self.assertTrue(mock_view.call_count == 1) | ||
self.assertContains(response, 'show-narratives-notification') | ||
|
||
@patch.object(ComplaintViews, 'search') | ||
def test_no_banner_when_data_invalid(self, mock_view): | ||
data_json = { | ||
'wrong_key': 5 | ||
} | ||
response = Response(data_json) | ||
mock_view.return_value = response | ||
response = ComplaintLandingView.as_view()(self.request) | ||
self.assertTrue(mock_view.call_count == 1) | ||
self.assertNoBanner(response) | ||
|
||
@patch.object(ComplaintViews, 'search') | ||
def test_no_banner_when_data_not_json(self, mock_view): | ||
data_json = "not json" | ||
response = Response(data_json) | ||
mock_view.return_value = response | ||
response = ComplaintLandingView.as_view()(self.request) | ||
self.assertTrue(mock_view.call_count == 1) | ||
self.assertNoBanner(response) | ||
|
||
@patch.object(ComplaintViews, 'search') | ||
def test_no_banner_when_data_fetch_fails(self, mock_view): | ||
mock_view.return_value = Response('bleh') | ||
mock_view.side_effect = ValueError("test value error") | ||
response = ComplaintLandingView.as_view()(self.request) | ||
self.assertTrue(mock_view.call_count == 1) | ||
self.assertNoBanner(response) | ||
|
||
@patch.object(ComplaintViews, 'search') | ||
def test_no_banner_when_key_error(self, mock_view): | ||
mock_view.return_value = Response('bleh') | ||
mock_view.side_effect = KeyError("test key error") | ||
response = ComplaintLandingView.as_view()(self.request) | ||
self.assertTrue(mock_view.call_count == 1) | ||
self.assertNoBanner(response) | ||
self.landing_url = reverse('complaint-landing') | ||
|
||
@override_settings( | ||
FLAGS={"CCDB_TECHNICAL_ISSUES": [("boolean", False)]}) | ||
def test_no_banner_when_flag_disabled(self): | ||
response = self.client.get(self.landing_url) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertNotContains(response, 'm-notification_explanation') | ||
|
||
@override_settings( | ||
FLAGS={"CCDB_TECHNICAL_ISSUES": [("boolean", True)]}) | ||
def test_banner_when_flag_enabled(self): | ||
response = self.client.get(self.landing_url) | ||
self.assertEqual(response.status_code, 200) | ||
self.assertContains(response, 'm-notification_explanation') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters