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)

[((22, 6, 22, 20), 'django.utils.timezone.now', 'timezone.now', ({}, {}), '()', False, 'from django.utils import timezone\n'), ((68, 5, 68, 65), 'mock.patch', 'mock.patch', ({(68, 16, 68, 64): '"""ask_cfpb.views.ServeView.serve_latest_revision"""'}, {}), "('ask_cfpb.views.ServeView.serve_latest_revision')", False, 'import mock\n'), ((149, 5, 149, 55), 'mock.patch', 'mock.patch', ({(149, 16, 149, 54): '"""ask_cfpb.views.SearchQuerySet.filter"""'}, {}), "('ask_cfpb.views.SearchQuerySet.filter')", False, 'import mock\n'), ((160, 5, 160, 48), 'mock.patch', 'mock.patch', ({(160, 16, 160, 47): '"""ask_cfpb.views.SearchQuerySet"""'}, {}), "('ask_cfpb.views.SearchQuerySet')", False, 'import mock\n'), ((198, 5, 198, 48), 'mock.patch', 'mock.patch', ({(198, 16, 198, 47): '"""ask_cfpb.views.SearchQuerySet"""'}, {}), "('ask_cfpb.views.SearchQuerySet')", False, 'import mock\n'), ((223, 5, 223, 69), 'django.test.override_settings', 'override_settings', (), '', False, 'from django.test import TestCase, override_settings\n'), ((224, 5, 224, 48), 'mock.patch', 'mock.patch', ({(224, 16, 224, 47): '"""ask_cfpb.views.SearchQuerySet"""'}, {}), "('ask_cfpb.views.SearchQuerySet')", False, 'import mock\n'), ((260, 5, 260, 53), 'mock.patch', 'mock.patch', ({(260, 16, 260, 52): '"""ask_cfpb.views.redirect_ask_search"""'}, {}), "('ask_cfpb.views.redirect_ask_search')", False, 'import mock\n'), ((267, 5, 267, 42), 'mock.patch', 'mock.patch', ({(267, 16, 267, 41): '"""ask_cfpb.views.redirect"""'}, {}), "('ask_cfpb.views.redirect')", False, 'import mock\n'), ((274, 5, 274, 42), 'mock.patch', 'mock.patch', ({(274, 16, 274, 41): '"""ask_cfpb.views.redirect"""'}, {}), "('ask_cfpb.views.redirect')", False, 'import mock\n'), ((282, 5, 282, 55), 'mock.patch', 'mock.patch', ({(282, 16, 282, 54): '"""ask_cfpb.views.SearchQuerySet.filter"""'}, {}), "('ask_cfpb.views.SearchQuerySet.filter')", False, 'import mock\n'), ((306, 5, 306, 55), 'mock.patch', 'mock.patch', ({(306, 16, 306, 54): '"""ask_cfpb.views.SearchQuerySet.filter"""'}, {}), "('ask_cfpb.views.SearchQuerySet.filter')", False, 'import mock\n'), ((333, 5, 333, 55), 'mock.patch', 'mock.patch', ({(333, 16, 333, 54): '"""ask_cfpb.views.SearchQuerySet.filter"""'}, {}), "('ask_cfpb.views.SearchQuerySet.filter')", False, 'import mock\n'), ((360, 5, 360, 55), 'mock.patch', 'mock.patch', ({(360, 16, 360, 54): '"""ask_cfpb.views.SearchQuerySet.filter"""'}, {}), "('ask_cfpb.views.SearchQuerySet.filter')", False, 'import mock\n'), ((399, 5, 399, 61), 'mock.patch', 'mock.patch', ({(399, 16, 399, 60): '"""ask_cfpb.views.SearchQuerySet.autocomplete"""'}, {}), "('ask_cfpb.views.SearchQuerySet.autocomplete')", False, 'import mock\n'), ((413, 5, 413, 61), 'mock.patch', 'mock.patch', ({(413, 16, 413, 60): '"""ask_cfpb.views.SearchQuerySet.autocomplete"""'}, {}), "('ask_cfpb.views.SearchQuerySet.autocomplete')", False, 'import mock\n'), ((30, 25, 30, 59), 'v1.models.HomePage.objects.get', 'HomePage.objects.get', (), '', False, 'from v1.models import HomePage\n'), ((31, 35, 39, 22), 'v1.util.migrations.get_or_create_page', 'get_or_create_page', (), '', False, 'from v1.util.migrations import get_or_create_page\n'), ((40, 35, 48, 22), 'v1.util.migrations.get_or_create_page', 'get_or_create_page', (), '', False, 'from v1.util.migrations import get_or_create_page\n'), ((49, 27, 55, 38), 'model_mommy.mommy.make', 'mommy.make', (), '', False, 'from model_mommy import mommy\n'), ((56, 20, 61, 33), 'model_mommy.mommy.make', 'mommy.make', (), '', False, 'from model_mommy import mommy\n'), ((62, 28, 66, 22), 'model_mommy.mommy.make', 'mommy.make', (), '', False, 'from model_mommy import mommy\n'), ((74, 23, 74, 36), 'django.http.HttpRequest', 'HttpRequest', ({}, {}), '()', False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((77, 8, 78, 69), 'ask_cfpb.views.view_answer', 'view_answer', ({(78, 12, 78, 24): 'test_request', (78, 26, 78, 41): '"""test-question"""', (78, 43, 78, 47): '"""en"""', (78, 49, 78, 68): 'self.test_answer.pk'}, {}), "(test_request, 'test-question', 'en', self.test_answer.pk)", False, 'from ask_cfpb.views import view_answer\n'), ((86, 23, 86, 36), 'django.http.HttpRequest', 'HttpRequest', ({}, {}), '()', False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((99, 25, 99, 59), 'v1.models.HomePage.objects.get', 'HomePage.objects.get', (), '', False, 'from v1.models import HomePage\n'), ((100, 35, 108, 22), 'v1.util.migrations.get_or_create_page', 'get_or_create_page', (), '', False, 'from v1.util.migrations import get_or_create_page\n'), ((109, 35, 117, 22), 'v1.util.migrations.get_or_create_page', 'get_or_create_page', (), '', False, 'from v1.util.migrations import get_or_create_page\n'), ((122, 36, 122, 63), 'ask_cfpb.views.annotate_links', 'annotate_links', ({(122, 51, 122, 62): 'mock_answer'}, {}), '(mock_answer)', False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((132, 36, 132, 63), 'ask_cfpb.views.annotate_links', 'annotate_links', ({(132, 51, 132, 62): 'mock_answer'}, {}), '(mock_answer)', False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((136, 15, 136, 53), 'wagtail.wagtailcore.models.Site.objects.get', 'Site.objects.get', (), '', False, 'from wagtail.wagtailcore.models import Site\n'), ((151, 24, 151, 35), 'mock.Mock', 'mock.Mock', ({}, {}), '()', False, 'import mock\n'), ((163, 20, 170, 26), 'v1.util.migrations.get_or_create_page', 'get_or_create_page', (), '', False, 'from v1.util.migrations import get_or_create_page\n'), ((172, 22, 172, 33), 'mock.Mock', 'mock.Mock', ({}, {}), '()', False, 'import mock\n'), ((177, 24, 177, 35), 'mock.Mock', 'mock.Mock', ({}, {}), '()', False, 'import mock\n'), ((201, 20, 208, 26), 'v1.util.migrations.get_or_create_page', 'get_or_create_page', (), '', False, 'from v1.util.migrations import get_or_create_page\n'), ((227, 20, 235, 22), 'v1.util.migrations.get_or_create_page', 'get_or_create_page', (), '', False, 'from v1.util.migrations import get_or_create_page\n'), ((237, 22, 237, 33), 'mock.Mock', 'mock.Mock', ({}, {}), '()', False, 'import mock\n'), ((242, 24, 242, 35), 'mock.Mock', 'mock.Mock', ({}, {}), '()', False, 'import mock\n'), ((262, 18, 262, 31), 'django.http.HttpRequest', 'HttpRequest', ({}, {}), '()', False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((264, 8, 264, 27), 'ask_cfpb.views.ask_search', 'ask_search', ({(264, 19, 264, 26): 'request'}, {}), '(request)', False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((269, 18, 269, 31), 'django.http.HttpRequest', 'HttpRequest', ({}, {}), '()', False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((271, 8, 271, 36), 'ask_cfpb.views.redirect_ask_search', 'redirect_ask_search', ({(271, 28, 271, 35): 'request'}, {}), '(request)', False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((277, 18, 277, 31), 'django.http.HttpRequest', 'HttpRequest', ({}, {}), '()', False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((279, 8, 279, 51), 'ask_cfpb.views.redirect_ask_search', 'redirect_ask_search', (), '', False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((284, 8, 292, 22), 'v1.util.migrations.get_or_create_page', 'get_or_create_page', (), '', False, 'from v1.util.migrations import get_or_create_page\n'), ((293, 22, 293, 33), 'mock.Mock', 'mock.Mock', ({}, {}), '()', False, 'import mock\n'), ((297, 24, 297, 35), 'mock.Mock', 'mock.Mock', ({}, {}), '()', False, 'import mock\n'), ((308, 15, 316, 22), 'v1.util.migrations.get_or_create_page', 'get_or_create_page', (), '', False, 'from v1.util.migrations import get_or_create_page\n'), ((317, 22, 317, 33), 'mock.Mock', 'mock.Mock', ({}, {}), '()', False, 'import mock\n'), ((320, 24, 320, 35), 'mock.Mock', 'mock.Mock', ({}, {}), '()', False, 'import mock\n'), ((335, 15, 343, 22), 'v1.util.migrations.get_or_create_page', 'get_or_create_page', (), '', False, 'from v1.util.migrations import get_or_create_page\n'), ((344, 22, 344, 33), 'mock.Mock', 'mock.Mock', ({}, {}), '()', False, 'import mock\n'), ((347, 24, 347, 35), 'mock.Mock', 'mock.Mock', ({}, {}), '()', False, 'import mock\n'), ((362, 8, 370, 22), 'v1.util.migrations.get_or_create_page', 'get_or_create_page', (), '', False, 'from v1.util.migrations import get_or_create_page\n'), ((371, 22, 371, 33), 'mock.Mock', 'mock.Mock', ({}, {}), '()', False, 'import mock\n'), ((375, 24, 375, 35), 'mock.Mock', 'mock.Mock', ({}, {}), '()', False, 'import mock\n'), ((389, 17, 389, 43), 'json.loads', 'json.loads', ({(389, 28, 389, 42): 'result.content'}, {}), '(result.content)', False, 'import json\n'), ((396, 17, 396, 43), 'json.loads', 'json.loads', ({(396, 28, 396, 42): 'result.content'}, {}), '(result.content)', False, 'import json\n'), ((401, 29, 401, 40), 'mock.Mock', 'mock.Mock', ({}, {}), '()', False, 'import mock\n'), ((408, 17, 408, 43), 'json.loads', 'json.loads', ({(408, 28, 408, 42): 'result.content'}, {}), '(result.content)', False, 'import json\n'), ((415, 29, 415, 40), 'mock.Mock', 'mock.Mock', ({}, {}), '()', False, 'import mock\n'), ((423, 17, 423, 43), 'json.loads', 'json.loads', ({(423, 28, 423, 42): 'result.content'}, {}), '(result.content)', False, 'import json\n'), ((432, 18, 432, 31), 'django.http.HttpRequest', 'HttpRequest', ({}, {}), '()', False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((437, 18, 437, 31), 'django.http.HttpRequest', 'HttpRequest', ({}, {}), '()', False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((443, 18, 443, 31), 'django.http.HttpRequest', 'HttpRequest', ({}, {}), '()', False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((456, 18, 456, 31), 'django.http.HttpRequest', 'HttpRequest', ({}, {}), '()', False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((457, 22, 457, 53), 'django.http.QueryDict', 'QueryDict', ({(457, 32, 457, 52): 'category_querystring'}, {}), '(category_querystring)', False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((458, 17, 458, 45), 'ask_cfpb.views.redirect_ask_search', 'redirect_ask_search', ({(458, 37, 458, 44): 'request'}, {}), '(request)', False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((466, 18, 466, 31), 'django.http.HttpRequest', 'HttpRequest', ({}, {}), '()', False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((467, 22, 467, 53), 'django.http.QueryDict', 'QueryDict', ({(467, 32, 467, 52): 'audience_querystring'}, {}), '(audience_querystring)', False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((468, 17, 468, 45), 'ask_cfpb.views.redirect_ask_search', 'redirect_ask_search', ({(468, 37, 468, 44): 'request'}, {}), '(request)', False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((478, 18, 478, 31), 'django.http.HttpRequest', 'HttpRequest', ({}, {}), '()', False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((479, 22, 479, 48), 'django.http.QueryDict', 'QueryDict', ({(479, 32, 479, 47): 'tag_querystring'}, {}), '(tag_querystring)', False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((480, 17, 480, 60), 'ask_cfpb.views.redirect_ask_search', 'redirect_ask_search', (), '', False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((491, 18, 491, 31), 'django.http.HttpRequest', 'HttpRequest', ({}, {}), '()', False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((492, 22, 492, 48), 'django.http.QueryDict', 'QueryDict', ({(492, 32, 492, 47): 'tag_querystring'}, {}), '(tag_querystring)', False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((493, 17, 493, 60), 'ask_cfpb.views.redirect_ask_search', 'redirect_ask_search', (), '', False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((502, 18, 502, 31), 'django.http.HttpRequest', 'HttpRequest', ({}, {}), '()', False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((503, 22, 503, 44), 'django.http.QueryDict', 'QueryDict', ({(503, 32, 503, 43): 'querystring'}, {}), '(querystring)', False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((88, 12, 92, 36), 'ask_cfpb.views.view_answer', 'view_answer', ({(89, 16, 89, 28): 'test_request', (90, 16, 90, 31): '"""test-question"""', (91, 16, 91, 20): '"""en"""', (92, 16, 92, 35): 'self.test_answer.pk'}, {}), "(test_request, 'test-question', 'en', self.test_answer.pk)", False, 'from ask_cfpb.views import view_answer\n'), ((140, 12, 140, 36), 'ask_cfpb.views.annotate_links', 'annotate_links', ({(140, 27, 140, 35): '"""answer"""'}, {}), "('answer')", False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((154, 35, 155, 28), 'django.core.urlresolvers.reverse', 'reverse', ({(155, 12, 155, 27): '"""ask-search-en"""'}, {}), "('ask-search-en')", False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((185, 35, 186, 28), 'django.core.urlresolvers.reverse', 'reverse', ({(186, 12, 186, 27): '"""ask-search-en"""'}, {}), "('ask-search-en')", False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((210, 35, 211, 28), 'django.core.urlresolvers.reverse', 'reverse', ({(211, 12, 211, 27): '"""ask-search-en"""'}, {}), "('ask-search-en')", False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((250, 35, 251, 28), 'django.core.urlresolvers.reverse', 'reverse', ({(251, 12, 251, 27): '"""ask-search-en"""'}, {}), "('ask-search-en')", False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((301, 24, 302, 55), 'django.core.urlresolvers.reverse', 'reverse', (), '', False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((324, 24, 325, 28), 'django.core.urlresolvers.reverse', 'reverse', ({(325, 12, 325, 27): '"""ask-search-en"""'}, {}), "('ask-search-en')", False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((351, 24, 352, 55), 'django.core.urlresolvers.reverse', 'reverse', (), '', False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((379, 35, 381, 39), 'django.core.urlresolvers.reverse', 'reverse', (), '', False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((387, 33, 388, 34), 'django.core.urlresolvers.reverse', 'reverse', ({(388, 12, 388, 33): '"""ask-autocomplete-en"""'}, {}), "('ask-autocomplete-en')", False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((393, 33, 395, 38), 'django.core.urlresolvers.reverse', 'reverse', (), '', False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((405, 33, 406, 34), 'django.core.urlresolvers.reverse', 'reverse', ({(406, 12, 406, 33): '"""ask-autocomplete-en"""'}, {}), "('ask-autocomplete-en')", False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((419, 33, 421, 38), 'django.core.urlresolvers.reverse', 'reverse', (), '', False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((434, 12, 434, 40), 'ask_cfpb.views.redirect_ask_search', 'redirect_ask_search', ({(434, 32, 434, 39): 'request'}, {}), '(request)', False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((440, 12, 440, 40), 'ask_cfpb.views.redirect_ask_search', 'redirect_ask_search', ({(440, 32, 440, 39): 'request'}, {}), '(request)', False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((446, 12, 446, 40), 'ask_cfpb.views.redirect_ask_search', 'redirect_ask_search', ({(446, 32, 446, 39): 'request'}, {}), '(request)', False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((505, 12, 505, 40), 'ask_cfpb.views.redirect_ask_search', 'redirect_ask_search', ({(505, 32, 505, 39): 'request'}, {}), '(request)', False, 'from ask_cfpb.views import annotate_links, ask_search, redirect_ask_search\n'), ((145, 28, 147, 42), 'django.core.urlresolvers.reverse', 'reverse', (), '', False, 'from django.core.urlresolvers import NoReverseMatch, reverse\n'), ((330, 30, 330, 43), 'django.http.HttpRequest', 'HttpRequest', ({}, {}), '()', False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((357, 30, 357, 43), 'django.http.HttpRequest', 'HttpRequest', ({}, {}), '()', False, 'from django.http import Http404, HttpRequest, QueryDict\n'), ((384, 25, 384, 53), 'json.loads', 'json.loads', ({(384, 36, 384, 52): 'response.content'}, {}), '(response.content)', False, 'import json\n')]