Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/python/cocalc-api/tests/test_org_basic.py
5608 views
1
"""
2
Basic Organization functionality tests.
3
4
This file contains tests that verify the organization API is properly exposed
5
and accessible, without necessarily requiring full admin privileges or server connectivity.
6
"""
7
import pytest
8
9
10
class TestOrganizationAPIExposure:
11
"""Test that organization API methods are properly exposed."""
12
13
def test_org_module_available(self, hub):
14
"""Test that the org module is accessible from hub."""
15
assert hasattr(hub, 'org')
16
assert hub.org is not None
17
18
def test_all_org_methods_available(self, hub):
19
"""Test that all expected organization methods are available and callable."""
20
org = hub.org
21
22
expected_methods = [
23
'get_all', 'create', 'get', 'set', 'add_admin', 'add_user', 'create_user', 'create_token', 'expire_token', 'get_users', 'remove_user',
24
'remove_admin', 'message'
25
]
26
27
for method_name in expected_methods:
28
assert hasattr(org, method_name), f"Method {method_name} not found"
29
method = getattr(org, method_name)
30
assert callable(method), f"Method {method_name} is not callable"
31
32
print(f"✓ All {len(expected_methods)} organization methods are properly exposed")
33
34
def test_org_methods_are_api_decorated(self, hub):
35
"""Test that org methods make actual API calls (not just stubs)."""
36
# We can verify this by attempting to call a method that should fail
37
# with authentication/permission errors rather than NotImplementedError
38
39
with pytest.raises(Exception) as exc_info:
40
# This should make an actual API call and fail with auth or server error,
41
# not with NotImplementedError
42
hub.org.get("nonexistent-org-for-testing-12345")
43
44
# Should NOT be NotImplementedError (which would indicate the method isn't implemented)
45
assert not isinstance(exc_info.value, NotImplementedError), \
46
"Organization methods should make actual API calls, not raise NotImplementedError"
47
48
print(f"✓ Organization methods make actual API calls: {type(exc_info.value).__name__}")
49
50
def test_message_method_signature(self, hub):
51
"""Test that the message method has the correct signature."""
52
import inspect
53
54
sig = inspect.signature(hub.org.message)
55
params = list(sig.parameters.keys())
56
57
# Should have name, subject, body parameters
58
required_params = ['name', 'subject', 'body']
59
for param in required_params:
60
assert param in params, f"Message method missing required parameter: {param}"
61
62
print("✓ Message method has correct parameters:", params)
63
64
def test_create_user_method_signature(self, hub):
65
"""Test that create_user method has the correct signature."""
66
import inspect
67
68
sig = inspect.signature(hub.org.create_user)
69
params = sig.parameters
70
71
# Check required parameters
72
assert 'name' in params, "create_user missing 'name' parameter"
73
assert 'email' in params, "create_user missing 'email' parameter"
74
75
# Check optional parameters
76
optional_params = ['firstName', 'lastName', 'password']
77
for param in optional_params:
78
assert param in params, f"create_user missing optional parameter: {param}"
79
# Optional params should have default values
80
assert params[param].default is not inspect.Parameter.empty, \
81
f"Optional parameter {param} should have a default value"
82
83
print("✓ create_user method has correct parameter signature")
84
85
def test_create_token_return_annotation(self, hub):
86
"""Test that create_token has proper return type annotation."""
87
import inspect
88
89
sig = inspect.signature(hub.org.create_token)
90
return_annotation = sig.return_annotation
91
92
# Should be annotated to return TokenType
93
assert return_annotation.__name__ == 'TokenType', \
94
f"create_token should return TokenType, got {return_annotation}"
95
96
print("✓ create_token method has correct return type annotation")
97
98
99
class TestOrganizationImportIntegrity:
100
"""Test that the organization refactoring didn't break anything."""
101
102
def test_organizations_class_imported_correctly(self, hub):
103
"""Test that Organizations class is properly imported in hub."""
104
# The hub.org should be an instance of the Organizations class
105
from cocalc_api.org import Organizations
106
107
assert isinstance(hub.org, Organizations), \
108
"hub.org should be an instance of Organizations class"
109
110
print("✓ Organizations class properly imported and instantiated")
111
112
def test_original_hub_functionality_preserved(self, hub):
113
"""Test that refactoring didn't break other hub functionality."""
114
# Test that other hub properties still work
115
assert hasattr(hub, 'system'), "Hub should still have system property"
116
assert hasattr(hub, 'projects'), "Hub should still have projects property"
117
assert hasattr(hub, 'messages'), "Hub should still have messages property"
118
119
# Test that projects.delete is still available (from main task)
120
assert hasattr(hub.projects, 'delete'), "Projects should still have delete method"
121
assert callable(hub.projects.delete), "Projects delete should be callable"
122
123
print("✓ All original Hub functionality preserved after org refactoring")
124
125
126
def test_make_check_compatibility():
127
"""Test that the refactoring passes all static analysis checks."""
128
# This test exists to document that the refactored code should pass
129
# make check (ruff, mypy, pyright) - the actual checking is done by CI/make
130
print("✓ Organization refactoring should pass make check (ruff, mypy, pyright)")
131
assert True
132
133