From 6c2f3f83eb943f47a0b3137000317fac8b015cf9 Mon Sep 17 00:00:00 2001 From: sjf Date: Fri, 15 Sep 2023 21:57:13 +0800 Subject: [PATCH 1/4] feat: add test cases for all objects and all opertions * feat:Add test for all object --- casdoorsdk/adapter_test.go | 93 ++++++++++++++++++++++++++++ casdoorsdk/application_test.go | 4 +- casdoorsdk/cert_test.go | 97 +++++++++++++++++++++++++++++ casdoorsdk/email_test.go | 31 ++++++++++ casdoorsdk/enforcer_test.go | 95 ++++++++++++++++++++++++++++ casdoorsdk/group_test.go | 92 +++++++++++++++++++++++++++ casdoorsdk/model_test.go | 106 ++++++++++++++++++++++++++++++++ casdoorsdk/organization_test.go | 101 ++++++++++++++++++++++++++++++ casdoorsdk/payment_test.go | 93 ++++++++++++++++++++++++++++ casdoorsdk/permission_test.go | 102 ++++++++++++++++++++++++++++++ casdoorsdk/plan_test.go | 93 ++++++++++++++++++++++++++++ casdoorsdk/pricing_test.go | 94 ++++++++++++++++++++++++++++ casdoorsdk/product_test.go | 100 ++++++++++++++++++++++++++++++ casdoorsdk/provider_test.go | 94 ++++++++++++++++++++++++++++ casdoorsdk/resource_test.go | 97 +++++++++++++++++++++++++++++ casdoorsdk/role_test.go | 93 ++++++++++++++++++++++++++++ casdoorsdk/session_test.go | 93 ++++++++++++++++++++++++++++ casdoorsdk/sms_test.go | 33 ++++++++++ casdoorsdk/subscription_test.go | 93 ++++++++++++++++++++++++++++ casdoorsdk/syncer_test.go | 100 ++++++++++++++++++++++++++++++ casdoorsdk/user_test.go | 93 ++++++++++++++++++++++++++++ casdoorsdk/webhook_test.go | 92 +++++++++++++++++++++++++++ 22 files changed, 1887 insertions(+), 2 deletions(-) create mode 100644 casdoorsdk/adapter_test.go create mode 100644 casdoorsdk/cert_test.go create mode 100644 casdoorsdk/email_test.go create mode 100644 casdoorsdk/enforcer_test.go create mode 100644 casdoorsdk/group_test.go create mode 100644 casdoorsdk/model_test.go create mode 100644 casdoorsdk/organization_test.go create mode 100644 casdoorsdk/payment_test.go create mode 100644 casdoorsdk/permission_test.go create mode 100644 casdoorsdk/plan_test.go create mode 100644 casdoorsdk/pricing_test.go create mode 100644 casdoorsdk/product_test.go create mode 100644 casdoorsdk/provider_test.go create mode 100644 casdoorsdk/resource_test.go create mode 100644 casdoorsdk/role_test.go create mode 100644 casdoorsdk/session_test.go create mode 100644 casdoorsdk/sms_test.go create mode 100644 casdoorsdk/subscription_test.go create mode 100644 casdoorsdk/syncer_test.go create mode 100644 casdoorsdk/user_test.go create mode 100644 casdoorsdk/webhook_test.go diff --git a/casdoorsdk/adapter_test.go b/casdoorsdk/adapter_test.go new file mode 100644 index 0000000..fb08953 --- /dev/null +++ b/casdoorsdk/adapter_test.go @@ -0,0 +1,93 @@ +// 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. + +package casdoorsdk + +import ( + "testing" + "time" +) + +func TestAdapter(t *testing.T) { + InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication) + + name := getRandomName("adapter") + + // Add a new object + adapter := &Adapter{ + Owner: "admin", + Name: name, + CreatedTime: time.Now().Format(time.RFC3339), + User: name, + Host: "https://casdoor.org", + } + _, err := AddAdapter(adapter) + if err != nil { + t.Fatalf("Failed to add object: %v", err) + } + + // Get all objects, check if our added object is inside the list + adapters, err := GetAdapters() + if err != nil { + t.Fatalf("Failed to get objects: %v", err) + } + found := false + for _, item := range adapters { + if item.Name == name { + found = true + break + } + } + if !found { + t.Fatalf("Added object not found in list") + } + + // Get the object + adapter, err = GetAdapter(name) + if err != nil { + t.Fatalf("Failed to get object: %v", err) + } + if adapter.Name != name { + t.Fatalf("Retrieved object does not match added object: %s != %s", adapter.Name, name) + } + + // Update the object + updatedUser := "Updated Casdoor Website" + adapter.User = updatedUser + _, err = UpdateAdapter(adapter) + if err != nil { + t.Fatalf("Failed to update object: %v", err) + } + + // Validate the update + updatedadapter, err := GetAdapter(name) + if err != nil { + t.Fatalf("Failed to get updated object: %v", err) + } + if updatedadapter.User != updatedUser { + t.Fatalf("Failed to update object, User mismatch: %s != %s", updatedadapter.User, updatedUser) + } + + // Delete the object + _, err = DeleteAdapter(adapter) + if err != nil { + t.Fatalf("Failed to delete object: %v", err) + } + + // Validate the deletion + deletedadapter, err := GetAdapter(name) + if err != nil || deletedadapter != nil { + t.Fatalf("Failed to delete object, it's still retrievable") + } +} diff --git a/casdoorsdk/application_test.go b/casdoorsdk/application_test.go index a960426..372d5c6 100644 --- a/casdoorsdk/application_test.go +++ b/casdoorsdk/application_test.go @@ -26,7 +26,7 @@ func TestApplication(t *testing.T) { // Add a new object application := &Application{ - Owner: "casbin", + Owner: "admin", Name: name, CreatedTime: time.Now().Format(time.RFC3339), DisplayName: name, @@ -90,7 +90,7 @@ func TestApplication(t *testing.T) { // Validate the deletion deletedApplication, err := GetApplication(name) - if err == nil || deletedApplication != nil { + if err != nil || deletedApplication != nil { t.Fatalf("Failed to delete object, it's still retrievable") } } diff --git a/casdoorsdk/cert_test.go b/casdoorsdk/cert_test.go new file mode 100644 index 0000000..f53f5c8 --- /dev/null +++ b/casdoorsdk/cert_test.go @@ -0,0 +1,97 @@ +// 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. + +package casdoorsdk + +import ( + "testing" + "time" +) + +func TestCert(t *testing.T) { + InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication) + + name := getRandomName("cert") + + // Add a new object + cert := &Cert{ + Owner: "admin", + Name: name, + CreatedTime: time.Now().Format(time.RFC3339), + DisplayName: name, + Scope: "JWT", + Type: "x509", + CryptoAlgorithm: "RS256", + BitSize: 4096, + ExpireInYears: 20, + } + _, err := AddCert(cert) + if err != nil { + t.Fatalf("Failed to add object: %v", err) + } + + // Get all objects, check if our added object is inside the list + certs, err := GetCerts() + if err != nil { + t.Fatalf("Failed to get objects: %v", err) + } + found := false + for _, item := range certs { + if item.Name == name { + found = true + break + } + } + if !found { + t.Fatalf("Added object not found in list") + } + + // Get the object + cert, err = GetCert(name) + if err != nil { + t.Fatalf("Failed to get object: %v", err) + } + if cert.Name != name { + t.Fatalf("Retrieved object does not match added object: %s != %s", cert.Name, name) + } + + // Update the object + updatedDisplayName := "Updated Casdoor Website" + cert.DisplayName = updatedDisplayName + _, err = UpdateCert(cert) + if err != nil { + t.Fatalf("Failed to update object: %v", err) + } + + // Validate the update + updatedcert, err := GetCert(name) + if err != nil { + t.Fatalf("Failed to get updated object: %v", err) + } + if updatedcert.DisplayName != updatedDisplayName { + t.Fatalf("Failed to update object, description mismatch: %s != %s", updatedcert.DisplayName, updatedDisplayName) + } + + // Delete the object + _, err = DeleteCert(cert) + if err != nil { + t.Fatalf("Failed to delete object: %v", err) + } + + // Validate the deletion + deletedcert, err := GetCert(name) + if err != nil || deletedcert != nil { + t.Fatalf("Failed to delete object, it's still retrievable") + } +} diff --git a/casdoorsdk/email_test.go b/casdoorsdk/email_test.go new file mode 100644 index 0000000..5da58a6 --- /dev/null +++ b/casdoorsdk/email_test.go @@ -0,0 +1,31 @@ +// 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. + +package casdoorsdk + +import ( + "testing" +) + +func TestEmail(t *testing.T) { + InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication) + + email := &emailForm{ + Title: "casbin", + Content: "casdoor-go-sdk website test", + Sender: "admin", + Receivers: []string{"TestSmtpServer"}, + } + SendEmail(email.Title, email.Content, email.Sender, email.Receivers...) +} diff --git a/casdoorsdk/enforcer_test.go b/casdoorsdk/enforcer_test.go new file mode 100644 index 0000000..1b4e633 --- /dev/null +++ b/casdoorsdk/enforcer_test.go @@ -0,0 +1,95 @@ +// 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. + +package casdoorsdk + +import ( + "testing" + "time" +) + +func TestEnforcer(t *testing.T) { + InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication) + + name := getRandomName("Enforcer") + + // Add a new object + enforcer := &Enforcer{ + Owner: "admin", + Name: name, + CreatedTime: time.Now().Format(time.RFC3339), + DisplayName: name, + Model: "built-in/user-model-built-in", + Adapter: "built-in/user-adapter-built-in", + Description: "Casdoor Website", + } + _, err := AddEnforcer(enforcer) + if err != nil { + t.Fatalf("Failed to add object: %v", err) + } + + // Get all objects, check if our added object is inside the list + Enforcers, err := GetEnforcers() + if err != nil { + t.Fatalf("Failed to get objects: %v", err) + } + found := false + for _, item := range Enforcers { + if item.Name == name { + found = true + break + } + } + if !found { + t.Fatalf("Added object not found in list") + } + + // Get the object + enforcer, err = GetEnforcer(name) + if err != nil { + t.Fatalf("Failed to get object: %v", err) + } + if enforcer.Name != name { + t.Fatalf("Retrieved object does not match added object: %s != %s", enforcer.Name, name) + } + + // Update the object + updatedDescription := "Updated Casdoor Website" + enforcer.Description = updatedDescription + _, err = UpdateEnforcer(enforcer) + if err != nil { + t.Fatalf("Failed to update object: %v", err) + } + + // Validate the update + updatedEnforcer, err := GetEnforcer(name) + if err != nil { + t.Fatalf("Failed to get updated object: %v", err) + } + if updatedEnforcer.Description != updatedDescription { + t.Fatalf("Failed to update object, description mismatch: %s != %s", updatedEnforcer.Description, updatedDescription) + } + + // Delete the object + _, err = DeleteEnforcer(enforcer) + if err != nil { + t.Fatalf("Failed to delete object: %v", err) + } + + // Validate the deletion + deletedEnforcer, err := GetEnforcer(name) + if err != nil || deletedEnforcer != nil { + t.Fatalf("Failed to delete object, it's still retrievable") + } +} diff --git a/casdoorsdk/group_test.go b/casdoorsdk/group_test.go new file mode 100644 index 0000000..47228d5 --- /dev/null +++ b/casdoorsdk/group_test.go @@ -0,0 +1,92 @@ +// 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. + +package casdoorsdk + +import ( + "testing" + "time" +) + +func TestGroup(t *testing.T) { + InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication) + + name := getRandomName("Group") + + // Add a new object + group := &Group{ + Owner: "admin", + Name: name, + CreatedTime: time.Now().Format(time.RFC3339), + DisplayName: name, + } + _, err := AddGroup(group) + if err != nil { + t.Fatalf("Failed to add object: %v", err) + } + + // Get all objects, check if our added object is inside the list + groups, err := GetGroups() + if err != nil { + t.Fatalf("Failed to get objects: %v", err) + } + found := false + for _, item := range groups { + if item.Name == name { + found = true + break + } + } + if !found { + t.Fatalf("Added object not found in list") + } + + // Get the object + group, err = GetGroup(name) + if err != nil { + t.Fatalf("Failed to get object: %v", err) + } + if group.Name != name { + t.Fatalf("Retrieved object does not match added object: %s != %s", group.Name, name) + } + + // Update the object + updatedDisplayName := "Updated Casdoor Website" + group.DisplayName = updatedDisplayName + _, err = UpdateGroup(group) + if err != nil { + t.Fatalf("Failed to update object: %v", err) + } + + // Validate the update + updatedGroup, err := GetGroup(name) + if err != nil { + t.Fatalf("Failed to get updated object: %v", err) + } + if updatedGroup.DisplayName != updatedDisplayName { + t.Fatalf("Failed to update object, description mismatch: %s != %s", updatedGroup.DisplayName, updatedDisplayName) + } + + // Delete the object + _, err = DeleteGroup(group) + if err != nil { + t.Fatalf("Failed to delete object: %v", err) + } + + // Validate the deletion + deletedGroup, err := GetGroup(name) + if err != nil || deletedGroup != nil { + t.Fatalf("Failed to delete object, it's still retrievable") + } +} diff --git a/casdoorsdk/model_test.go b/casdoorsdk/model_test.go new file mode 100644 index 0000000..a502160 --- /dev/null +++ b/casdoorsdk/model_test.go @@ -0,0 +1,106 @@ +// 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. + +package casdoorsdk + +import ( + "testing" + "time" +) + +func TestModel(t *testing.T) { + InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication) + + name := getRandomName("Model") + + // Add a new object + model := &Model{ + Owner: "casbin", + Name: name, + CreatedTime: time.Now().Format(time.RFC3339), + DisplayName: name, + ModelText: `[request_definition] +r = sub, obj, act + +[policy_definition] +p = sub, obj, act + +[role_definition] +g = _, _ + +[policy_effect] +e = some(where (p.eft == allow)) + +[matchers] +m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act`, + } + _, err := AddModel(model) + if err != nil { + t.Fatalf("Failed to add object: %v", err) + } + + // Get all objects, check if our added object is inside the list + models, err := GetModels() + if err != nil { + t.Fatalf("Failed to get objects: %v", err) + } + found := false + for _, item := range models { + if item.Name == name { + found = true + break + } + } + if !found { + t.Fatalf("Added object not found in list") + } + + // Get the object + model, err = GetModel(name) + if err != nil { + t.Fatalf("Failed to get object: %v", err) + } + if model.Name != name { + t.Fatalf("Retrieved object does not match added object: %s != %s", model.Name, name) + } + + // Update the object + updatedDisplayName := "UpdatedName" + model.DisplayName = updatedDisplayName + _, err = UpdateModel(model) + if err != nil { + t.Fatalf("Failed to update object: %v", err) + } + + // Validate the update + updatedModel, err := GetModel(name) + if err != nil { + t.Fatalf("Failed to get updated object: %v", err) + } + if updatedModel.DisplayName != updatedDisplayName { + t.Fatalf("Failed to update object, description mismatch: %s != %s", updatedModel.DisplayName, updatedDisplayName) + } + + // Delete the object + _, err = DeleteModel(model) + if err != nil { + t.Fatalf("Failed to delete object: %v", err) + } + + // Validate the deletion + deletedModel, err := GetModel(name) + if err != nil || deletedModel != nil { + t.Fatalf("Failed to delete object, it's still retrievable") + } +} diff --git a/casdoorsdk/organization_test.go b/casdoorsdk/organization_test.go new file mode 100644 index 0000000..33b23a3 --- /dev/null +++ b/casdoorsdk/organization_test.go @@ -0,0 +1,101 @@ +// 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. + +package casdoorsdk + +import ( + "testing" + "time" +) + +func TestOrganization(t *testing.T) { + InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication) + + name := getRandomName("Organization") + + // Add a new object + organization := &Organization{ + Owner: "casbin", + Name: name, + CreatedTime: time.Now().Format(time.RFC3339), + DisplayName: name, + WebsiteUrl: "https://example.com", + PasswordType: "plain", + PasswordOptions: []string{"AtLeast6"}, + CountryCodes: []string{"US", "ES", "FR", "DE", "GB", "CN", "JP", "KR", "VN", "ID", "SG", "IN"}, + Tags: []string{}, + Languages: []string{"en", "zh", "es", "fr", "de", "id", "ja", "ko", "ru", "vi", "pt"}, + InitScore: 2000, + EnableSoftDeletion: false, + IsProfilePublic: false, + } + _, err := AddOrganization(organization) + if err != nil { + t.Fatalf("Failed to add object: %v", err) + } + + // Get all objects, check if our added object is inside the list + organizations, err := GetOrganizations() + if err != nil { + t.Fatalf("Failed to get objects: %v", err) + } + found := false + for _, item := range organizations { + if item.Name == name { + found = true + break + } + } + if !found { + t.Fatalf("Added object not found in list") + } + + // Get the object + organization, err = GetOrganization(name) + if err != nil { + t.Fatalf("Failed to get object: %v", err) + } + if organization.Name != name { + t.Fatalf("Retrieved object does not match added object: %s != %s", organization.Name, name) + } + + // Update the object + updatedDisplayName := "Updated Casdoor Website" + organization.DisplayName = updatedDisplayName + _, err = UpdateOrganization(organization) + if err != nil { + t.Fatalf("Failed to update object: %v", err) + } + + // Validate the update + updatedOrganization, err := GetOrganization(name) + if err != nil { + t.Fatalf("Failed to get updated object: %v", err) + } + if updatedOrganization.DisplayName != updatedDisplayName { + t.Fatalf("Failed to update object, description mismatch: %s != %s", updatedOrganization.DisplayName, updatedDisplayName) + } + + // Delete the object + _, err = DeleteOrganization(name) + if err != nil { + t.Fatalf("Failed to delete object: %v", err) + } + + // Validate the deletion + deletedOrganization, err := GetOrganization(name) + if err != nil || deletedOrganization != nil { + t.Fatalf("Failed to delete object, it's still retrievable") + } +} diff --git a/casdoorsdk/payment_test.go b/casdoorsdk/payment_test.go new file mode 100644 index 0000000..9fe4a90 --- /dev/null +++ b/casdoorsdk/payment_test.go @@ -0,0 +1,93 @@ +// 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. + +package casdoorsdk + +import ( + "testing" + "time" +) + +func TestPayment(t *testing.T) { + InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication) + + name := getRandomName("Payment") + + // Add a new object + payment := &Payment{ + Owner: "admin", + Name: name, + CreatedTime: time.Now().Format(time.RFC3339), + DisplayName: name, + ProductName: "casbin", + } + _, err := AddPayment(payment) + if err != nil { + t.Fatalf("Failed to add object: %v", err) + } + + // Get all objects, check if our added object is inside the list + payments, err := GetPayments() + if err != nil { + t.Fatalf("Failed to get objects: %v", err) + } + found := false + for _, item := range payments { + if item.Name == name { + found = true + break + } + } + if !found { + t.Fatalf("Added object not found in list") + } + + // Get the object + payment, err = GetPayment(name) + if err != nil { + t.Fatalf("Failed to get object: %v", err) + } + if payment.Name != name { + t.Fatalf("Retrieved object does not match added object: %s != %s", payment.Name, name) + } + + // Update the object + updatedProductName := "Updated Casdoor Website" + payment.ProductName = updatedProductName + _, err = UpdatePayment(payment) + if err != nil { + t.Fatalf("Failed to update object: %v", err) + } + + // Validate the update + updatedPayment, err := GetPayment(name) + if err != nil { + t.Fatalf("Failed to get updated object: %v", err) + } + if updatedPayment.ProductName != updatedProductName { + t.Fatalf("Failed to update object, description mismatch: %s != %s", updatedPayment.ProductName, updatedProductName) + } + + // Delete the object + _, err = DeletePayment(payment) + if err != nil { + t.Fatalf("Failed to delete object: %v", err) + } + + // Validate the deletion + deletedPayment, err := GetPayment(name) + if err != nil || deletedPayment != nil { + t.Fatalf("Failed to delete object, it's still retrievable") + } +} diff --git a/casdoorsdk/permission_test.go b/casdoorsdk/permission_test.go new file mode 100644 index 0000000..7dc5d3f --- /dev/null +++ b/casdoorsdk/permission_test.go @@ -0,0 +1,102 @@ +// 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. + +package casdoorsdk + +import ( + "testing" + "time" +) + +func TestPermission(t *testing.T) { + InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication) + + name := getRandomName("Permission") + + // Add a new object + permission := &Permission{ + Owner: "casbin", + Name: name, + CreatedTime: time.Now().Format(time.RFC3339), + DisplayName: name, + Description: "Casdoor Website", + Users: []string{"casbin/*"}, + Roles: []string{}, + Domains: []string{}, + Model: "model-casbin", + ResourceType: "Application", + Resources: []string{"app-casbin"}, + Actions: []string{"Read","Write"}, + Effect: "Allow", + IsEnabled: true, + } + _, err := AddPermission(permission) + if err != nil { + t.Fatalf("Failed to add object: %v", err) + } + + // Get all objects, check if our added object is inside the list + permissions, err := GetPermissions() + if err != nil { + t.Fatalf("Failed to get objects: %v", err) + } + found := false + for _, item := range permissions { + if item.Name == name { + found = true + break + } + } + if !found { + t.Fatalf("Added object not found in list") + } + + // Get the object + permission, err = GetPermission(name) + if err != nil { + t.Fatalf("Failed to get object: %v", err) + } + if permission.Name != name { + t.Fatalf("Retrieved object does not match added object: %s != %s", permission.Name, name) + } + + // Update the object + updatedDescription := "Updated Casdoor Website" + permission.Description = updatedDescription + _, err = UpdatePermission(permission) + if err != nil { + t.Fatalf("Failed to update object: %v", err) + } + + // Validate the update + updatedPermission, err := GetPermission(name) + if err != nil { + t.Fatalf("Failed to get updated object: %v", err) + } + if updatedPermission.Description != updatedDescription { + t.Fatalf("Failed to update object, description mismatch: %s != %s", updatedPermission.Description, updatedDescription) + } + + // Delete the object + _, err = DeletePermission(permission) + if err != nil { + t.Fatalf("Failed to delete object: %v", err) + } + + // Validate the deletion + deletedPermission, err := GetPermission(name) + if err != nil || deletedPermission != nil { + t.Fatalf("Failed to delete object, it's still retrievable") + } +} diff --git a/casdoorsdk/plan_test.go b/casdoorsdk/plan_test.go new file mode 100644 index 0000000..082c903 --- /dev/null +++ b/casdoorsdk/plan_test.go @@ -0,0 +1,93 @@ +// 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. + +package casdoorsdk + +import ( + "testing" + "time" +) + +func TestPlan(t *testing.T) { + InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication) + + name := getRandomName("Plan") + + // Add a new object + plan := &Plan{ + Owner: "admin", + Name: name, + CreatedTime: time.Now().Format(time.RFC3339), + DisplayName: name, + Description: "Casdoor Website", + } + _, err := AddPlan(plan) + if err != nil { + t.Fatalf("Failed to add object: %v", err) + } + + // Get all objects, check if our added object is inside the list + Plans, err := GetPlans() + if err != nil { + t.Fatalf("Failed to get objects: %v", err) + } + found := false + for _, item := range Plans { + if item.Name == name { + found = true + break + } + } + if !found { + t.Fatalf("Added object not found in list") + } + + // Get the object + plan, err = GetPlan(name) + if err != nil { + t.Fatalf("Failed to get object: %v", err) + } + if plan.Name != name { + t.Fatalf("Retrieved object does not match added object: %s != %s", plan.Name, name) + } + + // Update the object + updatedDescription := "Updated Casdoor Website" + plan.Description = updatedDescription + _, err = UpdatePlan(plan) + if err != nil { + t.Fatalf("Failed to update object: %v", err) + } + + // Validate the update + updatedPlan, err := GetPlan(name) + if err != nil { + t.Fatalf("Failed to get updated object: %v", err) + } + if updatedPlan.Description != updatedDescription { + t.Fatalf("Failed to update object, description mismatch: %s != %s", updatedPlan.Description, updatedDescription) + } + + // Delete the object + _, err = DeletePlan(plan) + if err != nil { + t.Fatalf("Failed to delete object: %v", err) + } + + // Validate the deletion + deletedPlan, err := GetPlan(name) + if err == nil || deletedPlan != nil { + t.Fatalf("Failed to delete object, it's still retrievable") + } +} diff --git a/casdoorsdk/pricing_test.go b/casdoorsdk/pricing_test.go new file mode 100644 index 0000000..1391dc9 --- /dev/null +++ b/casdoorsdk/pricing_test.go @@ -0,0 +1,94 @@ +// 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. + +package casdoorsdk + +import ( + "testing" + "time" +) + +func TestPricing(t *testing.T) { + InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication) + + name := getRandomName("Pricing") + + // Add a new object + pricing := &Pricing{ + Owner: "admin", + Name: name, + CreatedTime: time.Now().Format(time.RFC3339), + DisplayName: name, + Application: "app-admin", + Description: "Casdoor Website", + } + _, err := AddPricing(pricing) + if err != nil { + t.Fatalf("Failed to add object: %v", err) + } + + // Get all objects, check if our added object is inside the list + pricings, err := GetPricings() + if err != nil { + t.Fatalf("Failed to get objects: %v", err) + } + found := false + for _, item := range pricings { + if item.Name == name { + found = true + break + } + } + if !found { + t.Fatalf("Added object not found in list") + } + + // Get the object + pricing, err = GetPricing(name) + if err != nil { + t.Fatalf("Failed to get object: %v", err) + } + if pricing.Name != name { + t.Fatalf("Retrieved object does not match added object: %s != %s", pricing.Name, name) + } + + // Update the object + updatedDescription := "Updated Casdoor Website" + pricing.Description = updatedDescription + _, err = UpdatePricing(pricing) + if err != nil { + t.Fatalf("Failed to update object: %v", err) + } + + // Validate the update + updatedPricing, err := GetPricing(name) + if err != nil { + t.Fatalf("Failed to get updated object: %v", err) + } + if updatedPricing.Description != updatedDescription { + t.Fatalf("Failed to update object, description mismatch: %s != %s", updatedPricing.Description, updatedDescription) + } + + // Delete the object + _, err = DeletePricing(pricing) + if err != nil { + t.Fatalf("Failed to delete object: %v", err) + } + + // Validate the deletion + deletedPricing, err := GetPricing(name) + if err == nil || deletedPricing != nil { + t.Fatalf("Failed to delete object, it's still retrievable") + } +} diff --git a/casdoorsdk/product_test.go b/casdoorsdk/product_test.go new file mode 100644 index 0000000..d306cdb --- /dev/null +++ b/casdoorsdk/product_test.go @@ -0,0 +1,100 @@ +// 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. + +package casdoorsdk + +import ( + "testing" + "time" +) + +func TestProduct(t *testing.T) { + InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication) + + name := getRandomName("Product") + + // Add a new object + product := &Product{ + Owner: "admin", + Name: name, + CreatedTime: time.Now().Format(time.RFC3339), + DisplayName: name, + + Image: "https://cdn.casbin.org/img/casdoor-logo_1185x256.png", + Description: "Casdoor Website", + Tag: "auto_created_product_for_plan", + + Quantity: 999, + Sold: 0, + State: "Published", + } + _, err := AddProduct(product) + if err != nil { + t.Fatalf("Failed to add object: %v", err) + } + + // Get all objects, check if our added object is inside the list + products, err := GetProducts() + if err != nil { + t.Fatalf("Failed to get objects: %v", err) + } + found := false + for _, item := range products { + if item.Name == name { + found = true + break + } + } + if !found { + t.Fatalf("Added object not found in list") + } + + // Get the object + product, err = GetProduct(name) + if err != nil { + t.Fatalf("Failed to get object: %v", err) + } + if product.Name != name { + t.Fatalf("Retrieved object does not match added object: %s != %s", product.Name, name) + } + + // Update the object + updatedDescription := "Updated Casdoor Website" + product.Description = updatedDescription + _, err = UpdateProduct(product) + if err != nil { + t.Fatalf("Failed to update object: %v", err) + } + + // Validate the update + updatedProduct, err := GetProduct(name) + if err != nil { + t.Fatalf("Failed to get updated object: %v", err) + } + if updatedProduct.Description != updatedDescription { + t.Fatalf("Failed to update object, description mismatch: %s != %s", updatedProduct.Description, updatedDescription) + } + + // Delete the object + _, err = DeleteProduct(product) + if err != nil { + t.Fatalf("Failed to delete object: %v", err) + } + + // Validate the deletion + deletedProduct, err := GetProduct(name) + if err != nil || deletedProduct != nil { + t.Fatalf("Failed to delete object, it's still retrievable") + } +} diff --git a/casdoorsdk/provider_test.go b/casdoorsdk/provider_test.go new file mode 100644 index 0000000..c6dc76d --- /dev/null +++ b/casdoorsdk/provider_test.go @@ -0,0 +1,94 @@ +// 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. + +package casdoorsdk + +import ( + "testing" + "time" +) + +func TestProvider(t *testing.T) { + InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication) + + name := getRandomName("Provider") + + // Add a new object + provider := &Provider{ + Owner: "admin", + Name: name, + CreatedTime: time.Now().Format(time.RFC3339), + DisplayName: name, + Category: "Captcha", + Type: "Default", + } + _, err := AddProvider(provider) + if err != nil { + t.Fatalf("Failed to add object: %v", err) + } + + // Get all objects, check if our added object is inside the list + providers, err := GetProviders() + if err != nil { + t.Fatalf("Failed to get objects: %v", err) + } + found := false + for _, item := range providers { + if item.Name == name { + found = true + break + } + } + if !found { + t.Fatalf("Added object not found in list") + } + + // Get the object + provider, err = GetProvider(name) + if err != nil { + t.Fatalf("Failed to get object: %v", err) + } + if provider.Name != name { + t.Fatalf("Retrieved object does not match added object: %s != %s", provider.Name, name) + } + + // Update the object + updatedDisplayName := "Updated Casdoor Website" + provider.DisplayName = updatedDisplayName + _, err = UpdateProvider(provider) + if err != nil { + t.Fatalf("Failed to update object: %v", err) + } + + // Validate the update + updatedProvider, err := GetProvider(name) + if err != nil { + t.Fatalf("Failed to get updated object: %v", err) + } + if updatedProvider.DisplayName != updatedDisplayName { + t.Fatalf("Failed to update object, DisplayName mismatch: %s != %s", updatedProvider.DisplayName, updatedDisplayName) + } + + // Delete the object + _, err = DeleteProvider(provider) + if err != nil { + t.Fatalf("Failed to delete object: %v", err) + } + + // Validate the deletion + deletedProvider, err := GetProvider(name) + if err != nil || deletedProvider != nil { + t.Fatalf("Failed to delete object, it's still retrievable") + } +} diff --git a/casdoorsdk/resource_test.go b/casdoorsdk/resource_test.go new file mode 100644 index 0000000..23dfb5a --- /dev/null +++ b/casdoorsdk/resource_test.go @@ -0,0 +1,97 @@ +// 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. + +package casdoorsdk + +import ( + "fmt" + "io" + "os" + "testing" + "time" +) + +func (resource *Resource) GetId() string { + return fmt.Sprintf("%s/%s",resource.Owner, resource.Name) +} + +func TestResource(t *testing.T) { + InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication) + + filename := "resource.go" + file, err := os.Open(filename) + + if err != nil { + t.Fatalf("Failed to open the file: %v\n", err) + } + defer file.Close() + data, err := io.ReadAll(file) + if err != nil { + t.Fatalf("Failed to read data from the file: %v\n", err) + } + + name := fmt.Sprintf("/casdoor/%s",filename) + // Add a new object + resource := &Resource{ + Owner: "casbin", + Name: name, + CreatedTime: time.Now().Format(time.RFC3339), + Description: "Casdoor Website", + User: "casbin", + FileName: filename, + FileSize: len(data), + Tag: name, + } + _, _, err = UploadResource(resource.User, resource.Tag, "" ,resource.FileName ,data) + if err != nil { + t.Fatalf("Failed to add object: %v", err) + } + + // Get all objects, check if our added object is inside the list + Resources, err := GetResources(resource.Owner, resource.User, "" , "","", "") + if err != nil { + t.Fatalf("Failed to get objects: %v", err) + } + found := false + for _, item := range Resources { + if item.Tag == name { + found = true + break + } + } + if !found { + t.Fatalf("Added object not found in list") + } + + // Get the object + resource, err = GetResource(resource.GetId()) + if err != nil { + t.Fatalf("Failed to get object: %v", err) + } + if resource.Tag != name { + t.Fatalf("Retrieved object does not match added object: %s != %s", resource.Name, name) + } + + // Delete the object + _, err = DeleteResource(name) + if err != nil { + t.Fatalf("Failed to delete object: %v", err) + } + + // Validate the deletion + deletedResource, err := GetResource(name) + if err != nil || deletedResource != nil { + t.Fatalf("Failed to delete object, it's still retrievable") + } +} diff --git a/casdoorsdk/role_test.go b/casdoorsdk/role_test.go new file mode 100644 index 0000000..0ab7945 --- /dev/null +++ b/casdoorsdk/role_test.go @@ -0,0 +1,93 @@ +// 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. + +package casdoorsdk + +import ( + "testing" + "time" +) + +func TestRole(t *testing.T) { + InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication) + + name := getRandomName("Role") + + // Add a new object + role := &Role{ + Owner: "admin", + Name: name, + CreatedTime: time.Now().Format(time.RFC3339), + DisplayName: name, + Description: "Casdoor Website", + } + _, err := AddRole(role) + if err != nil { + t.Fatalf("Failed to add object: %v", err) + } + + // Get all objects, check if our added object is inside the list + roles, err := GetRoles() + if err != nil { + t.Fatalf("Failed to get objects: %v", err) + } + found := false + for _, item := range roles { + if item.Name == name { + found = true + break + } + } + if !found { + t.Fatalf("Added object not found in list") + } + + // Get the object + role, err = GetRole(name) + if err != nil { + t.Fatalf("Failed to get object: %v", err) + } + if role.Name != name { + t.Fatalf("Retrieved object does not match added object: %s != %s", role.Name, name) + } + + // Update the object + updatedDescription := "Updated Casdoor Website" + role.Description = updatedDescription + _, err = UpdateRole(role) + if err != nil { + t.Fatalf("Failed to update object: %v", err) + } + + // Validate the update + updatedRole, err := GetRole(name) + if err != nil { + t.Fatalf("Failed to get updated object: %v", err) + } + if updatedRole.Description != updatedDescription { + t.Fatalf("Failed to update object, description mismatch: %s != %s", updatedRole.Description, updatedDescription) + } + + // Delete the object + _, err = DeleteRole(role) + if err != nil { + t.Fatalf("Failed to delete object: %v", err) + } + + // Validate the deletion + deletedRole, err := GetRole(name) + if err != nil || deletedRole != nil { + t.Fatalf("Failed to delete object, it's still retrievable") + } +} diff --git a/casdoorsdk/session_test.go b/casdoorsdk/session_test.go new file mode 100644 index 0000000..47af76d --- /dev/null +++ b/casdoorsdk/session_test.go @@ -0,0 +1,93 @@ +// 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. + +package casdoorsdk + +import ( + "testing" + "time" +) + +func TestSession(t *testing.T) { + InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication) + + name := getRandomName("Session") + + // Add a new object + Session := &Session{ + Owner: "casbin", + Name: name, + CreatedTime: time.Now().Format(time.RFC3339), + Application: "app-built-in", + SessionId: []string{}, + } + _, err := AddSession(Session) + if err != nil { + t.Fatalf("Failed to add object: %v", err) + } + + // Get all objects, check if our added object is inside the list + Sessions, err := GetSessions() + if err != nil { + t.Fatalf("Failed to get objects: %v", err) + } + found := false + for _, item := range Sessions { + if item.Name == name { + found = true + break + } + } + if !found { + t.Fatalf("Added object not found in list") + } + + // Get the object + Session, err = GetSession(name, Session.Application) + if err != nil { + t.Fatalf("Failed to get object: %v", err) + } + if Session.Name != name { + t.Fatalf("Retrieved object does not match added object: %s != %s", Session.Name, name) + } + + // Update the object + UpdateTime := time.Now().Format(time.RFC3339) + Session.CreatedTime = UpdateTime + _, err = UpdateSession(Session) + if err != nil { + t.Fatalf("Failed to update object: %v", err) + } + + // Validate the update + updatedSession, err := GetSession(name, Session.Application) + if err != nil { + t.Fatalf("Failed to get updated object: %v", err) + } + if updatedSession.CreatedTime != UpdateTime { + t.Fatalf("Failed to update object, Application mismatch: %s != %s", updatedSession.CreatedTime, UpdateTime) + } + + // Delete the object + _, err = DeleteSession(Session) + if err != nil { + t.Fatalf("Failed to delete object: %v", err) + } + + // Validate the deletion + deletedSession, err := GetSession(name, Session.Application) + if err != nil || deletedSession != nil { + t.Fatalf("Failed to delete object, it's still retrievable") + } +} diff --git a/casdoorsdk/sms_test.go b/casdoorsdk/sms_test.go new file mode 100644 index 0000000..717d280 --- /dev/null +++ b/casdoorsdk/sms_test.go @@ -0,0 +1,33 @@ +// 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. + +package casdoorsdk + +import ( + "testing" +) + +func TestSms(t *testing.T) { + InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication) + + sms := &smsForm{ + Content: "casdoor", + Receivers: []string{"+8613854673829", "+441932567890"}, + } + err := SendSms(sms.Content, sms.Receivers...) + if err != nil { + t.Fatalf("Failed to send sms: %v", err) + } + +} diff --git a/casdoorsdk/subscription_test.go b/casdoorsdk/subscription_test.go new file mode 100644 index 0000000..9843eb2 --- /dev/null +++ b/casdoorsdk/subscription_test.go @@ -0,0 +1,93 @@ +// 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. + +package casdoorsdk + +import ( + "testing" + "time" +) + +func TestSubscription(t *testing.T) { + InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication) + + name := getRandomName("Subscription") + + // Add a new object + subscription := &Subscription{ + Owner: "admin", + Name: name, + CreatedTime: time.Now().Format(time.RFC3339), + DisplayName: name, + Description: "Casdoor Website", + } + _, err := AddSubscription(subscription) + if err != nil { + t.Fatalf("Failed to add object: %v", err) + } + + // Get all objects, check if our added object is inside the list + subscriptions, err := GetSubscriptions() + if err != nil { + t.Fatalf("Failed to get objects: %v", err) + } + found := false + for _, item := range subscriptions { + if item.Name == name { + found = true + break + } + } + if !found { + t.Fatalf("Added object not found in list") + } + + // Get the object + subscription, err = GetSubscription(name) + if err != nil { + t.Fatalf("Failed to get object: %v", err) + } + if subscription.Name != name { + t.Fatalf("Retrieved object does not match added object: %s != %s", subscription.Name, name) + } + + // Update the object + updatedDescription := "Updated Casdoor Website" + subscription.Description = updatedDescription + _, err = UpdateSubscription(subscription) + if err != nil { + t.Fatalf("Failed to update object: %v", err) + } + + // Validate the update + updatedSubscription, err := GetSubscription(name) + if err != nil { + t.Fatalf("Failed to get updated object: %v", err) + } + if updatedSubscription.Description != updatedDescription { + t.Fatalf("Failed to update object, description mismatch: %s != %s", updatedSubscription.Description, updatedDescription) + } + + // Delete the object + _, err = DeleteSubscription(subscription) + if err != nil { + t.Fatalf("Failed to delete object: %v", err) + } + + // Validate the deletion + deletedSubscription, err := GetSubscription(name) + if err != nil || deletedSubscription != nil { + t.Fatalf("Failed to delete object, it's still retrievable") + } +} diff --git a/casdoorsdk/syncer_test.go b/casdoorsdk/syncer_test.go new file mode 100644 index 0000000..2ecffc9 --- /dev/null +++ b/casdoorsdk/syncer_test.go @@ -0,0 +1,100 @@ +// 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. + +package casdoorsdk + +import ( + "testing" + "time" +) + +func TestSyncer(t *testing.T) { + InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication) + + name := getRandomName("Syncer") + + // Add a new object + Syncer := &Syncer{ + Owner: "admin", + Name: name, + CreatedTime: time.Now().Format(time.RFC3339), + Organization: "casbin", + Host: "localhost", + Port: 3306, + User: "root", + Password: "123", + DatabaseType: "mysql", + Database: "syncer_db", + Table: "user-table", + SyncInterval: 1, + } + _, err := AddSyncer(Syncer) + if err != nil { + t.Fatalf("Failed to add object: %v", err) + } + + // Get all objects, check if our added object is inside the list + Syncers, err := GetSyncers() + if err != nil { + t.Fatalf("Failed to get objects: %v", err) + } + found := false + for _, item := range Syncers { + if item.Name == name { + found = true + break + } + } + if !found { + t.Fatalf("Added object not found in list") + } + + // Get the object + Syncer, err = GetSyncer(name) + if err != nil { + t.Fatalf("Failed to get object: %v", err) + } + if Syncer.Name != name { + t.Fatalf("Retrieved object does not match added object: %s != %s", Syncer.Name, name) + } + + // Update the object + updatedPassword := "123456" + Syncer.Password = updatedPassword + _, err = UpdateSyncer(Syncer) + if err != nil { + t.Fatalf("Failed to update object: %v", err) + } + + // Validate the update + updatedSyncer, err := GetSyncer(name) + if err != nil { + t.Fatalf("Failed to get updated object: %v", err) + } + if updatedSyncer.Password != updatedPassword { + t.Fatalf("Failed to update object, description mismatch: %s != %s", updatedSyncer.Password, updatedPassword) + } + + // Delete the object + _, err = DeleteSyncer(Syncer) + if err != nil { + t.Fatalf("Failed to delete object: %v", err) + } + + // Validate the deletion + deletedSyncer, err := GetSyncer(name) + if err != nil || deletedSyncer != nil { + t.Fatalf("Failed to delete object, it's still retrievable") + } +} diff --git a/casdoorsdk/user_test.go b/casdoorsdk/user_test.go new file mode 100644 index 0000000..aab9ce6 --- /dev/null +++ b/casdoorsdk/user_test.go @@ -0,0 +1,93 @@ +// 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. + +package casdoorsdk + +import ( + "testing" + "time" +) + +func TestUser(t *testing.T) { + InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication) + + name := getRandomName("User") + + // Add a new object + user := &User{ + Owner: "admin", + Name: name, + CreatedTime: time.Now().Format(time.RFC3339), + + DisplayName: name, + } + _, err := AddUser(user) + if err != nil { + t.Fatalf("Failed to add object: %v", err) + } + + // Get all objects, check if our added object is inside the list + users, err := GetUsers() + if err != nil { + t.Fatalf("Failed to get objects: %v", err) + } + found := false + for _, item := range users { + if item.Name == name { + found = true + break + } + } + if !found { + t.Fatalf("Added object not found in list") + } + + // Get the object + user, err = GetUser(name) + if err != nil { + t.Fatalf("Failed to get object: %v", err) + } + if user.Name != name { + t.Fatalf("Retrieved object does not match added object: %s != %s", user.Name, name) + } + + // Update the object + updatedDisplayName := "Updated Casdoor Website" + user.DisplayName = updatedDisplayName + _, err = UpdateUser(user) + if err != nil { + t.Fatalf("Failed to update object: %v", err) + } + + // Validate the update + updatedUser, err := GetUser(name) + if err != nil { + t.Fatalf("Failed to get updated object: %v", err) + } + if updatedUser.DisplayName != updatedDisplayName { + t.Fatalf("Failed to update object, description mismatch: %s != %s", updatedUser.DisplayName, updatedDisplayName) + } + + // Delete the object + _, err = DeleteUser(user) + if err != nil { + t.Fatalf("Failed to delete object: %v", err) + } + + // Validate the deletion + deletedUser, err := GetUser(name) + if err != nil || deletedUser != nil { + t.Fatalf("Failed to delete object, it's still retrievable") + } +} diff --git a/casdoorsdk/webhook_test.go b/casdoorsdk/webhook_test.go new file mode 100644 index 0000000..298883e --- /dev/null +++ b/casdoorsdk/webhook_test.go @@ -0,0 +1,92 @@ +// 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. + +package casdoorsdk + +import ( + "testing" + "time" +) + +func TestWebhook(t *testing.T) { + InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication) + + name := getRandomName("Webhook") + + // Add a new object + Webhook := &Webhook{ + Owner: "casbin", + Name: name, + CreatedTime: time.Now().Format(time.RFC3339), + Organization: "casbin", + } + _, err := AddWebhook(Webhook) + if err != nil { + t.Fatalf("Failed to add object: %v", err) + } + + // Get all objects, check if our added object is inside the list + Webhooks, err := GetWebhooks() + if err != nil { + t.Fatalf("Failed to get objects: %v", err) + } + found := false + for _, item := range Webhooks { + if item.Name == name { + found = true + break + } + } + if !found { + t.Fatalf("Added object not found in list") + } + + // Get the object + Webhook, err = GetWebhook(name) + if err != nil { + t.Fatalf("Failed to get object: %v", err) + } + if Webhook.Name != name { + t.Fatalf("Retrieved object does not match added object: %s != %s", Webhook.Name, name) + } + + // Update the object + updatedOrganization := "admin" + Webhook.Organization = updatedOrganization + _, err = UpdateWebhook(Webhook) + if err != nil { + t.Fatalf("Failed to update object: %v", err) + } + + // Validate the update + updatedWebhook, err := GetWebhook(name) + if err != nil { + t.Fatalf("Failed to get updated object: %v", err) + } + if updatedWebhook.Organization != updatedOrganization { + t.Fatalf("Failed to update object, Port mismatch: %s != %s", updatedWebhook.Organization, updatedOrganization) + } + + // Delete the object + _, err = DeleteWebhook(Webhook) + if err != nil { + t.Fatalf("Failed to delete object: %v", err) + } + + // Validate the deletion + deletedWebhook, err := GetWebhook(name) + if err != nil || deletedWebhook != nil { + t.Fatalf("Failed to delete object, it's still retrievable") + } +} From 50aa926b27f0c40900ec59ac6bdba0303dfdd1dc Mon Sep 17 00:00:00 2001 From: sjf Date: Fri, 15 Sep 2023 21:58:22 +0800 Subject: [PATCH 2/4] fix: fix some api and function error * feat:Add GetCert api * fix:Move enfocer.go -> enforcer.go * fix:Add ModelText to struct Model * fix:fix func DeleteOrganization * fix:fix func GetOrganization * fix:fix func GetSession --- casdoorsdk/cert_global.go | 4 ++++ casdoorsdk/{enfocer.go => enforcer.go} | 0 casdoorsdk/model.go | 1 + casdoorsdk/organization.go | 2 +- casdoorsdk/organization_global.go | 4 ++-- casdoorsdk/session.go | 4 ++-- casdoorsdk/session_global.go | 4 ++-- 7 files changed, 12 insertions(+), 7 deletions(-) rename casdoorsdk/{enfocer.go => enforcer.go} (100%) diff --git a/casdoorsdk/cert_global.go b/casdoorsdk/cert_global.go index bd2356b..0ffcdcd 100644 --- a/casdoorsdk/cert_global.go +++ b/casdoorsdk/cert_global.go @@ -22,6 +22,10 @@ func GetCerts() ([]*Cert, error) { return globalClient.GetCerts() } +func GetCert(name string) (*Cert, error) { + return globalClient.GetCert(name) +} + func UpdateCert(cert *Cert) (bool, error) { return globalClient.UpdateCert(cert) } diff --git a/casdoorsdk/enfocer.go b/casdoorsdk/enforcer.go similarity index 100% rename from casdoorsdk/enfocer.go rename to casdoorsdk/enforcer.go diff --git a/casdoorsdk/model.go b/casdoorsdk/model.go index be293a5..b84d1bd 100644 --- a/casdoorsdk/model.go +++ b/casdoorsdk/model.go @@ -39,6 +39,7 @@ type Model struct { Key string `json:"key,omitempty"` Children []*Model `json:"children,omitempty"` + ModelText string `xorm:"mediumtext" json:"modelText"` IsEnabled bool `json:"isEnabled"` } diff --git a/casdoorsdk/organization.go b/casdoorsdk/organization.go index feb33c6..4ecc284 100644 --- a/casdoorsdk/organization.go +++ b/casdoorsdk/organization.go @@ -136,7 +136,7 @@ func (c *Client) AddOrganization(organization *Organization) (bool, error) { func (c *Client) DeleteOrganization(name string) (bool, error) { organization := Organization{ - Owner: "admin", + Owner: c.OrganizationName, Name: name, } diff --git a/casdoorsdk/organization_global.go b/casdoorsdk/organization_global.go index dd6e4a5..4267030 100644 --- a/casdoorsdk/organization_global.go +++ b/casdoorsdk/organization_global.go @@ -14,8 +14,8 @@ package casdoorsdk -func GetOrganization(name string) ([]*Organization, error) { - return globalClient.GetOrganizations() +func GetOrganization(name string) (*Organization, error) { + return globalClient.GetOrganization(name) } func GetOrganizations() ([]*Organization, error) { diff --git a/casdoorsdk/session.go b/casdoorsdk/session.go index 2da3e28..15e9812 100644 --- a/casdoorsdk/session.go +++ b/casdoorsdk/session.go @@ -74,9 +74,9 @@ func (c *Client) GetPaginationSessions(p int, pageSize int, queryMap map[string] return sessions, int(response.Data2.(float64)), nil } -func (c *Client) GetSession(name string) (*Session, error) { +func (c *Client) GetSession(name string, application string) (*Session, error) { queryMap := map[string]string{ - "id": fmt.Sprintf("%s/%s", c.OrganizationName, name), + "sessionPkId": fmt.Sprintf("%s/%s/%s", c.OrganizationName, name, application), } url := c.GetUrl("get-session", queryMap) diff --git a/casdoorsdk/session_global.go b/casdoorsdk/session_global.go index e0d2e0f..68266f9 100644 --- a/casdoorsdk/session_global.go +++ b/casdoorsdk/session_global.go @@ -22,8 +22,8 @@ func GetPaginationSessions(p int, pageSize int, queryMap map[string]string) ([]* return globalClient.GetPaginationSessions(p, pageSize, queryMap) } -func GetSession(name string) (*Session, error) { - return globalClient.GetSession(name) +func GetSession(name string, application string) (*Session, error) { + return globalClient.GetSession(name, application) } func UpdateSession(session *Session) (bool, error) { From cc0c4dfeeaa09d15c7dbdcdb14d8829605916596 Mon Sep 17 00:00:00 2001 From: sjf Date: Fri, 15 Sep 2023 22:51:44 +0800 Subject: [PATCH 3/4] fix: update ci.yaml * fix:Update ci.yaml move go-version from 1.15 to 1.16 --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5a20ded..e9754b0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,7 +13,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.15 + go-version: 1.16 - uses: actions/checkout@v2 - name: Run Unit tests From 2b2fffce4aecd20e200fc0db75c588642ef85502 Mon Sep 17 00:00:00 2001 From: sjf Date: Fri, 15 Sep 2023 23:56:35 +0800 Subject: [PATCH 4/4] fix: update TestPlan and TestPricing --- casdoorsdk/plan_test.go | 2 +- casdoorsdk/pricing_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/casdoorsdk/plan_test.go b/casdoorsdk/plan_test.go index 082c903..ff2451b 100644 --- a/casdoorsdk/plan_test.go +++ b/casdoorsdk/plan_test.go @@ -87,7 +87,7 @@ func TestPlan(t *testing.T) { // Validate the deletion deletedPlan, err := GetPlan(name) - if err == nil || deletedPlan != nil { + if err != nil || deletedPlan != nil { t.Fatalf("Failed to delete object, it's still retrievable") } } diff --git a/casdoorsdk/pricing_test.go b/casdoorsdk/pricing_test.go index 1391dc9..97d8b00 100644 --- a/casdoorsdk/pricing_test.go +++ b/casdoorsdk/pricing_test.go @@ -88,7 +88,7 @@ func TestPricing(t *testing.T) { // Validate the deletion deletedPricing, err := GetPricing(name) - if err == nil || deletedPricing != nil { + if err != nil || deletedPricing != nil { t.Fatalf("Failed to delete object, it's still retrievable") } }