Preferences: refactor experimental apiserver and improve tests (#111596)

This commit is contained in:
Ryan McKinley
2025-09-26 19:44:02 +00:00
committed by GitHub
parent a333e8a8da
commit 82bcfba64b
23 changed files with 946 additions and 290 deletions
+6 -6
View File
@@ -17,15 +17,15 @@ const (
)
type OwnerReference struct {
Owner ResourceOwner // the resource owner
Name string // the team|user name
Owner ResourceOwner // the resource owner
Identifier string // the team|user name
}
func (o OwnerReference) AsName() string {
if o.Name == "" || o.Owner == NamespaceResourceOwner {
if o.Identifier == "" || o.Owner == NamespaceResourceOwner {
return string(o.Owner)
}
return string(o.Owner) + "-" + o.Name
return string(o.Owner) + "-" + o.Identifier
}
func ParseOwnerFromName(name string) (OwnerReference, bool) {
@@ -33,9 +33,9 @@ func ParseOwnerFromName(name string) (OwnerReference, bool) {
if found && len(after) > 0 {
switch before {
case "user":
return OwnerReference{Owner: UserResourceOwner, Name: after}, true
return OwnerReference{Owner: UserResourceOwner, Identifier: after}, true
case "team":
return OwnerReference{Owner: TeamResourceOwner, Name: after}, true
return OwnerReference{Owner: TeamResourceOwner, Identifier: after}, true
}
} else if name == "namespace" {
return OwnerReference{Owner: NamespaceResourceOwner}, true
@@ -24,7 +24,7 @@ func TestLegacyAuthorizer(t *testing.T) {
{
name: "with user",
input: "user-a",
output: utils.OwnerReference{Owner: utils.UserResourceOwner, Name: "a"},
output: utils.OwnerReference{Owner: utils.UserResourceOwner, Identifier: "a"},
found: true,
},
{
@@ -36,7 +36,7 @@ func TestLegacyAuthorizer(t *testing.T) {
{
name: "with team",
input: "team-b",
output: utils.OwnerReference{Owner: utils.TeamResourceOwner, Name: "b"},
output: utils.OwnerReference{Owner: utils.TeamResourceOwner, Identifier: "b"},
found: true,
},
{
@@ -0,0 +1,13 @@
package utils
import (
"context"
authlib "github.com/grafana/authlib/types"
)
//go:generate mockery --name TeamService --structname MockTeamService --inpackage --filename teams_mock.go --with-expecter
type TeamService interface {
InTeam(ctx context.Context, id authlib.AuthInfo, team string, admin bool) (bool, error)
GetTeams(ctx context.Context, id authlib.AuthInfo, admin bool) ([]string, error)
}
@@ -0,0 +1,156 @@
// Code generated by mockery v2.53.4. DO NOT EDIT.
package utils
import (
context "context"
types "github.com/grafana/authlib/types"
mock "github.com/stretchr/testify/mock"
)
// MockTeamService is an autogenerated mock type for the TeamService type
type MockTeamService struct {
mock.Mock
}
type MockTeamService_Expecter struct {
mock *mock.Mock
}
func (_m *MockTeamService) EXPECT() *MockTeamService_Expecter {
return &MockTeamService_Expecter{mock: &_m.Mock}
}
// GetTeams provides a mock function with given fields: ctx, id, admin
func (_m *MockTeamService) GetTeams(ctx context.Context, id types.AuthInfo, admin bool) ([]string, error) {
ret := _m.Called(ctx, id, admin)
if len(ret) == 0 {
panic("no return value specified for GetTeams")
}
var r0 []string
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, types.AuthInfo, bool) ([]string, error)); ok {
return rf(ctx, id, admin)
}
if rf, ok := ret.Get(0).(func(context.Context, types.AuthInfo, bool) []string); ok {
r0 = rf(ctx, id, admin)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]string)
}
}
if rf, ok := ret.Get(1).(func(context.Context, types.AuthInfo, bool) error); ok {
r1 = rf(ctx, id, admin)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockTeamService_GetTeams_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetTeams'
type MockTeamService_GetTeams_Call struct {
*mock.Call
}
// GetTeams is a helper method to define mock.On call
// - ctx context.Context
// - id types.AuthInfo
// - admin bool
func (_e *MockTeamService_Expecter) GetTeams(ctx interface{}, id interface{}, admin interface{}) *MockTeamService_GetTeams_Call {
return &MockTeamService_GetTeams_Call{Call: _e.mock.On("GetTeams", ctx, id, admin)}
}
func (_c *MockTeamService_GetTeams_Call) Run(run func(ctx context.Context, id types.AuthInfo, admin bool)) *MockTeamService_GetTeams_Call {
_c.Call.Run(func(args mock.Arguments) {
run(args[0].(context.Context), args[1].(types.AuthInfo), args[2].(bool))
})
return _c
}
func (_c *MockTeamService_GetTeams_Call) Return(_a0 []string, _a1 error) *MockTeamService_GetTeams_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *MockTeamService_GetTeams_Call) RunAndReturn(run func(context.Context, types.AuthInfo, bool) ([]string, error)) *MockTeamService_GetTeams_Call {
_c.Call.Return(run)
return _c
}
// InTeam provides a mock function with given fields: ctx, id, team, admin
func (_m *MockTeamService) InTeam(ctx context.Context, id types.AuthInfo, team string, admin bool) (bool, error) {
ret := _m.Called(ctx, id, team, admin)
if len(ret) == 0 {
panic("no return value specified for InTeam")
}
var r0 bool
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, types.AuthInfo, string, bool) (bool, error)); ok {
return rf(ctx, id, team, admin)
}
if rf, ok := ret.Get(0).(func(context.Context, types.AuthInfo, string, bool) bool); ok {
r0 = rf(ctx, id, team, admin)
} else {
r0 = ret.Get(0).(bool)
}
if rf, ok := ret.Get(1).(func(context.Context, types.AuthInfo, string, bool) error); ok {
r1 = rf(ctx, id, team, admin)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockTeamService_InTeam_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'InTeam'
type MockTeamService_InTeam_Call struct {
*mock.Call
}
// InTeam is a helper method to define mock.On call
// - ctx context.Context
// - id types.AuthInfo
// - team string
// - admin bool
func (_e *MockTeamService_Expecter) InTeam(ctx interface{}, id interface{}, team interface{}, admin interface{}) *MockTeamService_InTeam_Call {
return &MockTeamService_InTeam_Call{Call: _e.mock.On("InTeam", ctx, id, team, admin)}
}
func (_c *MockTeamService_InTeam_Call) Run(run func(ctx context.Context, id types.AuthInfo, team string, admin bool)) *MockTeamService_InTeam_Call {
_c.Call.Run(func(args mock.Arguments) {
run(args[0].(context.Context), args[1].(types.AuthInfo), args[2].(string), args[3].(bool))
})
return _c
}
func (_c *MockTeamService_InTeam_Call) Return(_a0 bool, _a1 error) *MockTeamService_InTeam_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *MockTeamService_InTeam_Call) RunAndReturn(run func(context.Context, types.AuthInfo, string, bool) (bool, error)) *MockTeamService_InTeam_Call {
_c.Call.Return(run)
return _c
}
// NewMockTeamService creates a new instance of MockTeamService. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
// The first argument is typically a *testing.T value.
func NewMockTeamService(t interface {
mock.TestingT
Cleanup(func())
}) *MockTeamService {
mock := &MockTeamService{}
mock.Mock.Test(t)
t.Cleanup(func() { mock.AssertExpectations(t) })
return mock
}