Add happy path test for sending discord notification
This commit is contained in:
@@ -14,9 +14,9 @@ stages:
|
|||||||
image: python:3.11.2
|
image: python:3.11.2
|
||||||
only:
|
only:
|
||||||
changes:
|
changes:
|
||||||
- "/poetry.lock"
|
- "poetry.lock"
|
||||||
- "/pyproject.toml"
|
- "pyproject.toml"
|
||||||
- "/app/*"
|
- "app/*"
|
||||||
|
|
||||||
.test:
|
.test:
|
||||||
extends: .base
|
extends: .base
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import os
|
|||||||
|
|
||||||
import requests
|
import requests
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
from app.twitch_client import StreamInformation
|
from app.twitch_client import StreamInformation
|
||||||
|
|
||||||
|
|
||||||
@@ -15,7 +16,7 @@ class DiscordClient:
|
|||||||
self, stream: StreamInformation, profile_image: str
|
self, stream: StreamInformation, profile_image: str
|
||||||
) -> str:
|
) -> str:
|
||||||
logger.info("Sending a message with an embed to the webhook...")
|
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(
|
response = requests.post(
|
||||||
url=f"{self._webhook_url}?wait=true",
|
url=f"{self._webhook_url}?wait=true",
|
||||||
json={
|
json={
|
||||||
@@ -61,7 +62,7 @@ class DiscordClient:
|
|||||||
self, stream: StreamInformation, profile_image: str
|
self, stream: StreamInformation, profile_image: str
|
||||||
) -> None:
|
) -> None:
|
||||||
logger.info("Updating stream information on Discord...")
|
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(
|
response = requests.patch(
|
||||||
url=f"{self._webhook_url}/messages/{self._notification_msg_id}",
|
url=f"{self._webhook_url}/messages/{self._notification_msg_id}",
|
||||||
json={
|
json={
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from app.discord_client import DiscordClient
|
|
||||||
from app.twitch_client import StreamInformation, TwitchClient
|
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from requests import HTTPError
|
from requests import HTTPError
|
||||||
|
|
||||||
|
from app.discord_client import DiscordClient
|
||||||
|
from app.twitch_client import StreamInformation, TwitchClient
|
||||||
|
|
||||||
|
|
||||||
class Main:
|
class Main:
|
||||||
is_live: bool = False
|
is_live: bool = False
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
|
import json
|
||||||
import os
|
import os
|
||||||
|
from typing import Any
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@@ -14,6 +16,45 @@ def test_require_webhook_url():
|
|||||||
DiscordClient()
|
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):
|
def test_send_information_to_discord_fails(mock_loggers):
|
||||||
stream = StreamInformation(
|
stream = StreamInformation(
|
||||||
user_id="",
|
user_id="",
|
||||||
@@ -23,7 +64,7 @@ def test_send_information_to_discord_fails(mock_loggers):
|
|||||||
started_at="",
|
started_at="",
|
||||||
title="",
|
title="",
|
||||||
viewer_count=0,
|
viewer_count=0,
|
||||||
_thumbnail_url=""
|
_thumbnail_url="",
|
||||||
)
|
)
|
||||||
|
|
||||||
with (
|
with (
|
||||||
|
|||||||
Reference in New Issue
Block a user