Skip to content

Commit

Permalink
Updated tests to use new endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
hagen-danswer committed Jan 8, 2025
1 parent 66930a6 commit 06ec369
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 159 deletions.
15 changes: 5 additions & 10 deletions backend/onyx/db/connector_credential_pair.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ def add_credential_to_connector(
auto_sync_options: dict | None = None,
initial_status: ConnectorCredentialPairStatus = ConnectorCredentialPairStatus.ACTIVE,
last_successful_index_time: datetime | None = None,
) -> StatusResponse:
) -> int:
connector = fetch_connector_by_id(connector_id, db_session)
credential = fetch_credential_by_id(
credential_id,
Expand Down Expand Up @@ -473,10 +473,9 @@ def add_credential_to_connector(
.one_or_none()
)
if existing_association is not None:
return StatusResponse(
success=False,
message=f"Connector {connector_id} already has Credential {credential_id}",
data=connector_id,
raise HTTPException(
status_code=400,
detail=f"Connector {connector_id} already has Credential {credential_id}",
)

association = ConnectorCredentialPair(
Expand All @@ -501,11 +500,7 @@ def add_credential_to_connector(

db_session.commit()

return StatusResponse(
success=True,
message=f"Creating new association between Connector {connector_id} and Credential {credential_id}",
data=association.id,
)
return association.id


def remove_credential_from_connector(
Expand Down
3 changes: 1 addition & 2 deletions backend/onyx/seeding/load_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def seed_initial_documents(

last_index_time = datetime.datetime.now(datetime.timezone.utc)

result = add_credential_to_connector(
cc_pair_id = add_credential_to_connector(
db_session=db_session,
user=None,
connector_id=connector_id,
Expand All @@ -190,7 +190,6 @@ def seed_initial_documents(
initial_status=ConnectorCredentialPairStatus.PAUSED,
last_successful_index_time=last_index_time,
)
cc_pair_id = cast(int, result.data)
processed_docs = fetch_versioned_implementation(
"onyx.seeding.load_docs",
"load_processed_docs",
Expand Down
8 changes: 6 additions & 2 deletions backend/onyx/server/documents/cc_pair.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ def associate_credential_to_connector(
)

try:
response = add_credential_to_connector(
cc_pair_id = add_credential_to_connector(
db_session=db_session,
user=user,
connector_id=connector_id,
Expand All @@ -533,7 +533,11 @@ def associate_credential_to_connector(
groups=metadata.groups,
)

return response
return StatusResponse(
success=True,
message="Credential associated successfully",
data=cc_pair_id,
)
except IntegrityError as e:
logger.error(f"IntegrityError: {e}")
raise HTTPException(status_code=400, detail="Name must be unique")
Expand Down
24 changes: 19 additions & 5 deletions backend/onyx/server/documents/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ def _validate_connector_allowed(source: DocumentSource) -> None:
)


@router.post("/admin/connector-and-link-credential")
@router.post("/admin/create-and-link-connector")
def create_connector_and_associate_credential(
connector_data: ConnectorCreateAndAssociateRequest,
user: User = Depends(current_curator_or_admin_user),
Expand Down Expand Up @@ -726,7 +726,7 @@ def create_connector_and_associate_credential(
# If a credential_id is provided, associate it with the connector
if connector_data.credential_id is not None:
try:
connector_credential_pair_response = add_credential_to_connector(
cc_pair_id = add_credential_to_connector(
db_session=db_session,
user=user,
connector_id=connector_id,
Expand All @@ -753,7 +753,14 @@ def create_connector_and_associate_credential(
db_session=db_session,
)

return connector_credential_pair_response
return StatusResponse(
success=True,
message="Connector created successfully",
data={
"cc_pair_id": cc_pair_id,
"connector_id": connector_id,
},
)
except ValueError as e:
logger.error(f"Error creating connector: {e}")
raise HTTPException(status_code=400, detail=str(e))
Expand Down Expand Up @@ -832,7 +839,7 @@ def create_connector_with_mock_credential(
db_session=db_session,
)

response = add_credential_to_connector(
cc_pair_id = add_credential_to_connector(
db_session=db_session,
user=user,
connector_id=cast(int, connector_response.id), # will aways be an int
Expand All @@ -850,7 +857,14 @@ def create_connector_with_mock_credential(
db_session=db_session,
)

return response
return StatusResponse(
success=True,
message="Connector created successfully",
data={
"cc_pair_id": cc_pair_id,
"connector_id": connector_response.id,
},
)

except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
Expand Down
5 changes: 2 additions & 3 deletions backend/onyx/server/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from typing import Generic
from typing import Optional
from typing import TypeVar
from uuid import UUID

Expand All @@ -14,8 +13,8 @@

class StatusResponse(BaseModel, Generic[DataT]):
success: bool
message: Optional[str] = None
data: Optional[DataT] = None
message: str | None = None
data: DataT | None = None


class ApiKey(BaseModel):
Expand Down
11 changes: 5 additions & 6 deletions backend/scripts/add_connector_creation_script.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from typing import Any
from typing import Dict

import requests

Expand All @@ -12,10 +11,10 @@ def create_connector(
name: str,
source: str,
input_type: str,
connector_specific_config: Dict[str, Any],
connector_specific_config: dict[str, Any],
is_public: bool = True,
groups: list[int] | None = None,
) -> Dict[str, Any]:
) -> dict[str, Any]:
connector_update_request = {
"name": name,
"source": source,
Expand All @@ -37,10 +36,10 @@ def create_connector(
def create_credential(
name: str,
source: str,
credential_json: Dict[str, Any],
credential_json: dict[str, Any],
is_public: bool = True,
groups: list[int] | None = None,
) -> Dict[str, Any]:
) -> dict[str, Any]:
credential_request = {
"name": name,
"source": source,
Expand All @@ -64,7 +63,7 @@ def create_cc_pair(
name: str,
access_type: str = "public",
groups: list[int] | None = None,
) -> Dict[str, Any]:
) -> dict[str, Any]:
cc_pair_request = {
"name": name,
"access_type": access_type,
Expand Down
31 changes: 31 additions & 0 deletions backend/tests/integration/common_utils/managers/cc_pair.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,37 @@ def create_from_scratch(
connector_specific_config: dict[str, Any] | None = None,
credential_json: dict[str, Any] | None = None,
user_performing_action: DATestUser | None = None,
) -> DATestCCPair:
credential = CredentialManager.create(
credential_json=credential_json,
name=name,
source=source,
curator_public=(access_type == AccessType.PUBLIC),
groups=groups,
user_performing_action=user_performing_action,
)
cc_pair = ConnectorManager.create_and_link_to_credential(
credential_id=credential.id,
name=name,
source=source,
input_type=input_type,
connector_specific_config=connector_specific_config,
access_type=access_type,
groups=groups,
user_performing_action=user_performing_action,
)
return cc_pair

@staticmethod
def old_create_from_scratch(
name: str | None = None,
access_type: AccessType = AccessType.PUBLIC,
groups: list[int] | None = None,
source: DocumentSource = DocumentSource.FILE,
input_type: InputType = InputType.LOAD_STATE,
connector_specific_config: dict[str, Any] | None = None,
credential_json: dict[str, Any] | None = None,
user_performing_action: DATestUser | None = None,
) -> DATestCCPair:
connector = ConnectorManager.create(
name=name,
Expand Down
41 changes: 41 additions & 0 deletions backend/tests/integration/common_utils/managers/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,56 @@

from onyx.connectors.models import InputType
from onyx.db.enums import AccessType
from onyx.server.documents.models import ConnectorCreateAndAssociateRequest
from onyx.server.documents.models import ConnectorUpdateRequest
from onyx.server.documents.models import DocumentSource
from tests.integration.common_utils.constants import API_SERVER_URL
from tests.integration.common_utils.constants import GENERAL_HEADERS
from tests.integration.common_utils.test_models import DATestCCPair
from tests.integration.common_utils.test_models import DATestConnector
from tests.integration.common_utils.test_models import DATestUser


class ConnectorManager:
@staticmethod
def create_and_link_to_credential(
credential_id: int,
name: str | None = None,
source: DocumentSource = DocumentSource.FILE,
input_type: InputType = InputType.LOAD_STATE,
connector_specific_config: dict[str, Any] | None = None,
access_type: AccessType = AccessType.PUBLIC,
groups: list[int] | None = None,
user_performing_action: DATestUser | None = None,
) -> DATestCCPair:
name = f"{name}-connector" if name else f"test-connector-{uuid4()}"
connector_create_and_associate_request = ConnectorCreateAndAssociateRequest(
name=name,
source=source,
input_type=input_type,
connector_specific_config=connector_specific_config or {},
access_type=access_type,
groups=groups or [],
credential_id=credential_id,
)
response = requests.post(
url=f"{API_SERVER_URL}/manage/admin/create-and-link-connector",
json=connector_create_and_associate_request.model_dump(),
headers=user_performing_action.headers
if user_performing_action
else GENERAL_HEADERS,
)
response.raise_for_status()
response_data = response.json()
return DATestCCPair(
id=response_data["data"]["cc_pair_id"],
name=name,
connector_id=response_data["data"]["connector_id"],
credential_id=credential_id,
access_type=access_type,
groups=groups or [],
)

@staticmethod
def create(
name: str | None = None,
Expand Down
Loading

0 comments on commit 06ec369

Please sign in to comment.