package mongoadapter import ( "reflect" "testing" "github.com/casbin/casbin/v3/model" "go.mongodb.org/mongo-driver/v2/bson" ) func TestRuleFromPolicyTrimsOnlyTrailingEmptyValues(t *testing.T) { rule := ruleFromPolicy("p", []string{"role", "org", "", "object", ""}) if got, want := rule.PType, "p"; got != want { t.Fatalf("PType = %q, want %q", got, want) } if got, want := rule.policyValues(), []string{"role", "org", "", "object"}; !reflect.DeepEqual(got, want) { t.Fatalf("policyValues() = %#v, want %#v", got, want) } } func TestSelectorForPolicySkipsEmptyAndOutOfRangeFields(t *testing.T) { got := selectorForPolicy("p", 1, "role", "", "object", "read", "ignored", "ignored") want := bson.M{ "ptype": "p", "v1": "role", "v3": "object", "v4": "read", "v5": "ignored", } if !reflect.DeepEqual(got, want) { t.Fatalf("selectorForPolicy() = %#v, want %#v", got, want) } } func TestLoadPolicyLineAddsPolicyToModel(t *testing.T) { m, err := model.NewModelFromString(` [request_definition] r = sub, obj, act [policy_definition] p = sub, obj, act [policy_effect] e = some(where (p.eft == allow)) [matchers] m = r.sub == p.sub && r.obj == p.obj && r.act == p.act `) if err != nil { t.Fatalf("NewModelFromString() error = %v", err) } err = loadPolicyLine(Rule{PType: "p", V0: "alice", V1: "data1", V2: "read"}, m) if err != nil { t.Fatalf("loadPolicyLine() error = %v", err) } got, err := m.GetPolicy("p", "p") if err != nil { t.Fatalf("GetPolicy() error = %v", err) } want := [][]string{{"alice", "data1", "read"}} if !reflect.DeepEqual(got, want) { t.Fatalf("GetPolicy() = %#v, want %#v", got, want) } }