from plone import api
from ploneintranet.news.behaviors import INewsSyndication
from ploneintranet.news.testing import IntegrationTestCase

import feedparser

PROJECTNAME = "ploneintranet.news"


atom_feed = """<?xml version="1.0" encoding="utf-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>ATOM feed</title>
  <id>wkilhjjgq6bsvlnbdp6ykqw8fcq</id>
  <link rel="self" href="https://example.com/collection/news.rss"/>
  <updated>2020-02-13T11:14:19.784656+00:00</updated>
  <subtitle>desc</subtitle>
    <entry>
      <title>First entry title</title>
      <link href="https://example.com/entry/1"/>
      <updated>2020-02-13T11:14:19.784656+00:00</updated>
      <summary>Watch out for &lt;span style="background-image:
-url(javascript:window.location='http://example.org/')"&gt;nasty
-tricks&lt;/span&gt;</summary>
      <author><name>Name 1</name></author>
      <id>https://example.com/entry/1</id>
      <content type="image/jpg" src="https://example.com/img"/>
    </entry>
    <entry>
      <title>Second entry title</title>
      <link href="https://example.com/entry/2"/>
      <updated>2020-02-13T16:16:28.785806+00:00</updated>
      <summary>Second entry description</summary>
      <author><name>Name 2</name></author>
      <id>https://example.com/entry/2</id>
    </entry>
</feed>
"""

rss_feed = """<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <atom:link rel="self" type="application/rss+xml"
               href="https://example.com/rss.xml" />
    <title>Feed</title>
    <link>https://example.com</link>
    <description></description>

    <language>de</language>
    <copyright>Creator</copyright>
    <generator>Plone</generator>
    <item>
      <title>First entry title</title>
      <description>Description of item 1</description>
      <content:encoded xmlns:content="http://purl.org/rss/1.0/modules/content/">
          <![CDATA[]]>
      </content:encoded>
      <pubDate>Fri, 14 Feb 2020 17:35:00 +0100</pubDate>
      <guid>xyz</guid>
      <enclosure url="https://example.com/img" length="47499" type="image/jpeg"/>
    </item>
    <item>
      <title>Second entry title</title>
      <description>Description of item 2</description>
      <content:encoded xmlns:content="http://purl.org/rss/1.0/modules/content/">
          <![CDATA[]]>
      </content:encoded>
      <pubDate>Wed, 12 Feb 2020 00:15:00 +0100</pubDate>
      <guid>abc</guid>
    </item>
</channel>
</rss>"""


class NewsSyndicationTest(IntegrationTestCase):
    def setUp(self):
        IntegrationTestCase.setUp(self)
        news_app = self.portal.news
        self.atom_section = api.content.create(
            container=news_app,
            type="ploneintranet.news.syndication",
            id="atom-syndication",
            feed_url=atom_feed,
        )
        self.atom_syndication = INewsSyndication(self.atom_section)
        self.rss_section = api.content.create(
            container=news_app,
            type="ploneintranet.news.syndication",
            id="rss-syndication",
            feed_url=rss_feed,
        )
        self.rss_syndication = INewsSyndication(self.rss_section)

    def test_create_news_from_atom(self):
        self.atom_syndication.sync_news()
        self.assertEqual(len(self.atom_section.objectIds()), 2)
        news_item = self.atom_section["first-entry-title"]
        self.assertEqual(news_item.Title(), "First entry title")

    def test_extract_image_url_from_atom_feed_entry(self):
        parsed_feed = feedparser.parse(atom_feed)
        first_entry = parsed_feed.entries[0]
        image_url = self.atom_syndication.get_image_url(first_entry)
        self.assertEqual(image_url, "https://example.com/img")

    def test_create_news_from_rss(self):
        self.rss_syndication.sync_news()
        self.assertEqual(len(self.rss_section.objectIds()), 2)
        news_item = self.rss_section["first-entry-title"]
        self.assertEqual(news_item.Title(), "First entry title")

    def test_extract_image_url_from_rss_feed_entry(self):
        parsed_feed = feedparser.parse(rss_feed)
        first_entry = parsed_feed.entries[0]
        image_url = self.rss_syndication.get_image_url(first_entry)
        self.assertEqual(image_url, "https://example.com/img")
