diff --git a/system/service/event/application.go b/system/service/event/application.go index f4a17ca02..a8d6d0ad6 100644 --- a/system/service/event/application.go +++ b/system/service/event/application.go @@ -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 } diff --git a/system/service/event/application_test.go b/system/service/event/application_test.go new file mode 100644 index 000000000..9d15ccd18 --- /dev/null +++ b/system/service/event/application_test.go @@ -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)) +} diff --git a/system/service/event/auth.go b/system/service/event/auth.go index 0ee6bf216..86cc83371 100644 --- a/system/service/event/auth.go +++ b/system/service/event/auth.go @@ -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) } diff --git a/system/service/event/auth_test.go b/system/service/event/auth_test.go new file mode 100644 index 000000000..a1a1c6b82 --- /dev/null +++ b/system/service/event/auth_test.go @@ -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)) +} diff --git a/system/service/event/role.go b/system/service/event/role.go index 37f850b2e..896136464 100644 --- a/system/service/event/role.go +++ b/system/service/event/role.go @@ -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 } diff --git a/system/service/event/role_member.go b/system/service/event/role_member.go index 220cd894d..1eb1e3d78 100644 --- a/system/service/event/role_member.go +++ b/system/service/event/role_member.go @@ -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)) } diff --git a/system/service/event/role_member_test.go b/system/service/event/role_member_test.go new file mode 100644 index 000000000..28a81f4af --- /dev/null +++ b/system/service/event/role_member_test.go @@ -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)) +} diff --git a/system/service/event/role_test.go b/system/service/event/role_test.go new file mode 100644 index 000000000..ad2294575 --- /dev/null +++ b/system/service/event/role_test.go @@ -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)) +} diff --git a/system/service/event/sink.go b/system/service/event/sink.go index dd6723d48..01840052e 100644 --- a/system/service/event/sink.go +++ b/system/service/event/sink.go @@ -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 } diff --git a/system/service/event/system.go b/system/service/event/system.go index 646169a65..11aa3965d 100644 --- a/system/service/event/system.go +++ b/system/service/event/system.go @@ -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 } diff --git a/system/service/event/system_test.go b/system/service/event/system_test.go new file mode 100644 index 000000000..bce5bedab --- /dev/null +++ b/system/service/event/system_test.go @@ -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"))) +} diff --git a/system/service/event/user.go b/system/service/event/user.go index ba52342a7..78a4e856c 100644 --- a/system/service/event/user.go +++ b/system/service/event/user.go @@ -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 } diff --git a/system/service/event/user_test.go b/system/service/event/user_test.go new file mode 100644 index 000000000..7b3447539 --- /dev/null +++ b/system/service/event/user_test.go @@ -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)) +}