github.com/cs3org/reva/v2@v2.27.7/pkg/share/share_test.go (about)

     1  // Copyright 2018-2021 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package share
    20  
    21  import (
    22  	"testing"
    23  
    24  	groupv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/group/v1beta1"
    25  	userv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
    26  	collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1"
    27  	provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
    28  )
    29  
    30  func TestIsCreatedByUser(t *testing.T) {
    31  	user := userv1beta1.User{
    32  		Id: &userv1beta1.UserId{
    33  			Idp:      "sampleidp",
    34  			OpaqueId: "user",
    35  		},
    36  	}
    37  
    38  	s1 := collaboration.Share{
    39  		Owner: &userv1beta1.UserId{
    40  			Idp:      "sampleidp",
    41  			OpaqueId: "user",
    42  		},
    43  	}
    44  
    45  	s2 := collaboration.Share{
    46  		Creator: &userv1beta1.UserId{
    47  			Idp:      "sampleidp",
    48  			OpaqueId: "user",
    49  		},
    50  	}
    51  
    52  	s3 := collaboration.Share{
    53  		Owner: &userv1beta1.UserId{
    54  			Idp:      "sampleidp",
    55  			OpaqueId: "user",
    56  		},
    57  		Creator: &userv1beta1.UserId{
    58  			Idp:      "sampleidp",
    59  			OpaqueId: "user",
    60  		},
    61  	}
    62  
    63  	if !IsCreatedByUser(&s1, &user) || !IsCreatedByUser(&s2, &user) || !IsCreatedByUser(&s3, &user) {
    64  		t.Error("Expected share to be created by user")
    65  	}
    66  
    67  	anotherUser := userv1beta1.User{
    68  		Id: &userv1beta1.UserId{
    69  			Idp:      "sampleidp",
    70  			OpaqueId: "another",
    71  		},
    72  	}
    73  
    74  	if IsCreatedByUser(&s1, &anotherUser) {
    75  		t.Error("Expected share not to be created by user")
    76  	}
    77  }
    78  
    79  func TestIsGrantedToUser(t *testing.T) {
    80  	user := userv1beta1.User{
    81  		Id: &userv1beta1.UserId{
    82  			Idp:      "sampleidp",
    83  			OpaqueId: "another",
    84  		},
    85  		Groups: []string{"groupid"},
    86  	}
    87  
    88  	s1 := collaboration.Share{
    89  		Grantee: &provider.Grantee{
    90  			Type: provider.GranteeType_GRANTEE_TYPE_USER,
    91  			Id: &provider.Grantee_UserId{
    92  				UserId: &userv1beta1.UserId{
    93  					Idp:      "sampleidp",
    94  					OpaqueId: "another",
    95  				},
    96  			},
    97  		},
    98  	}
    99  
   100  	s2 := collaboration.Share{
   101  		Grantee: &provider.Grantee{
   102  			Type: provider.GranteeType_GRANTEE_TYPE_GROUP,
   103  			Id: &provider.Grantee_GroupId{
   104  				GroupId: &groupv1beta1.GroupId{OpaqueId: "groupid"}},
   105  		},
   106  	}
   107  
   108  	if !IsGrantedToUser(&s1, &user) || !IsGrantedToUser(&s2, &user) {
   109  		t.Error("Expected the share to be granted to user")
   110  	}
   111  
   112  	s3 := collaboration.Share{
   113  		Grantee: &provider.Grantee{},
   114  	}
   115  
   116  	if IsGrantedToUser(&s3, &user) {
   117  		t.Error("Expecte the share not to be granted to user")
   118  	}
   119  }
   120  
   121  func TestMatchesFilter(t *testing.T) {
   122  	id := &provider.ResourceId{StorageId: "storage", OpaqueId: "opaque"}
   123  	share := &collaboration.Share{
   124  		ResourceId: id,
   125  		Grantee: &provider.Grantee{
   126  			Type: provider.GranteeType_GRANTEE_TYPE_USER,
   127  		},
   128  		Permissions: &collaboration.SharePermissions{Permissions: &provider.ResourcePermissions{}},
   129  	}
   130  
   131  	if !MatchesFilter(share, NoState, ResourceIDFilter(id)) {
   132  		t.Errorf("Expected share to pass the id filter. Share: %v", share)
   133  	}
   134  	if MatchesFilter(share, NoState, GroupGranteeFilter()) {
   135  		t.Errorf("Expected share to not pass the grantee type filter. Share: %v", share)
   136  	}
   137  	if MatchesFilter(share, NoState, &collaboration.Filter{Type: collaboration.Filter_TYPE_EXCLUDE_DENIALS}) {
   138  		t.Errorf("Expected share to not pass the exclude denials filter. Share: %v", share)
   139  	}
   140  	if MatchesFilter(share, NoState, &collaboration.Filter{Type: collaboration.Filter_TYPE_INVALID}) {
   141  		t.Errorf("Expected share to not pass an invalid filter. Share: %v", share)
   142  	}
   143  }
   144  
   145  func TestMatchesAnyFilter(t *testing.T) {
   146  	id := &provider.ResourceId{StorageId: "storage", OpaqueId: "opaque"}
   147  	share := &collaboration.Share{
   148  		ResourceId: id,
   149  		Grantee: &provider.Grantee{
   150  			Type: provider.GranteeType_GRANTEE_TYPE_USER,
   151  		},
   152  		Permissions: &collaboration.SharePermissions{Permissions: &provider.ResourcePermissions{}},
   153  	}
   154  
   155  	f1 := []*collaboration.Filter{UserGranteeFilter(), GroupGranteeFilter()}
   156  	if !MatchesAnyFilter(share, NoState, f1) {
   157  		t.Errorf("Expected share to match any of the given filters. Share: %v, Filters: %v", share, f1)
   158  	}
   159  
   160  	f2 := []*collaboration.Filter{ResourceIDFilter(&provider.ResourceId{StorageId: "something", OpaqueId: "different"}), GroupGranteeFilter()}
   161  	if MatchesAnyFilter(share, NoState, f2) {
   162  		t.Errorf("Expected share to not match any of the given filters. Share: %v, Filters: %v", share, f2)
   163  	}
   164  }
   165  
   166  func TestMatchesFilters(t *testing.T) {
   167  	id := &provider.ResourceId{StorageId: "storage", OpaqueId: "opaque"}
   168  	share := &collaboration.Share{
   169  		ResourceId: id,
   170  		Grantee: &provider.Grantee{
   171  			Type: provider.GranteeType_GRANTEE_TYPE_USER,
   172  		},
   173  		Permissions: &collaboration.SharePermissions{Permissions: &provider.ResourcePermissions{}},
   174  	}
   175  
   176  	f1 := []*collaboration.Filter{ResourceIDFilter(id), GroupGranteeFilter()}
   177  	if MatchesFilters(share, f1) {
   178  		t.Errorf("Expected share to not match the filters. Share %v, Filters %v", share, f1)
   179  	}
   180  
   181  	f2 := []*collaboration.Filter{ResourceIDFilter(id), UserGranteeFilter(), GroupGranteeFilter()}
   182  	if !MatchesFilters(share, f2) {
   183  		t.Errorf("Expected share to match the filters. Share %v, Filters %v", share, f2)
   184  	}
   185  }
   186  
   187  func TestGroupFiltersByType(t *testing.T) {
   188  	id := &provider.ResourceId{StorageId: "storage", OpaqueId: "opaque"}
   189  	filters := []*collaboration.Filter{UserGranteeFilter(), GroupGranteeFilter(), ResourceIDFilter(id)}
   190  
   191  	grouped := GroupFiltersByType(filters)
   192  
   193  	for fType, f := range grouped {
   194  		switch fType {
   195  		case collaboration.Filter_TYPE_GRANTEE_TYPE:
   196  			if len(f) != 2 {
   197  				t.Errorf("Expected 2 grantee type filters got %d", len(f))
   198  			}
   199  			for i := range f {
   200  				if f[i].Type != fType {
   201  					t.Errorf("Filter %v doesn't belong to this type %v", f[i], t)
   202  				}
   203  			}
   204  		case collaboration.Filter_TYPE_RESOURCE_ID:
   205  			if len(f) != 1 {
   206  				t.Errorf("Expected 1 resource id filter got %d", len(f))
   207  			}
   208  			if f[0].Type != fType {
   209  				t.Errorf("Filter %v doesn't belong to this type %v", f[0], t)
   210  			}
   211  		}
   212  	}
   213  }