Add integ. tests for automation scripts & triggers
This commit is contained in:
@@ -77,7 +77,7 @@ func (ctrl AutomationTrigger) List(ctx context.Context, r *request.AutomationTri
|
||||
func (ctrl AutomationTrigger) Create(ctx context.Context, r *request.AutomationTriggerCreate) (interface{}, error) {
|
||||
s, _, err := ctrl.loadCombo(ctx, r.NamespaceID, r.ScriptID, 0)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "can not create trigger")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -94,9 +94,9 @@ func (ctrl AutomationTrigger) Create(ctx context.Context, r *request.AutomationT
|
||||
}
|
||||
|
||||
func (ctrl AutomationTrigger) Read(ctx context.Context, r *request.AutomationTriggerRead) (interface{}, error) {
|
||||
_, t, err := ctrl.loadCombo(ctx, r.NamespaceID, r.ScriptID, 0)
|
||||
_, t, err := ctrl.loadCombo(ctx, r.NamespaceID, r.ScriptID, r.TriggerID)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "can not read trigger")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ctrl.makePayload(ctx, t, err)
|
||||
@@ -105,7 +105,7 @@ func (ctrl AutomationTrigger) Read(ctx context.Context, r *request.AutomationTri
|
||||
func (ctrl AutomationTrigger) Update(ctx context.Context, r *request.AutomationTriggerUpdate) (interface{}, error) {
|
||||
s, t, err := ctrl.loadCombo(ctx, r.NamespaceID, r.ScriptID, r.TriggerID)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "can not update trigger")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
t.Event = r.Event
|
||||
@@ -120,7 +120,7 @@ func (ctrl AutomationTrigger) Update(ctx context.Context, r *request.AutomationT
|
||||
func (ctrl AutomationTrigger) Delete(ctx context.Context, r *request.AutomationTriggerDelete) (interface{}, error) {
|
||||
s, t, err := ctrl.loadCombo(ctx, r.NamespaceID, r.ScriptID, r.TriggerID)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "can not update trigger")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resputil.OK(), ctrl.triggers.Delete(ctx, s, t)
|
||||
@@ -128,13 +128,15 @@ func (ctrl AutomationTrigger) Delete(ctx context.Context, r *request.AutomationT
|
||||
|
||||
func (ctrl AutomationTrigger) loadCombo(ctx context.Context, namespaceID, scriptID, triggerID uint64) (s *automation.Script, t *automation.Trigger, err error) {
|
||||
if triggerID > 0 {
|
||||
t, err = ctrl.triggers.FindByID(ctx, triggerID)
|
||||
return
|
||||
if t, err = ctrl.triggers.FindByID(ctx, triggerID); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if scriptID > 0 {
|
||||
s, err = ctrl.scripts.FindByID(ctx, namespaceID, scriptID)
|
||||
if err != nil && s.NamespaceID != namespaceID {
|
||||
|
||||
if err == nil && s.NamespaceID != namespaceID {
|
||||
err = repository.ErrNamespaceNotFound
|
||||
}
|
||||
|
||||
|
||||
@@ -58,6 +58,8 @@ func AutomationScript(sm automationScriptManager) automationScript {
|
||||
func (svc automationScript) FindByID(ctx context.Context, namespaceID, scriptID uint64) (*automation.Script, error) {
|
||||
if _, s, err := svc.loadCombo(ctx, namespaceID, scriptID); err != nil {
|
||||
return nil, err
|
||||
} else if !svc.ac.CanReadAutomationScript(ctx, s) {
|
||||
return nil, ErrNoReadPermissions
|
||||
} else {
|
||||
return s, nil
|
||||
}
|
||||
@@ -114,7 +116,7 @@ func (svc automationScript) Update(ctx context.Context, namespaceID uint64, mod
|
||||
}
|
||||
|
||||
if !svc.ac.CanUpdateAutomationScript(ctx, s) {
|
||||
return ErrNoCreatePermissions.withStack()
|
||||
return ErrNoUpdatePermissions.withStack()
|
||||
}
|
||||
|
||||
// Users need to have grant privileges to
|
||||
@@ -152,7 +154,7 @@ func (svc automationScript) Delete(ctx context.Context, namespaceID, scriptID ui
|
||||
if _, s, err := svc.loadCombo(ctx, namespaceID, scriptID); err != nil {
|
||||
return err
|
||||
} else if !svc.ac.CanDeleteAutomationScript(ctx, s) {
|
||||
return ErrNoCreatePermissions.withStack()
|
||||
return ErrNoDeletePermissions.withStack()
|
||||
} else {
|
||||
return svc.scriptManager.DeleteScript(ctx, s)
|
||||
}
|
||||
@@ -174,9 +176,6 @@ func (svc automationScript) loadCombo(ctx context.Context, namespaceID, scriptID
|
||||
if scriptID > 0 {
|
||||
if s, err = svc.scriptManager.FindScriptByID(ctx, scriptID); err != nil {
|
||||
return
|
||||
} else if !svc.ac.CanReadAutomationScript(ctx, s) {
|
||||
err = ErrNoCreatePermissions.withStack()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package service
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/cortezaproject/corteza-server/compose/types"
|
||||
@@ -93,13 +94,17 @@ func (svc automationTrigger) Delete(ctx context.Context, s *automation.Script, t
|
||||
|
||||
// Validates trigger (in compose context)
|
||||
func (svc automationTrigger) isValid(ctx context.Context, s *automation.Script, t *automation.Trigger) error {
|
||||
if s == nil {
|
||||
return errors.WithStack(automation.ErrAutomationScriptInvalid)
|
||||
}
|
||||
|
||||
if !t.Enabled {
|
||||
return nil
|
||||
}
|
||||
|
||||
if t.Resource != "compose:record" {
|
||||
// Accepting only compose:record resources
|
||||
return automation.ErrAutomationTriggerInvalidResource
|
||||
return errors.WithStack(automation.ErrAutomationTriggerInvalidResource)
|
||||
}
|
||||
|
||||
if t.IsDeferred() {
|
||||
@@ -114,18 +119,18 @@ func (svc automationTrigger) isValid(ctx context.Context, s *automation.Script,
|
||||
var moduleID = t.Uint64Condition()
|
||||
|
||||
if t.Event != "manual" && moduleID == 0 {
|
||||
return automation.ErrAutomationTriggerInvalidCondition
|
||||
return errors.WithStack(automation.ErrAutomationTriggerInvalidCondition)
|
||||
}
|
||||
|
||||
if moduleID > 0 {
|
||||
if m, err := svc.mod.With(ctx).FindByID(s.NamespaceID, moduleID); err != nil {
|
||||
return err
|
||||
} else if !svc.ac.CanManageAutomationTriggersOnModule(ctx, m) {
|
||||
return ErrNoTriggerManagementPermissions
|
||||
return errors.WithStack(ErrNoTriggerManagementPermissions)
|
||||
}
|
||||
}
|
||||
default:
|
||||
return automation.ErrAutomationTriggerInvalidEvent
|
||||
return errors.WithStack(automation.ErrAutomationTriggerInvalidEvent)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
+29
-19
@@ -22,6 +22,12 @@ type (
|
||||
Watch(ctx context.Context)
|
||||
}
|
||||
|
||||
automationManager interface {
|
||||
automationScriptManager
|
||||
automationTriggerManager
|
||||
automationScriptsFinder
|
||||
}
|
||||
|
||||
Config struct {
|
||||
Storage options.StorageOpt
|
||||
Corredor options.CorredorOpt
|
||||
@@ -40,10 +46,13 @@ var (
|
||||
// DefaultAccessControl Access control checking
|
||||
DefaultAccessControl *accessControl
|
||||
|
||||
// DefaultAutomationScriptManager manages scripts
|
||||
// DefaultInternalAutomationManager manages automation scripts, triggers, runnable scripts
|
||||
DefaultInternalAutomationManager automationManager
|
||||
|
||||
// DefaultAutomationScriptManager manages compose automation scripts
|
||||
DefaultAutomationScriptManager automationScript
|
||||
|
||||
// DefaultAutomationTriggerManager manages triggerManager
|
||||
// DefaultAutomationTriggerManager manages compose automation triggers
|
||||
DefaultAutomationTriggerManager automationTrigger
|
||||
|
||||
// DefaultAutomationRunner runs automation scripts by listening to triggerManager and invoking Corredor service
|
||||
@@ -95,23 +104,24 @@ func Init(ctx context.Context, log *zap.Logger, c Config) (err error) {
|
||||
DefaultSystemUser = SystemUser(systemProto.NewUsersClient(systemClientConn))
|
||||
}
|
||||
|
||||
// ias: Internal Automatinon Service
|
||||
// handles script & trigger management & keeping runnables cripts in internal cache
|
||||
ias := automation.Service(automation.AutomationServiceConfig{
|
||||
Logger: DefaultLogger,
|
||||
DbTablePrefix: "compose",
|
||||
DB: db,
|
||||
TokenMaker: func(ctx context.Context, userID uint64) (s string, e error) {
|
||||
ctx = auth.SetSuperUserContext(ctx)
|
||||
return DefaultSystemUser.MakeJWT(ctx, userID)
|
||||
},
|
||||
})
|
||||
|
||||
// Pass automation manager to
|
||||
DefaultAutomationTriggerManager = AutomationTrigger(ias)
|
||||
DefaultAutomationScriptManager = AutomationScript(ias)
|
||||
|
||||
{
|
||||
if DefaultInternalAutomationManager == nil {
|
||||
// handles script & trigger management & keeping runnable scripts in internal cache
|
||||
DefaultInternalAutomationManager = automation.Service(automation.AutomationServiceConfig{
|
||||
Logger: DefaultLogger,
|
||||
DbTablePrefix: "compose",
|
||||
DB: db,
|
||||
TokenMaker: func(ctx context.Context, userID uint64) (s string, e error) {
|
||||
ctx = auth.SetSuperUserContext(ctx)
|
||||
return DefaultSystemUser.MakeJWT(ctx, userID)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// Pass internal automation manager to compose's script & trigger managers
|
||||
DefaultAutomationTriggerManager = AutomationTrigger(DefaultInternalAutomationManager)
|
||||
DefaultAutomationScriptManager = AutomationScript(DefaultInternalAutomationManager)
|
||||
|
||||
var scriptRunnerClient corredor.ScriptRunnerClient
|
||||
|
||||
if c.Corredor.Enabled {
|
||||
@@ -131,7 +141,7 @@ func Init(ctx context.Context, log *zap.Logger, c Config) (err error) {
|
||||
ApiBaseURLMessaging: c.Corredor.ApiBaseURLMessaging,
|
||||
ApiBaseURLCompose: c.Corredor.ApiBaseURLCompose,
|
||||
},
|
||||
ias,
|
||||
DefaultInternalAutomationManager,
|
||||
scriptRunnerClient,
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user