from __future__ import unicode_literals

import json

from django.apps import apps
from django.core.urlresolvers import NoReverseMatch, reverse
from django.http import Http404, HttpRequest, QueryDict
from django.test import TestCase, override_settings
from django.utils import timezone

from wagtail.wagtailcore.models import Site
from wagtailsharing.models import SharingSite

import mock
from model_mommy import mommy

from ask_cfpb.models import ENGLISH_PARENT_SLUG, SPANISH_PARENT_SLUG
from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search
from v1.util.migrations import get_or_create_page


now = timezone.now()


class AnswerPagePreviewCase(TestCase):

    def setUp(self):
        from v1.models import HomePage
        from ask_cfpb.models import Answer
        self.ROOT_PAGE = HomePage.objects.get(slug='cfgov')
        self.english_parent_page = get_or_create_page(
            apps,
            'ask_cfpb',
            'AnswerLandingPage',
            'Ask CFPB',
            ENGLISH_PARENT_SLUG,
            self.ROOT_PAGE,
            language='en',
            live=True)
        self.spanish_parent_page = get_or_create_page(
            apps,
            'ask_cfpb',
            'AnswerLandingPage',
            'Obtener respuestas',
            SPANISH_PARENT_SLUG,
            self.ROOT_PAGE,
            language='es',
            live=True)
        self.test_answer = mommy.make(
            Answer,
            answer="Test answer.",
            question="Test question.",
            slug='test-question',
            update_english_page=True,
            update_spanish_page=False)
        self.site = mommy.make(
            Site,
            root_page=self.ROOT_PAGE,
            hostname='localhost',
            port=8000,
            is_default_site=True)
        self.sharing_site = mommy.make(
            SharingSite,
            site=self.site,
            hostname='preview.localhost',
            port=8000)

    @mock.patch('ask_cfpb.views.ServeView.serve_latest_revision')
    def test_preview_page(self, mock_serve):
        from ask_cfpb.views import view_answer
        page = self.test_answer.english_page
        revision = page.save_revision()
        revision.publish()
        test_request = HttpRequest()
        test_request.META['SERVER_NAME'] = 'preview.localhost'
        test_request.META['SERVER_PORT'] = 8000
        view_answer(
            test_request, 'test-question', 'en', self.test_answer.pk)
        self.assertEqual(mock_serve.call_count, 1)

    def test_answer_page_not_live(self):
        from ask_cfpb.views import view_answer
        page = self.test_answer.english_page
        page.live = False
        page.save()
        test_request = HttpRequest()
        with self.assertRaises(Http404):
            view_answer(
                test_request,
                'test-question',
                'en',
                self.test_answer.pk)


class AnswerViewTestCase(TestCase):

    def setUp(self):
        from v1.models import HomePage
        self.ROOT_PAGE = HomePage.objects.get(slug='cfgov')
        self.english_parent_page = get_or_create_page(
            apps,
            'ask_cfpb',
            'AnswerLandingPage',
            'Ask CFPB',
            ENGLISH_PARENT_SLUG,
            self.ROOT_PAGE,
            language='en',
            live=True)
        self.spanish_parent_page = get_or_create_page(
            apps,
            'ask_cfpb',
            'AnswerLandingPage',
            'Obtener respuestas',
            SPANISH_PARENT_SLUG,
            self.ROOT_PAGE,
            language='es',
            live=True)

    def test_annotate_links(self):
        mock_answer = (
            '<p>Answer with a <a href="http://fake.com">fake link.</a></p>')
        (annotated_answer, links) = annotate_links(mock_answer)
        self.assertEqual(
            annotated_answer,
            '<html><body><p>Answer with a <a href="http://fake.com">fake '
            'link.</a><sup>1</sup></p></body></html>')
        self.assertEqual(links, [(1, str('http://fake.com'))])

    def test_annotate_links_no_href(self):
        mock_answer = (
            '<p>Answer with a <a>fake link.</a></p>')
        (annotated_answer, links) = annotate_links(mock_answer)
        self.assertEqual(links, [])

    def test_annotate_links_no_site(self):
        site = Site.objects.get(is_default_site=True)
        site.is_default_site = False
        site.save()
        with self.assertRaises(RuntimeError) as context:
            annotate_links('answer')
        self.assertIn('no default wagtail site', str(context.exception))

    def test_bad_language_search(self):
        with self.assertRaises(NoReverseMatch):
            self.client.get(reverse(
                'ask-search-en',
                kwargs={'language': 'zz'}), {'q': 'payday'})

    @mock.patch('ask_cfpb.views.SearchQuerySet.filter')
    def test_en_search_results_page_not_created(self, mock_filter):
        mock_queryset = mock.Mock()
        mock_queryset.count.return_value = 0
        mock_filter.return_value = [mock_queryset]
        response = self.client.get(reverse(
            'ask-search-en'), {'q': 'payday'})
        self.assertEqual(mock_filter.call_count, 1)
        self.assertTrue(mock_filter.called_with(language='en', q='payday'))
        self.assertEqual(response.status_code, 404)

    @mock.patch('ask_cfpb.views.SearchQuerySet')
    def test_en_search(self, mock_sqs):
        from v1.util.migrations import get_or_create_page
        mock_page = get_or_create_page(
            apps,
            'ask_cfpb',
            'AnswerResultsPage',
            'Mock results page',
            'ask-cfpb-search-results',
            self.ROOT_PAGE,
            language='en')

        mock_return = mock.Mock()
        mock_return.url = 'mockcfpb.gov'
        mock_return.autocomplete = 'A mock question'
        mock_return.text = 'Mock answer text.'

        mock_queryset = mock.Mock()
        mock_queryset.__iter__ = mock.Mock(return_value=iter([mock_return]))
        mock_queryset.count.return_value = 1

        mock_sqs_instance = mock_sqs.return_value.models.return_value
        mock_sqs_instance.filter.return_value = mock_queryset
        mock_sqs_instance.spelling_suggestion.return_value = 'payday'

        response = self.client.get(reverse(
            'ask-search-en'), {'q': 'payday'})
        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.context_data['page'],
            mock_page)
        self.assertEqual(
            response.context_data['page'].suggestion,
            None)
        self.assertEqual(mock_sqs_instance.filter.call_count, 1)
        self.assertTrue(mock_sqs_instance.filter.called_with(
            language='en', q='payday'))

    @mock.patch('ask_cfpb.views.SearchQuerySet')
    def test_en_search_no_term(self, mock_sqs):
        from v1.util.migrations import get_or_create_page
        mock_page = get_or_create_page(
            apps,
            'ask_cfpb',
            'AnswerResultsPage',
            'Mock results page',
            'ask-cfpb-search-results',
            self.ROOT_PAGE,
            language='en')

        response = self.client.get(reverse(
            'ask-search-en'), {'q': ''})
        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.context_data['page'],
            mock_page)
        self.assertEqual(
            response.context_data['page'].query,
            '')
        self.assertEqual(
            response.context_data['page'].result_query,
            '')

    @override_settings(FLAGS={'ASK_SEARCH_TYPOS': {'boolean': True}})
    @mock.patch('ask_cfpb.views.SearchQuerySet')
    def test_en_search_suggestion(self, mock_sqs):
        from v1.util.migrations import get_or_create_page
        mock_page = get_or_create_page(
            apps,
            'ask_cfpb',
            'AnswerResultsPage',
            'Mock results page',
            'ask-cfpb-search-results',
            self.english_parent_page,
            language='en',
            live=True)

        mock_return = mock.Mock()
        mock_return.url = 'mockcfpb.gov'
        mock_return.autocomplete = 'A mock question'
        mock_return.text = 'Mock answer text.'

        mock_queryset = mock.Mock()
        mock_queryset.__iter__ = mock.Mock(return_value=iter([mock_return]))
        mock_queryset.count.return_value = 0

        mock_sqs_instance = mock_sqs.return_value.models.return_value
        mock_sqs_instance.filter.return_value = mock_queryset
        mock_sqs_instance.spelling_suggestion.return_value = 'payday'

        response = self.client.get(reverse(
            'ask-search-en'), {'q': 'paydya'})
        self.assertEqual(response.status_code, 200)
        response_page = response.context_data['page']

        self.assertEqual(response_page, mock_page)
        self.assertEqual(response_page.suggestion, 'paydya')
        self.assertEqual(response_page.result_query, 'payday')
        self.assertEqual(response_page.query, 'paydya')

    @mock.patch('ask_cfpb.views.redirect_ask_search')
    def test_ask_search_encounters_facets(self, mock_redirect):
        request = HttpRequest()
        request.GET['selected_facets'] = 'category_exact:my_category'
        ask_search(request)
        self.assertEqual(mock_redirect.call_count, 1)

    @mock.patch('ask_cfpb.views.redirect')
    def test_redirect_ask_search_passes_query_string(self, mock_redirect):
        request = HttpRequest()
        request.GET['q'] = 'hoodoo'
        redirect_ask_search(request)
        self.assertEqual(mock_redirect.call_count, 1)

    @mock.patch('ask_cfpb.views.redirect')
    def test_spanish_redirect_ask_search_passes_query_string(
            self, mock_redirect):
        request = HttpRequest()
        request.GET['selected_facets'] = 'category_exact:my_categoria'
        redirect_ask_search(request, language='es')
        self.assertEqual(mock_redirect.call_count, 1)

    @mock.patch('ask_cfpb.views.SearchQuerySet.filter')
    def test_es_search(self, mock_filter):
        get_or_create_page(
            apps,
            'ask_cfpb',
            'AnswerResultsPage',
            'Mock Spanish results page',
            'respuestas',
            self.spanish_parent_page,
            language='es',
            live=True)
        mock_return = mock.Mock()
        mock_return.url = 'mockcfpb.gov'
        mock_return.autocomplete = 'A mock question'
        mock_return.text = 'Mock answer text.'
        mock_queryset = mock.Mock()
        mock_queryset.__iter__ = mock.Mock(return_value=iter([mock_return]))
        mock_queryset.count.return_value = 1
        mock_filter.return_value = mock_queryset
        self.client.get(reverse(
            'ask-search-es', kwargs={'language': 'es'}), {'q': 'payday'})
        self.assertEqual(mock_filter.call_count, 1)
        self.assertTrue(mock_filter.called_with(language='es', q='payday'))

    @mock.patch('ask_cfpb.views.SearchQuerySet.filter')
    def test_search_page_en_selection(self, mock_filter):
        page = get_or_create_page(
            apps,
            'ask_cfpb',
            'AnswerResultsPage',
            'Mock results page',
            'ask-cfpb-search-results',
            self.english_parent_page,
            language='en',
            live=True)
        mock_return = mock.Mock()
        mock_return.url = 'url'
        mock_return.autocomplete = 'question text'
        mock_queryset = mock.Mock()
        mock_queryset.__iter__ = mock.Mock(return_value=iter([mock_return]))
        mock_queryset.count.return_value = 1
        mock_filter.return_value = mock_queryset
        self.client.get(reverse(
            'ask-search-en'), {'q': 'tuition'})
        self.assertEqual(mock_filter.call_count, 1)
        self.assertEqual(page.language, 'en')
        self.assertEqual(page.answers, [])
        self.assertEqual(
            page.get_template(HttpRequest()),
            'ask-cfpb/answer-search-results.html')

    @mock.patch('ask_cfpb.views.SearchQuerySet.filter')
    def test_search_page_es_selection(self, mock_filter):
        page = get_or_create_page(
            apps,
            'ask_cfpb',
            'AnswerResultsPage',
            'Mock Spanish results page',
            'respuestas',
            self.spanish_parent_page,
            language='es',
            live=True)
        mock_return = mock.Mock()
        mock_return.url = 'url'
        mock_return.autocomplete = 'question text'
        mock_queryset = mock.Mock()
        mock_queryset.__iter__ = mock.Mock(return_value=iter([mock_return]))
        mock_queryset.count.return_value = 1
        mock_filter.return_value = mock_queryset
        self.client.get(reverse(
            'ask-search-es', kwargs={'language': 'es'}), {'q': 'hipotecas'})
        self.assertEqual(mock_filter.call_count, 1)
        self.assertEqual(page.language, 'es')
        self.assertEqual(page.answers, [])
        self.assertEqual(
            page.get_template(HttpRequest()),
            'ask-cfpb/answer-search-spanish-results.html')

    @mock.patch('ask_cfpb.views.SearchQuerySet.filter')
    def test_json_response(self, mock_filter):
        get_or_create_page(
            apps,
            'ask_cfpb',
            'AnswerResultsPage',
            'Mock results page',
            'ask-cfpb-search-results',
            self.english_parent_page,
            language='en',
            live=True)
        mock_return = mock.Mock()
        mock_return.url = "inscisive_url.com"
        mock_return.autocomplete = "inscisive question"
        mock_return.text = "inscisive text"
        mock_queryset = mock.Mock()
        mock_queryset.__iter__ = mock.Mock(return_value=iter([mock_return]))
        mock_queryset.count.return_value = 1
        mock_filter.return_value = mock_queryset
        response = self.client.get(reverse(
            'ask-search-en-json',
            kwargs={'as_json': 'json'}), {'q': 'tuition'})
        self.assertEqual(response.status_code, 200)
        self.assertEqual(mock_filter.call_count, 1)
        self.assertEqual(json.loads(response.content)['query'], 'tuition')

    def test_autocomplete_en_blank_term(self):
        result = self.client.get(reverse(
            'ask-autocomplete-en'), {'term': ''})
        output = json.loads(result.content)
        self.assertEqual(output, [])

    def test_autocomplete_es_blank_term(self):
        result = self.client.get(reverse(
            'ask-autocomplete-es',
            kwargs={'language': 'es'}), {'term': ''})
        output = json.loads(result.content)
        self.assertEqual(output, [])

    @mock.patch('ask_cfpb.views.SearchQuerySet.autocomplete')
    def test_autocomplete_en(self, mock_autocomplete):
        mock_search_result = mock.Mock()
        mock_search_result.autocomplete = 'question'
        mock_search_result.url = 'url'
        mock_autocomplete.return_value = [mock_search_result]
        result = self.client.get(reverse(
            'ask-autocomplete-en'), {'term': 'question'})
        self.assertEqual(mock_autocomplete.call_count, 1)
        output = json.loads(result.content)
        self.assertEqual(
            sorted(output[0].keys()),
            ['question', 'url'])

    @mock.patch('ask_cfpb.views.SearchQuerySet.autocomplete')
    def test_autocomplete_es(self, mock_autocomplete):
        mock_search_result = mock.Mock()
        mock_search_result.autocomplete = 'question'
        mock_search_result.url = 'url'
        mock_autocomplete.return_value = [mock_search_result]
        result = self.client.get(reverse(
            'ask-autocomplete-es',
            kwargs={'language': 'es'}), {'term': 'question'})
        self.assertEqual(mock_autocomplete.call_count, 1)
        output = json.loads(result.content)
        self.assertEqual(
            sorted(output[0].keys()),
            ['question', 'url'])


class RedirectAskSearchTestCase(TestCase):

    def test_redirect_search_no_facets(self):
        request = HttpRequest()
        with self.assertRaises(Http404):
            redirect_ask_search(request)

    def test_redirect_search_blank_facets(self):
        request = HttpRequest()
        request.GET['selected_facets'] = ''
        with self.assertRaises(Http404):
            redirect_ask_search(request)

    def test_redirect_search_no_query(self):
        request = HttpRequest()
        request.GET['q'] = ' '
        with self.assertRaises(Http404):
            redirect_ask_search(request)

    def test_redirect_search_with_category(self):
        category_querystring = (
            'selected_facets=category_exact:my_category'
            '&selected_facets=category_exact:my_category2'
            '&selected_facets=audience_exact:Older+Americans'
            '&selected_facets=audience_exact:my_audience2'
            '&selected_facets=tag_exact:mytag1'
            '&selected_facets=tag_exact:mytag2')
        request = HttpRequest()
        request.GET = QueryDict(category_querystring)
        result = redirect_ask_search(request)
        self.assertEqual(result.get('location'),
                         '/ask-cfpb/category-my_category/')

    def test_redirect_search_with_audience(self):
        audience_querystring = (
            'selected_facets=audience_exact:Older+Americans'
            '&selected_facets=audience_exact:my_audience2')
        request = HttpRequest()
        request.GET = QueryDict(audience_querystring)
        result = redirect_ask_search(request)
        self.assertEqual(
            result.get('location'),
            '/ask-cfpb/audience-older-americans/')

    def test_spanish_redirect_search_with_tag(self):
        target_tag = 'spanishtag1'
        tag_querystring = (
            'selected_facets=tag_exact:{}'
            '&selected_facets=tag_exact:spanishtag2'.format(target_tag))
        request = HttpRequest()
        request.GET = QueryDict(tag_querystring)
        result = redirect_ask_search(request, language='es')
        self.assertEqual(
            result.get('location'),
            '/es/obtener-respuestas/buscar-por-etiqueta/{}/'.format(
                target_tag))

    def test_english_redirect_search_with_tag(self):
        target_tag = 'englishtag1'
        tag_querystring = (
            'selected_facets=tag_exact:{}'
            '&selected_facets=tag_exact:englishtag2'.format(target_tag))
        request = HttpRequest()
        request.GET = QueryDict(tag_querystring)
        result = redirect_ask_search(request, language='en')
        self.assertEqual(
            result.get('location'),
            '/ask-cfpb/search-by-tag/{}/'.format(
                target_tag))

    def test_redirect_search_with_unrecognized_facet_raises_404(self):
        querystring = \
            'sort=-updated_at&selected_facets=imtkfidycqszgfdb&page=60'
        request = HttpRequest()
        request.GET = QueryDict(querystring)
        with self.assertRaises(Http404):
            redirect_ask_search(request)

[((623, 637), 'django.utils.timezone.now', 'timezone.now', ([], {}), '()\n', (635, 637), False, 'from django.utils import timezone\n'), ((1970, 2030), 'mock.patch', 'mock.patch', (['"""ask_cfpb.views.ServeView.serve_latest_revision"""'], {}), "('ask_cfpb.views.ServeView.serve_latest_revision')\n", (1980, 2030), False, 'import mock\n'), ((4849, 4899), 'mock.patch', 'mock.patch', (['"""ask_cfpb.views.SearchQuerySet.filter"""'], {}), "('ask_cfpb.views.SearchQuerySet.filter')\n", (4859, 4899), False, 'import mock\n'), ((5377, 5420), 'mock.patch', 'mock.patch', (['"""ask_cfpb.views.SearchQuerySet"""'], {}), "('ask_cfpb.views.SearchQuerySet')\n", (5387, 5420), False, 'import mock\n'), ((6806, 6849), 'mock.patch', 'mock.patch', (['"""ask_cfpb.views.SearchQuerySet"""'], {}), "('ask_cfpb.views.SearchQuerySet')\n", (6816, 6849), False, 'import mock\n'), ((7623, 7687), 'django.test.override_settings', 'override_settings', ([], {'FLAGS': "{'ASK_SEARCH_TYPOS': {'boolean': True}}"}), "(FLAGS={'ASK_SEARCH_TYPOS': {'boolean': True}})\n", (7640, 7687), False, 'from django.test import TestCase, override_settings\n'), ((7693, 7736), 'mock.patch', 'mock.patch', (['"""ask_cfpb.views.SearchQuerySet"""'], {}), "('ask_cfpb.views.SearchQuerySet')\n", (7703, 7736), False, 'import mock\n'), ((9095, 9143), 'mock.patch', 'mock.patch', (['"""ask_cfpb.views.redirect_ask_search"""'], {}), "('ask_cfpb.views.redirect_ask_search')\n", (9105, 9143), False, 'import mock\n'), ((9398, 9435), 'mock.patch', 'mock.patch', (['"""ask_cfpb.views.redirect"""'], {}), "('ask_cfpb.views.redirect')\n", (9408, 9435), False, 'import mock\n'), ((9676, 9713), 'mock.patch', 'mock.patch', (['"""ask_cfpb.views.redirect"""'], {}), "('ask_cfpb.views.redirect')\n", (9686, 9713), False, 'import mock\n'), ((10025, 10075), 'mock.patch', 'mock.patch', (['"""ask_cfpb.views.SearchQuerySet.filter"""'], {}), "('ask_cfpb.views.SearchQuerySet.filter')\n", (10035, 10075), False, 'import mock\n'), ((11000, 11050), 'mock.patch', 'mock.patch', (['"""ask_cfpb.views.SearchQuerySet.filter"""'], {}), "('ask_cfpb.views.SearchQuerySet.filter')\n", (11010, 11050), False, 'import mock\n'), ((12054, 12104), 'mock.patch', 'mock.patch', (['"""ask_cfpb.views.SearchQuerySet.filter"""'], {}), "('ask_cfpb.views.SearchQuerySet.filter')\n", (12064, 12104), False, 'import mock\n'), ((13140, 13190), 'mock.patch', 'mock.patch', (['"""ask_cfpb.views.SearchQuerySet.filter"""'], {}), "('ask_cfpb.views.SearchQuerySet.filter')\n", (13150, 13190), False, 'import mock\n'), ((14691, 14747), 'mock.patch', 'mock.patch', (['"""ask_cfpb.views.SearchQuerySet.autocomplete"""'], {}), "('ask_cfpb.views.SearchQuerySet.autocomplete')\n", (14701, 14747), False, 'import mock\n'), ((15303, 15359), 'mock.patch', 'mock.patch', (['"""ask_cfpb.views.SearchQuerySet.autocomplete"""'], {}), "('ask_cfpb.views.SearchQuerySet.autocomplete')\n", (15313, 15359), False, 'import mock\n'), ((808, 842), 'v1.models.HomePage.objects.get', 'HomePage.objects.get', ([], {'slug': '"""cfgov"""'}), "(slug='cfgov')\n", (828, 842), False, 'from v1.models import HomePage\n'), ((878, 1014), 'v1.util.migrations.get_or_create_page', 'get_or_create_page', (['apps', '"""ask_cfpb"""', '"""AnswerLandingPage"""', '"""Ask CFPB"""', 'ENGLISH_PARENT_SLUG', 'self.ROOT_PAGE'], {'language': '"""en"""', 'live': '(True)'}), "(apps, 'ask_cfpb', 'AnswerLandingPage', 'Ask CFPB',\n    ENGLISH_PARENT_SLUG, self.ROOT_PAGE, language='en', live=True)\n", (896, 1014), False, 'from v1.util.migrations import get_or_create_page\n'), ((1143, 1294), 'v1.util.migrations.get_or_create_page', 'get_or_create_page', (['apps', '"""ask_cfpb"""', '"""AnswerLandingPage"""', '"""Obtener respuestas"""', 'SPANISH_PARENT_SLUG', 'self.ROOT_PAGE'], {'language': '"""es"""', 'live': '(True)'}), "(apps, 'ask_cfpb', 'AnswerLandingPage',\n    'Obtener respuestas', SPANISH_PARENT_SLUG, self.ROOT_PAGE, language=\n    'es', live=True)\n", (1161, 1294), False, 'from v1.util.migrations import get_or_create_page\n'), ((1410, 1558), 'model_mommy.mommy.make', 'mommy.make', (['Answer'], {'answer': '"""Test answer."""', 'question': '"""Test question."""', 'slug': '"""test-question"""', 'update_english_page': '(True)', 'update_spanish_page': '(False)'}), "(Answer, answer='Test answer.', question='Test question.', slug=\n    'test-question', update_english_page=True, update_spanish_page=False)\n", (1420, 1558), False, 'from model_mommy import mommy\n'), ((1647, 1748), 'model_mommy.mommy.make', 'mommy.make', (['Site'], {'root_page': 'self.ROOT_PAGE', 'hostname': '"""localhost"""', 'port': '(8000)', 'is_default_site': '(True)'}), "(Site, root_page=self.ROOT_PAGE, hostname='localhost', port=8000,\n    is_default_site=True)\n", (1657, 1748), False, 'from model_mommy import mommy\n'), ((1834, 1919), 'model_mommy.mommy.make', 'mommy.make', (['SharingSite'], {'site': 'self.site', 'hostname': '"""preview.localhost"""', 'port': '(8000)'}), "(SharingSite, site=self.site, hostname='preview.localhost', port=8000\n    )\n", (1844, 1919), False, 'from model_mommy import mommy\n'), ((2258, 2271), 'django.http.HttpRequest', 'HttpRequest', ([], {}), '()\n', (2269, 2271), False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((2391, 2460), 'ask_cfpb.views.view_answer', 'view_answer', (['test_request', '"""test-question"""', '"""en"""', 'self.test_answer.pk'], {}), "(test_request, 'test-question', 'en', self.test_answer.pk)\n", (2402, 2460), False, 'from ask_cfpb.views import view_answer\n'), ((2728, 2741), 'django.http.HttpRequest', 'HttpRequest', ([], {}), '()\n', (2739, 2741), False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((3054, 3088), 'v1.models.HomePage.objects.get', 'HomePage.objects.get', ([], {'slug': '"""cfgov"""'}), "(slug='cfgov')\n", (3074, 3088), False, 'from v1.models import HomePage\n'), ((3124, 3260), 'v1.util.migrations.get_or_create_page', 'get_or_create_page', (['apps', '"""ask_cfpb"""', '"""AnswerLandingPage"""', '"""Ask CFPB"""', 'ENGLISH_PARENT_SLUG', 'self.ROOT_PAGE'], {'language': '"""en"""', 'live': '(True)'}), "(apps, 'ask_cfpb', 'AnswerLandingPage', 'Ask CFPB',\n    ENGLISH_PARENT_SLUG, self.ROOT_PAGE, language='en', live=True)\n", (3142, 3260), False, 'from v1.util.migrations import get_or_create_page\n'), ((3389, 3540), 'v1.util.migrations.get_or_create_page', 'get_or_create_page', (['apps', '"""ask_cfpb"""', '"""AnswerLandingPage"""', '"""Obtener respuestas"""', 'SPANISH_PARENT_SLUG', 'self.ROOT_PAGE'], {'language': '"""es"""', 'live': '(True)'}), "(apps, 'ask_cfpb', 'AnswerLandingPage',\n    'Obtener respuestas', SPANISH_PARENT_SLUG, self.ROOT_PAGE, language=\n    'es', live=True)\n", (3407, 3540), False, 'from v1.util.migrations import get_or_create_page\n'), ((3802, 3829), 'ask_cfpb.views.annotate_links', 'annotate_links', (['mock_answer'], {}), '(mock_answer)\n', (3816, 3829), False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((4237, 4264), 'ask_cfpb.views.annotate_links', 'annotate_links', (['mock_answer'], {}), '(mock_answer)\n', (4251, 4264), False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((4360, 4398), 'wagtail.wagtailcore.models.Site.objects.get', 'Site.objects.get', ([], {'is_default_site': '(True)'}), '(is_default_site=True)\n', (4376, 4398), False, 'from wagtail.wagtailcore.models import Site\n'), ((4992, 5003), 'mock.Mock', 'mock.Mock', ([], {}), '()\n', (5001, 5003), False, 'import mock\n'), ((5539, 5683), 'v1.util.migrations.get_or_create_page', 'get_or_create_page', (['apps', '"""ask_cfpb"""', '"""AnswerResultsPage"""', '"""Mock results page"""', '"""ask-cfpb-search-results"""', 'self.ROOT_PAGE'], {'language': '"""en"""'}), "(apps, 'ask_cfpb', 'AnswerResultsPage',\n    'Mock results page', 'ask-cfpb-search-results', self.ROOT_PAGE,\n    language='en')\n", (5557, 5683), False, 'from v1.util.migrations import get_or_create_page\n'), ((5784, 5795), 'mock.Mock', 'mock.Mock', ([], {}), '()\n', (5793, 5795), False, 'import mock\n'), ((5962, 5973), 'mock.Mock', 'mock.Mock', ([], {}), '()\n', (5971, 5973), False, 'import mock\n'), ((6976, 7120), 'v1.util.migrations.get_or_create_page', 'get_or_create_page', (['apps', '"""ask_cfpb"""', '"""AnswerResultsPage"""', '"""Mock results page"""', '"""ask-cfpb-search-results"""', 'self.ROOT_PAGE'], {'language': '"""en"""'}), "(apps, 'ask_cfpb', 'AnswerResultsPage',\n    'Mock results page', 'ask-cfpb-search-results', self.ROOT_PAGE,\n    language='en')\n", (6994, 7120), False, 'from v1.util.migrations import get_or_create_page\n'), ((7866, 8032), 'v1.util.migrations.get_or_create_page', 'get_or_create_page', (['apps', '"""ask_cfpb"""', '"""AnswerResultsPage"""', '"""Mock results page"""', '"""ask-cfpb-search-results"""', 'self.english_parent_page'], {'language': '"""en"""', 'live': '(True)'}), "(apps, 'ask_cfpb', 'AnswerResultsPage',\n    'Mock results page', 'ask-cfpb-search-results', self.\n    english_parent_page, language='en', live=True)\n", (7884, 8032), False, 'from v1.util.migrations import get_or_create_page\n'), ((8144, 8155), 'mock.Mock', 'mock.Mock', ([], {}), '()\n', (8153, 8155), False, 'import mock\n'), ((8322, 8333), 'mock.Mock', 'mock.Mock', ([], {}), '()\n', (8331, 8333), False, 'import mock\n'), ((9226, 9239), 'django.http.HttpRequest', 'HttpRequest', ([], {}), '()\n', (9237, 9239), False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((9318, 9337), 'ask_cfpb.views.ask_search', 'ask_search', (['request'], {}), '(request)\n', (9328, 9337), False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((9529, 9542), 'django.http.HttpRequest', 'HttpRequest', ([], {}), '()\n', (9540, 9542), False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((9587, 9615), 'ask_cfpb.views.redirect_ask_search', 'redirect_ask_search', (['request'], {}), '(request)\n', (9606, 9615), False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((9828, 9841), 'django.http.HttpRequest', 'HttpRequest', ([], {}), '()\n', (9839, 9841), False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((9921, 9964), 'ask_cfpb.views.redirect_ask_search', 'redirect_ask_search', (['request'], {'language': '"""es"""'}), "(request, language='es')\n", (9940, 9964), False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((10127, 10287), 'v1.util.migrations.get_or_create_page', 'get_or_create_page', (['apps', '"""ask_cfpb"""', '"""AnswerResultsPage"""', '"""Mock Spanish results page"""', '"""respuestas"""', 'self.spanish_parent_page'], {'language': '"""es"""', 'live': '(True)'}), "(apps, 'ask_cfpb', 'AnswerResultsPage',\n    'Mock Spanish results page', 'respuestas', self.spanish_parent_page,\n    language='es', live=True)\n", (10145, 10287), False, 'from v1.util.migrations import get_or_create_page\n'), ((10399, 10410), 'mock.Mock', 'mock.Mock', ([], {}), '()\n', (10408, 10410), False, 'import mock\n'), ((10576, 10587), 'mock.Mock', 'mock.Mock', ([], {}), '()\n', (10585, 10587), False, 'import mock\n'), ((11124, 11290), 'v1.util.migrations.get_or_create_page', 'get_or_create_page', (['apps', '"""ask_cfpb"""', '"""AnswerResultsPage"""', '"""Mock results page"""', '"""ask-cfpb-search-results"""', 'self.english_parent_page'], {'language': '"""en"""', 'live': '(True)'}), "(apps, 'ask_cfpb', 'AnswerResultsPage',\n    'Mock results page', 'ask-cfpb-search-results', self.\n    english_parent_page, language='en', live=True)\n", (11142, 11290), False, 'from v1.util.migrations import get_or_create_page\n'), ((11401, 11412), 'mock.Mock', 'mock.Mock', ([], {}), '()\n', (11410, 11412), False, 'import mock\n'), ((11520, 11531), 'mock.Mock', 'mock.Mock', ([], {}), '()\n', (11529, 11531), False, 'import mock\n'), ((12178, 12338), 'v1.util.migrations.get_or_create_page', 'get_or_create_page', (['apps', '"""ask_cfpb"""', '"""AnswerResultsPage"""', '"""Mock Spanish results page"""', '"""respuestas"""', 'self.spanish_parent_page'], {'language': '"""es"""', 'live': '(True)'}), "(apps, 'ask_cfpb', 'AnswerResultsPage',\n    'Mock Spanish results page', 'respuestas', self.spanish_parent_page,\n    language='es', live=True)\n", (12196, 12338), False, 'from v1.util.migrations import get_or_create_page\n'), ((12450, 12461), 'mock.Mock', 'mock.Mock', ([], {}), '()\n', (12459, 12461), False, 'import mock\n'), ((12569, 12580), 'mock.Mock', 'mock.Mock', ([], {}), '()\n', (12578, 12580), False, 'import mock\n'), ((13246, 13412), 'v1.util.migrations.get_or_create_page', 'get_or_create_page', (['apps', '"""ask_cfpb"""', '"""AnswerResultsPage"""', '"""Mock results page"""', '"""ask-cfpb-search-results"""', 'self.english_parent_page'], {'language': '"""en"""', 'live': '(True)'}), "(apps, 'ask_cfpb', 'AnswerResultsPage',\n    'Mock results page', 'ask-cfpb-search-results', self.\n    english_parent_page, language='en', live=True)\n", (13264, 13412), False, 'from v1.util.migrations import get_or_create_page\n'), ((13523, 13534), 'mock.Mock', 'mock.Mock', ([], {}), '()\n', (13532, 13534), False, 'import mock\n'), ((13705, 13716), 'mock.Mock', 'mock.Mock', ([], {}), '()\n', (13714, 13716), False, 'import mock\n'), ((14361, 14387), 'json.loads', 'json.loads', (['result.content'], {}), '(result.content)\n', (14371, 14387), False, 'import json\n'), ((14621, 14647), 'json.loads', 'json.loads', (['result.content'], {}), '(result.content)\n', (14631, 14647), False, 'import json\n'), ((14832, 14843), 'mock.Mock', 'mock.Mock', ([], {}), '()\n', (14841, 14843), False, 'import mock\n'), ((15173, 15199), 'json.loads', 'json.loads', (['result.content'], {}), '(result.content)\n', (15183, 15199), False, 'import json\n'), ((15444, 15455), 'mock.Mock', 'mock.Mock', ([], {}), '()\n', (15453, 15455), False, 'import mock\n'), ((15824, 15850), 'json.loads', 'json.loads', (['result.content'], {}), '(result.content)\n', (15834, 15850), False, 'import json\n'), ((16058, 16071), 'django.http.HttpRequest', 'HttpRequest', ([], {}), '()\n', (16069, 16071), False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((16222, 16235), 'django.http.HttpRequest', 'HttpRequest', ([], {}), '()\n', (16233, 16235), False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((16426, 16439), 'django.http.HttpRequest', 'HttpRequest', ([], {}), '()\n', (16437, 16439), False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((16989, 17002), 'django.http.HttpRequest', 'HttpRequest', ([], {}), '()\n', (17000, 17002), False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((17025, 17056), 'django.http.QueryDict', 'QueryDict', (['category_querystring'], {}), '(category_querystring)\n', (17034, 17056), False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((17074, 17102), 'ask_cfpb.views.redirect_ask_search', 'redirect_ask_search', (['request'], {}), '(request)\n', (17093, 17102), False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((17435, 17448), 'django.http.HttpRequest', 'HttpRequest', ([], {}), '()\n', (17446, 17448), False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((17471, 17502), 'django.http.QueryDict', 'QueryDict', (['audience_querystring'], {}), '(audience_querystring)\n', (17480, 17502), False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((17520, 17548), 'ask_cfpb.views.redirect_ask_search', 'redirect_ask_search', (['request'], {}), '(request)\n', (17539, 17548), False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((17913, 17926), 'django.http.HttpRequest', 'HttpRequest', ([], {}), '()\n', (17924, 17926), False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((17949, 17975), 'django.http.QueryDict', 'QueryDict', (['tag_querystring'], {}), '(tag_querystring)\n', (17958, 17975), False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((17993, 18036), 'ask_cfpb.views.redirect_ask_search', 'redirect_ask_search', (['request'], {'language': '"""es"""'}), "(request, language='es')\n", (18012, 18036), False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((18448, 18461), 'django.http.HttpRequest', 'HttpRequest', ([], {}), '()\n', (18459, 18461), False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((18484, 18510), 'django.http.QueryDict', 'QueryDict', (['tag_querystring'], {}), '(tag_querystring)\n', (18493, 18510), False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((18528, 18571), 'ask_cfpb.views.redirect_ask_search', 'redirect_ask_search', (['request'], {'language': '"""en"""'}), "(request, language='en')\n", (18547, 18571), False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((18899, 18912), 'django.http.HttpRequest', 'HttpRequest', ([], {}), '()\n', (18910, 18912), False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((18935, 18957), 'django.http.QueryDict', 'QueryDict', (['querystring'], {}), '(querystring)\n', (18944, 18957), False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((2795, 2864), 'ask_cfpb.views.view_answer', 'view_answer', (['test_request', '"""test-question"""', '"""en"""', 'self.test_answer.pk'], {}), "(test_request, 'test-question', 'en', self.test_answer.pk)\n", (2806, 2864), False, 'from ask_cfpb.views import view_answer\n'), ((4525, 4549), 'ask_cfpb.views.annotate_links', 'annotate_links', (['"""answer"""'], {}), "('answer')\n", (4539, 4549), False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((5135, 5159), 'django.core.urlresolvers.reverse', 'reverse', (['"""ask-search-en"""'], {}), "('ask-search-en')\n", (5142, 5159), False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((6335, 6359), 'django.core.urlresolvers.reverse', 'reverse', (['"""ask-search-en"""'], {}), "('ask-search-en')\n", (6342, 6359), False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((7234, 7258), 'django.core.urlresolvers.reverse', 'reverse', (['"""ask-search-en"""'], {}), "('ask-search-en')\n", (7241, 7258), False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((8695, 8719), 'django.core.urlresolvers.reverse', 'reverse', (['"""ask-search-en"""'], {}), "('ask-search-en')\n", (8702, 8719), False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((10783, 10834), 'django.core.urlresolvers.reverse', 'reverse', (['"""ask-search-es"""'], {'kwargs': "{'language': 'es'}"}), "('ask-search-es', kwargs={'language': 'es'})\n", (10790, 10834), False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((11727, 11751), 'django.core.urlresolvers.reverse', 'reverse', (['"""ask-search-en"""'], {}), "('ask-search-en')\n", (11734, 11751), False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((12776, 12827), 'django.core.urlresolvers.reverse', 'reverse', (['"""ask-search-es"""'], {'kwargs': "{'language': 'es'}"}), "('ask-search-es', kwargs={'language': 'es'})\n", (12783, 12827), False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((13923, 13980), 'django.core.urlresolvers.reverse', 'reverse', (['"""ask-search-en-json"""'], {'kwargs': "{'as_json': 'json'}"}), "('ask-search-en-json', kwargs={'as_json': 'json'})\n", (13930, 13980), False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((14285, 14315), 'django.core.urlresolvers.reverse', 'reverse', (['"""ask-autocomplete-en"""'], {}), "('ask-autocomplete-en')\n", (14292, 14315), False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((14506, 14563), 'django.core.urlresolvers.reverse', 'reverse', (['"""ask-autocomplete-es"""'], {'kwargs': "{'language': 'es'}"}), "('ask-autocomplete-es', kwargs={'language': 'es'})\n", (14513, 14563), False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((15031, 15061), 'django.core.urlresolvers.reverse', 'reverse', (['"""ask-autocomplete-en"""'], {}), "('ask-autocomplete-en')\n", (15038, 15061), False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((15643, 15700), 'django.core.urlresolvers.reverse', 'reverse', (['"""ask-autocomplete-es"""'], {'kwargs': "{'language': 'es'}"}), "('ask-autocomplete-es', kwargs={'language': 'es'})\n", (15650, 15700), False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((16125, 16153), 'ask_cfpb.views.redirect_ask_search', 'redirect_ask_search', (['request'], {}), '(request)\n', (16144, 16153), False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((16333, 16361), 'ask_cfpb.views.redirect_ask_search', 'redirect_ask_search', (['request'], {}), '(request)\n', (16352, 16361), False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((16524, 16552), 'ask_cfpb.views.redirect_ask_search', 'redirect_ask_search', (['request'], {}), '(request)\n', (16543, 16552), False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((19011, 19039), 'ask_cfpb.views.redirect_ask_search', 'redirect_ask_search', (['request'], {}), '(request)\n', (19030, 19039), False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((4740, 4791), 'django.core.urlresolvers.reverse', 'reverse', (['"""ask-search-en"""'], {'kwargs': "{'language': 'zz'}"}), "('ask-search-en', kwargs={'language': 'zz'})\n", (4747, 4791), False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((11981, 11994), 'django.http.HttpRequest', 'HttpRequest', ([], {}), '()\n', (11992, 11994), False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((13059, 13072), 'django.http.HttpRequest', 'HttpRequest', ([], {}), '()\n', (13070, 13072), False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((14154, 14182), 'json.loads', 'json.loads', (['response.content'], {}), '(response.content)\n', (14164, 14182), False, 'import json\n')]