queue_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package executor
  2. import (
  3. "testing"
  4. "time"
  5. auth "github.com/OliveTin/OliveTin/internal/auth"
  6. config "github.com/OliveTin/OliveTin/internal/config"
  7. "github.com/google/uuid"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestGetActiveExecutionsACLFiltersFinishedAndACL(t *testing.T) {
  12. e, cfg := testingExecutor()
  13. allowedAction := &config.Action{
  14. Title: "allowed",
  15. Shell: "sleep 1",
  16. Acls: []string{"view-logs"},
  17. }
  18. secretAction := &config.Action{
  19. Title: "secret",
  20. Shell: "sleep 1",
  21. }
  22. cfg.Actions = append(cfg.Actions, allowedAction, secretAction)
  23. cfg.DefaultPermissions.Logs = false
  24. cfg.AccessControlLists = []*config.AccessControlList{
  25. {
  26. Name: "view-logs",
  27. MatchUsernames: []string{"guest"},
  28. Permissions: config.PermissionsList{
  29. Logs: true,
  30. },
  31. },
  32. }
  33. cfg.Sanitize()
  34. e.RebuildActionMap()
  35. allowedBinding := e.FindBindingWithNoEntity(allowedAction)
  36. secretBinding := e.FindBindingWithNoEntity(secretAction)
  37. require.NotNil(t, allowedBinding)
  38. require.NotNil(t, secretBinding)
  39. activeAllowed := newQueueTestLogEntry(allowedBinding, false)
  40. finishedAllowed := newQueueTestLogEntry(allowedBinding, true)
  41. activeSecret := newQueueTestLogEntry(secretBinding, false)
  42. e.SetLog(activeAllowed.ExecutionTrackingID, activeAllowed)
  43. e.SetLog(finishedAllowed.ExecutionTrackingID, finishedAllowed)
  44. e.SetLog(activeSecret.ExecutionTrackingID, activeSecret)
  45. user := auth.UserGuest(cfg)
  46. active := e.GetActiveExecutionsACL(cfg, user)
  47. require.Len(t, active, 1)
  48. assert.Equal(t, activeAllowed.ExecutionTrackingID, active[0].ExecutionTrackingID)
  49. }
  50. func newQueueTestLogEntry(binding *ActionBinding, finished bool) *InternalLogEntry {
  51. entry := &InternalLogEntry{
  52. Binding: binding,
  53. DatetimeStarted: time.Now(),
  54. ExecutionTrackingID: uuid.NewString(),
  55. ActionTitle: binding.Action.Title,
  56. ExecutionFinished: finished,
  57. }
  58. if finished {
  59. entry.DatetimeFinished = time.Now()
  60. }
  61. return entry
  62. }