Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add more test cases except resource #77

Merged
merged 2 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install black ruff pre-commit
pip install -r requirements.txt

- name: Run linter
run: git diff --name-only HEAD~10 HEAD | xargs pre-commit run --files

- name: Run tests
run: python -m unittest src/tests/load_tests.py -v

- name: Release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ repos:


- repo: https://github.com/commitizen-tools/commitizen
rev: 3.2.2
rev: v3.2.2
hooks:
- id: commitizen
stages: [ commit-msg ]
47 changes: 38 additions & 9 deletions src/casdoor/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ def __init__(self):
self.tableNamePrefix = ""
self.isEnabled = False

@classmethod
def new(cls, owner, name, created_time, host, user):
self = cls()
self.owner = owner
self.name = name
self.createdTime = created_time
self.host = host
self.user = user
return self

@classmethod
def from_dict(cls, data: dict):
if data is None:
return None

adapter = cls()
for key, value in data.items():
if hasattr(adapter, key):
setattr(adapter, key, value)
return adapter

def __str__(self):
return str(self.__dict__)

Expand All @@ -42,7 +63,7 @@ def to_dict(self) -> dict:


class _AdapterSDK:
def get_adapters(self) -> List[Dict]:
def get_adapters(self) -> List[Adapter]:
"""
Get the adapters from Casdoor.

Expand All @@ -55,10 +76,15 @@ def get_adapters(self) -> List[Dict]:
"clientSecret": self.client_secret,
}
r = requests.get(url, params)
adapters = r.json()
response = r.json()
if response["status"] != "ok":
raise Exception(response["msg"])
adapters = []
for adapter in response["data"]:
adapters.append(Adapter.from_dict(adapter))
return adapters

def get_adapter(self, adapter_id: str) -> Dict:
def get_adapter(self, adapter_id: str) -> Adapter:
"""
Get the adapter from Casdoor providing the adapter_id.

Expand All @@ -72,13 +98,14 @@ def get_adapter(self, adapter_id: str) -> Dict:
"clientSecret": self.client_secret,
}
r = requests.get(url, params)
adapter = r.json()
return adapter
response = r.json()
if response["status"] != "ok":
raise Exception(response["msg"])
return Adapter.from_dict(response["data"])

def modify_adapter(self, method: str, adapter: Adapter) -> Dict:
def modify_adapter(self, method: str, adapter: Adapter) -> str:
url = self.endpoint + f"/api/{method}"
if adapter.owner == "":
adapter.owner = self.org_name
adapter.owner = self.org_name
params = {
"id": f"{adapter.owner}/{adapter.name}",
"clientId": self.client_id,
Expand All @@ -87,7 +114,9 @@ def modify_adapter(self, method: str, adapter: Adapter) -> Dict:
adapter_info = json.dumps(adapter.to_dict())
r = requests.post(url, params=params, data=adapter_info)
response = r.json()
return response
if response["status"] != "ok":
raise Exception(response["msg"])
return str(response["data"])

def add_adapter(self, adapter: Adapter) -> Dict:
response = self.modify_adapter("add-adapter", adapter)
Expand Down
63 changes: 49 additions & 14 deletions src/casdoor/cert.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

import json
from typing import Dict, List
from typing import List

import requests

Expand All @@ -34,6 +34,31 @@ def __init__(self):
self.authorityPublicKey = ""
self.authorityRootPublicKey = ""

@classmethod
def new(cls, owner, name, created_time, display_name, scope, type, crypto_algorithm, bit_size, expire_in_years):
self = cls()
self.owner = owner
self.name = name
self.createdTime = created_time
self.displayName = display_name
self.scope = scope
self.type = type
self.cryptoAlgorithm = crypto_algorithm
self.bitSize = bit_size
self.expireInYears = expire_in_years
return self

@classmethod
def from_dict(cls, data: dict):
if data is None:
return None

cert = cls()
for key, value in data.items():
if hasattr(cert, key):
setattr(cert, key, value)
return cert

def __str__(self):
return str(self.__dict__)

Expand All @@ -42,7 +67,7 @@ def to_dict(self) -> dict:


class _CertSDK:
def get_certs(self) -> List[Dict]:
def get_certs(self) -> List[Cert]:
"""
Get the certs from Casdoor.

Expand All @@ -55,10 +80,16 @@ def get_certs(self) -> List[Dict]:
"clientSecret": self.client_secret,
}
r = requests.get(url, params)
certs = r.json()
return certs
response = r.json()
if response["status"] != "ok":
raise ValueError(response["msg"])

def get_cert(self, cert_id: str) -> Dict:
res = []
for element in response["data"]:
res.append(Cert.from_dict(element))
return res

def get_cert(self, cert_id: str) -> Cert:
"""
Get the cert from Casdoor providing the cert_id.

Expand All @@ -72,13 +103,15 @@ def get_cert(self, cert_id: str) -> Dict:
"clientSecret": self.client_secret,
}
r = requests.get(url, params)
cert = r.json()
return cert
response = r.json()
if response["status"] != "ok":
raise ValueError(response["msg"])

def modify_cert(self, method: str, cert: Cert) -> Dict:
return Cert.from_dict(response["data"])

def modify_cert(self, method: str, cert: Cert) -> str:
url = self.endpoint + f"/api/{method}"
if cert.owner == "":
cert.owner = self.org_name
cert.owner = self.org_name
params = {
"id": f"{cert.owner}/{cert.name}",
"clientId": self.client_id,
Expand All @@ -87,16 +120,18 @@ def modify_cert(self, method: str, cert: Cert) -> Dict:
cert_info = json.dumps(cert.to_dict())
r = requests.post(url, params=params, data=cert_info)
response = r.json()
return response
if response["status"] != "ok":
raise ValueError(response["msg"])
return str(response["data"])

def add_cert(self, cert: Cert) -> Dict:
def add_cert(self, cert: Cert) -> str:
response = self.modify_cert("add-cert", cert)
return response

def update_cert(self, cert: Cert) -> Dict:
def update_cert(self, cert: Cert) -> str:
response = self.modify_cert("update-cert", cert)
return response

def delete_cert(self, cert: Cert) -> Dict:
def delete_cert(self, cert: Cert) -> str:
response = self.modify_cert("delete-cert", cert)
return response
40 changes: 35 additions & 5 deletions src/casdoor/enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ def __init__(self):
self.adapter = ""
self.isEnabled = False

@classmethod
def new(cls, owner, name, created_time, display_name, description, model, adapter):
self = cls()
self.owner = owner
self.name = name
self.createdTime = created_time
self.displayName = display_name
self.description = description
self.model = model
self.adapter = adapter
return self

@classmethod
def from_dict(cls, data: dict):
if data is None:
return None

enforcer = cls()
for key, value in data.items():
if hasattr(enforcer, key):
setattr(enforcer, key, value)
return enforcer

def __str__(self):
return str(self.__dict__)

Expand All @@ -51,7 +74,12 @@ def get_enforcers(self) -> List[Dict]:
"clientSecret": self.client_secret,
}
r = requests.get(url, params)
enforcers = r.json()
response = r.json()
if response["status"] != "ok":
raise Exception(response["msg"])
enforcers = []
for enforcer in response["data"]:
enforcers.append(Enforcer.from_dict(enforcer))
return enforcers

def get_enforcer(self, enforcer_id: str) -> Dict:
Expand All @@ -68,13 +96,15 @@ def get_enforcer(self, enforcer_id: str) -> Dict:
"clientSecret": self.client_secret,
}
r = requests.get(url, params)
enforcer = r.json()
return enforcer
response = r.json()
if response["status"] != "ok":
raise Exception(response["msg"])

return Enforcer.from_dict(response["data"])

def modify_enforcer(self, method: str, enforcer: Enforcer) -> Dict:
url = self.endpoint + f"/api/{method}"
if enforcer.owner == "":
enforcer.owner = self.org_name
enforcer.owner = self.org_name
params = {
"id": f"{enforcer.owner}/{enforcer.name}",
"clientId": self.client_id,
Expand Down
2 changes: 1 addition & 1 deletion src/casdoor/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from .model import _ModelSDK
from .organization import _OrganizationSDK
from .payment import _PaymentSDK
from .permisssion import _PermissionSDK
from .permission import _PermissionSDK
from .plan import _PlanSDK
from .pricing import _PricingSDK
from .product import _ProductSDK
Expand Down
43 changes: 37 additions & 6 deletions src/casdoor/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,29 @@ def __init__(self):
self.title = ""
self.key = ""
self.children = [Model]
self.modelText = ""
self.isEnabled = False

@classmethod
def new(cls, owner, name, created_time, display_name, model_text):
self = cls()
self.owner = owner
self.name = name
self.createdTime = created_time
self.displayName = display_name
self.modelText = model_text
return self

@classmethod
def from_dict(cls, data: dict):
if not data:
return None
model = cls()
for key, value in data.items():
if hasattr(model, key):
setattr(model, key, value)
return model

def __str__(self):
return str(self.__dict__)

Expand All @@ -59,7 +80,12 @@ def get_models(self) -> List[Dict]:
"clientSecret": self.client_secret,
}
r = requests.get(url, params)
models = r.json()
response = r.json()
if response["status"] != "ok":
raise Exception(response["msg"])
models = []
for model in response["data"]:
models.append(Model.from_dict(model))
return models

def get_model(self, model_id: str) -> Dict:
Expand All @@ -76,19 +102,20 @@ def get_model(self, model_id: str) -> Dict:
"clientSecret": self.client_secret,
}
r = requests.get(url, params)
model = r.json()
return model
response = r.json()
if response["status"] != "ok":
raise Exception(response["msg"])
return Model.from_dict(response["data"])

def modify_model(self, method: str, model: Model) -> Dict:
url = self.endpoint + f"/api/{method}"
if model.owner == "":
model.owner = self.org_name
model.owner = self.org_name
params = {
"id": f"{model.owner}/{model.name}",
"clientId": self.client_id,
"clientSecret": self.client_secret,
}
model_info = json.dumps(model.to_dict())
model_info = json.dumps(model.to_dict(), default=self.custom_encoder)
r = requests.post(url, params=params, data=model_info)
response = r.json()
return response
Expand All @@ -104,3 +131,7 @@ def update_model(self, model: Model) -> Dict:
def delete_model(self, model: Model) -> Dict:
response = self.modify_model("delete-model", model)
return response

def custom_encoder(self, o):
if isinstance(o, (Model, User)):
return o.__dict__
Loading
Loading