Add happy path test for sending discord notification

This commit is contained in:
Deko
2023-03-04 00:38:22 +01:00
parent b5a7d409f8
commit 3a194988c1
4 changed files with 51 additions and 8 deletions

View File

@@ -2,6 +2,7 @@ import os
import requests
from loguru import logger
from app.twitch_client import StreamInformation
@@ -15,7 +16,7 @@ class DiscordClient:
self, stream: StreamInformation, profile_image: str
) -> str:
logger.info("Sending a message with an embed to the webhook...")
streamer_url = f"https://www.twitch.tv/{stream.user_login}/"
streamer_url = f"https://www.twitch.tv/{stream.user_login}"
response = requests.post(
url=f"{self._webhook_url}?wait=true",
json={
@@ -61,7 +62,7 @@ class DiscordClient:
self, stream: StreamInformation, profile_image: str
) -> None:
logger.info("Updating stream information on Discord...")
streamer_url = f"https://www.twitch.tv/{stream.user_login}/"
streamer_url = f"https://www.twitch.tv/{stream.user_login}"
response = requests.patch(
url=f"{self._webhook_url}/messages/{self._notification_msg_id}",
json={

View File

@@ -1,11 +1,12 @@
import os
import time
from app.discord_client import DiscordClient
from app.twitch_client import StreamInformation, TwitchClient
from loguru import logger
from requests import HTTPError
from app.discord_client import DiscordClient
from app.twitch_client import StreamInformation, TwitchClient
class Main:
is_live: bool = False

View File

@@ -1,4 +1,6 @@
import json
import os
from typing import Any
from unittest import mock
import pytest
@@ -14,6 +16,45 @@ def test_require_webhook_url():
DiscordClient()
def test_send_information_to_discord(mock_loggers):
stream = StreamInformation(
user_id="0",
user_name="Test",
user_login="test",
game_name="game",
started_at="never",
title="title",
viewer_count=0,
_thumbnail_url="https://thumbnail.com/{width}-{height}.png",
)
with (
mock.patch.dict(
os.environ, {"DISCORD_WEBHOOK_URL": "https://test/url"}
),
requests_mock.Mocker() as requests_mocker,
):
requests_mocker.post(url="https://test/url", json={"id": "0"})
discord_client = DiscordClient()
discord_client.send_information_to_discord(
stream=stream, profile_image="profile_image.png"
)
webhook_call = requests_mocker.request_history[0]
assert webhook_call.url == "https://test/url?wait=true"
embed: dict[str, Any] = json.loads(webhook_call.text)["embeds"][0]
assert embed["title"] == "title"
assert embed["timestamp"] == "never"
assert embed["url"] == f"https://www.twitch.tv/test"
assert embed["author"]["name"] == "Test"
assert embed["author"]["url"] == embed["url"]
assert embed["author"]["icon_url"] == "profile_image.png"
assert embed["image"]["url"] == "https://thumbnail.com/1280-720.png"
assert embed["fields"][0]["value"] == "game"
assert embed["fields"][1]["value"] == 0
def test_send_information_to_discord_fails(mock_loggers):
stream = StreamInformation(
user_id="",
@@ -23,7 +64,7 @@ def test_send_information_to_discord_fails(mock_loggers):
started_at="",
title="",
viewer_count=0,
_thumbnail_url=""
_thumbnail_url="",
)
with (