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 missing parameter for enforce and add test for enforce api #113

Merged
merged 3 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions casdoorsdk/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Adapter struct {
Name string `xorm:"varchar(100) notnull pk" json:"name"`
CreatedTime string `xorm:"varchar(100)" json:"createdTime"`

UseSameDb bool `json:"useSameDb"`
Type string `xorm:"varchar(100)" json:"type"`
DatabaseType string `xorm:"varchar(100)" json:"databaseType"`
Host string `xorm:"varchar(100)" json:"host"`
Expand Down
101 changes: 101 additions & 0 deletions casdoorsdk/enforce_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2024 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 TestEnforce(t *testing.T) {
InitConfig(TestCasdoorEndpoint, TestClientId, TestClientSecret, TestJwtPublicKey, TestCasdoorOrganization, TestCasdoorApplication)

modelName := getRandomName("enforceModel")

affected, err := AddModel(&Model{Owner: "built-in", Name: modelName, DisplayName: modelName, ModelText: `[request_definition]
dacongda marked this conversation as resolved.
Show resolved Hide resolved
r = subOwner, subName, method, urlPath, objOwner, objName

[policy_definition]
p = subOwner, subName, method, urlPath, objOwner, objName

[role_definition]
g = _, _

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = (r.subOwner == p.subOwner || p.subOwner == "*") && \
(r.subName == p.subName || p.subName == "*" || r.subName != "anonymous" && p.subName == "!anonymous") && \
(r.method == p.method || p.method == "*") && \
(r.urlPath == p.urlPath || p.urlPath == "*") && \
(r.objOwner == p.objOwner || p.objOwner == "*") && \
(r.objName == p.objName || p.objName == "*") || \
(r.subOwner == r.objOwner && r.subName == r.objName)`})

if err != nil {
t.Fatalf("Failed to add model: %v", err.Error())
}
if !affected {
t.Fatalf("Failed to add model")
}

adapterName := getRandomName("enforceAdapter")
affected, err = AddAdapter(&Adapter{Owner: "casbin", Name: adapterName, Table: "casbin_api_rule", UseSameDb: true})
if err != nil {
t.Fatalf("Failed to add adapter: %v", err.Error())
}
if !affected {
t.Fatalf("Failed to add adapter")
}

enforcerId := getRandomName("enforceEnforcer")
affected, err = AddEnforcer(&Enforcer{Owner: "casbin", Name: enforcerId, DisplayName: enforcerId, Model: "casbin/" + modelName, Adapter: "casbin/" + adapterName})
if err != nil {
t.Fatalf("Failed to add enforcer: %v", err.Error())
}
if !affected {
t.Fatalf("Failed to add enforcer")
}

var req []interface{}
req = append(req, "*", "*", "POST", "/api/signup", "*", "*")
dacongda marked this conversation as resolved.
Show resolved Hide resolved

res, err := Enforce("", "", "", "casbin/"+enforcerId, "", req)
if err != nil {
t.Fatalf("Failed to enforce: %v", err.Error())
}
if !res {
t.Fatalf("Enforce fail")
}

reqFail := []interface{}{"*", "*", "GET", "/api/sg", "*", ""}
res, err = Enforce("", "", "", "casbin/"+enforcerId, "", reqFail)
if err != nil {
t.Fatalf("Failed to enforce: %v", err.Error())
}

if res {
t.Fatalf("Enforce test fail")
}

resBatch, err := BatchEnforce("", "", "", "casbin/"+enforcerId, "", [][]interface{}{req, reqFail})
if err != nil {
t.Fatalf("Failed to batchEnforce: %v", err.Error())
}
if !resBatch[0][0] {
t.Fatalf("BatchEnforce test fail")
}
if resBatch[0][1] {
t.Fatalf("BatchEnforce test fail")
}
}
Loading