Skip to content

Commit

Permalink
Added api tests to improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunandhita B authored and Sunandhita B committed Dec 9, 2024
1 parent 7b22cb5 commit 07f2585
Show file tree
Hide file tree
Showing 7 changed files with 1,314 additions and 6 deletions.
195 changes: 191 additions & 4 deletions api/tests/test_bot.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from unittest.mock import patch
import pytest
from lib.data_models import Flow, BotConfig, BotIntent, FlowIntent, Bot
from lib.models import JBBot
from app.handlers.v2.bot import list_bots, install
from app.jb_schema import JBBotCode
from lib.data_models import Flow, BotConfig, FlowIntent, Bot
from lib.models import JBBot, JBChannel
from app.handlers.v2.bot import list_bots, install, add_credentials, add_channel, delete
from app.jb_schema import JBBotCode, JBChannelContent

@pytest.mark.asyncio
async def test_list_bots():
Expand Down Expand Up @@ -55,3 +55,190 @@ async def test_install():
assert result.bot_config.bot.version == mock_bot1.version

mock_create_bot.assert_awaited_once_with(mock_jbbot_code.model_dump())

@pytest.mark.asyncio
async def test_add_credentials_success():
bot_id = "test_bot_id"
credentials = {"key":"test_key"}

mock_bot = JBBot(id="test_bot_id", name="mock_bot", status="active")

with patch("app.handlers.v2.bot.get_bot_by_id", return_value = mock_bot) as mock_get_bot_by_id, \
patch("app.handlers.v2.bot.update_bot", return_value = bot_id) as mock_update_bot:

result = await add_credentials(bot_id,credentials)

assert len(result) == 1
assert 'status' in result
assert result.get('status') == 'success'

mock_get_bot_by_id.assert_awaited_once_with(bot_id)
mock_update_bot.assert_awaited_once()

@pytest.mark.asyncio
async def test_add_credentials_failure():
bot_id = "test_bot_id"
credentials = {"key":"test_key"}

with patch("app.handlers.v2.bot.get_bot_by_id", return_value = None) as mock_get_bot_by_id, \
patch("app.handlers.v2.bot.update_bot", return_value = bot_id) as mock_update_bot:

result = await add_credentials(bot_id,credentials)

assert len(result) == 2
assert 'status' in result
assert result.get('status') == 'error'
assert 'message' in result
assert result.get('message') == 'Bot not found'

mock_get_bot_by_id.assert_awaited_once_with(bot_id)
mock_update_bot.assert_not_awaited()

@pytest.mark.asyncio
async def test_add_channel_when_bot_with_bot_id_does_not_exist():

bot_id = "test_bot_id"

channel_content = JBChannelContent(
name = "test_channel_content",
type = "test_type",
url = "test_url",
app_id = "12345678",
key = "test_key",
status = "active"
)

with patch("app.handlers.v2.bot.get_bot_by_id", return_value = None) as mock_get_bot_by_id:
result = await add_channel(bot_id,channel_content)

assert len(result) == 2
assert 'status' in result
assert result.get('status') == 'error'
assert 'message' in result
assert result.get('message') == "Bot not found"

mock_get_bot_by_id.assert_awaited_once_with(bot_id)

@pytest.mark.asyncio
async def test_add_channel_when_channel_already_in_use_by_bot():

bot_id = "test_bot_id"

channel_content = JBChannelContent(
name = "test_channel_content",
type = "test_type",
url = "test_url",
app_id = "12345678",
key = "test_key",
status = "active"
)

mock_bot = JBBot(id="test_bot_id", name="Bot1", status="active")

mock_existing_channel = JBChannel(
id = "test_channel_id",
bot_id = "test_bot_id",
status = "active",
name = "test_channel",
type = "test_type",
key = "test_key",
app_id = "12345678",
url = "test_url"
)

with patch("app.handlers.v2.bot.get_bot_by_id", return_value = mock_bot) as mock_get_bot_by_id, \
patch("app.handlers.v2.bot.get_active_channel_by_identifier", return_value = mock_existing_channel) as mock_get_active_channel_by_identifier:

result = await add_channel(bot_id,channel_content)

assert len(result) == 2
assert 'status' in result
assert result.get('status') == 'error'
assert 'message' in result

mock_get_bot_by_id.assert_awaited_once_with(bot_id)
mock_get_active_channel_by_identifier.assert_awaited_once_with(identifier = channel_content.app_id,
channel_type = channel_content.type)

@pytest.mark.asyncio
async def test_add_channel_when_channel_creation_is_success():

bot_id = "test_bot_id"

channel_content = JBChannelContent(
name = "test_channel_content",
type = "test_type",
url = "test_url",
app_id = "12345678",
key = "test_key",
status = "active"
)

mock_bot = JBBot(id="test_bot_id", name="Bot1", status="active")

mock_channel = JBChannel(
id = "test_channel_id",
bot_id = "test_bot_id",
status = "active",
name = "test_channel",
type = "test_type",
key = "test_key",
app_id = "12345678",
url = "test_url"
)

with patch("app.handlers.v2.bot.get_bot_by_id", return_value = mock_bot) as mock_get_bot_by_id, \
patch("app.handlers.v2.bot.get_active_channel_by_identifier", return_value = None) as mock_get_active_channel_by_identifier, \
patch("app.handlers.v2.bot.create_channel",return_value = mock_channel) as mock_create_channel:

result = await add_channel(bot_id,channel_content)

assert len(result) == 1
assert 'status' in result
assert result.get('status') == 'success'

mock_get_bot_by_id.assert_awaited_once_with(bot_id)
mock_get_active_channel_by_identifier.assert_awaited_once_with(identifier = channel_content.app_id,
channel_type = channel_content.type)
mock_create_channel.assert_awaited_once_with(bot_id, channel_content.model_dump())

@pytest.mark.asyncio
async def test_delete_success():

bot_id = "test_bot_id"

mock_bot = JBBot(id="test_bot_id", name="Bot1", status="active")

bot_data = {"status": "deleted"}
channel_data = {"status": "deleted"}

with patch("app.handlers.v2.bot.get_bot_by_id", return_value = mock_bot) as mock_get_bot_by_id, \
patch("app.handlers.v2.bot.update_bot", return_value = bot_id) as mock_update_bot,\
patch("app.handlers.v2.bot.update_channel_by_bot_id", return_value = bot_id) as mock_update_channel_by_bot_id:

result = await delete(bot_id)

assert len(result) == 1
assert 'status' in result
assert result.get('status') == 'success'

mock_get_bot_by_id.assert_awaited_once_with(bot_id)
mock_update_bot.assert_awaited_once_with(bot_id, bot_data)
mock_update_channel_by_bot_id.assert_awaited_once_with(bot_id, channel_data)

@pytest.mark.asyncio
async def test_delete_failure():

bot_id = "test_bot_id"

with patch("app.handlers.v2.bot.get_bot_by_id", return_value = None) as mock_get_bot_by_id:

result = await delete(bot_id)

assert len(result) == 2
assert 'status' in result
assert result.get('status') == 'error'
assert 'message' in result
assert result.get('message') == 'Bot not found'

mock_get_bot_by_id.assert_awaited_once_with(bot_id)
Loading

0 comments on commit 07f2585

Please sign in to comment.