Something went wrong. Try again.
Self-hosted file management and sharing system, supports multiple storage providers. forked from tdvorak.tngl.sh/cloudreve
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110package middleware
import ( "context" "net/http/httptest" "testing"
"github.com/cloudreve/Cloudreve/v4/application/dependency" "github.com/cloudreve/Cloudreve/v4/ent" "github.com/cloudreve/Cloudreve/v4/inventory" "github.com/cloudreve/Cloudreve/v4/inventory/types" "github.com/cloudreve/Cloudreve/v4/pkg/boolset" "github.com/cloudreve/Cloudreve/v4/pkg/logging" "github.com/cloudreve/Cloudreve/v4/pkg/util" "github.com/gin-gonic/gin")
func perms(bits ...types.GroupPermission) *boolset.BooleanSet { b := &boolset.BooleanSet{} for _, p := range bits { boolset.Set(int(p), true, b) } return b}
func newAdminRequest(t *testing.T, permissions *boolset.BooleanSet) *gin.Context { t.Helper() w := httptest.NewRecorder() c := gin.CreateTestContextOnly(w, testEngine) c.Request = httptest.NewRequest("GET", "/api/v4/admin/summary", nil) dep := dependency.NewDependency( dependency.WithLogger(logging.NewConsoleLogger(logging.LevelDebug)), ) c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), dependency.DepCtx{}, dep)) u := &ent.User{ ID: 2, Edges: ent.UserEdges{ Group: &ent.Group{Permissions: permissions}, }, } util.WithValue(c, inventory.UserCtx{}, u) return c}
func TestIsAdminOrDelegated(t *testing.T) { gin.SetMode(gin.TestMode) handler := IsAdminOrDelegated()
t.Run("full admin passes", func(t *testing.T) { c := newAdminRequest(t, perms(types.GroupPermissionIsAdmin)) handler(c) if c.IsAborted() { t.Fatal("full admin was rejected") } })
t.Run("delegated admin passes", func(t *testing.T) { c := newAdminRequest(t, perms(types.GroupPermissionAdminUsers)) handler(c) if c.IsAborted() { t.Fatal("delegated admin was rejected") } })
t.Run("regular user rejected", func(t *testing.T) { c := newAdminRequest(t, perms(types.GroupPermissionShare)) handler(c) if !c.IsAborted() { t.Fatal("regular user was not rejected") } })}
func TestAdminSection(t *testing.T) { gin.SetMode(gin.TestMode) handler := AdminSection(types.GroupPermissionAdminUsers, types.GroupPermissionAdminGroups)
t.Run("full admin passes any section", func(t *testing.T) { c := newAdminRequest(t, perms(types.GroupPermissionIsAdmin)) handler(c) if c.IsAborted() { t.Fatal("full admin was rejected") } })
t.Run("matching delegated section passes", func(t *testing.T) { c := newAdminRequest(t, perms(types.GroupPermissionAdminUsers)) handler(c) if c.IsAborted() { t.Fatal("delegated admin with matching section was rejected") } })
t.Run("non-matching delegated section rejected", func(t *testing.T) { c := newAdminRequest(t, perms(types.GroupPermissionAdminFiles)) handler(c) if !c.IsAborted() { t.Fatal("delegated admin without matching section was not rejected") } })
t.Run("regular user rejected", func(t *testing.T) { c := newAdminRequest(t, perms()) handler(c) if !c.IsAborted() { t.Fatal("regular user was not rejected") } })}