Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ThorntonMatthewD committed Nov 20, 2023
1 parent 2875fac commit 31a3ce7
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 87 deletions.
8 changes: 4 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
from fastapi.testclient import TestClient

import bot
import config
import database
from server import API


@pytest.fixture
def test_client():
"""Returns a Starlette test API instance"""
return TestClient(API)
return TestClient(config.API)


@pytest.fixture
Expand Down Expand Up @@ -95,8 +95,8 @@ def single_event_data():


@pytest.fixture
def mock_slack_bolt_async_app(monkeypatch):
def mock_slack_bolt_async_app(request, monkeypatch):
"""
Monkeypatch slack_bolt.async_app's AsyncApp with our stub
"""
monkeypatch.setattr(bot, "APP", mocks.AsyncApp())
monkeypatch.setattr(f"{request.param}.SLACK_APP", mocks.AsyncApp())
11 changes: 11 additions & 0 deletions tests/mocks/async_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,21 @@ async def chat_postMessage(
self, channel, blocks, text, unfurl_links, unfurl_media
): # pylint: disable=invalid-name
"""Simulates posting a new Slack message"""
del channel, blocks, text, unfurl_links, unfurl_media

return {"ts": "1503435956.000247"}

async def chat_update(self, ts, channel, blocks, text):
"""Simulates updating an existing Slack message"""
del ts, channel, blocks, text

async def users_info(self, user=""):
"""Simulates getting info on a user"""

return {
"ok": True,
"user": {"id": user, "name": "Tester", "is_admin": "admin" in user.lower()},
}


class AsyncApp:
Expand Down
26 changes: 26 additions & 0 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
Tests functions contained in src/auth.py
"""

import pytest

import auth


@pytest.mark.parametrize("mock_slack_bolt_async_app", ["auth"], indirect=True)
class TestAuth:
"""Groups tests for auth.py into a single scope"""

@pytest.mark.asyncio
async def test_is_admin_when_user_is_not_admin(self, mock_slack_bolt_async_app):
"""Tests when a user is NOT a workspace admin"""
result = await auth.is_admin("regular_user")

assert result is False

@pytest.mark.asyncio
async def test_is_admin_when_user_is_admin(self, mock_slack_bolt_async_app):
"""Tests when a user is a workspace admin"""
result = await auth.is_admin("admin_user")

assert result is True
171 changes: 88 additions & 83 deletions tests/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,86 +14,91 @@
week = datetime.datetime.strptime("10/22/2023", "%m/%d/%Y").replace(tzinfo=pytz.utc)


@pytest.mark.asyncio
async def test_post_or_update_messages_expansion_with_next_week_already_posted(
caplog, db_cleanup, mock_slack_bolt_async_app
):
"""
post_or_update_messages fails if it determines that a
weeks posts will require a new message(s), but the next week
has already had posts sent for it.
"""

slack_id = "fake_slack_id"

# Create a "channel" and a message
await database.add_channel(slack_id)

# This message is for the next week
await database.create_message(
"2023-10-29 00:00:00+00:00",
"test",
"1698119853.135399",
slack_id,
1,
)
# Message for this week
await database.create_message(
"2023-10-22 00:00:00+00:00",
"test",
"1698119853.135399",
slack_id,
1,
)

# Try adding more message than what currently exists
await post_or_update_messages(
week, [{"text": "message 1", "blocks": []}, {"text": "message 2", "blocks": []}]
)

assert (
"Cannot update messages for 10/22/2023 for channel fake_slack_id. "
"New events have caused the number of messages needed to increase, "
"but the next week's post has already been sent. Cannot resize. "
"Existing message count: 1 --- New message count: 2." in caplog.text
)


@pytest.mark.asyncio
async def test_post_or_update_messages_expansion_without_new_weeks_posts(
caplog, db_cleanup, mock_slack_bolt_async_app
):
"""
post_or_update_messages will allow for additional messages to be posted
as long as the next week hasn't had any posts sent for it yet.
"""

slack_id = "fake_slack_id_two"

# Create a "channel" and a message
await database.add_channel(slack_id)
# Message for this week
await database.create_message(
"2023-10-22 00:00:00+00:00",
"test",
"1698119853.135399",
slack_id,
1,
)

# Try adding more message than what currently exists
await post_or_update_messages(
week, [{"text": "message 1", "blocks": []}, {"text": "message 2", "blocks": []}]
)

assert (
"Cannot update messages for 10/22/2023 for channel fake_slack_id_two. "
"New events have caused the number of messages needed to increase, "
"but the next week's post has already been sent. Cannot resize. "
"Existing message count: 1 --- New message count: 2." not in caplog.text
)

messages = await database.get_messages(week)

# Make sure the second message was recorded as if it were posted
assert set(["message 1", "message 2"]) == {msg["message"] for msg in messages}
@pytest.mark.parametrize("mock_slack_bolt_async_app", ["bot"], indirect=True)
class TestBot:
"""Groups tests for bot.py into a single scope"""

@pytest.mark.asyncio
async def test_post_or_update_messages_expansion_with_next_week_already_posted(
self, caplog, db_cleanup, mock_slack_bolt_async_app
):
"""
post_or_update_messages fails if it determines that a
weeks posts will require a new message(s), but the next week
has already had posts sent for it.
"""

slack_id = "fake_slack_id"

# Create a "channel" and a message
await database.add_channel(slack_id)

# This message is for the next week
await database.create_message(
"2023-10-29 00:00:00+00:00",
"test",
"1698119853.135399",
slack_id,
1,
)
# Message for this week
await database.create_message(
"2023-10-22 00:00:00+00:00",
"test",
"1698119853.135399",
slack_id,
1,
)

# Try adding more message than what currently exists
await post_or_update_messages(
week,
[{"text": "message 1", "blocks": []}, {"text": "message 2", "blocks": []}],
)

assert (
"Cannot update messages for 10/22/2023 for channel fake_slack_id. "
"New events have caused the number of messages needed to increase, "
"but the next week's post has already been sent. Cannot resize. "
"Existing message count: 1 --- New message count: 2." in caplog.text
)

@pytest.mark.asyncio
async def test_post_or_update_messages_expansion_without_new_weeks_posts(
self, caplog, db_cleanup, mock_slack_bolt_async_app
):
"""
post_or_update_messages will allow for additional messages to be posted
as long as the next week hasn't had any posts sent for it yet.
"""

slack_id = "fake_slack_id_two"

# Create a "channel" and a message
await database.add_channel(slack_id)
# Message for this week
await database.create_message(
"2023-10-22 00:00:00+00:00",
"test",
"1698119853.135399",
slack_id,
1,
)

# Try adding more message than what currently exists
await post_or_update_messages(
week,
[{"text": "message 1", "blocks": []}, {"text": "message 2", "blocks": []}],
)

assert (
"Cannot update messages for 10/22/2023 for channel fake_slack_id_two. "
"New events have caused the number of messages needed to increase, "
"but the next week's post has already been sent. Cannot resize. "
"Existing message count: 1 --- New message count: 2." not in caplog.text
)

messages = await database.get_messages(week)

# Make sure the second message was recorded as if it were posted
assert set(["message 1", "message 2"]) == {msg["message"] for msg in messages}

0 comments on commit 31a3ce7

Please sign in to comment.