Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkLark86 committed Apr 11, 2024
1 parent 8eeb301 commit 773c3a0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion superdesk/media/media_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def download_file_from_url(
try:
rv = session.get(url, **request_kwargs)
except requests.exceptions.MissingSchema: # any route will do here, we only need host
rv = session.get(urljoin(url_for("static", filename="x", _external=True), url), timeout=15, **request_kwargs)
rv = session.get(urljoin(url_for("static", filename="x", _external=True), url), **request_kwargs)
if rv.status_code not in (200, 201):
raise SuperdeskApiError.internalError("Failed to retrieve file from URL: %s" % url)
content = BytesIO(rv.content)
Expand Down
4 changes: 2 additions & 2 deletions superdesk/media/renditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ def update_renditions(item, href, old_item, request_kwargs=None, feeding_service
item["filemeta_json"] = old_item.get("filemeta_json")
return

if feeding_service is not None and getattr(feeding_service, "download_file"):
if feeding_service is not None and hasattr(feeding_service, "download_file"):
content, filename, content_type = feeding_service.download_file(href, **request_kwargs or {})
else:
content, filename, content_type = download_file_from_url(href, request_kwargs)
Expand Down Expand Up @@ -406,7 +406,7 @@ def transfer_renditions(renditions, request_kwargs=None, feeding_service=None):
rend["href"] = app.media.url_for_media(rend["media"], local.content_type)
continue

if feeding_service is not None and getattr(feeding_service, "download_file"):
if feeding_service is not None and hasattr(feeding_service, "download_file"):
content, filename, content_type = feeding_service.download_file(rend.get("href"), **request_kwargs or {})
else:
content, filename, content_type = download_file_from_url(rend.get("href"), request_kwargs)
Expand Down
8 changes: 4 additions & 4 deletions tests/io/feeding_services/ap_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ def setUp(self):
@mock.patch.object(ap.APFeedingService, "get_feed_parser")
def test_feeding(self, get_feed_parser, requests):
get_feed_parser.return_value = newsml_2_0.NewsMLTwoFeedParser()
mock_get = requests.get.return_value
mock_get.content = self.feed_raw
provider = deepcopy(PROVIDER)
service = ap.APFeedingService()
service.provider = provider
mock_get = service.session.get.return_value
mock_get.content = self.feed_raw
items = service._update(provider, {})[0]
self.assertEqual(len(items), 3)

Expand All @@ -65,11 +65,11 @@ def test_items_order(self, get_feed_parser, requests):
"""
feed_parser = newsml_2_0.NewsMLTwoFeedParser()
get_feed_parser.return_value = feed_parser
mock_get = requests.get.return_value
mock_get.content = self.feed_raw
provider = deepcopy(PROVIDER)
service = ap.APFeedingService()
service.provider = provider
mock_get = service.session.get.return_value
mock_get.content = self.feed_raw

self.assertNotIn("private", provider)
with mock.patch.object(feed_parser, "parse"):
Expand Down
4 changes: 2 additions & 2 deletions tests/io/feeding_services/ritzau_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ def setUp(self):
@mock.patch.object(ritzau.RitzauFeedingService, "get_feed_parser")
def test_feeding(self, get_feed_parser, requests):
get_feed_parser.return_value = ritzau_feed.RitzauFeedParser()
mock_get = requests.get.return_value
mock_get.text = self.feed_raw
provider = PROVIDER.copy()
service = ritzau.RitzauFeedingService()
service.provider = provider
mock_get = service.session.get.return_value
mock_get.text = self.feed_raw
items = service._update(provider, {})[0]
self.assertEqual(len(items), 2)
34 changes: 17 additions & 17 deletions tests/io/feeding_services/rss_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from superdesk.io.commands.update_ingest import LAST_ITEM_UPDATE

feed_parse = MagicMock()
requests_get = MagicMock()

nrk_xml = """
<rss version="2.0">
Expand Down Expand Up @@ -240,25 +239,24 @@ def test_returns_items_and_package_list_if_entry_contains_image_urls(self):
self.assertCountEqual(items, expected_items)


@mock.patch("superdesk.io.feeding_services.http_base_service.requests.get", requests_get)
@mock.patch("superdesk.io.feeding_services.http_base_service.IngestApiError", FakeIngestApiError)
class FetchDataMethodTestCase(RssIngestServiceTest):
"""Tests for the _fetch_data() method."""

def setUp(self):
requests_get.reset_mock()
self.instance.session.get = MagicMock()
self.fake_provider = MagicMock(name="fake provider")
self.instance.provider = self.fake_provider
self.fake_provider.__getitem__.return_value = self.config
self.fake_provider.setdefault.return_value = self.config

def test_retrieves_feed_from_correct_url(self):
requests_get.return_value = MagicMock(ok=True)
self.instance.session.get.return_value = MagicMock(ok=True)
self.config.update(dict(url="http://news.com/rss"))

self.instance._fetch_data()

call_args = requests_get.call_args[0]
call_args = self.instance.session.get.call_args[0]
self.assertEqual(call_args[0], "http://news.com/rss")

def test_stores_auth_info_in_instance_if_auth_required(self):
Expand All @@ -271,7 +269,7 @@ def test_stores_auth_info_in_instance_if_auth_required(self):
}
)

requests_get.return_value = MagicMock(ok=False)
self.instance.session.get.return_value = MagicMock(ok=False)

try:
self.instance._update(self.fake_provider, {})
Expand All @@ -281,24 +279,24 @@ def test_stores_auth_info_in_instance_if_auth_required(self):
self.assertEqual(self.instance.auth_info, {"username": "james", "password": "bond+007"})

def test_provides_auth_info_if_required(self):
requests_get.return_value = MagicMock(ok=True)
self.instance.session.get.return_value = MagicMock(ok=True)
self.config.update(dict(url="http://news.com/rss", auth_required=True, username="johndoe", password="secret"))

self.instance._fetch_data()

kw_call_args = requests_get.call_args[1]
kw_call_args = self.instance.session.get.call_args[1]
self.assertEqual(kw_call_args.get("auth"), ("johndoe", "secret"))

def test_returns_fetched_data_on_success(self):
requests_get.return_value = MagicMock(ok=True, content="<rss>X</rss>")
self.instance.session.get.return_value = MagicMock(ok=True, content="<rss>X</rss>")
self.config.update(dict(url="http://news.com/rss"))

response = self.instance._fetch_data()

self.assertEqual(response, "<rss>X</rss>")

def test_raises_auth_error_on_401(self):
requests_get.return_value = MagicMock(ok=False, status_code=401, reason="invalid credentials")
self.instance.session.get.return_value = MagicMock(ok=False, status_code=401, reason="invalid credentials")
self.config.update(dict(url="http://news.com/rss"))

try:
Expand All @@ -313,7 +311,7 @@ def test_raises_auth_error_on_401(self):
self.assertIs(ex.provider, self.fake_provider)

def test_raises_auth_error_on_403(self):
requests_get.return_value = MagicMock(ok=False, status_code=403, reason="access forbidden")
self.instance.session.get.return_value = MagicMock(ok=False, status_code=403, reason="access forbidden")
self.config.update(dict(url="http://news.com/rss"))

try:
Expand All @@ -328,7 +326,7 @@ def test_raises_auth_error_on_403(self):
self.assertIs(ex.provider, self.fake_provider)

def test_raises_not_found_error_on_404(self):
requests_get.return_value = MagicMock(ok=False, status_code=404, reason="resource not found")
self.instance.session.get.return_value = MagicMock(ok=False, status_code=404, reason="resource not found")
self.config.update(dict(url="http://news.com/rss"))

try:
Expand All @@ -343,7 +341,7 @@ def test_raises_not_found_error_on_404(self):
self.assertIs(ex.provider, self.fake_provider)

def test_raises_general_error_on_unknown_error(self):
requests_get.return_value = MagicMock(ok=False, status_code=500, reason="server down")
self.instance.session.get.return_value = MagicMock(ok=False, status_code=500, reason="server down")
self.config.update(dict(url="http://news.com/rss"))

try:
Expand Down Expand Up @@ -643,8 +641,9 @@ def test_aliases_fields_are_skipped_unless_themselves_aliased(self):

def test_guid_not_permalink(self):
self.instance.provider = provider = {"config": {"url": "http://example.com/rss"}}
with mock.patch("superdesk.io.feeding_services.http_base_service.requests.get", return_value=RssResponse()):
items = self.instance._update(provider, None)[0]
self.instance.session.get = MagicMock()
self.instance.session.get.return_value = RssResponse()
items = self.instance._update(provider, None)[0]
self.assertEqual(1, len(items))
self.assertEqual("https://www.nrk.no/finnmark/stanset-ikke-for-fotgjenger-1.13362571", items[0]["uri"])
self.assertEqual("tag:www.nrk.no:1.13362571", items[0]["guid"])
Expand All @@ -654,8 +653,9 @@ def test_guid_not_set(self):
self.instance.provider = provider = {"config": {"url": "http://example.com/rss"}}
response = RssResponse()
response.content = nrk_xml.replace('<guid isPermaLink="false">1.13362571</guid>', "")
with mock.patch("superdesk.io.feeding_services.http_base_service.requests.get", return_value=response):
items = self.instance._update(provider, None)[0]
self.instance.session.get = MagicMock()
self.instance.session.get.return_value = response
items = self.instance._update(provider, None)[0]
self.assertEqual("tag:www.nrk.no:finnmark:stanset-ikke-for-fotgjenger-1.13362571", items[0]["guid"])


Expand Down

0 comments on commit 773c3a0

Please sign in to comment.