示例#1
0
    def test_get_news_feed_articles_none_200_response(self):
        self.license_keys = LicenseKeys()
        self.license_keys.load_license_key_file(
            os.path.dirname(__file__) + os.sep + "test.keys")

        mock_api = MockNewsApiApi()

        newsapi = NewsAPI(self.license_keys, mock_api)
        self.assertIsNotNone(newsapi)

        mock_api.response = MockResponse(
            401, {"content-type": "application/json"},
            json.loads("""
        {
            "articles": [
                {
                "title":        "test title",
                "description":  "test description",
                "publishedAt":  "test publishedAt",
                "author":       "test author",
                "url":          "test url",
                "urlToImage":   "test urlToImage"
                }
            ]
        }
        """))
        mock_url = NewsAPI._format_url("testservice", "key")

        articles = newsapi._get_news_feed_articles(mock_url, 10, True, False)
        self.assertIsNotNone(articles)
        self.assertEqual(0, len(articles))
示例#2
0
 def test_format_url(self):
     self.assertEqual(
         "https://newsapi.org/v1/articles?source=testservice&sortBy=top&apiKey=key",
         NewsAPI._format_url("testservice", "key"))
     self.assertEqual(
         "https://newsapi.org/v1/articles?source=testservice&sortBy=bottom&apiKey=key",
         NewsAPI._format_url("testservice", "key", sort_by="bottom"))
示例#3
0
    def test_get_news_feed_articles_content_not_json(self):
        self.license_keys = LicenseKeys()
        self.license_keys.load_license_key_file(
            os.path.dirname(__file__) + os.sep + "test.keys")

        mock_api = MockNewsApiApi()

        newsapi = NewsAPI(self.license_keys, mock_api)
        self.assertIsNotNone(newsapi)

        mock_api.response = MockResponse(200,
                                         {"content-type": "application/xml"},
                                         None)

        mock_url = NewsAPI._format_url("testservice", "key")

        articles = newsapi._get_news_feed_articles(mock_url, 10, True, False)
        self.assertIsNotNone(articles)
        self.assertEqual(0, len(articles))
示例#4
0
 def get_news_api_api(self, context):
     return NewsAPI(context.client.license_keys)
示例#5
0
    def test_init(self):
        newsapi = NewsAPI(self.license_keys)
        self.assertIsNotNone(newsapi)

        articles = newsapi.get_headlines(NewsAPI.BBC_NEWS)
        self.assertIsNotNone(articles)
示例#6
0
import os

from programr.utils.license.keys import LicenseKeys
from programr.utils.newsapi.newsapi import NewsAPI

if __name__ == '__main__':

    # Running these tools drops test files into the newapi test folder

    app_license_keys = LicenseKeys()
    app_license_keys.load_license_key_file(
        os.path.dirname(__file__) +
        '/../../../../bots/y-bot/config/license.keys')

    news_api = NewsAPI(app_license_keys)

    results = news_api.get_headlines(NewsAPI.BBC_NEWS)

    json_data = NewsAPI.to_json(results)

    NewsAPI.json_to_file('../../../test/utils/newsapi/newsapi.json', json_data)

    # Running these tools drops test files into the geocode test folder
示例#7
0
 def test_missing_keys(self):
     self.license_keys = LicenseKeys()
     self.license_keys.load_license_key_file(
         os.path.dirname(__file__) + os.sep + "bad_test.keys")
     with self.assertRaises(Exception):
         newsapi = NewsAPI(self.license_keys)
示例#8
0
 def test_missing_license_keys(self):
     with self.assertRaises(Exception):
         newsapi = NewsAPI(None)