Skip to content

Commit

Permalink
Add tests to test User-Agent headers in request
Browse files Browse the repository at this point in the history
  • Loading branch information
ogajduse committed Dec 12, 2023
1 parent 09f7ded commit 6dbc55b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
2 changes: 1 addition & 1 deletion custom_components/feedparser/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
DEFAULT_SCAN_INTERVAL = timedelta(hours=1)
DEFAULT_THUMBNAIL = "https://www.home-assistant.io/images/favicon-192x192-full.png"
DEFAULT_TOPN = 9999
USER_AGENT = f"Home Assistant Feedparser Integration {__version__}"
USER_AGENT = f"Home Assistant Feed-parser Integration {__version__}"

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
Expand Down
19 changes: 13 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ classifiers = [
"Programming Language :: Python :: 3.11",
]
requires-python = ">=3.11.0"
dependencies = ["python-dateutil", "feedparser==6.0.10", "homeassistant"]
dependencies = [
"feedparser==6.0.10",
"homeassistant",
"python-dateutil",
"requests-file",
"requests",
]

[project.optional-dependencies]
dev = [
Expand All @@ -34,6 +40,7 @@ dev = [
"ruff",
"types-python-dateutil",
"types-PyYAML",
"types-requests",
"voluptuous-stubs",
"pyyaml",

Expand Down Expand Up @@ -97,11 +104,11 @@ select = [

# Q000,ANN,PT009,D,E501,
ignore = [
"D107", # Missing docstring in __init__
"FBT001", # Boolean positional arg in function definition
"D203", # 1 blank line required before class docstring
"D213", # Multi-line docstring summary should start at the first line
"FBT001" # Boolean positional argument in function definition
"D107", # Missing docstring in __init__
"FBT001", # Boolean positional arg in function definition
"D203", # 1 blank line required before class docstring
"D213", # Multi-line docstring summary should start at the first line
"FBT001", # Boolean positional argument in function definition
]

# Allow autofix for all enabled rules (when `--fix`) is provided.
Expand Down
11 changes: 11 additions & 0 deletions tests/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,14 @@
DEFAULT_EXCLUSIONS: list[str] = []
DEFAULT_INCLUSIONS = ["image", "title", "link", "published"]
DATE_FORMAT = "%a, %d %b %Y %H:%M:%S UTC%z"

URLS_HEADERS_REQUIRED = [
{
"name": "elcomercio_gijon",
"url": "https://www.elcomercio.es/rss/2.0/?section=gijon",
},
{
"name": "nasdaq_options",
"url": "https://www.nasdaq.com/feed/rssoutbound?category=Options",
},
]
23 changes: 22 additions & 1 deletion tests/test_sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import feedparser
import pytest
from constants import DATE_FORMAT
from constants import DATE_FORMAT, URLS_HEADERS_REQUIRED
from feedsource import FeedSource

from custom_components.feedparser.sensor import (
Expand Down Expand Up @@ -157,3 +157,24 @@ def test_check_duplicates(feed_sensor: FeedParserSensor) -> None:
feed_sensor.update()
after_second_update = len(feed_sensor.feed_entries)
assert after_first_update == after_second_update


@pytest.mark.parametrize(
"online_feed",
URLS_HEADERS_REQUIRED,
ids=lambda feed_url: feed_url["name"],
)
def test_fetch_data_headers_required(online_feed: dict) -> None:
"""Test fetching feed from remote server that requires request with headers."""
feed_sensor = FeedParserSensor(
feed=online_feed["url"],
name=online_feed["name"],
date_format=DATE_FORMAT,
local_time=False,
show_topn=9999,
inclusions=["image", "title", "link", "published"],
exclusions=[],
scan_interval=DEFAULT_SCAN_INTERVAL,
)
feed_sensor.update()
assert feed_sensor.feed_entries

0 comments on commit 6dbc55b

Please sign in to comment.