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 test cases for all objects and all opertions #84

Merged
merged 4 commits into from
Sep 15, 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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
93 changes: 93 additions & 0 deletions casdoorsdk/adapter_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
4 changes: 2 additions & 2 deletions casdoorsdk/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")
}
}
4 changes: 4 additions & 0 deletions casdoorsdk/cert_global.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
97 changes: 97 additions & 0 deletions casdoorsdk/cert_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
31 changes: 31 additions & 0 deletions casdoorsdk/email_test.go
Original file line number Diff line number Diff line change
@@ -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...)
}
File renamed without changes.
95 changes: 95 additions & 0 deletions casdoorsdk/enforcer_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
Loading
Loading