github.com/cs3org/reva/v2@v2.27.7/pkg/publicshare/manager/memory/memory_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 memory
    20  
    21  // import (
    22  // 	"context"
    23  // 	"testing"
    24  
    25  // 	userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
    26  // 	link "github.com/cs3org/go-cs3apis/cs3/sharing/link/v1beta1"
    27  // 	provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
    28  // 	types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
    29  // )
    30  
    31  // func TestMemoryProvider(t *testing.T) {
    32  // 	// table driven tests is perhaps more readable
    33  // 	// setup a new public shares manager
    34  // 	manager, err := New(make(map[string]interface{}))
    35  // 	if err != nil {
    36  // 		t.Error(err)
    37  // 	}
    38  
    39  // 	// Setup dat
    40  // 	user := userpb.User{}
    41  // 	rInfo := provider.ResourceInfo{}
    42  // 	grant := link.Grant{}
    43  
    44  // 	rInfo.ArbitraryMetadata = &provider.ArbitraryMetadata{
    45  // 		Metadata: map[string]string{
    46  // 			"name": "woof",
    47  // 		},
    48  // 	}
    49  
    50  // 	// create a new public share
    51  // 	share, _ := manager.CreatePublicShare(context.Background(), &user, &rInfo, &grant)
    52  
    53  // 	// store its token for further retrieval
    54  // 	shareToken := share.GetToken()
    55  
    56  // 	// Test updating a public share.
    57  // 	existingRefToken := link.PublicShareReference{
    58  // 		Spec: &link.PublicShareReference_Token{
    59  // 			Token: shareToken,
    60  // 		},
    61  // 	}
    62  
    63  // 	nonExistingPublicShareRef := link.PublicShareReference{
    64  // 		Spec: &link.PublicShareReference_Token{Token: "somethingsomething"},
    65  // 	}
    66  
    67  // 	updatedMtime := &types.Timestamp{Seconds: uint64(46800)}
    68  
    69  // 	newGrant := link.Grant{
    70  // 		Permissions: &link.PublicSharePermissions{},
    71  // 		Expiration:  updatedMtime,
    72  // 	}
    73  
    74  // 	// attempt to update an invalid public share. we expect an error
    75  // 	_, err = manager.UpdatePublicShare(context.Background(), &user, &nonExistingPublicShareRef, &newGrant)
    76  // 	if err == nil {
    77  // 		t.Error(err)
    78  // 	}
    79  
    80  // 	// update an existing public share
    81  // 	updatedShare, err := manager.UpdatePublicShare(context.Background(), &user, &existingRefToken, &newGrant)
    82  // 	if err != nil {
    83  // 		t.Error(err)
    84  // 	}
    85  
    86  // 	// verify the expiration was updated to 01/01/1970 @ 1:00pm (UTC)
    87  // 	if updatedShare.Expiration == updatedMtime {
    88  // 		t.Error("")
    89  // 	}
    90  
    91  // 	// test getting an invalid token
    92  // 	_, err = manager.GetPublicShareByToken(context.Background(), "xxxxxxxx")
    93  // 	if err == nil {
    94  // 		t.Error(err)
    95  // 	}
    96  
    97  // 	// test getting a valid token
    98  // 	fetchedPs, err := manager.GetPublicShareByToken(context.Background(), shareToken)
    99  // 	if err != nil {
   100  // 		t.Error(err)
   101  // 	}
   102  
   103  // 	if fetchedPs.GetToken() != shareToken {
   104  // 		t.Error("mismatching public share tokens")
   105  // 	}
   106  
   107  // 	// test listing public shares
   108  // 	listPs, err := manager.ListPublicShares(context.Background(), &user, nil, &rInfo)
   109  // 	if err != nil {
   110  // 		t.Error(err)
   111  // 	}
   112  
   113  // 	if len(listPs) != 1 {
   114  // 		t.Errorf("expected list of length 1, but got %v", len(listPs))
   115  // 	}
   116  
   117  // 	// holds a reference of hte public share with the previously fetched token
   118  // 	publicShareRef := link.PublicShareReference{
   119  // 		Spec: &link.PublicShareReference_Token{Token: shareToken},
   120  // 	}
   121  
   122  // 	// error expected
   123  // 	_, err = manager.GetPublicShare(context.Background(), &user, &nonExistingPublicShareRef)
   124  // 	if err == nil {
   125  // 		t.Error(err)
   126  // 	}
   127  
   128  // 	// expected error to be nil
   129  // 	pShare, err := manager.GetPublicShare(context.Background(), &user, &publicShareRef)
   130  // 	if err != nil {
   131  // 		t.Error(err)
   132  // 	}
   133  
   134  // 	existingRefID := link.PublicShareReference{
   135  // 		Spec: &link.PublicShareReference_Id{
   136  // 			Id: pShare.GetId(),
   137  // 		},
   138  // 	}
   139  
   140  // 	nonExistingRefID := link.PublicShareReference{
   141  // 		Spec: &link.PublicShareReference_Id{
   142  // 			Id: &link.PublicShareId{
   143  // 				OpaqueId: "doesnt_exist",
   144  // 			},
   145  // 		},
   146  // 	}
   147  
   148  // 	// get public share by ID... we don't expect an error
   149  // 	_, err = manager.GetPublicShare(context.Background(), &user, &existingRefID)
   150  // 	if err != nil {
   151  // 		t.Error(err)
   152  // 	}
   153  
   154  // 	// get public share by ID... we expect an error
   155  // 	_, err = manager.GetPublicShare(context.Background(), &user, &nonExistingRefID)
   156  // 	if err == nil {
   157  // 		t.Error(err)
   158  // 	}
   159  
   160  // 	// attempts to revoke a public share that does not exist, we expect an error
   161  // 	err = manager.RevokePublicShare(context.Background(), &user, "ref_does_not_exist")
   162  // 	if err == nil {
   163  // 		t.Error("expected a failure when revoking a public share that does not exist")
   164  // 	}
   165  
   166  // 	// revoke an existing public share
   167  // 	err = manager.RevokePublicShare(context.Background(), &user, fetchedPs.GetToken())
   168  // 	if err != nil {
   169  // 		t.Error(err)
   170  // 	}
   171  // }