Ejemplo n.º 1
0
    def test_get_suggestions(self):
        url = reverse('feedback', args=(u'firefox',))
        desc = u'slow browser please speed improve i am wait speed improv 2'

        # Post some basic feedback that meets the SUMO Suggest
        # Provider standards and follow through to the Thank You
        # page. This triggers the suggestions and docs should be
        # in the session.
        resp = self.client.post(url, {
            'happy': 0,
            'description': desc,
        }, follow=True)

        feedback_id = self.client.session['response_id']
        session_key = SUMO_SUGGEST_SESSION_KEY.format(feedback_id)

        # Verify we get the right number of docs from the SUMO Suggest
        # API and that the urls start with SUMO_HOST.
        docs = self.client.session[session_key]
        ok_(0 < len(docs) <= 3)
        for doc in docs:
            ok_(doc['url'].startswith(SUMO_HOST))

        # Note: Since SUMO content changes, we can't check specific
        # strings since we don't really know what it's going to
        # return.

        links = resp.context['suggestions']
        eq_(links[0].provider, 'sumosuggest')
        eq_(links[0].provider_version, 1)

        # Verify that the first link has non-empty summary, url and
        # description.
        ok_(links[0].summary)
        ok_(links[0].url)
        ok_(links[0].description)
Ejemplo n.º 2
0
    def test_mocked_get_suggestions(self):
        """Tests the whole thing with mocked API calls"""
        sumo_api_ret = {
            'documents': [
                {
                    'id': 12345,
                    'locale': 'en-US',
                    'products': ['firefox'],
                    'slug': 'firefox-uses-too-much-memory-ram',
                    'title': 'Firefox uses too much memory',
                    'summary': 'This article describes how to fix',
                    'html': '<p>Why Firefox uses too much memory</p>',
                    'topics': ['slowness-or-hanging'],
                    'url': '/en-US/kb/firefox-uses-too-much-memory-ram',
                },
                {
                    'id': 12346,
                    'locale': 'en-US',
                    'products': ['firefox'],
                    'slug': 'firefox-takes-long-time-start-up',
                    'title': 'Firefox can take a long time to start up',
                    'summary': 'This article describes how to fix',
                    'html': '<p>Why Firefox could have long startup times</p>',
                    'topics': ['slowness-or-hanging'],
                    'url': '/en-US/kb/firefox-takes-long-time-start-up',
                },
                {
                    'id': 12347,
                    'locale': 'en-US',
                    'products': ['firefox'],
                    'slug': 'firefox--cant-load-sites',
                    'title': 'Firefox sometimes can\'t load sites',
                    'summary': 'This article describes how to fix',
                    'html': (
                        '<p>Possible reasons Firefox can\'t load sites</p>'
                    ),
                    'topics': ['slowness-or-hanging'],
                    'url': '/en-US/kb/firefox-cant-load-sites',
                }
            ]
        }

        with requests_mock.Mocker() as m:
            m.get(SUMO_SUGGEST_API_URL, json=sumo_api_ret)
            m.post(GOOGLE_API_URL, text='whatever')

            url = reverse('feedback', args=(u'firefox',))
            desc = u'slow browser please speed improve i am wait speed improv'

            # Post some basic feedback that meets the SUMO Suggest
            # Provider standards and follow through to the Thank You
            # page. This triggers the suggestions and docs should be
            # in the session.
            resp = self.client.post(url, {
                'happy': 0,
                'description': desc,
            }, follow=True)

            feedback_id = self.client.session['response_id']
            session_key = SUMO_SUGGEST_SESSION_KEY.format(feedback_id)

            # Check the docs in the session. Abuse the fact that we
            # know what order the docs are in since we're mocking.
            docs = self.client.session[session_key]
            for i in range(3):
                doc = docs[i]
                ret_doc = sumo_api_ret['documents'][i]
                eq_(doc['url'], SUMO_HOST + ret_doc['url'])
                eq_(doc['summary'], ret_doc['title'])
                eq_(doc['description'], ret_doc['summary'])

            links = resp.context['suggestions']

            # Abuse the fact that we know what order the links are in
            # since we're mocking.
            for i, link in enumerate(links):
                if i == 3:
                    # This is the aaq link.
                    eq_(link.cssclass, u'support')
                    eq_(link.provider, 'sumosuggest')
                    eq_(link.provider_version, 1)
                    eq_(link.url, '/redirect?r=sumosuggest.aaq')

                    # Fetch the link and make sure it redirects to the
                    # right place.
                    resp = self.client.get(link.url)
                    # Temporary redirect.
                    eq_(resp.status_code, 302)
                    # Redirects to the actual SUMO url.
                    eq_(resp['Location'], SUMO_AAQ_URL)

                else:
                    # This is a kb link.
                    ret_doc = sumo_api_ret['documents'][i]
                    eq_(link.provider, 'sumosuggest')
                    eq_(link.provider_version, 1)
                    eq_(link.cssclass, u'document')
                    eq_(link.url, '/redirect?r=sumosuggest.{0}'.format(i))
                    eq_(link.summary, ret_doc['title'])
                    eq_(link.description, ret_doc['summary'])

                    # Fetch the link and make sure it redirects to the
                    # right place.
                    resp = self.client.get(link.url)
                    # Temporary redirect.
                    eq_(resp.status_code, 302)
                    # Redirects to the actual SUMO url.
                    eq_(resp['Location'], docs[i]['url'])