Skip to content

Commit 85d7b56

Browse files
haakonvtdoctrino
authored andcommitted
Give actions better error messages (#1476)
1 parent 4c5f019 commit 85d7b56

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

cognite/client/data_classes/capabilities.py

+5
Original file line numberDiff line numberDiff line change
@@ -867,3 +867,8 @@ class Scope:
867867
_CAPABILITY_CLASS_BY_NAME: dict[str, type[Capability]] = {
868868
c._capability_name: c for c in Capability.__subclasses__() if not issubclass(c, UnknownAcl)
869869
}
870+
# Give all Actions a better error message (instead of implementing __missing__ for all):
871+
for acl in _CAPABILITY_CLASS_BY_NAME.values():
872+
if acl.Action.__members__:
873+
_cls = type(next(iter(acl.Action.__members__.values())))
874+
_cls.__name__ = f"{acl.__name__} {_cls.__name__}"

tests/tests_unit/test_data_classes/test_capabilities.py

+25
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,28 @@ def test_load_dump_unknown(self, raw: dict[str, Any]) -> None:
183183
assert all(action is UnknownAcl.Action.Unknown for action in capability.actions)
184184
assert capability.raw_data == raw
185185
assert capability.dump(camel_case=True) == raw
186+
187+
@pytest.mark.parametrize(
188+
"acl_cls_name, bad_action, dumped",
189+
[
190+
("AssetsAcl", "SONG-WRITER", {"assetsAcl": {"actions": ["READ", "SONG-WRITER"], "scope": {"all": {}}}}),
191+
(
192+
"DocumentFeedbackAcl",
193+
"CTRL-ALT-DELETE",
194+
{"documentFeedbackAcl": {"actions": ["CREATE", "CTRL-ALT-DELETE"], "scope": {"all": {}}}},
195+
),
196+
(
197+
"FilesAcl",
198+
"COMPRESS",
199+
{"filesAcl": {"actions": ["COMPRESS"], "scope": {"datasetScope": {"ids": ["2332579", "372"]}}}},
200+
),
201+
(
202+
"NotificationsAcl",
203+
"SEND-PIGEON",
204+
{"notificationsAcl": {"actions": ["READ", "SEND-PIGEON"], "scope": {"all": {}}}},
205+
),
206+
],
207+
)
208+
def test_load__action_does_not_exist(self, acl_cls_name: str, bad_action: str, dumped: dict[str, Any]) -> None:
209+
with pytest.raises(ValueError, match=rf"^'{bad_action}' is not a valid {acl_cls_name} Action$"):
210+
Capability.load(dumped)

0 commit comments

Comments
 (0)