github.com/cs3org/reva/v2@v2.27.7/pkg/share/manager/jsoncs3/receivedsharecache/receivedsharecache_test.go (about)

     1  // Copyright 2018-2022 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 receivedsharecache_test
    20  
    21  import (
    22  	"context"
    23  	"os"
    24  	"time"
    25  
    26  	collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1"
    27  	"github.com/cs3org/reva/v2/pkg/share/manager/jsoncs3/receivedsharecache"
    28  	"github.com/cs3org/reva/v2/pkg/storage/utils/metadata"
    29  
    30  	. "github.com/onsi/ginkgo/v2"
    31  	. "github.com/onsi/gomega"
    32  )
    33  
    34  var _ = Describe("Cache", func() {
    35  	var (
    36  		c       receivedsharecache.Cache
    37  		storage metadata.Storage
    38  
    39  		userID  = "user"
    40  		spaceID = "spaceid"
    41  		shareID = "storageid$spaceid!share1"
    42  		share   = &collaboration.Share{
    43  			Id: &collaboration.ShareId{
    44  				OpaqueId: shareID,
    45  			},
    46  		}
    47  		ctx    context.Context
    48  		tmpdir string
    49  	)
    50  
    51  	BeforeEach(func() {
    52  		ctx = context.Background()
    53  
    54  		var err error
    55  		tmpdir, err = os.MkdirTemp("", "providercache-test")
    56  		Expect(err).ToNot(HaveOccurred())
    57  
    58  		err = os.MkdirAll(tmpdir, 0755)
    59  		Expect(err).ToNot(HaveOccurred())
    60  
    61  		storage, err = metadata.NewDiskStorage(tmpdir)
    62  		Expect(err).ToNot(HaveOccurred())
    63  
    64  		c = receivedsharecache.New(storage, 0*time.Second)
    65  		Expect(c).ToNot(BeNil()) //nolint:all
    66  	})
    67  
    68  	AfterEach(func() {
    69  		if tmpdir != "" {
    70  			os.RemoveAll(tmpdir)
    71  		}
    72  	})
    73  
    74  	Describe("Add", func() {
    75  		It("adds an entry", func() {
    76  			rs := &collaboration.ReceivedShare{
    77  				Share: share,
    78  				State: collaboration.ShareState_SHARE_STATE_PENDING,
    79  			}
    80  			err := c.Add(ctx, userID, spaceID, rs)
    81  			Expect(err).ToNot(HaveOccurred())
    82  
    83  			s, err := c.Get(ctx, userID, spaceID, shareID)
    84  			Expect(err).ToNot(HaveOccurred())
    85  			Expect(s).ToNot(BeNil())
    86  		})
    87  
    88  		It("persists the new entry", func() {
    89  			rs := &collaboration.ReceivedShare{
    90  				Share: share,
    91  				State: collaboration.ShareState_SHARE_STATE_PENDING,
    92  			}
    93  			err := c.Add(ctx, userID, spaceID, rs)
    94  			Expect(err).ToNot(HaveOccurred())
    95  
    96  			c = receivedsharecache.New(storage, 0*time.Second)
    97  			s, err := c.Get(ctx, userID, spaceID, shareID)
    98  			Expect(err).ToNot(HaveOccurred())
    99  			Expect(s).ToNot(BeNil())
   100  		})
   101  	})
   102  
   103  	Describe("with an existing entry", func() {
   104  		BeforeEach(func() {
   105  			rs := &collaboration.ReceivedShare{
   106  				Share: share,
   107  				State: collaboration.ShareState_SHARE_STATE_PENDING,
   108  			}
   109  			Expect(c.Add(ctx, userID, spaceID, rs)).To(Succeed())
   110  		})
   111  
   112  		Describe("Get", func() {
   113  			It("handles unknown users", func() {
   114  				s, err := c.Get(ctx, "something", spaceID, shareID)
   115  				Expect(err).ToNot(HaveOccurred())
   116  				Expect(s).To(BeNil())
   117  			})
   118  
   119  			It("handles unknown spaces", func() {
   120  				s, err := c.Get(ctx, userID, "something", shareID)
   121  				Expect(err).ToNot(HaveOccurred())
   122  				Expect(s).To(BeNil())
   123  			})
   124  
   125  			It("handles unknown shares", func() {
   126  				s, err := c.Get(ctx, userID, spaceID, "something")
   127  				Expect(err).ToNot(HaveOccurred())
   128  				Expect(s).To(BeNil())
   129  			})
   130  
   131  			It("gets the entry", func() {
   132  				s, err := c.Get(ctx, userID, spaceID, shareID)
   133  				Expect(err).ToNot(HaveOccurred())
   134  				Expect(s).ToNot(BeNil())
   135  			})
   136  		})
   137  
   138  		Describe("Remove", func() {
   139  			It("removes the entry", func() {
   140  				err := c.Remove(ctx, userID, spaceID, shareID)
   141  				Expect(err).ToNot(HaveOccurred())
   142  
   143  				s, err := c.Get(ctx, userID, spaceID, shareID)
   144  				Expect(err).ToNot(HaveOccurred())
   145  				Expect(s).To(BeNil())
   146  			})
   147  
   148  			It("persists the removal", func() {
   149  				err := c.Remove(ctx, userID, spaceID, shareID)
   150  				Expect(err).ToNot(HaveOccurred())
   151  
   152  				c = receivedsharecache.New(storage, 0*time.Second)
   153  				s, err := c.Get(ctx, userID, spaceID, shareID)
   154  				Expect(err).ToNot(HaveOccurred())
   155  				Expect(s).To(BeNil())
   156  			})
   157  		})
   158  	})
   159  })