|
| 1 | +import os |
| 2 | +from collections.abc import Generator |
| 3 | +from datetime import datetime |
| 4 | +from datetime import timezone |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from onyx.configs.constants import DocumentSource |
| 9 | +from onyx.connectors.models import InputType |
| 10 | +from onyx.connectors.sharepoint.connector import SharepointAuthMethod |
| 11 | +from onyx.db.enums import AccessType |
| 12 | +from tests.integration.common_utils.managers.cc_pair import CCPairManager |
| 13 | +from tests.integration.common_utils.managers.connector import ConnectorManager |
| 14 | +from tests.integration.common_utils.managers.credential import CredentialManager |
| 15 | +from tests.integration.common_utils.managers.llm_provider import LLMProviderManager |
| 16 | +from tests.integration.common_utils.managers.user import UserManager |
| 17 | +from tests.integration.common_utils.reset import reset_all |
| 18 | +from tests.integration.common_utils.test_models import DATestCCPair |
| 19 | +from tests.integration.common_utils.test_models import DATestConnector |
| 20 | +from tests.integration.common_utils.test_models import DATestCredential |
| 21 | +from tests.integration.common_utils.test_models import DATestUser |
| 22 | + |
| 23 | +SharepointTestEnvSetupTuple = tuple[ |
| 24 | + DATestUser, # admin_user |
| 25 | + DATestUser, # regular_user_1 |
| 26 | + DATestUser, # regular_user_2 |
| 27 | + DATestCredential, |
| 28 | + DATestConnector, |
| 29 | + DATestCCPair, |
| 30 | +] |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture(scope="module") |
| 34 | +def sharepoint_test_env_setup() -> Generator[SharepointTestEnvSetupTuple]: |
| 35 | + # Reset all data before running the test |
| 36 | + reset_all() |
| 37 | + # Required environment variables for SharePoint certificate authentication |
| 38 | + sp_client_id = os.environ["PERM_SYNC_SHAREPOINT_CLIENT_ID"] |
| 39 | + sp_private_key = os.environ[ |
| 40 | + "PERM_SYNC_SHAREPOINT_PRIVATE_KEY" |
| 41 | + ] # Base64 encoded PFX data |
| 42 | + sp_certificate_password = os.environ["PERM_SYNC_SHAREPOINT_CERTIFICATE_PASSWORD"] |
| 43 | + sp_directory_id = os.environ["PERM_SYNC_SHAREPOINT_DIRECTORY_ID"] |
| 44 | + sp_tenant_domain = os.environ["PERM_SYNC_SHAREPOINT_TENANT_DOMAIN"] |
| 45 | + sharepoint_sites = "https://danswerai.sharepoint.com/sites/Permisisonsync" |
| 46 | + admin_email = "admin@onyx.app" |
| 47 | + user1_email = "subash@onyx.app" |
| 48 | + user2_email = "raunak@onyx.app" |
| 49 | + |
| 50 | + # Certificate-based credentials |
| 51 | + credentials = { |
| 52 | + "authentication_method": SharepointAuthMethod.CERTIFICATE.value, |
| 53 | + "sp_client_id": sp_client_id, |
| 54 | + "sp_private_key": sp_private_key, |
| 55 | + "sp_certificate_password": sp_certificate_password, |
| 56 | + "sp_directory_id": sp_directory_id, |
| 57 | + "sp_tenant_domain": sp_tenant_domain, |
| 58 | + } |
| 59 | + |
| 60 | + # Create users |
| 61 | + admin_user: DATestUser = UserManager.create(email=admin_email) |
| 62 | + regular_user_1: DATestUser = UserManager.create(email=user1_email) |
| 63 | + regular_user_2: DATestUser = UserManager.create(email=user2_email) |
| 64 | + |
| 65 | + # Create LLM provider for search functionality |
| 66 | + LLMProviderManager.create(user_performing_action=admin_user) |
| 67 | + |
| 68 | + # Create credential |
| 69 | + credential: DATestCredential = CredentialManager.create( |
| 70 | + source=DocumentSource.SHAREPOINT, |
| 71 | + credential_json=credentials, |
| 72 | + user_performing_action=admin_user, |
| 73 | + ) |
| 74 | + |
| 75 | + # Create connector with SharePoint-specific configuration |
| 76 | + connector: DATestConnector = ConnectorManager.create( |
| 77 | + name="SharePoint Test", |
| 78 | + input_type=InputType.POLL, |
| 79 | + source=DocumentSource.SHAREPOINT, |
| 80 | + connector_specific_config={ |
| 81 | + "sites": sharepoint_sites.split(","), |
| 82 | + }, |
| 83 | + access_type=AccessType.SYNC, # Enable permission sync |
| 84 | + user_performing_action=admin_user, |
| 85 | + ) |
| 86 | + |
| 87 | + # Create CC pair with permission sync enabled |
| 88 | + cc_pair: DATestCCPair = CCPairManager.create( |
| 89 | + credential_id=credential.id, |
| 90 | + connector_id=connector.id, |
| 91 | + access_type=AccessType.SYNC, # Enable permission sync |
| 92 | + user_performing_action=admin_user, |
| 93 | + ) |
| 94 | + |
| 95 | + # Wait for both indexing and permission sync to complete |
| 96 | + before = datetime.now(tz=timezone.utc) |
| 97 | + CCPairManager.wait_for_indexing_completion( |
| 98 | + cc_pair=cc_pair, |
| 99 | + after=before, |
| 100 | + user_performing_action=admin_user, |
| 101 | + timeout=float("inf"), |
| 102 | + ) |
| 103 | + |
| 104 | + # Wait for permission sync completion specifically |
| 105 | + CCPairManager.wait_for_sync( |
| 106 | + cc_pair=cc_pair, |
| 107 | + after=before, |
| 108 | + user_performing_action=admin_user, |
| 109 | + timeout=float("inf"), |
| 110 | + ) |
| 111 | + |
| 112 | + yield admin_user, regular_user_1, regular_user_2, credential, connector, cc_pair |
0 commit comments