<commit_before># coding=utf-8
"""
Functional testing of courtlistener RSS feeds
"""
import os
import feedparser
from django.conf import settings
from django.core.urlresolvers import reverse
from django.test.utils import override_settings
from cl.lib.storage import IncrementingFileSystemStorage
from cl.search.models import Court
from cl.tests.base import BaseSeleniumTest


@override_settings(
    MEDIA_ROOT=os.path.join(settings.INSTALL_ROOT, 'cl/assets/media/test/')
)
class FeedsFunctionalTest(BaseSeleniumTest):
    """Tests the Feeds page and functionality"""

    fixtures = ['test_court.json', 'judge_judy.json', 'functest_opinions.json',
                'functest_audio.json']

    @classmethod
    def setUpClass(cls):
        """
        Need to work around issue reported and fixed in Django project:
        https://code.djangoproject.com/ticket/26038

        Overwriting the current 1.8/1.9 logic on FileSystemStorage:
            def path(self, name):
                    return safe_join(self.location, name)

        """
        def patched_path(self, name):
            """ Patching Path method to use MEDIA_ROOT properly """
            return '%s%s' % (settings.MEDIA_ROOT, name,)

        IncrementingFileSystemStorage.path = patched_path
        super(FeedsFunctionalTest, cls).setUpClass()

    def test_can_get_to_feeds_from_homepage(self):
        """Can we get to the feeds/podcasts page from the homepage?"""
        self.browser.get(self.server_url)
        link = self.browser.find_element_by_link_text('Feeds & Podcasts')
        link.click()

        self.assertIn('Feeds & Podcasts', self.browser.title)
        self.assertIn('/feeds', self.browser.current_url)
        self.assert_text_in_body('Feeds & Podcasts')

    def test_feeds_page_shows_jurisdiction_links(self):
        """
        Does the feeds page show all the proper links for each jurisdiction?
        """
        courts = Court.objects.all()
        self.browser.get('%s%s' % (self.server_url, reverse('feeds_info')))
        self.assert_text_in_body('Jurisdiction Feeds for Opinions')

        for court in courts:
            link = self.browser.find_element_by_link_text(court.full_name)
            self.assertEqual(
                link.get_attribute('href'),
                '%s/feed/court/%s/' % (self.server_url, court.pk,)
            )
            link.click()
            self.assertIn(
                'feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-us"',
                self.browser.page_source
            )
            self.browser.back()

    def test_all_jurisdiction_opinion_rss_feeds_usable_in_rss_reader(self):
        """
        Can the RSS feed for ALL jurisdictions render properly in an RSS reader?
        """
        f = feedparser.parse(
            '%s%s' % (self.server_url, reverse('all_jurisdictions_feed'))
        )
        self.assertEqual(
            u'CourtListener.com: All Opinions (High Volume)',
            f.feed.title
        )
        # Per https://pythonhosted.org/feedparser/bozo.html
        self.assertEqual(f.bozo, 0, 'Feed should be wellformed')

    def test_court_opinion_rss_feeds_usable_in_rss_reader(self):
        """<commit_msg>Updates feed test to work with jurisdictions that don't have scrapers.<commit_after># coding=utf-8
"""
Functional testing of courtlistener RSS feeds
"""
import os
import feedparser
from django.conf import settings
from django.core.urlresolvers import reverse
from django.test.utils import override_settings
from cl.lib.storage import IncrementingFileSystemStorage
from cl.search.models import Court
from cl.tests.base import BaseSeleniumTest


@override_settings(
    MEDIA_ROOT=os.path.join(settings.INSTALL_ROOT, 'cl/assets/media/test/')
)
class FeedsFunctionalTest(BaseSeleniumTest):
    """Tests the Feeds page and functionality"""

    fixtures = ['test_court.json', 'judge_judy.json', 'functest_opinions.json',
                'functest_audio.json']

    @classmethod
    def setUpClass(cls):
        """
        Need to work around issue reported and fixed in Django project:
        https://code.djangoproject.com/ticket/26038

        Overwriting the current 1.8/1.9 logic on FileSystemStorage:
            def path(self, name):
                    return safe_join(self.location, name)

        """
        def patched_path(self, name):
            """ Patching Path method to use MEDIA_ROOT properly """
            return '%s%s' % (settings.MEDIA_ROOT, name,)

        IncrementingFileSystemStorage.path = patched_path
        super(FeedsFunctionalTest, cls).setUpClass()

    def test_can_get_to_feeds_from_homepage(self):
        """Can we get to the feeds/podcasts page from the homepage?"""
        self.browser.get(self.server_url)
        link = self.browser.find_element_by_link_text('Feeds & Podcasts')
        link.click()

        self.assertIn('Feeds & Podcasts', self.browser.title)
        self.assertIn('/feeds', self.browser.current_url)
        self.assert_text_in_body('Feeds & Podcasts')

    def test_feeds_page_shows_jurisdiction_links(self):
        """
        Does the feeds page show all the proper links for each jurisdiction?
        """
        courts = Court.objects.filter(
            in_use=True,
            has_opinion_scraper=True,
        )
        self.browser.get('%s%s' % (self.server_url, reverse('feeds_info')))
        self.assert_text_in_body('Jurisdiction Feeds for Opinions')

        for court in courts:
            link = self.browser.find_element_by_link_text(court.full_name)
            print "Testing link to %s..." % court.full_name,
            self.assertEqual(
                link.get_attribute('href'),
                '%s/feed/court/%s/' % (self.server_url, court.pk,)
            )
            link.click()
            print "clicked...",
            self.assertIn(
                'feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-us"',
                self.browser.page_source
            )
            self.browser.back()
            print "✓"


    def test_all_jurisdiction_opinion_rss_feeds_usable_in_rss_reader(self):
        """
        Can the RSS feed for ALL jurisdictions render properly in an RSS reader?
        """
        f = feedparser.parse(
            '%s%s' % (self.server_url, reverse('all_jurisdictions_feed'))
        )
        self.assertEqual(
            u'CourtListener.com: All Opinions (High Volume)',
            f.feed.title
        )
        # Per https://pythonhosted.org/feedparser/bozo.html
        self.assertEqual(f.bozo, 0, 'Feed should be wellformed')

    def test_court_opinion_rss_feeds_usable_in_rss_reader(self):
        """
{'fraction_non_alphanumeric': 0.10418548629930743, 'fraction_numerical': 0.004817825956037338, 'mean_word_length': 2.548611111111111, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4854857', 'n_tokens_mistral': 2074, 'n_tokens_neox': 1940, 'n_words': 422}