github.com/cs3org/reva/v2@v2.27.7/pkg/share/manager/json/json_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 json_test
    20  
    21  import (
    22  	"context"
    23  	"os"
    24  	"sync"
    25  
    26  	userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
    27  	collaboration "github.com/cs3org/go-cs3apis/cs3/sharing/collaboration/v1beta1"
    28  	providerv1beta1 "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
    29  	ctxpkg "github.com/cs3org/reva/v2/pkg/ctx"
    30  	"github.com/cs3org/reva/v2/pkg/share"
    31  	"github.com/cs3org/reva/v2/pkg/share/manager/json"
    32  	"google.golang.org/protobuf/types/known/fieldmaskpb"
    33  
    34  	. "github.com/onsi/ginkgo/v2"
    35  	. "github.com/onsi/gomega"
    36  )
    37  
    38  var _ = Describe("Json", func() {
    39  	var (
    40  		user1 = &userpb.User{
    41  			Id: &userpb.UserId{
    42  				Idp:      "https://localhost:9200",
    43  				OpaqueId: "admin",
    44  			},
    45  		}
    46  		user2 = &userpb.User{
    47  			Id: &userpb.UserId{
    48  				Idp:      "https://localhost:9200",
    49  				OpaqueId: "einstein",
    50  			},
    51  		}
    52  
    53  		sharedResource = &providerv1beta1.ResourceInfo{
    54  			Id: &providerv1beta1.ResourceId{
    55  				StorageId: "storageid",
    56  				OpaqueId:  "opaqueid",
    57  			},
    58  		}
    59  
    60  		m          share.Manager
    61  		tmpFile    *os.File
    62  		ctx        context.Context
    63  		granteeCtx context.Context
    64  	)
    65  
    66  	BeforeEach(func() {
    67  		var err error
    68  		tmpFile, err = os.CreateTemp("", "reva-unit-test-*.json")
    69  		Expect(err).ToNot(HaveOccurred())
    70  
    71  		config := map[string]interface{}{
    72  			"file":         tmpFile.Name(),
    73  			"gateway_addr": "https://localhost:9200",
    74  		}
    75  		m, err = json.New(config)
    76  		Expect(err).ToNot(HaveOccurred())
    77  
    78  		ctx = ctxpkg.ContextSetUser(context.Background(), user1)
    79  		granteeCtx = ctxpkg.ContextSetUser(context.Background(), user2)
    80  	})
    81  
    82  	AfterEach(func() {
    83  		os.Remove(tmpFile.Name())
    84  	})
    85  
    86  	Describe("Dump", func() {
    87  		JustBeforeEach(func() {
    88  			share, err := m.Share(ctx, sharedResource, &collaboration.ShareGrant{
    89  				Grantee: &providerv1beta1.Grantee{
    90  					Type: providerv1beta1.GranteeType_GRANTEE_TYPE_USER,
    91  					Id:   &providerv1beta1.Grantee_UserId{UserId: user2.Id},
    92  				},
    93  			})
    94  			Expect(err).ToNot(HaveOccurred())
    95  
    96  			rs, err := m.GetReceivedShare(granteeCtx, &collaboration.ShareReference{Spec: &collaboration.ShareReference_Id{Id: share.Id}})
    97  			Expect(err).ToNot(HaveOccurred())
    98  			Expect(rs.State).To(Equal(collaboration.ShareState_SHARE_STATE_PENDING))
    99  			rs.State = collaboration.ShareState_SHARE_STATE_ACCEPTED
   100  			rs.MountPoint = &providerv1beta1.Reference{Path: "newPath/"}
   101  
   102  			_, err = m.UpdateReceivedShare(granteeCtx,
   103  				rs, &fieldmaskpb.FieldMask{Paths: []string{"state", "mount_point"}}, nil)
   104  			Expect(err).ToNot(HaveOccurred())
   105  		})
   106  
   107  		It("dumps all shares", func() {
   108  			sharesChan := make(chan *collaboration.Share)
   109  			receivedChan := make(chan share.ReceivedShareWithUser)
   110  
   111  			shares := []*collaboration.Share{}
   112  
   113  			wg := sync.WaitGroup{}
   114  			wg.Add(2)
   115  			go func() {
   116  				for s := range sharesChan {
   117  					if s != nil {
   118  						shares = append(shares, s)
   119  					}
   120  				}
   121  				wg.Done()
   122  			}()
   123  			go func() {
   124  				for range receivedChan {
   125  				}
   126  				wg.Done()
   127  			}()
   128  			err := m.(share.DumpableManager).Dump(ctx, sharesChan, receivedChan)
   129  			Expect(err).ToNot(HaveOccurred())
   130  			close(sharesChan)
   131  			close(receivedChan)
   132  			wg.Wait()
   133  
   134  			Expect(len(shares)).To(Equal(1))
   135  			Expect(shares[0].Creator).To(Equal(user1.Id))
   136  			Expect(shares[0].Grantee.GetUserId()).To(Equal(user2.Id))
   137  			Expect(shares[0].ResourceId).To(Equal(sharedResource.Id))
   138  		})
   139  
   140  		It("dumps all received shares", func() {
   141  			sharesChan := make(chan *collaboration.Share)
   142  			receivedChan := make(chan share.ReceivedShareWithUser)
   143  
   144  			shares := []share.ReceivedShareWithUser{}
   145  
   146  			wg := sync.WaitGroup{}
   147  			wg.Add(2)
   148  			go func() {
   149  				for range sharesChan {
   150  				}
   151  				wg.Done()
   152  			}()
   153  			go func() {
   154  				for rs := range receivedChan {
   155  					if rs.UserID != nil && rs.ReceivedShare != nil {
   156  						shares = append(shares, rs)
   157  					}
   158  				}
   159  
   160  				wg.Done()
   161  			}()
   162  			err := m.(share.DumpableManager).Dump(ctx, sharesChan, receivedChan)
   163  			Expect(err).ToNot(HaveOccurred())
   164  			close(sharesChan)
   165  			close(receivedChan)
   166  			wg.Wait()
   167  
   168  			Expect(len(shares)).To(Equal(1))
   169  			Expect(shares[0].UserID).To(Equal(user2.Id))
   170  			Expect(shares[0].ReceivedShare.State).To(Equal(collaboration.ShareState_SHARE_STATE_ACCEPTED))
   171  			Expect(shares[0].ReceivedShare.MountPoint.Path).To(Equal("newPath/"))
   172  			Expect(shares[0].ReceivedShare.Share.Creator).To(Equal(user1.Id))
   173  			Expect(shares[0].ReceivedShare.Share.Grantee.GetUserId()).To(Equal(user2.Id))
   174  			Expect(shares[0].ReceivedShare.Share.ResourceId).To(Equal(sharedResource.Id))
   175  		})
   176  	})
   177  })