3
0

Implement constraint checking for system events

This commit is contained in:
Denis Arh
2020-01-10 10:00:27 +01:00
parent abf4d106bb
commit 8cb2c4c03d
13 changed files with 230 additions and 60 deletions
+19 -9
View File
@@ -1,15 +1,25 @@
package event
import "github.com/cortezaproject/corteza-server/pkg/eventbus"
import (
"github.com/cortezaproject/corteza-server/pkg/eventbus"
"github.com/cortezaproject/corteza-server/system/types"
)
// Match returns false if given conditions do not match event & resource internals
func (res applicationBase) Match(c eventbus.ConstraintMatcher) bool {
// By default we match no mather what kind of constraints we receive
//
// Function will be called multiple times - once for every trigger constraint
// All should match (return true):
// constraint#1 AND constraint#2 AND constraint#3 ...
//
// When there are multiple values, Match() can decide how to treat them (OR, AND...)
return true
return applicationMatch(res.application, c, false)
}
// Handles application matchers
func applicationMatch(r *types.Application, c eventbus.ConstraintMatcher, def bool) bool {
switch c.Name() {
// not supported yet
//case "application", "application.handle":
// return c.Match(r.Handle)
case "application.name":
return c.Match(r.Name)
}
return def
}
+22
View File
@@ -0,0 +1,22 @@
package event
import (
"github.com/cortezaproject/corteza-server/pkg/eventbus"
"github.com/cortezaproject/corteza-server/system/types"
"testing"
"github.com/stretchr/testify/assert"
)
func TestApplicationMatching(t *testing.T) {
var (
a = assert.New(t)
res = &applicationBase{
application: &types.Application{Name: "someApp"},
}
cApp = eventbus.MustMakeConstraint("application.name", "eq", "someApp")
)
a.True(res.Match(cApp))
}
+1 -8
View File
@@ -4,12 +4,5 @@ import "github.com/cortezaproject/corteza-server/pkg/eventbus"
// Match returns false if given conditions do not match event & resource internals
func (res authBase) Match(c eventbus.ConstraintMatcher) bool {
// By default we match no mather what kind of constraints we receive
//
// Function will be called multiple times - once for every trigger constraint
// All should match (return true):
// constraint#1 AND constraint#2 AND constraint#3 ...
//
// When there are multiple values, Match() can decide how to treat them (OR, AND...)
return true
return userMatch(res.user, c, false)
}
+22
View File
@@ -0,0 +1,22 @@
package event
import (
"github.com/cortezaproject/corteza-server/pkg/eventbus"
"github.com/cortezaproject/corteza-server/system/types"
"testing"
"github.com/stretchr/testify/assert"
)
func TestAuthMatching(t *testing.T) {
var (
a = assert.New(t)
res = &authBase{
user: &types.User{Handle: "user"},
}
cUsr = eventbus.MustMakeConstraint("user", "eq", "user")
)
a.True(res.Match(cUsr))
}
+17 -9
View File
@@ -1,15 +1,23 @@
package event
import "github.com/cortezaproject/corteza-server/pkg/eventbus"
import (
"github.com/cortezaproject/corteza-server/pkg/eventbus"
"github.com/cortezaproject/corteza-server/system/types"
)
// Match returns false if given conditions do not match event & resource internals
func (res roleBase) Match(c eventbus.ConstraintMatcher) bool {
// By default we match no mather what kind of constraints we receive
//
// Function will be called multiple times - once for every trigger constraint
// All should match (return true):
// constraint#1 AND constraint#2 AND constraint#3 ...
//
// When there are multiple values, Match() can decide how to treat them (OR, AND...)
return true
return roleMatch(res.role, c, false)
}
// Handles role matchers
func roleMatch(r *types.Role, c eventbus.ConstraintMatcher, def bool) bool {
switch c.Name() {
case "role", "role.handle":
return c.Match(r.Handle)
case "role.name":
return c.Match(r.Name)
}
return def
}
+4 -9
View File
@@ -1,15 +1,10 @@
package event
import "github.com/cortezaproject/corteza-server/pkg/eventbus"
import (
"github.com/cortezaproject/corteza-server/pkg/eventbus"
)
// Match returns false if given conditions do not match event & resource internals
func (res roleMemberBase) Match(c eventbus.ConstraintMatcher) bool {
// By default we match no mather what kind of constraints we receive
//
// Function will be called multiple times - once for every trigger constraint
// All should match (return true):
// constraint#1 AND constraint#2 AND constraint#3 ...
//
// When there are multiple values, Match() can decide how to treat them (OR, AND...)
return true
return userMatch(res.user, c, roleMatch(res.role, c, false))
}
+25
View File
@@ -0,0 +1,25 @@
package event
import (
"github.com/cortezaproject/corteza-server/pkg/eventbus"
"github.com/cortezaproject/corteza-server/system/types"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRoleMemberMatching(t *testing.T) {
var (
a = assert.New(t)
res = &roleMemberBase{
role: &types.Role{Handle: "admin"},
user: &types.User{Handle: "user"},
}
cRol = eventbus.MustMakeConstraint("role", "eq", "admin")
cUsr = eventbus.MustMakeConstraint("user", "eq", "user")
)
a.True(res.Match(cRol))
a.True(res.Match(cUsr))
}
+22
View File
@@ -0,0 +1,22 @@
package event
import (
"github.com/cortezaproject/corteza-server/pkg/eventbus"
"github.com/cortezaproject/corteza-server/system/types"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRoleMatching(t *testing.T) {
var (
a = assert.New(t)
res = &roleBase{
role: &types.Role{Handle: "admin"},
}
cRol = eventbus.MustMakeConstraint("role", "eq", "admin")
)
a.True(res.Match(cRol))
}
+18 -8
View File
@@ -1,15 +1,25 @@
package event
import "github.com/cortezaproject/corteza-server/pkg/eventbus"
import (
"github.com/cortezaproject/corteza-server/pkg/eventbus"
"github.com/cortezaproject/corteza-server/system/types"
)
// Match returns false if given conditions do not match event & resource internals
func (res sinkBase) Match(c eventbus.ConstraintMatcher) bool {
// By default we match no mather what kind of constraints we receive
//
// Function will be called multiple times - once for every trigger constraint
// All should match (return true):
// constraint#1 AND constraint#2 AND constraint#3 ...
//
// When there are multiple values, Match() can decide how to treat them (OR, AND...)
if !sinkMatch(res.request, c) {
return false
}
return false
}
// Handles sink matchers
func sinkMatch(r *types.SinkRequest, c eventbus.ConstraintMatcher) bool {
switch c.Name() {
case "url", "request.url":
return c.Match(r.RequestURL)
}
return true
}
+2 -8
View File
@@ -17,12 +17,6 @@ func (res systemOnTimestamp) Match(c eventbus.ConstraintMatcher) bool {
// Match returns false if given conditions do not match event & resource internals
func (res systemBase) Match(c eventbus.ConstraintMatcher) bool {
// By default we match no mather what kind of constraints we receive
//
// Function will be called multiple times - once for every trigger constraint
// All should match (return true):
// constraint#1 AND constraint#2 AND constraint#3 ...
//
// When there are multiple values, Match() can decide how to treat them (OR, AND...)
return true
// No constraints are supported for system.
return false
}
+39
View File
@@ -0,0 +1,39 @@
package event
import (
"github.com/cortezaproject/corteza-server/pkg/eventbus"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSystemOnIntervalMatching(t *testing.T) {
var (
res = &systemOnInterval{}
cInt = eventbus.MustMakeConstraint("", "", "* * * * *")
)
// Just make sure it runs
// not bothering with precise test setup (scheduler's tests cover all that
res.Match(cInt)
}
func TestSystemOnTimestampMatching(t *testing.T) {
var (
a = assert.New(t)
res = &systemOnInterval{}
cTStamp = eventbus.MustMakeConstraint("", "", "2000-01-01T00:00:00Z") // Y2k!
)
a.False(res.Match(cTStamp), "Year 2000?! How did we get here? Anyhow, happy New year!")
}
func TestSystemMatching(t *testing.T) {
var (
a = assert.New(t)
res = &systemBase{}
)
a.False(res.Match(eventbus.MustMakeConstraint("foo", "", "bar")))
}
+17 -9
View File
@@ -1,15 +1,23 @@
package event
import "github.com/cortezaproject/corteza-server/pkg/eventbus"
import (
"github.com/cortezaproject/corteza-server/pkg/eventbus"
"github.com/cortezaproject/corteza-server/system/types"
)
// Match returns false if given conditions do not match event & resource internals
func (res userBase) Match(c eventbus.ConstraintMatcher) bool {
// By default we match no mather what kind of constraints we receive
//
// Function will be called multiple times - once for every trigger constraint
// All should match (return true):
// constraint#1 AND constraint#2 AND constraint#3 ...
//
// When there are multiple values, Match() can decide how to treat them (OR, AND...)
return true
return userMatch(res.user, c, false)
}
// Handles user matchers
func userMatch(r *types.User, c eventbus.ConstraintMatcher, def bool) bool {
switch c.Name() {
case "user", "user.handle":
return r != nil && c.Match(r.Handle)
case "user.email":
return r != nil && c.Match(r.Email)
}
return def
}
+22
View File
@@ -0,0 +1,22 @@
package event
import (
"github.com/cortezaproject/corteza-server/pkg/eventbus"
"github.com/cortezaproject/corteza-server/system/types"
"testing"
"github.com/stretchr/testify/assert"
)
func TestUserMatching(t *testing.T) {
var (
a = assert.New(t)
res = &userBase{
user: &types.User{Email: "user@example.tld"},
}
cUsr = eventbus.MustMakeConstraint("user.email", "eq", "user@example.tld")
)
a.True(res.Match(cUsr))
}