Skip to content

Commit

Permalink
added bot unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
minimike86 committed Dec 1, 2024
1 parent bd700ab commit 5309a46
Show file tree
Hide file tree
Showing 14 changed files with 343 additions and 63 deletions.
1 change: 1 addition & 0 deletions .env_example
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ AWS_REGION=
AWS_DEFAULT_REGION=
API_GATEWAY_INVOKE_URL=
API_GATEWAY_ROUTE=
DYNAMODB_USER_TABLE_NAME=
19 changes: 18 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ nest-asyncio = "^1.6.0"
boto3 = "^1.35.50"
twitchio = "^2.10.0"
vt-py = "^0.18.4"
pytest-responses = "^0.5.1"

[tool.poetry.group.dev.dependencies]
setuptools = "^75.2.0"
Expand Down
3 changes: 2 additions & 1 deletion tests/config/test_bot_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def set_environment_variables(monkeypatch):
"https://12345.execute-api.eu-west-2.amazonaws.com",
)
monkeypatch.setenv("API_GATEWAY_ROUTE", "/twitch/oauth2/authorization_code")
monkeypatch.setenv("DYNAMODB_USER_TABLE_NAME", "MSecBot_User")


def test_config(monkeypatch):
Expand Down Expand Up @@ -79,7 +80,7 @@ def test_config(monkeypatch):
)
assert (
BOT_CONFIG.get("bot_features").get("virus_total").get("enable_virus_total")
is False
is True
)
assert (
BOT_CONFIG.get("bot_features").get("virus_total").get("virus_total_api_key")
Expand Down
28 changes: 15 additions & 13 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import logging
import os

import boto3
import pytest
Expand All @@ -21,7 +22,7 @@
}

logging.basicConfig(
level=logging.DEBUG,
level=logging.INFO,
format="%(asctime)s | %(levelname)-8s | %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
Expand All @@ -41,6 +42,7 @@ def set_environment_variables(monkeypatch):
monkeypatch.setenv("BOT_JOIN_CHANNEL_ID", "123456")
monkeypatch.setenv("MAX_VIP_SLOTS", "10")
monkeypatch.setenv("VIRUS_TOTAL_API_KEY", "xyz")
monkeypatch.setenv("DYNAMODB_USER_TABLE_NAME", "User")


@pytest.fixture
Expand Down Expand Up @@ -159,9 +161,9 @@ def test_main(mocker, capfd, bot_user):
mock_dynamodb = boto3.resource(
"dynamodb", region_name=BOT_CONFIG.get("aws").get("region_name")
)
mock_table_name = "user"
table_name = os.getenv("DYNAMODB_USER_TABLE_NAME")
mock_dynamodb.create_table(
TableName=mock_table_name,
TableName=table_name,
KeySchema=[
{"AttributeName": "id", "KeyType": "HASH"}, # Partition key
],
Expand All @@ -173,7 +175,7 @@ def test_main(mocker, capfd, bot_user):
"WriteCapacityUnits": 5,
},
)
mock_table = mock_dynamodb.Table(mock_table_name)
mock_table = mock_dynamodb.Table(table_name)
mock_table.put_item(
Item={
"id": "123456",
Expand Down Expand Up @@ -226,9 +228,9 @@ def test_setup_bot_if_bot_user_has_no_access_token_should_raise_value_error(
mock_dynamodb = boto3.resource(
"dynamodb", region_name=BOT_CONFIG.get("aws").get("region_name")
)
mock_table_name = "user"
table_name = os.getenv("DYNAMODB_USER_TABLE_NAME")
mock_table = mock_dynamodb.create_table(
TableName=mock_table_name,
TableName=table_name,
KeySchema=[
{"AttributeName": "id", "KeyType": "HASH"}, # Partition key
],
Expand All @@ -240,7 +242,7 @@ def test_setup_bot_if_bot_user_has_no_access_token_should_raise_value_error(
"WriteCapacityUnits": 5,
},
)
mock_table.meta.client.get_waiter("table_exists").wait(TableName=mock_table_name)
mock_table.meta.client.get_waiter("table_exists").wait(TableName=table_name)
mock_table.put_item(
Item={
"id": "875992093",
Expand Down Expand Up @@ -276,9 +278,9 @@ def test_setup_bot_if_db_has_no_bot_user_should_raise_value_error(mocker):
mock_dynamodb = boto3.resource(
"dynamodb", region_name=BOT_CONFIG.get("aws").get("region_name")
)
mock_table_name = "user"
table_name = os.getenv("DYNAMODB_USER_TABLE_NAME")
mock_dynamodb.create_table(
TableName=mock_table_name,
TableName=table_name,
KeySchema=[
{"AttributeName": "id", "KeyType": "HASH"}, # Partition key
],
Expand All @@ -290,7 +292,7 @@ def test_setup_bot_if_db_has_no_bot_user_should_raise_value_error(mocker):
"WriteCapacityUnits": 5,
},
)
mock_table = mock_dynamodb.Table(mock_table_name)
mock_table = mock_dynamodb.Table(table_name)
mock_get_item = mocker.patch("twitchrce.main.user_table.get_item")
mock_get_item.return_value = mock_table.get_item(
Key={"id": BOT_CONFIG.get("twitch").get("bot_user_id")}
Expand Down Expand Up @@ -318,9 +320,9 @@ def test_setup_bot_if_bot_user_has_access_token_but_describe_instances_has_no_re
mock_dynamodb = boto3.resource(
"dynamodb", region_name=BOT_CONFIG.get("aws").get("region_name")
)
mock_table_name = "user"
table_name = os.getenv("DYNAMODB_USER_TABLE_NAME")
mock_dynamodb.create_table(
TableName=mock_table_name,
TableName=table_name,
KeySchema=[
{"AttributeName": "id", "KeyType": "HASH"}, # Partition key
],
Expand All @@ -332,7 +334,7 @@ def test_setup_bot_if_bot_user_has_access_token_but_describe_instances_has_no_re
"WriteCapacityUnits": 5,
},
)
mock_table = mock_dynamodb.Table(mock_table_name)
mock_table = mock_dynamodb.Table(table_name)
mock_table.put_item(
Item={
"id": "875992093",
Expand Down
170 changes: 170 additions & 0 deletions tests/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,173 @@ async def test_check_valid_token_is_valid(mocker):
def test_redact_secret_string(secret_string, visible_chars, expected):
result = Utils.redact_secret_string(secret_string, visible_chars)
assert result == expected


@pytest.mark.parametrize(
"currency_string, expected",
[
("AED", "د.إ"),
("AFN", "؋"),
("ALL", "L"),
("AMD", "֏"),
("ANG", "ƒ"),
("AOA", "Kz"),
("ARS", "$"),
("AUD", "$"),
("AWG", "ƒ"),
("AZN", "₼"),
("BAM", "KM"),
("BBD", "$"),
("BDT", "৳"),
("BGN", "лв"),
("BHD", ".د.ب"),
("BIF", "FBu"),
("BMD", "$"),
("BND", "$"),
("BOB", "Bs."),
("BRL", "R$"),
("BSD", "$"),
("BTN", "Nu."),
("BWP", "P"),
("BYN", "Br"),
("BZD", "BZ$"),
("CAD", "$"),
("CDF", "FC"),
("CHF", "CHF"),
("CLP", "$"),
("CNY", "¥"),
("COP", "$"),
("CRC", "₡"),
("CUP", "$"),
("CVE", "$"),
("CZK", "Kč"),
("DJF", "Fdj"),
("DKK", "kr"),
("DOP", "$"),
("DZD", "د.ج"),
("EGP", "E£"),
("ERN", "Nfk"),
("ETB", "Br"),
("EUR", "€"),
("FJD", "$"),
("FKP", "£"),
("GBP", "£"),
("GEL", "₾"),
("GGP", "£"),
("GHS", "₵"),
("GIP", "£"),
("GMD", "D"),
("GNF", "FG"),
("GTQ", "Q"),
("GYD", "$"),
("HKD", "$"),
("HNL", "L"),
("HRK", "kn"),
("HTG", "G"),
("HUF", "Ft"),
("IDR", "Rp"),
("ILS", "₪"),
("IMP", "£"),
("INR", "₹"),
("IQD", "ع.د"),
("IRR", "﷼"),
("ISK", "kr"),
("JEP", "£"),
("JMD", "$"),
("JOD", "د.ا"),
("JPY", "¥"),
("KES", "KSh"),
("KGS", "сом"),
("KHR", "៛"),
("KID", "$"),
("KMF", "CF"),
("KRW", "₩"),
("KWD", "د.ك"),
("KYD", "$"),
("KZT", "₸"),
("LAK", "₭"),
("LBP", "ل.ل"),
("LKR", "₨"),
("LRD", "$"),
("LSL", "L"),
("LYD", "ل.د"),
("MAD", "د.م."),
("MDL", "L"),
("MGA", "Ar"),
("MKD", "ден"),
("MMK", "K"),
("MNT", "₮"),
("MOP", "P"),
("MRU", "UM"),
("MUR", "₨"),
("MVR", "Rf"),
("MWK", "MK"),
("MXN", "$"),
("MYR", "RM"),
("MZN", "MT"),
("NAD", "$"),
("NGN", "₦"),
("NIO", "C$"),
("NOK", "kr"),
("NPR", "₨"),
("NZD", "$"),
("OMR", "ر.ع."),
("PAB", "B/."),
("PEN", "S/."),
("PGK", "K"),
("PHP", "₱"),
("PKR", "₨"),
("PLN", "zł"),
("PYG", "₲"),
("QAR", "ر.ق"),
("RON", "lei"),
("RSD", "дин."),
("RUB", "₽"),
("RWF", "RF"),
("SAR", "ر.س"),
("SBD", "$"),
("SCR", "₨"),
("SDG", "ج.س."),
("SEK", "kr"),
("SGD", "$"),
("SHP", "£"),
("SLL", "Le"),
("SOS", "Sh"),
("SRD", "$"),
("SSP", "£"),
("STN", "Db"),
("SYP", "£"),
("SZL", "L"),
("THB", "฿"),
("TJS", "SM"),
("TMT", "T"),
("TND", "د.ت"),
("TOP", "T$"),
("TRY", "₺"),
("TTD", "$"),
("TVD", "$"),
("TWD", "NT$"),
("TZS", "TSh"),
("UAH", "₴"),
("UGX", "USh"),
("USD", "$"),
("UYU", "$"),
("UZS", "лв"),
("VES", "Bs."),
("VND", "₫"),
("VUV", "VT"),
("WST", "WS$"),
("XAF", "FCFA"),
("XCD", "$"),
("XDR", "SDR"),
("XOF", "CFA"),
("XPF", "CFP"),
("YER", "﷼"),
("ZAR", "R"),
("ZMW", "ZK"),
("ZWL", "$"),
],
)
def test_get_currency_symbols(currency_string, expected):
currency_symbols = Utils.get_currency_symbols()
assert currency_symbols.get(currency_string) == expected
22 changes: 21 additions & 1 deletion twitchrce/cogs/ascii_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, bot: custom_bot.CustomBot):
async def event_message(self, message: twitchio.Message): # pragma: no cover
if message.echo:
return
logger.info('[RCECog]: ', message.author.name, message.content)
logger.debug(f"[RCECog]: {message.author.name}, {message.content}")

@commands.command()
async def kill_everyone(self, ctx: commands.Context):
Expand Down Expand Up @@ -268,3 +268,23 @@ async def timehascome(self, ctx: commands.Context):
f"⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣦⣤⣾⣿⣿⣿⣿⣆⠁ "
f" ⠀⠀⠀⠀🈵⠀YOUR TIME HAS COME 🈵⠀"
)

@commands.command()
async def daddy(self, ctx: commands.Context):
await ctx.send(
f"⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿"
f"⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿"
f"⣿⣿⣿⣿⠉⢿⣿⡟⡝⡝⣿⡟⡏⡟⣿⣿⡏⠹⣿⡿⠉⣿⡏⠹⣿⠋⣹⣿⣿⣿"
f"⣿⣿⣿⣿⢈⢺⣿⢸⠸⡸⡸⡱⡱⡱⡪⣿⡇⠁⣿⡇⡁⢿⣷⠁⡏⣂⣿⣿⣿⣿"
f"⣿⣿⣿⣿⠠⣹⣿⡕⡕⡕⡝⡜⡎⡞⣼⣿⠅⡂⢼⠂⡀⣻⣿⡆⠂⣼⣿⣿⣿⣿"
f"⣿⣿⣿⣿⠂⣺⣿⣿⣎⢎⢞⡜⣎⣮⣿⣿⠡⢰⢐⢰⠐⢸⣿⡇⠅⣿⣿⣿⣿⣿"
f"⣿⣿⣿⣿⢐⢸⣿⣿⣿⣮⣣⢣⣷⣿⣿⣿⠡⢸⠔⢸⠄⣹⣿⡇⠅⣿⣿⣿⣿⣿"
f"⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣷⣿⣿⣿⣿⣿⣷⣾⣿⣿⣷⣾⣿⣿⣾⣿⣿⣿⣿⣿"
f"⣿⣿⣿⡏⡐⡈⠽⣿⣿⠉⠘⣿⣿⡁⣂⢉⢻⣿⠁⡂⡩⢿⡍⠹⣿⠉⣽⣿⣿⣿"
f"⣿⣿⣿⡧⢸⣷⠐⣿⡟⡀⠁⢻⣿⡐⢼⡖⡘⣿⠐⣿⠔⣸⣷⠁⡏⢄⣿⣿⣿⣿"
f"⣿⣿⣿⡇⢺⣿⠠⣿⡇⢠⡇⢸⣿⢂⢽⡇⢌⣿⠈⣿⡇⢸⣿⡆⠂⣾⣿⣿⣿⣿"
f"⣿⣿⣿⡏⢸⣟⠄⣿⠂⢌⠡⠨⣿⠂⣺⡇⢸⣿⠈⣿⡂⢹⣿⡇⡑⣿⣿⣿⣿⣿"
f"⣿⣿⣿⡇⠊⠅⣨⣿⠈⣾⣿⠐⣿⠂⠊⠅⣼⣿⠄⠍⢄⣽⣿⡇⢌⣿⣿⣿⣿⣿"
f"⣿⣿⣿⣿⣷⣿⣿⣿⣾⣿⣿⣿⣿⣿⣾⣿⣿⣿⣾⣾⣿⣿⣿⣿⣶⣿⣿⣿⣿⣿"
f"⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿"
)
4 changes: 2 additions & 2 deletions twitchrce/cogs/rce.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from twitchrce.custom_bot import CustomBot

logging.basicConfig(
level=logging.DEBUG,
level=logging.INFO,
format="%(asctime)s | %(levelname)-8s | %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
Expand All @@ -39,7 +39,7 @@ async def event_message(self, message: twitchio.Message):
async def exec(self, ctx: commands.Context):
# get channel broadcaster
broadcaster: User = (
await self.bot.fetch_users(ids=[], logins=[ctx.channel.name])
await self.bot.fetch_users(ids=[], names=[ctx.channel.name])
)[0]

if ctx.message.content == "!exec --help":
Expand Down
Loading

0 comments on commit 5309a46

Please sign in to comment.