181 lines
6.5 KiB
Python
181 lines
6.5 KiB
Python
"""
|
|
Unit tests for Dynamic Options API (mainDynamicOptions.py).
|
|
Tests option retrieval, validation, and context-aware options.
|
|
"""
|
|
|
|
import pytest
|
|
from unittest.mock import Mock, patch
|
|
from modules.features.dynamicOptions.mainDynamicOptions import (
|
|
getOptions,
|
|
getAvailableOptionsNames,
|
|
STANDARD_ROLES,
|
|
AUTH_AUTHORITY_OPTIONS,
|
|
CONNECTION_STATUS_OPTIONS
|
|
)
|
|
from modules.datamodels.datamodelUam import User, UserConnection, AuthAuthority
|
|
|
|
|
|
class TestMainOptions:
|
|
"""Test Options API functionality."""
|
|
|
|
def testGetOptionsUserRole(self):
|
|
"""Test getting user role options."""
|
|
options = getOptions("user.role")
|
|
|
|
assert isinstance(options, list)
|
|
assert len(options) == 4 # sysadmin, admin, user, viewer
|
|
|
|
# Check structure
|
|
for option in options:
|
|
assert "value" in option
|
|
assert "label" in option
|
|
assert isinstance(option["label"], dict)
|
|
assert "en" in option["label"]
|
|
assert "fr" in option["label"]
|
|
|
|
# Check specific values
|
|
values = [opt["value"] for opt in options]
|
|
assert "sysadmin" in values
|
|
assert "admin" in values
|
|
assert "user" in values
|
|
assert "viewer" in values
|
|
|
|
def testGetOptionsAuthAuthority(self):
|
|
"""Test getting auth authority options."""
|
|
options = getOptions("auth.authority")
|
|
|
|
assert isinstance(options, list)
|
|
assert len(options) == 3 # local, google, msft
|
|
|
|
# Check structure
|
|
for option in options:
|
|
assert "value" in option
|
|
assert "label" in option
|
|
|
|
# Check specific values
|
|
values = [opt["value"] for opt in options]
|
|
assert "local" in values
|
|
assert "google" in values
|
|
assert "msft" in values
|
|
|
|
def testGetOptionsConnectionStatus(self):
|
|
"""Test getting connection status options."""
|
|
options = getOptions("connection.status")
|
|
|
|
assert isinstance(options, list)
|
|
assert len(options) == 5 # active, expired, revoked, pending, error
|
|
|
|
# Check structure
|
|
for option in options:
|
|
assert "value" in option
|
|
assert "label" in option
|
|
|
|
# Check specific values
|
|
values = [opt["value"] for opt in options]
|
|
assert "active" in values
|
|
assert "expired" in values
|
|
assert "revoked" in values
|
|
assert "pending" in values
|
|
assert "error" in values
|
|
|
|
def testGetOptionsUserConnection(self):
|
|
"""Test getting user connection options (context-aware)."""
|
|
# Without currentUser, should return empty list
|
|
options = getOptions("user.connection")
|
|
assert options == []
|
|
|
|
# With currentUser but no connections
|
|
user = User(
|
|
id="user1",
|
|
username="testuser",
|
|
roleLabels=["user"],
|
|
mandateId="mandate1"
|
|
)
|
|
|
|
with patch('modules.features.dynamicOptions.mainDynamicOptions.getInterface') as mockGetInterface:
|
|
mockInterface = Mock()
|
|
mockInterface.getUserConnections.return_value = []
|
|
mockGetInterface.return_value = mockInterface
|
|
|
|
options = getOptions("user.connection", currentUser=user)
|
|
assert options == []
|
|
|
|
def testGetOptionsUserConnectionWithData(self):
|
|
"""Test getting user connection options with actual connections."""
|
|
user = User(
|
|
id="user1",
|
|
username="testuser",
|
|
roleLabels=["user"],
|
|
mandateId="mandate1"
|
|
)
|
|
|
|
# Mock connections
|
|
mockConn1 = Mock(spec=UserConnection)
|
|
mockConn1.id = "conn1"
|
|
mockConn1.authority = AuthAuthority.GOOGLE
|
|
mockConn1.externalUsername = "user@example.com"
|
|
mockConn1.externalId = None
|
|
|
|
mockConn2 = Mock(spec=UserConnection)
|
|
mockConn2.id = "conn2"
|
|
mockConn2.authority = AuthAuthority.MSFT
|
|
mockConn2.externalUsername = None
|
|
mockConn2.externalId = "external-id-123"
|
|
|
|
with patch('modules.features.dynamicOptions.mainDynamicOptions.getInterface') as mockGetInterface:
|
|
mockInterface = Mock()
|
|
mockInterface.getUserConnections.return_value = [mockConn1, mockConn2]
|
|
mockGetInterface.return_value = mockInterface
|
|
|
|
options = getOptions("user.connection", currentUser=user)
|
|
|
|
assert len(options) == 2
|
|
assert options[0]["value"] == "conn1"
|
|
assert options[1]["value"] == "conn2"
|
|
|
|
# Check labels contain authority and username/id
|
|
assert "google" in options[0]["label"]["en"].lower()
|
|
assert "user@example.com" in options[0]["label"]["en"]
|
|
|
|
def testGetOptionsCaseInsensitive(self):
|
|
"""Test that options name matching is case-insensitive."""
|
|
options1 = getOptions("user.role")
|
|
options2 = getOptions("USER.ROLE")
|
|
options3 = getOptions("User.Role")
|
|
|
|
assert options1 == options2 == options3
|
|
|
|
def testGetOptionsUnknown(self):
|
|
"""Test that unknown options name raises ValueError."""
|
|
with pytest.raises(ValueError, match="Unknown options name"):
|
|
getOptions("unknown.options")
|
|
|
|
def testGetAvailableOptionsNames(self):
|
|
"""Test getting list of available options names."""
|
|
names = getAvailableOptionsNames()
|
|
|
|
assert isinstance(names, list)
|
|
assert "user.role" in names
|
|
assert "auth.authority" in names
|
|
assert "connection.status" in names
|
|
assert "user.connection" in names
|
|
assert len(names) == 4
|
|
|
|
def testStandardRolesConstant(self):
|
|
"""Test that STANDARD_ROLES constant is properly defined."""
|
|
assert isinstance(STANDARD_ROLES, list)
|
|
assert len(STANDARD_ROLES) == 4
|
|
|
|
for role in STANDARD_ROLES:
|
|
assert "value" in role
|
|
assert "label" in role
|
|
|
|
def testAuthAuthorityOptionsConstant(self):
|
|
"""Test that AUTH_AUTHORITY_OPTIONS constant is properly defined."""
|
|
assert isinstance(AUTH_AUTHORITY_OPTIONS, list)
|
|
assert len(AUTH_AUTHORITY_OPTIONS) == 3
|
|
|
|
def testConnectionStatusOptionsConstant(self):
|
|
"""Test that CONNECTION_STATUS_OPTIONS constant is properly defined."""
|
|
assert isinstance(CONNECTION_STATUS_OPTIONS, list)
|
|
assert len(CONNECTION_STATUS_OPTIONS) == 5 # active, expired, revoked, pending, error
|