Readd request to context and add tests

This commit is contained in:
2023-01-28 19:44:59 +01:00
parent a9c79a7a94
commit e571ad9ee2
2 changed files with 51 additions and 18 deletions
discord_rss_bot
tests

@ -147,7 +147,7 @@ async def post_set_whitelist(
@app.get("/whitelist", response_class=HTMLResponse)
async def get_whitelist(feed_url):
async def get_whitelist(feed_url, request: Request):
"""Get the whitelist.
Args:
@ -162,6 +162,7 @@ async def get_whitelist(feed_url):
whitelist_content: str = get_whitelist_content(reader, feed)
context = {
"request": request,
"feed": feed,
"whitelist_title": whitelist_title,
"whitelist_summary": whitelist_summary,
@ -186,8 +187,6 @@ async def post_set_blacklist(
blacklist_content: Blacklisted words for when checking the content.
feed_url: What feed we should set the blacklist for.
"""
# Add the blacklist to the feed.
if blacklist_title:
reader.set_tag(feed_url, "blacklist_title", blacklist_title)
if blacklist_summary:
@ -195,17 +194,12 @@ async def post_set_blacklist(
if blacklist_content:
reader.set_tag(feed_url, "blacklist_content", blacklist_content)
clean_url = urllib.parse.quote(feed_url)
return RedirectResponse(url=f"/feed/?feed_url={clean_url}", status_code=303)
return RedirectResponse(url=f"/feed/?feed_url={urllib.parse.quote(feed_url)}", status_code=303)
@app.get("/blacklist", response_class=HTMLResponse)
async def get_blacklist(feed_url, request: Request):
# Make feed_url a valid URL.
url: str = urllib.parse.unquote(feed_url)
feed: Feed = reader.get_feed(url)
feed: Feed = reader.get_feed(urllib.parse.unquote(feed_url))
# Get previous data, this is used when creating the form.
blacklist_title: str = get_blacklist_title(reader, feed)
@ -558,7 +552,7 @@ async def get_webhooks(request: Request):
@app.get("/", response_class=HTMLResponse)
def index(request: Request):
def get_index(request: Request):
"""
This is the root of the website.

@ -9,13 +9,7 @@ client: TestClient = TestClient(app)
webhook_name: str = "Hello, I am a webhook!"
webhook_url: str = "https://discord.com/api/webhooks/1234567890/abcdefghijklmnopqrstuvwxyz"
feed_url: str = "https://lovinator.space/rss_test.xml"
encoded_feed_url: str = encode_url(webhook_url)
def test_read_main() -> None:
"""Test the main page."""
response: Response = client.get("/")
assert response.status_code == 200
encoded_feed_url: str = encode_url(feed_url)
def test_search() -> None:
@ -87,6 +81,51 @@ def test_create_feed() -> None:
assert feed_url in response.text
def test_get() -> None:
"""Test the /create_feed page."""
# Remove the feed if it already exists before we run the test.
feeds: Response = client.get("/")
if feed_url in feeds.text:
client.post("/remove", data={"feed_url": feed_url})
client.post("/remove", data={"feed_url": encoded_feed_url})
# Add the feed.
response: Response = client.post("/add", data={"feed_url": feed_url, "webhook_dropdown": webhook_name})
assert response.status_code == 200
# Check that the feed was added.
response = client.get("/")
assert response.status_code == 200
assert feed_url in response.text
response: Response = client.get("/add")
assert response.status_code == 200
response: Response = client.get("/add_webhook")
assert response.status_code == 200
response: Response = client.get("/blacklist", params={"feed_url": encoded_feed_url})
assert response.status_code == 200
response: Response = client.get("/custom", params={"feed_url": encoded_feed_url})
assert response.status_code == 200
response: Response = client.get("/embed", params={"feed_url": encoded_feed_url})
assert response.status_code == 200
response: Response = client.get("/feed", params={"feed_url": encoded_feed_url})
assert response.status_code == 200
response: Response = client.get("/")
assert response.status_code == 200
response: Response = client.get("/webhooks")
assert response.status_code == 200
response: Response = client.get("/whitelist", params={"feed_url": encoded_feed_url})
assert response.status_code == 200
def test_pause_feed() -> None:
"""Test the /pause_feed page."""
# Remove the feed if it already exists before we run the test.