2025-09-26 13:53:49 +02:00
|
|
|
import base64
|
|
|
|
|
|
2025-03-23 00:03:04 +01:00
|
|
|
import pytest
|
|
|
|
|
|
2025-03-24 11:28:52 +01:00
|
|
|
from servala.core.models import (
|
2025-10-07 15:04:12 +02:00
|
|
|
BillingEntity,
|
2025-03-24 11:28:52 +01:00
|
|
|
Organization,
|
|
|
|
|
OrganizationMembership,
|
|
|
|
|
OrganizationOrigin,
|
|
|
|
|
User,
|
|
|
|
|
)
|
2025-09-26 13:53:49 +02:00
|
|
|
from servala.core.models.service import (
|
|
|
|
|
CloudProvider,
|
|
|
|
|
Service,
|
|
|
|
|
ServiceCategory,
|
|
|
|
|
ServiceOffering,
|
|
|
|
|
)
|
2025-03-23 00:03:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def origin():
|
|
|
|
|
return OrganizationOrigin.objects.create(name="TESTORIGIN")
|
|
|
|
|
|
2025-03-24 11:28:52 +01:00
|
|
|
|
2025-10-07 15:04:12 +02:00
|
|
|
@pytest.fixture
|
|
|
|
|
def billing_entity():
|
|
|
|
|
return BillingEntity.objects.create(name="Test Entity")
|
|
|
|
|
|
|
|
|
|
|
2025-03-23 00:03:04 +01:00
|
|
|
@pytest.fixture
|
|
|
|
|
def organization(origin):
|
2025-04-09 16:06:45 +02:00
|
|
|
return Organization.objects.create(name="Test Org", origin=origin)
|
2025-03-23 00:03:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def other_organization(origin):
|
2025-04-09 16:06:45 +02:00
|
|
|
return Organization.objects.create(name="Test Org Alternate", origin=origin)
|
2025-03-23 00:03:04 +01:00
|
|
|
|
2025-03-24 11:28:52 +01:00
|
|
|
|
2025-03-23 00:03:04 +01:00
|
|
|
@pytest.fixture
|
|
|
|
|
def org_owner(organization):
|
|
|
|
|
user = User.objects.create(email="user@example.org", password="example")
|
2025-03-24 11:28:52 +01:00
|
|
|
OrganizationMembership.objects.create(
|
|
|
|
|
organization=organization, user=user, role="owner"
|
|
|
|
|
)
|
2025-03-23 00:03:04 +01:00
|
|
|
return user
|
2025-09-26 13:53:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def test_service_category():
|
|
|
|
|
return ServiceCategory.objects.create(
|
|
|
|
|
name="Databases",
|
|
|
|
|
description="Database services",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def test_service(test_service_category):
|
|
|
|
|
return Service.objects.create(
|
|
|
|
|
name="Redis",
|
|
|
|
|
slug="redis",
|
|
|
|
|
category=test_service_category,
|
|
|
|
|
description="Redis database service",
|
2025-10-02 09:49:03 +02:00
|
|
|
osb_service_id="test-service-123",
|
2025-09-26 13:53:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def test_cloud_provider():
|
|
|
|
|
return CloudProvider.objects.create(
|
|
|
|
|
name="Exoscale",
|
|
|
|
|
description="Exoscale cloud provider",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def test_service_offering(test_service, test_cloud_provider):
|
|
|
|
|
return ServiceOffering.objects.create(
|
|
|
|
|
service=test_service,
|
|
|
|
|
provider=test_cloud_provider,
|
|
|
|
|
description="Redis on Exoscale",
|
2025-10-02 09:49:03 +02:00
|
|
|
osb_plan_id="test-plan-123",
|
2025-09-26 13:53:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def osb_client(client):
|
|
|
|
|
credentials = base64.b64encode(b"testuser:testpass").decode("ascii")
|
|
|
|
|
client.defaults = {"HTTP_AUTHORIZATION": f"Basic {credentials}"}
|
|
|
|
|
return client
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def mock_odoo_success(mocker):
|
|
|
|
|
"""
|
|
|
|
|
Mock Odoo client with successful responses for organization creation.
|
|
|
|
|
Returns the mock object for further customization if needed.
|
|
|
|
|
"""
|
|
|
|
|
mock_client = mocker.patch("servala.core.models.organization.CLIENT")
|
|
|
|
|
|
|
|
|
|
# Default successful responses for organization creation
|
|
|
|
|
mock_client.execute.side_effect = [
|
|
|
|
|
123, # company_id
|
|
|
|
|
456, # invoice_address_id
|
|
|
|
|
789, # sale_order_id
|
|
|
|
|
]
|
|
|
|
|
mock_client.search_read.return_value = [{"name": "SO001"}]
|
|
|
|
|
|
|
|
|
|
return mock_client
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def mock_odoo_failure(mocker):
|
|
|
|
|
"""
|
|
|
|
|
Mock Odoo client that raises an exception to simulate failure.
|
|
|
|
|
"""
|
|
|
|
|
mock_client = mocker.patch("servala.core.models.organization.CLIENT")
|
|
|
|
|
mock_client.execute.side_effect = Exception("Odoo connection failed")
|
|
|
|
|
return mock_client
|