Skip to content

Commit

Permalink
Asserts error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
amandasavluchinske committed Jun 20, 2024
1 parent bfd3ab0 commit 370844a
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions tests/test_use_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,21 @@ def test_get_single_assistant_info_raises_exception_when_assistant_not_defined()
assistant_id = "not_defined"
user = User()

with pytest.raises(AIAssistantNotDefinedError):
with pytest.raises(AIAssistantNotDefinedError) as exc_info:
use_cases.get_single_assistant_info(assistant_id, user)

assert str(exc_info.value) == "Assistant with id=not_defined not found"


def test_get_single_assistant_info_raises_exception_when_user_not_allowed(use_fake_permissions):
assistant_id = "temperature_assistant"
user = User()

with pytest.raises(AIUserNotAllowedError):
with pytest.raises(AIUserNotAllowedError) as exc_info:
use_cases.get_single_assistant_info(assistant_id, user)

assert str(exc_info.value) == "User is not allowed to use this assistant"


def test_get_assistants_info_returns_info():
user = User()
Expand Down Expand Up @@ -155,14 +159,16 @@ def test_create_message_raises_exception_when_user_not_allowed():
user = baker.make(User)
thread = baker.make(Thread)

with pytest.raises(AIUserNotAllowedError):
with pytest.raises(AIUserNotAllowedError) as exc_info:
use_cases.create_message(
"temperature_assistant",
thread,
user,
"Hello, will I have to use my umbrella in Lisbon tomorrow?",
)

assert str(exc_info.value) == "User is not allowed to create messages in this thread"


# Thread tests

Expand All @@ -180,9 +186,11 @@ def test_create_thread():
def test_create_thread_raises_exception_when_user_not_allowed(use_fake_permissions):
user = baker.make(User)

with pytest.raises(AIUserNotAllowedError):
with pytest.raises(AIUserNotAllowedError) as exc_info:
use_cases.create_thread("My thread", user)

assert str(exc_info.value) == "User is not allowed to create threads"


@pytest.mark.django_db(transaction=True)
def test_get_single_thread():
Expand All @@ -198,9 +206,11 @@ def test_get_single_thread_raises_exception_when_user_not_allowed():
user = baker.make(User)
thread = baker.make(Thread)

with pytest.raises(AIUserNotAllowedError):
with pytest.raises(AIUserNotAllowedError) as exc_info:
use_cases.get_single_thread(thread.id, user)

assert str(exc_info.value) == "User is not allowed to view this thread"


@pytest.mark.django_db(transaction=True)
def test_get_threads():
Expand Down Expand Up @@ -234,9 +244,11 @@ def test_update_thread_raises_exception_when_user_not_allowed():
user = baker.make(User)
thread = baker.make(Thread)

with pytest.raises(AIUserNotAllowedError):
with pytest.raises(AIUserNotAllowedError) as exc_info:
use_cases.update_thread(thread, "My updated thread", user)

assert str(exc_info.value) == "User is not allowed to update this thread"


@pytest.mark.django_db(transaction=True)
def test_delete_thread():
Expand All @@ -252,9 +264,11 @@ def test_delete_thread_raises_exception_when_user_not_allowed():
user = baker.make(User)
thread = baker.make(Thread)

with pytest.raises(AIUserNotAllowedError):
with pytest.raises(AIUserNotAllowedError) as exc_info:
use_cases.delete_thread(thread, user)

assert str(exc_info.value) == "User is not allowed to delete this thread"


# Thread message tests

Expand All @@ -279,9 +293,11 @@ def test_get_thread_messages_raises_exception_when_user_not_allowed():
Message, message={"type": "human", "data": {"content": "hi"}}, thread=thread, _quantity=3
)

with pytest.raises(AIUserNotAllowedError):
with pytest.raises(AIUserNotAllowedError) as exc_info:
use_cases.get_thread_messages(thread.id, user)

assert str(exc_info.value) == "User is not allowed to view messages in this thread"


@pytest.mark.django_db(transaction=True)
def test_create_thread_message_as_user():
Expand All @@ -297,9 +313,11 @@ def test_create_thread_message_as_user_raises_exception_when_user_not_allowed():
user = baker.make(User)
thread = baker.make(Thread)

with pytest.raises(AIUserNotAllowedError):
with pytest.raises(AIUserNotAllowedError) as exc_info:
use_cases.create_thread_message_as_user(thread.id, "Hello, how are you?", user)

assert str(exc_info.value) == "User is not allowed to create messages in this thread"


@pytest.mark.django_db(transaction=True)
def test_delete_message():
Expand All @@ -316,5 +334,7 @@ def test_delete_message_raises_exception_when_user_not_allowed():
user = baker.make(User)
message = baker.make(Message)

with pytest.raises(AIUserNotAllowedError):
with pytest.raises(AIUserNotAllowedError) as exc_info:
use_cases.delete_message(message, user)

assert str(exc_info.value) == "User is not allowed to delete this message"

0 comments on commit 370844a

Please sign in to comment.