Skip to content

Commit

Permalink
added crud tests for tool_auth
Browse files Browse the repository at this point in the history
  • Loading branch information
chantelle-cohere committed Aug 15, 2024
1 parent 8b42191 commit a33aef2
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/backend/tests/unit/crud/test_tool_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import pytest
from datetime import datetime

from backend.config.tools import ToolName
from backend.crud import tool_auth as tool_auth_crud
from backend.database_models.tool_auth import ToolAuth
from backend.tests.unit.factories import get_factory

def test_create_tool_auth(session, user):

tool_auth_data = ToolAuth(
user_id = user.id,
tool_id = ToolName.Google_Drive,
token_type = "Bearer",
encrypted_access_token = bytes(b'foobar'),
encrypted_refresh_token = bytes(b'foobar'),
expires_at = datetime.strptime("12/31/2124 00:00:00", '%m/%d/%Y %H:%M:%S'),
created_at = datetime.strptime("01/01/2000 00:00:00", '%m/%d/%Y %H:%M:%S'),
updated_at = datetime.strptime("01/01/2010 00:00:00", '%m/%d/%Y %H:%M:%S')
)

tool_auth = tool_auth_crud.create_tool_auth(
session, tool_auth_data
)

assert tool_auth.user_id == tool_auth_data.user_id
assert tool_auth.tool_id == tool_auth_data.tool_id
assert tool_auth.token_type == tool_auth_data.token_type
assert tool_auth.encrypted_access_token == tool_auth_data.encrypted_access_token
assert tool_auth.encrypted_refresh_token == tool_auth_data.encrypted_refresh_token
assert tool_auth.expires_at == tool_auth_data.expires_at
assert tool_auth.id == tool_auth_data.id
assert tool_auth.created_at == tool_auth_data.created_at
assert tool_auth.updated_at == tool_auth_data.updated_at

def test_delete_tool_auth_by_tool_id(session, user):
tool_auth = get_factory("ToolAuth", session).create(
user_id = user.id,
tool_id = ToolName.Google_Drive,
token_type = "Bearer",
encrypted_access_token = bytes(b'foobar'),
encrypted_refresh_token = bytes(b'foobar'),
expires_at = datetime.strptime("12/31/2124 00:00:00", '%m/%d/%Y %H:%M:%S'),
created_at = datetime.strptime("01/01/2000 00:00:00", '%m/%d/%Y %H:%M:%S'),
updated_at = datetime.strptime("01/01/2010 00:00:00", '%m/%d/%Y %H:%M:%S')
)

tool_auth_crud.delete_tool_auth(
session, user.id, tool_auth.tool_id
)

tool_auth = tool_auth_crud.get_tool_auth(
session, tool_auth.tool_id, user.id
)
assert tool_auth is None
2 changes: 2 additions & 0 deletions src/backend/tests/unit/factories/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
SnapshotLinkFactory,
)
from backend.tests.unit.factories.tool_call import ToolCallFactory
from backend.tests.unit.factories.tool_auth import ToolAuthFactory
from backend.tests.unit.factories.user import UserFactory

FACTORY_MAPPING = {
Expand All @@ -40,6 +41,7 @@
"Agent": AgentFactory,
"Organization": OrganizationFactory,
"ToolCall": ToolCallFactory,
"ToolAuth": ToolAuthFactory,
"Snapshot": SnapshotFactory,
"SnapshotLink": SnapshotLinkFactory,
"SnapshotAccess": SnapshotAccessFactory,
Expand Down
21 changes: 21 additions & 0 deletions src/backend/tests/unit/factories/tool_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import factory
from datetime import datetime

from backend.database_models.tool_auth import ToolAuth
from backend.config.tools import ToolName

from .base import BaseFactory


class ToolAuthFactory(BaseFactory):
class Meta:
model = ToolAuth

user_id = factory.Faker("uuid4")
tool_id = ToolName.Google_Drive
token_type = "Bearer"
encrypted_access_token = bytes(b'foobar')
encrypted_refresh_token = bytes(b'foobar')
expires_at = datetime.strptime("12/31/2124 00:00:00", '%m/%d/%Y %H:%M:%S')
created_at = datetime.strptime("01/01/2000 00:00:00", '%m/%d/%Y %H:%M:%S')
updated_at = datetime.strptime("01/01/2010 00:00:00", '%m/%d/%Y %H:%M:%S')

0 comments on commit a33aef2

Please sign in to comment.