add contains helpers to exchange scope (#487)
This commit is contained in:
parent
9326690338
commit
2d6c765ee8
10
src/internal/common/slices.go
Normal file
10
src/internal/common/slices.go
Normal file
@ -0,0 +1,10 @@
|
||||
package common
|
||||
|
||||
func ContainsString(super []string, sub string) bool {
|
||||
for _, s := range super {
|
||||
if s == sub {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
27
src/internal/common/slices_test.go
Normal file
27
src/internal/common/slices_test.go
Normal file
@ -0,0 +1,27 @@
|
||||
package common_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
"github.com/alcionai/corso/internal/common"
|
||||
)
|
||||
|
||||
type CommonSlicesSuite struct {
|
||||
suite.Suite
|
||||
}
|
||||
|
||||
func TestCommonSlicesSuite(t *testing.T) {
|
||||
suite.Run(t, new(CommonSlicesSuite))
|
||||
}
|
||||
|
||||
func (suite *CommonSlicesSuite) TestContainsString() {
|
||||
t := suite.T()
|
||||
target := "fnords"
|
||||
good := []string{"fnords"}
|
||||
bad := []string{"foo", "bar"}
|
||||
assert.True(t, common.ContainsString(good, target))
|
||||
assert.False(t, common.ContainsString(bad, target))
|
||||
}
|
||||
@ -493,6 +493,24 @@ func (s ExchangeScope) IncludesCategory(cat exchangeCategory) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Contains returns true if the category is included in the scope's
|
||||
// data type, and the target string is included in the scope.
|
||||
func (s ExchangeScope) Contains(cat exchangeCategory, target string) bool {
|
||||
if !s.IncludesCategory(cat) {
|
||||
return false
|
||||
}
|
||||
return contains(s, cat.String(), target)
|
||||
}
|
||||
|
||||
// returns true if the category is included in the scope's data type,
|
||||
// and the value is set to Any().
|
||||
func (s ExchangeScope) IsAny(cat exchangeCategory) bool {
|
||||
if !s.IncludesCategory(cat) {
|
||||
return false
|
||||
}
|
||||
return s[cat.String()] == AnyTgt
|
||||
}
|
||||
|
||||
// Get returns the data category in the scope. If the scope
|
||||
// contains all data types for a user, it'll return the
|
||||
// ExchangeUser category.
|
||||
@ -587,7 +605,7 @@ func (s ExchangeScope) matchesPath(cat exchangeCategory, path []string) bool {
|
||||
// all parts of the scope must match
|
||||
isAny := target[0] == AnyTgt
|
||||
if !isAny {
|
||||
if !contains(target, id) {
|
||||
if !common.ContainsString(target, id) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
@ -595,16 +613,6 @@ func (s ExchangeScope) matchesPath(cat exchangeCategory, path []string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// temporary helper until filters replace string values for scopes.
|
||||
func contains(super []string, sub string) bool {
|
||||
for _, s := range super {
|
||||
if s == sub {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Restore Point Filtering
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@ -871,3 +871,74 @@ func (suite *ExchangeSourceSuite) TestMatchExchangeEntry() {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (suite *ExchangeSourceSuite) TestContains() {
|
||||
target := "fnords"
|
||||
var (
|
||||
es = NewExchangeRestore()
|
||||
anyUser = extendExchangeScopeValues(es.Users(Any()))
|
||||
noMail = extendExchangeScopeValues(es.Mails(None(), None(), None()))
|
||||
does = extendExchangeScopeValues(es.Mails(Any(), Any(), []string{target}))
|
||||
doesNot = extendExchangeScopeValues(es.Mails(Any(), Any(), []string{"smarf"}))
|
||||
wrongType = extendExchangeScopeValues(es.Contacts(Any(), Any(), Any()))
|
||||
wrongTypeGoodTarget = extendExchangeScopeValues(es.Contacts(Any(), Any(), Any()))
|
||||
)
|
||||
table := []struct {
|
||||
name string
|
||||
scopes []ExchangeScope
|
||||
expect assert.BoolAssertionFunc
|
||||
}{
|
||||
{"any user", anyUser, assert.True},
|
||||
{"no mail", noMail, assert.False},
|
||||
{"does contain", does, assert.True},
|
||||
{"does not contain", doesNot, assert.False},
|
||||
{"wrong type", wrongType, assert.False},
|
||||
{"wrong type but right target", wrongTypeGoodTarget, assert.False},
|
||||
}
|
||||
for _, test := range table {
|
||||
suite.T().Run(test.name, func(t *testing.T) {
|
||||
var result bool
|
||||
for _, scope := range test.scopes {
|
||||
if scope.Contains(ExchangeMail, target) {
|
||||
result = true
|
||||
break
|
||||
}
|
||||
}
|
||||
test.expect(t, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (suite *ExchangeSourceSuite) TestIsAny() {
|
||||
var (
|
||||
es = NewExchangeRestore()
|
||||
anyUser = extendExchangeScopeValues(es.Users(Any()))
|
||||
noUser = extendExchangeScopeValues(es.Users(None()))
|
||||
specificMail = extendExchangeScopeValues(es.Mails(Any(), Any(), []string{"mail"}))
|
||||
anyMail = extendExchangeScopeValues(es.Mails(Any(), Any(), Any()))
|
||||
)
|
||||
table := []struct {
|
||||
name string
|
||||
scopes []ExchangeScope
|
||||
cat exchangeCategory
|
||||
expect assert.BoolAssertionFunc
|
||||
}{
|
||||
{"any user", anyUser, ExchangeUser, assert.True},
|
||||
{"no user", noUser, ExchangeUser, assert.False},
|
||||
{"specific mail", specificMail, ExchangeMail, assert.False},
|
||||
{"any mail", anyMail, ExchangeMail, assert.True},
|
||||
{"wrong category", anyMail, ExchangeEvent, assert.False},
|
||||
}
|
||||
for _, test := range table {
|
||||
suite.T().Run(test.name, func(t *testing.T) {
|
||||
var result bool
|
||||
for _, scope := range test.scopes {
|
||||
if scope.IsAny(test.cat) {
|
||||
result = true
|
||||
break
|
||||
}
|
||||
}
|
||||
test.expect(t, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -154,6 +154,22 @@ func appendIncludes[T baseScope](
|
||||
}
|
||||
}
|
||||
|
||||
// contains returns true if the provided scope is Any(), or contains the
|
||||
// target string.
|
||||
func contains(scope map[string]string, key, target string) bool {
|
||||
compare := scope[key]
|
||||
if len(compare) == 0 {
|
||||
return false
|
||||
}
|
||||
if compare == NoneTgt {
|
||||
return false
|
||||
}
|
||||
if compare == AnyTgt {
|
||||
return true
|
||||
}
|
||||
return strings.Contains(compare, target)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Printing Selectors for Human Reading
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@ -151,3 +151,13 @@ func (suite *SelectorSuite) TestToResourceTypeMap() {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (suite *SelectorSuite) TestContains() {
|
||||
t := suite.T()
|
||||
key := "key"
|
||||
target := "fnords"
|
||||
does := map[string]string{key: target}
|
||||
doesNot := map[string]string{key: "smarf"}
|
||||
assert.True(t, contains(does, key, target))
|
||||
assert.False(t, contains(doesNot, key, target))
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user