diff --git a/src/casdoor/application.py b/src/casdoor/application.py index e1777a8..7f176a6 100644 --- a/src/casdoor/application.py +++ b/src/casdoor/application.py @@ -96,6 +96,20 @@ def __init__(self): self.signinHtml = "" 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 + + def __str__(self): return str(self.__dict__) diff --git a/src/tests/test_application.py b/src/tests/test_application.py new file mode 100644 index 0000000..b9fe99c --- /dev/null +++ b/src/tests/test_application.py @@ -0,0 +1,74 @@ +# 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 unittest +import datetime + +from src.casdoor import CasdoorSDK +from src.casdoor.application import Application +from src.tests.test_util import * + + +class TestApplication(unittest.TestCase): + def setUp(self): + CasdoorSDK(TestEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestOrganization, TestApplication) + + def test_application(self): + name = get_random_name("application") + + # Add a new object + application = Application.new( + owner="admin", + name=name, + created_time=datetime.datetime.now().isoformat(), + display_name=name, + logo="https://cdn.casbin.org/img/casdoor-logo_1185x256.png", + homepage_url="https://casdoor.org", + description="Casdoor Website", + organization="casbin" + ) + + result, err = CasdoorSDK.add_application(application) + self.assertIsNone(err, f"Failed to add object: {err}") + + # Get all objects, check if our added object is inside the list + applications, err = CasdoorSDK.get_applications() + self.assertIsNone(err, f"Failed to get objects: {err}") + names = [item.name for item in applications] + self.assertIn(name, names, "Added object not found in list") + + # Get the object + retrieved_application, err = CasdoorSDK.get_application(name) + self.assertIsNone(err, f"Failed to get object: {err}") + self.assertEqual(retrieved_application.name, name) + + # Update the object + updated_description = "Updated Casdoor Website" + application.description = updated_description + result, err = CasdoorSDK.update_application(application) + self.assertIsNone(err, f"Failed to update object: {err}") + + # Validate the update + updated_application, err = CasdoorSDK.get_application(name) + self.assertIsNone(err, f"Failed to get updated object: {err}") + self.assertEqual(updated_application.description, updated_description) + + # Delete the object + result, err = CasdoorSDK.delete_application(application) + self.assertIsNone(err, f"Failed to delete object: {err}") + + # Validate the deletion + deleted_application, err = CasdoorSDK.get_application(name) + self.assertIsNone(err) + self.assertIsNone(deleted_application, "Failed to delete object, it's still retrievable") diff --git a/src/tests/test_util.py b/src/tests/test_util.py new file mode 100644 index 0000000..ddbf38f --- /dev/null +++ b/src/tests/test_util.py @@ -0,0 +1,60 @@ +# 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 random + +TestEndpoint = "https://demo.casdoor.com" +TestClientId = "294b09fbc17f95daf2fe" +TestClientSecret = "dd8982f7046ccba1bbd7851d5c1ece4e52bf039d" +TestOrganization = "casbin" +TestApplication = "app-vue-python-example" +TestJwtPublicKey = '''-----BEGIN CERTIFICATE----- +MIIE+TCCAuGgAwIBAgIDAeJAMA0GCSqGSIb3DQEBCwUAMDYxHTAbBgNVBAoTFENh +c2Rvb3IgT3JnYW5pemF0aW9uMRUwEwYDVQQDEwxDYXNkb29yIENlcnQwHhcNMjEx +MDE1MDgxMTUyWhcNNDExMDE1MDgxMTUyWjA2MR0wGwYDVQQKExRDYXNkb29yIE9y +Z2FuaXphdGlvbjEVMBMGA1UEAx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Cg54IL7XVLG +omKNNNcc8h1FCeKj/nnbGMhodnFWKDTsJcbNmcOPNHo6ixzqMy/Hqc+mWYv7maAG +Jtevs3qgMZ8F9Qzr3HpUc6R3ZYYWDY/xxPisuKftOPZgtH979XC4mdf0WPnOBLqL +2DJ1zaBmjiGJolvb7XNVKcUfDXYw85ZTZQ5b9clI4e+6bmyWqQItlwt+Ati/uFEV +XzCj70B4lALX6xau1kLEpV9O1GERizYRz5P9NJNA7KoO5AVMp9w0DQTkt+LbXnZE +HHnWKy8xHQKZF9sR7YBPGLs/Ac6tviv5Ua15OgJ/8dLRZ/veyFfGo2yZsI+hKVU5 +nCCJHBcAyFnm1hdvdwEdH33jDBjNB6ciotJZrf/3VYaIWSalADosHAgMWfXuWP+h +8XKXmzlxuHbTMQYtZPDgspS5aK+S4Q9wb8RRAYo= +-----END CERTIFICATE-----''' + + +def get_random_code(length): + std_nums = "0123456789" + result = ''.join(random.choice(std_nums) for _ in range(length)) + return result + + +def get_random_name(prefix): + return f"{prefix}_{get_random_code(6)}"