Skip to content

Commit

Permalink
Add test_application.py
Browse files Browse the repository at this point in the history
  • Loading branch information
hsluoyz committed Nov 27, 2023
1 parent 0a1939a commit 0d0ad94
Show file tree
Hide file tree
Showing 23 changed files with 253 additions and 40 deletions.
3 changes: 2 additions & 1 deletion src/casdoor/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def get_adapter(self, adapter_id: str) -> Dict:

def modify_adapter(self, method: str, adapter: Adapter) -> Dict:
url = self.endpoint + f"/api/{method}"
adapter.owner = self.org_name
if adapter.owner == "":
adapter.owner = self.org_name
params = {
"id": f"{adapter.owner}/{adapter.name}",
"clientId": self.client_id,
Expand Down
77 changes: 57 additions & 20 deletions src/casdoor/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
# limitations under the License.

import json
from typing import Dict, List

import requests
from typing import List

from .organization import Organization, ThemeData
# from .organization import Organization, ThemeData
from .provider import Provider


Expand Down Expand Up @@ -76,10 +75,10 @@ def __init__(self):
self.enableLinkWithEmail = False
self.orgChoiceMode = ""
self.samlReplyUrl = ""
self.providers = [ProviderItem]
self.signupItems = [SignupItem]
# self.providers = [ProviderItem]
# self.signupItems = [SignupItem]
self.grantTypes = [""]
self.organizationObj = Organization
# self.organizationObj = Organization
self.tags = [""]
self.clientId = ""
self.clientSecret = ""
Expand All @@ -94,7 +93,31 @@ def __init__(self):
self.termsOfUse = ""
self.signupHtml = ""
self.signinHtml = ""
self.themeData = ThemeData
# self.themeData = ThemeData

@classmethod
def new(cls, owner, name, created_time, display_name, logo, homepage_url, description, organization):
self = cls()
self.owner = owner
self.name = name
self.createdTime = created_time
self.displayName = display_name
self.logo = logo
self.homepageUrl = homepage_url
self.description = description
self.organization = organization
return self

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

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

def __str__(self):
return str(self.__dict__)
Expand All @@ -104,7 +127,7 @@ def to_dict(self) -> dict:


class _ApplicationSDK:
def get_applications(self) -> List[Dict]:
def get_applications(self) -> List[Application]:
"""
Get the applications from Casdoor.
Expand All @@ -117,10 +140,16 @@ def get_applications(self) -> List[Dict]:
"clientSecret": self.client_secret,
}
r = requests.get(url, params)
applications = r.json()
return applications
response = r.json()
if response["status"] != "ok":
raise ValueError(response.msg)

res = []
for element in response["data"]:
res.append(Application.from_dict(element))
return res

def get_application(self, application_id: str) -> Dict:
def get_application(self, application_id: str) -> Application:
"""
Get the application from Casdoor providing the application_id.
Expand All @@ -129,17 +158,20 @@ def get_application(self, application_id: str) -> Dict:
"""
url = self.endpoint + "/api/get-application"
params = {
"id": f"{self.org_name}/{application_id}",
"id": f"admin/{application_id}",
"clientId": self.client_id,
"clientSecret": self.client_secret,
}
r = requests.get(url, params)
application = r.json()
return application
response = r.json()
if response["status"] != "ok":
raise ValueError(response.msg)
return Application.from_dict(response["data"])

def modify_application(self, method: str, application: Application) -> Dict:
def modify_application(self, method: str, application: Application) -> str:
url = self.endpoint + f"/api/{method}"
application.owner = self.org_name
if application.owner == "":
application.owner = self.org_name
params = {
"id": f"{application.owner}/{application.name}",
"clientId": self.client_id,
Expand All @@ -148,16 +180,21 @@ def modify_application(self, method: str, application: Application) -> Dict:
application_info = json.dumps(application.to_dict())
r = requests.post(url, params=params, data=application_info)
response = r.json()
return response
if response["status"] != "ok":
raise ValueError(response.msg)
return str(response["data"])

def add_application(self, application: Application) -> Dict:
def add_application(self, application: Application) -> str:
application.owner = "admin"
response = self.modify_application("add-application", application)
return response

def update_application(self, application: Application) -> Dict:
def update_application(self, application: Application) -> str:
application.owner = "admin"
response = self.modify_application("update-application", application)
return response

def delete_application(self, application: Application) -> Dict:
def delete_application(self, application: Application) -> str:
application.owner = "admin"
response = self.modify_application("delete-application", application)
return response
3 changes: 2 additions & 1 deletion src/casdoor/cert.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def get_cert(self, cert_id: str) -> Dict:

def modify_cert(self, method: str, cert: Cert) -> Dict:
url = self.endpoint + f"/api/{method}"
cert.owner = self.org_name
if cert.owner == "":
cert.owner = self.org_name
params = {
"id": f"{cert.owner}/{cert.name}",
"clientId": self.client_id,
Expand Down
3 changes: 2 additions & 1 deletion src/casdoor/enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ def get_enforcer(self, enforcer_id: str) -> Dict:

def modify_enforcer(self, method: str, enforcer: Enforcer) -> Dict:
url = self.endpoint + f"/api/{method}"
enforcer.owner = self.org_name
if enforcer.owner == "":
enforcer.owner = self.org_name
params = {
"id": f"{enforcer.owner}/{enforcer.name}",
"clientId": self.client_id,
Expand Down
3 changes: 2 additions & 1 deletion src/casdoor/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def get_group(self, group_id: str) -> Dict:

def modify_group(self, method: str, group: Group) -> Dict:
url = self.endpoint + f"/api/{method}"
group.owner = self.org_name
if group.owner == "":
group.owner = self.org_name
params = {
"id": f"{group.owner}/{group.name}",
"clientId": self.client_id,
Expand Down
3 changes: 2 additions & 1 deletion src/casdoor/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def get_model(self, model_id: str) -> Dict:

def modify_model(self, method: str, model: Model) -> Dict:
url = self.endpoint + f"/api/{method}"
model.owner = self.org_name
if model.owner == "":
model.owner = self.org_name
params = {
"id": f"{model.owner}/{model.name}",
"clientId": self.client_id,
Expand Down
3 changes: 2 additions & 1 deletion src/casdoor/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ def get_organization(self, organization_id: str) -> Dict:

def modify_organization(self, method: str, organization: Organization) -> Dict:
url = self.endpoint + f"/api/{method}"
organization.owner = self.org_name
if organization.owner == "":
organization.owner = self.org_name
params = {
"id": f"{organization.owner}/{organization.name}",
"clientId": self.client_id,
Expand Down
3 changes: 2 additions & 1 deletion src/casdoor/payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ def get_payment(self, payment_id: str) -> Dict:

def modify_payment(self, method: str, payment: Payment) -> Dict:
url = self.endpoint + f"/api/{method}"
payment.owner = self.org_name
if payment.owner == "":
payment.owner = self.org_name
params = {
"id": f"{payment.owner}/{payment.name}",
"clientId": self.client_id,
Expand Down
3 changes: 2 additions & 1 deletion src/casdoor/permisssion.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ def get_permission(self, permission_id: str) -> Dict:

def modify_permission(self, method: str, permission: Permission) -> Dict:
url = self.endpoint + f"/api/{method}"
permission.owner = self.org_name
if permission.owner == "":
permission.owner = self.org_name
params = {
"id": f"{permission.owner}/{permission.name}",
"clientId": self.client_id,
Expand Down
3 changes: 2 additions & 1 deletion src/casdoor/plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ def get_plan(self, plan_id: str) -> Dict:

def modify_plan(self, method: str, plan: Plan) -> Dict:
url = self.endpoint + f"/api/{method}"
plan.owner = self.org_name
if plan.owner == "":
plan.owner = self.org_name
params = {
"id": f"{plan.owner}/{plan.name}",
"clientId": self.client_id,
Expand Down
3 changes: 2 additions & 1 deletion src/casdoor/pricing.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def get_pricing(self, pricing_id: str) -> Dict:

def modify_pricing(self, method: str, pricing: Pricing) -> Dict:
url = self.endpoint + f"/api/{method}"
pricing.owner = self.org_name
if pricing.owner == "":
pricing.owner = self.org_name
params = {
"id": f"{pricing.owner}/{pricing.name}",
"clientId": self.client_id,
Expand Down
3 changes: 2 additions & 1 deletion src/casdoor/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def get_product(self, product_id: str) -> Dict:

def modify_product(self, method: str, product: Product) -> Dict:
url = self.endpoint + f"/api/{method}"
product.owner = self.org_name
if product.owner == "":
product.owner = self.org_name
params = {
"id": f"{product.owner}/{product.name}",
"clientId": self.client_id,
Expand Down
3 changes: 2 additions & 1 deletion src/casdoor/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ def get_provider(self, provider_id: str) -> Dict:

def modify_provider(self, method: str, provider: Provider) -> Dict:
url = self.endpoint + f"/api/{method}"
provider.owner = self.org_name
if provider.owner == "":
provider.owner = self.org_name
params = {
"id": f"{provider.owner}/{provider.name}",
"clientId": self.client_id,
Expand Down
3 changes: 2 additions & 1 deletion src/casdoor/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ def get_resource(self, resource_id: str) -> Dict:

def modify_resource(self, method: str, resource: Resource) -> Dict:
url = self.endpoint + f"/api/{method}"
resource.owner = self.org_name
if resource.owner == "":
resource.owner = self.org_name
params = {
"id": f"{resource.owner}/{resource.name}",
"clientId": self.client_id,
Expand Down
3 changes: 2 additions & 1 deletion src/casdoor/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ def get_role(self, role_id: str) -> Dict:

def modify_role(self, method: str, role: Role) -> Dict:
url = self.endpoint + f"/api/{method}"
role.owner = self.org_name
if role.owner == "":
role.owner = self.org_name
params = {
"id": f"{role.owner}/{role.name}",
"clientId": self.client_id,
Expand Down
3 changes: 2 additions & 1 deletion src/casdoor/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ def get_session(self, session_id: str) -> Dict:

def modify_session(self, method: str, session: Session) -> Dict:
url = self.endpoint + f"/api/{method}"
session.owner = self.org_name
if session.owner == "":
session.owner = self.org_name
params = {
"id": f"{session.owner}/{session.name}",
"clientId": self.client_id,
Expand Down
3 changes: 2 additions & 1 deletion src/casdoor/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def get_subscription(self, subscription_id: str) -> Dict:

def modify_subscription(self, method: str, subscription: Subscription) -> Dict:
url = self.endpoint + f"/api/{method}"
subscription.owner = self.org_name
if subscription.owner == "":
subscription.owner = self.org_name
params = {
"id": f"{subscription.owner}/{subscription.name}",
"clientId": self.client_id,
Expand Down
3 changes: 2 additions & 1 deletion src/casdoor/syncer.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ def get_syncer(self, syncer_id: str) -> Dict:

def modify_syncer(self, method: str, syncer: Syncer) -> Dict:
url = self.endpoint + f"/api/{method}"
syncer.owner = self.org_name
if syncer.owner == "":
syncer.owner = self.org_name
params = {
"id": f"{syncer.owner}/{syncer.name}",
"clientId": self.client_id,
Expand Down
3 changes: 2 additions & 1 deletion src/casdoor/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def get_token(self, token_id: str) -> Dict:

def modify_token(self, method: str, token: Token) -> Dict:
url = self.endpoint + f"/api/{method}"
token.owner = self.org_name
if token.owner == "":
token.owner = self.org_name
params = {
"id": f"{token.owner}/{token.name}",
"clientId": self.client_id,
Expand Down
3 changes: 2 additions & 1 deletion src/casdoor/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ def get_user_count(self, is_online: bool = None) -> int:

def modify_user(self, method: str, user: User) -> Dict:
url = self.endpoint + f"/api/{method}"
user.owner = self.org_name
if user.owner == "":
user.owner = self.org_name
params = {
"id": f"{user.owner}/{user.name}",
"clientId": self.client_id,
Expand Down
3 changes: 2 additions & 1 deletion src/casdoor/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ def get_webhook(self, webhook_id: str) -> Dict:

def modify_webhook(self, method: str, webhook: Webhook) -> Dict:
url = self.endpoint + f"/api/{method}"
webhook.owner = self.org_name
if webhook.owner == "":
webhook.owner = self.org_name
params = {
"id": f"{webhook.owner}/{webhook.name}",
"clientId": self.client_id,
Expand Down
Loading

0 comments on commit 0d0ad94

Please sign in to comment.