diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 35bca98..47ccc43 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -14,9 +14,9 @@ stages: image: python:3.11.2 only: changes: - - "/poetry.lock" - - "/pyproject.toml" - - "/app/*" + - "poetry.lock" + - "pyproject.toml" + - "app/*" .test: extends: .base diff --git a/app/discord_client.py b/app/discord_client.py index a7a5d7b..e47c6d8 100644 --- a/app/discord_client.py +++ b/app/discord_client.py @@ -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={ diff --git a/app/main.py b/app/main.py index 353d939..bbab36c 100644 --- a/app/main.py +++ b/app/main.py @@ -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 diff --git a/app/tests/test_discord_client.py b/app/tests/test_discord_client.py index 5422787..4c11c6a 100644 --- a/app/tests/test_discord_client.py +++ b/app/tests/test_discord_client.py @@ -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 (