Skip to content

Commit

Permalink
feat: add test case and necessary functions for resource
Browse files Browse the repository at this point in the history
  • Loading branch information
xiao-kong-long committed Dec 11, 2023
1 parent ecbb772 commit d0664ef
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
36 changes: 34 additions & 2 deletions src/casdoor/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Empty file added src/tests/casbinTest.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions src/tests/test_resource.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d0664ef

Please sign in to comment.