From d0664eff30b0d311f16c9e3494dbcaa79965d8da Mon Sep 17 00:00:00 2001 From: xiao-kong-long <2745240762@qq.com> Date: Mon, 11 Dec 2023 19:17:47 +0800 Subject: [PATCH] feat: add test case and necessary functions for resource --- CHANGELOG.md | 4 +++ src/casdoor/resource.py | 36 ++++++++++++++++++++++++-- src/tests/casbinTest.svg | 0 src/tests/test_resource.py | 52 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 src/tests/casbinTest.svg create mode 100644 src/tests/test_resource.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 4dbbe13..1e35547 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ # [1.18.0](https://github.com/casdoor/casdoor-python-sdk/compare/v1.17.2...v1.18.0) (2023-11-30) +### Features + +* add test cases for organization and group ([#76](https://github.com/casdoor/casdoor-python-sdk/issues/76)) ([4319243](https://github.com/casdoor/casdoor-python-sdk/commit/4319243429ae48feb80de0bbb05f3078e36afd54)) + ## [1.17.2](https://github.com/casdoor/casdoor-python-sdk/compare/v1.17.1...v1.17.2) (2023-11-24) diff --git a/src/casdoor/resource.py b/src/casdoor/resource.py index 696872f..8b56579 100644 --- a/src/casdoor/resource.py +++ b/src/casdoor/resource.py @@ -128,6 +128,38 @@ def update_resource(self, resource: Resource) -> Dict: response = self.modify_resource("update-resource", resource) return response - def delete_resource(self, resource: Resource) -> Dict: - response = self.modify_resource("delete-resource", resource) + def upload_resource(self, user, tag, parent, full_File_path, file) -> Dict: + url = self.endpoint + "/api/upload-resource" + params = { + "owner": self.org_name, + "user": user, + "application": self.application_name, + "tag": tag, + "parent": parent, + "fullFilePath": full_File_path, + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + + files = {"file": (full_File_path, "application/octet-stream")} + r = requests.post(url, params=params, files=files) + response = r.json() + if response["status"] != "ok": + raise Exception(response["msg"]) + return response + + def delete_resource(self, name) -> Dict: + resource = Resource.new(self.org_name, name) + user_str = json.dumps(resource.to_dict()) + url = self.endpoint + "/api/delete-resource" + params = { + "owner": self.org_name, + "name": name, + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.post(url, params=params, data=user_str) + response = r.json() + if response["status"] != "ok": + raise Exception(response["msg"]) return response diff --git a/src/tests/casbinTest.svg b/src/tests/casbinTest.svg new file mode 100644 index 0000000..e69de29 diff --git a/src/tests/test_resource.py b/src/tests/test_resource.py new file mode 100644 index 0000000..5372396 --- /dev/null +++ b/src/tests/test_resource.py @@ -0,0 +1,52 @@ +# Copyright 2023 The Casdoor Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import unittest + +from src.casdoor import CasdoorSDK +from src.casdoor.resource import Resource +from src.tests.test_util import ( + TestApplication, + TestClientId, + TestClientSecret, + TestEndpoint, + TestJwtPublicKey, + TestOrganization, +) + + +class ResourceTest(unittest.TestCase): + def test_resource(self): + # upload_resource + filename = "casbinTest.svg" + script_dir = os.path.dirname(os.path.abspath(__file__)) + file_path = os.path.join(script_dir, filename) + with open(file_path, "rb") as file: + data = file.read() + name = f"/casdoor/{filename}" + resource = Resource.new(owner="casbin", name=name) + + sdk = CasdoorSDK( + TestEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestOrganization, TestApplication + ) + + response = sdk.upload_resource(resource.owner, name, "", filename, data) + self.assertEqual("ok", response["status"]) + + # Delete the resource + delete_resource = sdk.delete_resource(name) + self.assertEqual("ok", delete_resource["status"]) + # There is no get method + # so there is no way to test the effect of deletion, only to assert the returned status code