2021-09-29 17:07:10 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/viewer"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/mock"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/ptr"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGlobalScheduleAuth(t *testing.T) {
|
|
|
|
ds := new(mock.Store)
|
2022-11-15 14:08:05 +00:00
|
|
|
svc, ctx := newTestService(t, ds, nil, nil)
|
2021-09-29 17:07:10 +00:00
|
|
|
|
2022-01-18 01:52:09 +00:00
|
|
|
ds.ListScheduledQueriesInPackWithStatsFunc = func(ctx context.Context, id uint, opts fleet.ListOptions) ([]*fleet.ScheduledQuery, error) {
|
2021-09-29 17:07:10 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
ds.EnsureGlobalPackFunc = func(ctx context.Context) (*fleet.Pack, error) {
|
|
|
|
return &fleet.Pack{}, nil
|
|
|
|
}
|
2021-12-15 14:06:34 +00:00
|
|
|
ds.NewScheduledQueryFunc = func(ctx context.Context, sq *fleet.ScheduledQuery, opts ...fleet.OptionalArg) (*fleet.ScheduledQuery, error) {
|
|
|
|
return sq, nil
|
|
|
|
}
|
|
|
|
ds.ScheduledQueryFunc = func(ctx context.Context, id uint) (*fleet.ScheduledQuery, error) {
|
|
|
|
return &fleet.ScheduledQuery{}, nil
|
|
|
|
}
|
|
|
|
ds.SaveScheduledQueryFunc = func(ctx context.Context, sq *fleet.ScheduledQuery) (*fleet.ScheduledQuery, error) {
|
|
|
|
return sq, nil
|
|
|
|
}
|
|
|
|
ds.DeleteScheduledQueryFunc = func(ctx context.Context, id uint) error {
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-29 17:07:10 +00:00
|
|
|
|
2022-01-18 01:52:09 +00:00
|
|
|
testCases := []struct {
|
2021-09-29 17:07:10 +00:00
|
|
|
name string
|
|
|
|
user *fleet.User
|
|
|
|
shouldFailWrite bool
|
|
|
|
shouldFailRead bool
|
|
|
|
}{
|
|
|
|
{
|
2022-04-18 17:27:30 +00:00
|
|
|
name: "global admin",
|
|
|
|
user: &fleet.User{GlobalRole: ptr.String(fleet.RoleAdmin)},
|
|
|
|
shouldFailWrite: false,
|
|
|
|
shouldFailRead: false,
|
2021-09-29 17:07:10 +00:00
|
|
|
},
|
|
|
|
{
|
2022-04-18 17:27:30 +00:00
|
|
|
name: "global maintainer",
|
|
|
|
user: &fleet.User{GlobalRole: ptr.String(fleet.RoleMaintainer)},
|
|
|
|
shouldFailWrite: false,
|
|
|
|
shouldFailRead: false,
|
2021-09-29 17:07:10 +00:00
|
|
|
},
|
|
|
|
{
|
2022-04-18 17:27:30 +00:00
|
|
|
name: "global observer",
|
|
|
|
user: &fleet.User{GlobalRole: ptr.String(fleet.RoleObserver)},
|
|
|
|
shouldFailWrite: true,
|
|
|
|
shouldFailRead: false,
|
2021-09-29 17:07:10 +00:00
|
|
|
},
|
2021-10-13 15:34:59 +00:00
|
|
|
{
|
2022-04-18 17:27:30 +00:00
|
|
|
name: "team admin",
|
|
|
|
user: &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleAdmin}}},
|
|
|
|
shouldFailWrite: true,
|
|
|
|
shouldFailRead: false,
|
2021-10-13 15:34:59 +00:00
|
|
|
},
|
2021-09-29 17:07:10 +00:00
|
|
|
{
|
2022-04-18 17:27:30 +00:00
|
|
|
name: "team maintainer",
|
|
|
|
user: &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleMaintainer}}},
|
|
|
|
shouldFailWrite: true,
|
|
|
|
shouldFailRead: false,
|
2021-09-29 17:07:10 +00:00
|
|
|
},
|
|
|
|
{
|
2022-04-18 17:27:30 +00:00
|
|
|
name: "team observer",
|
|
|
|
user: &fleet.User{Teams: []fleet.UserTeam{{Team: fleet.Team{ID: 1}, Role: fleet.RoleObserver}}},
|
|
|
|
shouldFailWrite: true,
|
|
|
|
shouldFailRead: false,
|
2021-09-29 17:07:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range testCases {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-11-15 14:08:05 +00:00
|
|
|
ctx := viewer.NewContext(ctx, viewer.Viewer{User: tt.user})
|
2021-09-29 17:07:10 +00:00
|
|
|
|
|
|
|
_, err := svc.GetGlobalScheduledQueries(ctx, fleet.ListOptions{})
|
|
|
|
checkAuthErr(t, tt.shouldFailRead, err)
|
2021-12-15 14:06:34 +00:00
|
|
|
|
2022-05-26 21:54:21 +00:00
|
|
|
_, err = svc.GlobalScheduleQuery(ctx, &fleet.ScheduledQuery{Name: "query", QueryName: "query", Interval: 10})
|
2021-12-15 14:06:34 +00:00
|
|
|
checkAuthErr(t, tt.shouldFailWrite, err)
|
|
|
|
|
|
|
|
_, err = svc.ModifyGlobalScheduledQueries(ctx, 1, fleet.ScheduledQueryPayload{})
|
|
|
|
checkAuthErr(t, tt.shouldFailWrite, err)
|
|
|
|
|
|
|
|
err = svc.DeleteGlobalScheduledQueries(ctx, 1)
|
|
|
|
checkAuthErr(t, tt.shouldFailWrite, err)
|
2021-09-29 17:07:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|