github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/model/sharing/rule_test.go (about)

     1  package sharing
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/cozy/cozy-stack/pkg/consts"
     7  	"github.com/cozy/cozy-stack/pkg/couchdb"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestValidatesRules(t *testing.T) {
    12  	s := Sharing{}
    13  	assert.Equal(t, ErrNoRules, s.ValidateRules())
    14  	s.Rules = []Rule{
    15  		{
    16  			Title:   "values is missing",
    17  			DocType: "io.cozy.tests",
    18  		},
    19  	}
    20  	assert.Equal(t, ErrInvalidRule, s.ValidateRules())
    21  	s.Rules = []Rule{
    22  		{
    23  			Title:  "doctype is missing",
    24  			Values: []string{"foo"},
    25  		},
    26  	}
    27  	assert.Equal(t, ErrInvalidRule, s.ValidateRules())
    28  	s.Rules = []Rule{
    29  		{
    30  			Title:   "doctype is blocked",
    31  			DocType: consts.Jobs,
    32  			Values:  []string{"foo"},
    33  		},
    34  	}
    35  	assert.Equal(t, ErrInvalidRule, s.ValidateRules())
    36  	s.Rules = []Rule{
    37  		{
    38  			Title:   "add is invalid",
    39  			DocType: "io.cozy.tests",
    40  			Values:  []string{"foo"},
    41  			Add:     "flip",
    42  		},
    43  	}
    44  	assert.Equal(t, ErrInvalidRule, s.ValidateRules())
    45  	s.Rules = []Rule{
    46  		{
    47  			Title:   "update is invalid",
    48  			DocType: "io.cozy.tests",
    49  			Values:  []string{"foo"},
    50  			Update:  "flip",
    51  		},
    52  	}
    53  	assert.Equal(t, ErrInvalidRule, s.ValidateRules())
    54  	s.Rules = []Rule{
    55  		{
    56  			Title:   "remove is invalid",
    57  			DocType: "io.cozy.tests",
    58  			Values:  []string{"foo"},
    59  			Remove:  "flip",
    60  		},
    61  	}
    62  	assert.Equal(t, ErrInvalidRule, s.ValidateRules())
    63  	s.Rules = []Rule{
    64  		{
    65  			Title:    "selector is OK",
    66  			DocType:  "io.cozy.tests",
    67  			Selector: "qux",
    68  			Values:   []string{"quux"},
    69  		},
    70  		{
    71  			Title:   "add, update and remove are OK",
    72  			DocType: consts.Contacts,
    73  			Values:  []string{"id1", "id2", "id3"},
    74  			Add:     "Sync",
    75  			Update:  "none",
    76  			Remove:  "revoke",
    77  		},
    78  		{
    79  			Title:   "files is OK",
    80  			DocType: consts.Files,
    81  			Values:  []string{"foo"},
    82  		},
    83  	}
    84  	assert.NoError(t, s.ValidateRules())
    85  	s.Rules = []Rule{
    86  		{
    87  			Title:    "referenced_by is OK",
    88  			DocType:  consts.Files,
    89  			Selector: couchdb.SelectorReferencedBy,
    90  			Values:   []string{"io.cozy.tests/123"},
    91  		},
    92  	}
    93  	assert.NoError(t, s.ValidateRules())
    94  	s.Rules = []Rule{
    95  		{
    96  			Title:   "root cannot be shared",
    97  			DocType: consts.Files,
    98  			Values:  []string{consts.RootDirID},
    99  		},
   100  	}
   101  	assert.Equal(t, ErrInvalidRule, s.ValidateRules())
   102  }
   103  
   104  func TestRuleAccept(t *testing.T) {
   105  	doc := map[string]interface{}{
   106  		"_id":    "foo",
   107  		"bar":    "baz",
   108  		"groups": []string{"group1", "group2"},
   109  		"one": map[string]interface{}{
   110  			"two": map[string]interface{}{
   111  				"three": "123",
   112  			},
   113  		},
   114  	}
   115  	doctype := "io.cozy.test.foos"
   116  	r := Rule{
   117  		Title:   "test",
   118  		DocType: doctype,
   119  		Values:  []string{"foo"},
   120  	}
   121  	assert.True(t, r.Accept(doctype, doc))
   122  	r.Local = true
   123  	assert.False(t, r.Accept(doctype, doc))
   124  	r.Local = false
   125  	r.DocType = consts.Files
   126  	assert.False(t, r.Accept(doctype, doc))
   127  
   128  	// Nested
   129  	r.DocType = doctype
   130  	r.Selector = "bar"
   131  	r.Values = []string{"hello", "baz"}
   132  	assert.True(t, r.Accept(doctype, doc))
   133  	r.Selector = "one.two.three"
   134  	r.Values = []string{"123"}
   135  	assert.True(t, r.Accept(doctype, doc))
   136  	r.Selector = "foo.bar.baz"
   137  	assert.False(t, r.Accept(doctype, doc))
   138  	r.Selector = "one.four.nine"
   139  	assert.False(t, r.Accept(doctype, doc))
   140  
   141  	// Arrays
   142  	r.Selector = "groups"
   143  	r.Values = []string{"group1"}
   144  	assert.True(t, r.Accept(doctype, doc))
   145  	r.Values = []string{"group2", "group3"}
   146  	assert.True(t, r.Accept(doctype, doc))
   147  	r.Values = []string{"group4"}
   148  	assert.False(t, r.Accept(doctype, doc))
   149  
   150  	// Referenced_by
   151  	file := map[string]interface{}{
   152  		"_id": "84fa49e2-3409-11e8-86de-7fff926238b1",
   153  		couchdb.SelectorReferencedBy: []map[string]interface{}{
   154  			{"type": "io.cozy.playlists", "id": "list1"},
   155  			{"type": "io.cozy.playlists", "id": "list2"},
   156  		},
   157  	}
   158  	r = Rule{
   159  		Title:    "test referenced_by",
   160  		DocType:  consts.Files,
   161  		Selector: couchdb.SelectorReferencedBy,
   162  		Values:   []string{"io.cozy.playlists/list1"},
   163  	}
   164  	assert.True(t, r.Accept(consts.Files, file))
   165  	r.Values = []string{"io.cozy.playlists/list3"}
   166  	assert.False(t, r.Accept(consts.Files, file))
   167  }
   168  
   169  func TestTriggersArgs(t *testing.T) {
   170  	r := Rule{
   171  		Title:    "test triggers args",
   172  		DocType:  consts.Files,
   173  		Selector: couchdb.SelectorReferencedBy,
   174  		Values:   []string{"io.cozy.playlists/list1"},
   175  		Update:   "sync",
   176  	}
   177  	expected := "io.cozy.files:CREATED,UPDATED:io.cozy.playlists/list1:referenced_by"
   178  	assert.Equal(t, expected, r.TriggerArgs())
   179  
   180  	doctype := "io.cozy.test.foos"
   181  	r = Rule{
   182  		Title:   "test",
   183  		DocType: doctype,
   184  		Values:  []string{"foo"},
   185  		Add:     "push",
   186  		Update:  "push",
   187  		Remove:  "revoke",
   188  	}
   189  	expected = "io.cozy.test.foos:CREATED,UPDATED,DELETED:foo"
   190  	assert.Equal(t, expected, r.TriggerArgs())
   191  
   192  	r.Local = true
   193  	assert.Equal(t, "", r.TriggerArgs())
   194  }