github.com/cs3org/reva/v2@v2.27.7/pkg/storage/registry/static/static_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 static_test
    20  
    21  import (
    22  	"context"
    23  
    24  	userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
    25  	provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
    26  	registrypb "github.com/cs3org/go-cs3apis/cs3/storage/registry/v1beta1"
    27  	ctxpkg "github.com/cs3org/reva/v2/pkg/ctx"
    28  	"github.com/cs3org/reva/v2/pkg/storage/registry/static"
    29  
    30  	. "github.com/onsi/ginkgo/v2"
    31  	. "github.com/onsi/gomega"
    32  )
    33  
    34  var _ = Describe("Static", func() {
    35  
    36  	rootProviders, eosProviders := 31, 29
    37  
    38  	handler, err := static.New(map[string]interface{}{
    39  		"home_provider": "/home",
    40  		"rules": map[string]interface{}{
    41  			"/home": map[string]interface{}{
    42  				"mapping": "/home-{{substr 0 1 .Id.OpaqueId}}",
    43  				"aliases": map[string]interface{}{
    44  					"/home-[a-fg-o]": map[string]string{
    45  						"address": "home-00-home",
    46  					},
    47  					"/home-[pqrstu]": map[string]string{
    48  						"address": "home-01-home",
    49  					},
    50  					"/home-[v-z]": map[string]string{
    51  						"address": "home-02-home",
    52  					},
    53  				},
    54  			},
    55  			"/MyShares": map[string]interface{}{
    56  				"mapping": "/MyShares-{{substr 0 1 .Id.OpaqueId}}",
    57  				"aliases": map[string]interface{}{
    58  					"/MyShares-[a-fg-o]": map[string]string{
    59  						"address": "home-00-shares",
    60  					},
    61  					"/MyShares-[pqrstu]": map[string]string{
    62  						"address": "home-01-shares",
    63  					},
    64  					"/MyShares-[v-z]": map[string]string{
    65  						"address": "home-02-shares",
    66  					},
    67  				},
    68  			},
    69  			"/eos/user/[a-fg-o]": map[string]interface{}{
    70  				"address": "home-00-eos",
    71  			},
    72  			"/eos/user/[pqrstu]": map[string]interface{}{
    73  				"address": "home-01-eos",
    74  			},
    75  			"/eos/user/[v-z]": map[string]interface{}{
    76  				"address": "home-02-eos",
    77  			},
    78  			"/eos/project": map[string]interface{}{
    79  				"address": "project-00",
    80  			},
    81  			"/eos/media": map[string]interface{}{
    82  				"address": "media-00",
    83  			},
    84  			"123e4567-e89b-12d3-a456-426655440000": map[string]interface{}{
    85  				"address": "home-00-home",
    86  			},
    87  			"123e4567-e89b-12d3-a456-426655440001": map[string]interface{}{
    88  				"address": "home-01-home",
    89  			},
    90  			"/eos/": map[string]interface{}{
    91  				"address": "unspecific-rule-that-should-never-been-hit",
    92  			},
    93  		},
    94  	})
    95  	Expect(err).ToNot(HaveOccurred())
    96  
    97  	ctxAlice := ctxpkg.ContextSetUser(context.Background(), &userpb.User{
    98  		Id: &userpb.UserId{
    99  			OpaqueId: "alice",
   100  		},
   101  	})
   102  	ctxRobert := ctxpkg.ContextSetUser(context.Background(), &userpb.User{
   103  		Id: &userpb.UserId{
   104  			OpaqueId: "robert",
   105  		},
   106  	})
   107  
   108  	Describe("GetProvider", func() {
   109  		It("get provider for user alice", func() {
   110  			provider, err := handler.GetProvider(ctxAlice, &provider.StorageSpace{
   111  				Owner: &userpb.User{
   112  					Id: &userpb.UserId{
   113  						OpaqueId: "alice",
   114  					},
   115  				},
   116  				SpaceType: "personal",
   117  			})
   118  			Expect(err).ToNot(HaveOccurred())
   119  			Expect(provider).To(Not(BeNil()))
   120  		})
   121  
   122  		It("get provider for user robert", func() {
   123  			provider, err := handler.GetProvider(ctxRobert, &provider.StorageSpace{
   124  				Owner: &userpb.User{
   125  					Id: &userpb.UserId{
   126  						OpaqueId: "robert",
   127  					},
   128  				},
   129  				SpaceType: "personal",
   130  			})
   131  			Expect(err).ToNot(HaveOccurred())
   132  			Expect(provider).To(Not(BeNil()))
   133  		})
   134  	})
   135  
   136  	home00 := &registrypb.ProviderInfo{
   137  		ProviderPath: "/home",
   138  		Address:      "home-00-home",
   139  	}
   140  	home01 := &registrypb.ProviderInfo{
   141  		ProviderPath: "/home",
   142  		Address:      "home-01-home",
   143  	}
   144  	/*
   145  		Describe("GetHome", func() {
   146  			It("get the home provider for user alice", func() {
   147  				home, err := handler.GetHome(ctxAlice)
   148  				Expect(err).ToNot(HaveOccurred())
   149  				Expect(home).To(Equal(home00))
   150  			})
   151  
   152  			It("get the home provider for user robert", func() {
   153  				home, err := handler.GetHome(ctxRobert)
   154  				Expect(err).ToNot(HaveOccurred())
   155  				Expect(home).To(Equal(home01))
   156  			})
   157  		})
   158  	*/
   159  
   160  	Describe("FindProviders for home path filter", func() {
   161  		filters := map[string]string{
   162  			"path": "/home/abcd",
   163  		}
   164  
   165  		It("finds all providers for user alice for a home path filter", func() {
   166  			providers, err := handler.ListProviders(ctxAlice, filters)
   167  			Expect(err).ToNot(HaveOccurred())
   168  			Expect(providers).To(Equal([]*registrypb.ProviderInfo{home00}))
   169  		})
   170  
   171  		It("finds all providers for user robert for a home path filter", func() {
   172  			providers, err := handler.ListProviders(ctxRobert, filters)
   173  			Expect(err).ToNot(HaveOccurred())
   174  			Expect(providers).To(Equal([]*registrypb.ProviderInfo{home01}))
   175  		})
   176  	})
   177  
   178  	Describe("FindProviders for eos path filter", func() {
   179  		filters := map[string]string{
   180  			"path": "/eos/user/b/bob/xyz",
   181  		}
   182  		eosUserB := &registrypb.ProviderInfo{
   183  			ProviderPath: "/eos/user/b",
   184  			Address:      "home-00-eos",
   185  		}
   186  
   187  		It("finds all providers for user alice for an eos path filter", func() {
   188  			providers, err := handler.ListProviders(ctxAlice, filters)
   189  			Expect(err).ToNot(HaveOccurred())
   190  			Expect(providers).To(Equal([]*registrypb.ProviderInfo{eosUserB}))
   191  		})
   192  
   193  		It("finds all providers for user robert for an eos path filter", func() {
   194  			providers, err := handler.ListProviders(ctxRobert, filters)
   195  			Expect(err).ToNot(HaveOccurred())
   196  			Expect(providers).To(Equal([]*registrypb.ProviderInfo{eosUserB}))
   197  		})
   198  	})
   199  
   200  	Describe("FindProviders for project reference", func() {
   201  		filters := map[string]string{
   202  			"path": "/eos/project/pqr",
   203  		}
   204  		eosProject := &registrypb.ProviderInfo{
   205  			ProviderPath: "/eos/project",
   206  			Address:      "project-00",
   207  		}
   208  
   209  		It("finds all providers for user alice for a project path filter", func() {
   210  			providers, err := handler.ListProviders(ctxAlice, filters)
   211  			Expect(err).ToNot(HaveOccurred())
   212  			Expect(providers).To(Equal([]*registrypb.ProviderInfo{eosProject}))
   213  		})
   214  
   215  		It("finds all providers for user robert for a project path filter", func() {
   216  			providers, err := handler.ListProviders(ctxRobert, filters)
   217  			Expect(err).ToNot(HaveOccurred())
   218  			Expect(providers).To(Equal([]*registrypb.ProviderInfo{eosProject}))
   219  		})
   220  	})
   221  
   222  	Describe("FindProviders for virtual references", func() {
   223  		filtersEos := map[string]string{
   224  			"path": "/eos",
   225  		}
   226  		filtersRoot := map[string]string{
   227  			"path": "/",
   228  		}
   229  
   230  		It("finds all providers for user alice for a virtual eos path filter", func() {
   231  			providers, err := handler.ListProviders(ctxAlice, filtersEos)
   232  			Expect(err).ToNot(HaveOccurred())
   233  			Expect(len(providers)).To(Equal(eosProviders))
   234  		})
   235  
   236  		It("finds all providers for user robert for a virtual eos path filter", func() {
   237  			providers, err := handler.ListProviders(ctxRobert, filtersEos)
   238  			Expect(err).ToNot(HaveOccurred())
   239  			Expect(len(providers)).To(Equal(eosProviders))
   240  		})
   241  
   242  		It("finds all providers for user alice for a virtual root path filter", func() {
   243  			providers, err := handler.ListProviders(ctxAlice, filtersRoot)
   244  			Expect(err).ToNot(HaveOccurred())
   245  			Expect(len(providers)).To(Equal(rootProviders))
   246  		})
   247  
   248  		It("finds all providers for user robert for a virtual root path filter", func() {
   249  			providers, err := handler.ListProviders(ctxRobert, filtersRoot)
   250  			Expect(err).ToNot(HaveOccurred())
   251  			Expect(len(providers)).To(Equal(rootProviders))
   252  		})
   253  	})
   254  
   255  	Describe("FindProviders for reference containing ID", func() {
   256  		filters := map[string]string{
   257  			"storage_id": "123e4567-e89b-12d3-a456-426655440000",
   258  		}
   259  		home00ID := &registrypb.ProviderInfo{
   260  			ProviderId: "123e4567-e89b-12d3-a456-426655440000",
   261  			Address:    "home-00-home",
   262  		}
   263  
   264  		It("finds all providers for user alice for filters containing ID", func() {
   265  			providers, err := handler.ListProviders(ctxAlice, filters)
   266  			Expect(err).ToNot(HaveOccurred())
   267  			Expect(providers).To(Equal([]*registrypb.ProviderInfo{home00ID}))
   268  		})
   269  
   270  		It("finds all providers for user robert for filters containing ID", func() {
   271  			providers, err := handler.ListProviders(ctxRobert, filters)
   272  			Expect(err).ToNot(HaveOccurred())
   273  			Expect(providers).To(Equal([]*registrypb.ProviderInfo{home00ID}))
   274  		})
   275  	})
   276  })