Skip to content

Commit 94af23d

Browse files
committed
refactor: split resoruce.py by operation type
1 parent c7d6ef8 commit 94af23d

5 files changed

Lines changed: 154 additions & 118 deletions

File tree

scim2_tester/resource.py

Lines changed: 6 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,16 @@
11
from scim2_models import Mutability
2-
from scim2_models import Resource
32
from scim2_models import ResourceType
43

54
from scim2_tester.filling import fill_with_random_values
5+
from scim2_tester.resource_delete import check_object_deletion
6+
from scim2_tester.resource_get import check_object_query
7+
from scim2_tester.resource_get import check_object_query_without_id
8+
from scim2_tester.resource_get import model_from_resource_type
9+
from scim2_tester.resource_post import check_object_creation
10+
from scim2_tester.resource_put import check_object_replacement
611
from scim2_tester.utils import CheckConfig
712
from scim2_tester.utils import CheckResult
813
from scim2_tester.utils import Status
9-
from scim2_tester.utils import checker
10-
11-
12-
def model_from_resource_type(
13-
conf: CheckConfig, resource_type: ResourceType
14-
) -> type[Resource] | None:
15-
for resource_model in conf.client.resource_models:
16-
if resource_model.model_fields["schemas"].default[0] == resource_type.schema_:
17-
return resource_model
18-
19-
return None
20-
21-
22-
@checker
23-
def check_object_creation(conf: CheckConfig, obj: type[Resource]) -> CheckResult:
24-
"""Perform an object creation.
25-
26-
Todo:
27-
- check if the fields of the result object are the same than the
28-
fields of the request object
29-
30-
"""
31-
response = conf.client.create(
32-
obj, expected_status_codes=conf.expected_status_codes or [201]
33-
)
34-
35-
return CheckResult(
36-
conf,
37-
status=Status.SUCCESS,
38-
reason=f"Successful creation of a {obj.__class__.__name__} object with id {response.id}",
39-
data=response,
40-
)
41-
42-
43-
@checker
44-
def check_object_query(conf: CheckConfig, obj: type[Resource]) -> CheckResult:
45-
"""Perform an object query by knowing its id.
46-
47-
Todo:
48-
- check if the fields of the result object are the same than the
49-
fields of the request object
50-
51-
"""
52-
response = conf.client.query(
53-
obj.__class__, obj.id, expected_status_codes=conf.expected_status_codes or [200]
54-
)
55-
return CheckResult(
56-
conf,
57-
status=Status.SUCCESS,
58-
reason=f"Successful query of a {obj.__class__.__name__} object with id {response.id}",
59-
data=response,
60-
)
61-
62-
63-
@checker
64-
def check_object_query_without_id(
65-
conf: CheckConfig, obj: type[Resource]
66-
) -> CheckResult:
67-
"""Perform the query of all objects of one kind.
68-
69-
Todo:
70-
- look for the object across several pages
71-
- check if the fields of the result object are the same than the
72-
fields of the request object
73-
74-
"""
75-
response = conf.client.query(
76-
obj.__class__, expected_status_codes=conf.expected_status_codes or [200]
77-
)
78-
found = any(obj.id == resource.id for resource in response.resources)
79-
if not found:
80-
return CheckResult(
81-
conf,
82-
status=Status.ERROR,
83-
reason=f"Could not find object {obj.__class__.__name__} with id : {obj.id}",
84-
data=response,
85-
)
86-
87-
return CheckResult(
88-
conf,
89-
status=Status.SUCCESS,
90-
reason=f"Successful query of a {obj.__class__.__name__} object with id {obj.id}",
91-
data=response,
92-
)
93-
94-
95-
@checker
96-
def check_object_replacement(conf: CheckConfig, obj: type[Resource]) -> CheckResult:
97-
"""Perform an object replacement.
98-
99-
Todo:
100-
- check if the fields of the result object are the same than the
101-
fields of the request object
102-
103-
"""
104-
response = conf.client.replace(
105-
obj, expected_status_codes=conf.expected_status_codes or [200]
106-
)
107-
return CheckResult(
108-
conf,
109-
status=Status.SUCCESS,
110-
reason=f"Successful replacement of a {obj.__class__.__name__} object with id {response.id}",
111-
data=response,
112-
)
113-
114-
115-
@checker
116-
def check_object_deletion(conf: CheckConfig, obj: type[Resource]) -> CheckResult:
117-
"""Perform an object deletion."""
118-
conf.client.delete(
119-
obj.__class__, obj.id, expected_status_codes=conf.expected_status_codes or [204]
120-
)
121-
return CheckResult(
122-
conf,
123-
status=Status.SUCCESS,
124-
reason=f"Successful deletion of a {obj.__class__.__name__} object with id {obj.id}",
125-
)
12614

12715

12816
def check_resource_type(

scim2_tester/resource_delete.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from scim2_models import Resource
2+
3+
from scim2_tester.utils import CheckConfig
4+
from scim2_tester.utils import CheckResult
5+
from scim2_tester.utils import Status
6+
from scim2_tester.utils import checker
7+
8+
9+
@checker
10+
def check_object_deletion(conf: CheckConfig, obj: Resource) -> CheckResult:
11+
"""Perform an object deletion."""
12+
conf.client.delete(
13+
obj.__class__, obj.id, expected_status_codes=conf.expected_status_codes or [204]
14+
)
15+
return CheckResult(
16+
conf,
17+
status=Status.SUCCESS,
18+
reason=f"Successful deletion of a {obj.__class__.__name__} object with id {obj.id}",
19+
)

scim2_tester/resource_get.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
from scim2_models import Resource
2+
from scim2_models import ResourceType
3+
4+
from scim2_tester.utils import CheckConfig
5+
from scim2_tester.utils import CheckResult
6+
from scim2_tester.utils import Status
7+
from scim2_tester.utils import checker
8+
9+
10+
def model_from_resource_type(
11+
conf: CheckConfig, resource_type: ResourceType
12+
) -> type[Resource] | None:
13+
"""Return the Resource model class from a given ResourceType.
14+
15+
ResourceType.name contains the resource type name (e.g., "User", "Group").
16+
This function looks up the corresponding model class from the client.
17+
18+
:param conf: The check configuration containing the SCIM client
19+
:param resource_type: The ResourceType object containing metadata
20+
:returns: The Resource model class, or None if not found
21+
"""
22+
for resource_model in conf.client.resource_models:
23+
if resource_model.model_fields["schemas"].default[0] == resource_type.schema_:
24+
return resource_model
25+
26+
return None
27+
28+
29+
@checker
30+
def check_object_query(conf: CheckConfig, obj: Resource) -> CheckResult:
31+
"""Perform an object query by knowing its id.
32+
33+
Todo:
34+
- check if the fields of the result object are the same than the
35+
fields of the request object
36+
37+
"""
38+
response = conf.client.query(
39+
obj.__class__, obj.id, expected_status_codes=conf.expected_status_codes or [200]
40+
)
41+
return CheckResult(
42+
conf,
43+
status=Status.SUCCESS,
44+
reason=f"Successful query of a {obj.__class__.__name__} object with id {response.id}",
45+
data=response,
46+
)
47+
48+
49+
@checker
50+
def check_object_query_without_id(conf: CheckConfig, obj: Resource) -> CheckResult:
51+
"""Perform the query of all objects of one kind.
52+
53+
Todo:
54+
- look for the object across several pages
55+
- check if the fields of the result object are the same than the
56+
fields of the request object
57+
58+
"""
59+
response = conf.client.query(
60+
obj.__class__, expected_status_codes=conf.expected_status_codes or [200]
61+
)
62+
found = any(obj.id == resource.id for resource in response.resources)
63+
if not found:
64+
return CheckResult(
65+
conf,
66+
status=Status.ERROR,
67+
reason=f"Could not find object {obj.__class__.__name__} with id : {obj.id}",
68+
data=response,
69+
)
70+
71+
return CheckResult(
72+
conf,
73+
status=Status.SUCCESS,
74+
reason=f"Successful query of a {obj.__class__.__name__} object with id {obj.id}",
75+
data=response,
76+
)

scim2_tester/resource_post.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from scim2_models import Resource
2+
3+
from scim2_tester.utils import CheckConfig
4+
from scim2_tester.utils import CheckResult
5+
from scim2_tester.utils import Status
6+
from scim2_tester.utils import checker
7+
8+
9+
@checker
10+
def check_object_creation(conf: CheckConfig, obj: Resource) -> CheckResult:
11+
"""Perform an object creation.
12+
13+
Todo:
14+
- check if the fields of the result object are the same than the
15+
fields of the request object
16+
17+
"""
18+
response = conf.client.create(
19+
obj, expected_status_codes=conf.expected_status_codes or [201]
20+
)
21+
22+
return CheckResult(
23+
conf,
24+
status=Status.SUCCESS,
25+
reason=f"Successful creation of a {obj.__class__.__name__} object with id {response.id}",
26+
data=response,
27+
)

scim2_tester/resource_put.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from scim2_models import Resource
2+
3+
from scim2_tester.utils import CheckConfig
4+
from scim2_tester.utils import CheckResult
5+
from scim2_tester.utils import Status
6+
from scim2_tester.utils import checker
7+
8+
9+
@checker
10+
def check_object_replacement(conf: CheckConfig, obj: Resource) -> CheckResult:
11+
"""Perform an object replacement.
12+
13+
Todo:
14+
- check if the fields of the result object are the same than the
15+
fields of the request object
16+
17+
"""
18+
response = conf.client.replace(
19+
obj, expected_status_codes=conf.expected_status_codes or [200]
20+
)
21+
return CheckResult(
22+
conf,
23+
status=Status.SUCCESS,
24+
reason=f"Successful replacement of a {obj.__class__.__name__} object with id {response.id}",
25+
data=response,
26+
)

0 commit comments

Comments
 (0)