github.com/cs3org/reva/v2@v2.27.7/internal/grpc/services/gateway/appprovider_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 gateway 20 21 import ( 22 "context" 23 "testing" 24 25 providerpb "github.com/cs3org/go-cs3apis/cs3/app/provider/v1beta1" 26 auth "github.com/cs3org/go-cs3apis/cs3/auth/provider/v1beta1" 27 gatewayv1beta1 "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1" 28 user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" 29 providerv1beta1 "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" 30 ctxpkg "github.com/cs3org/reva/v2/pkg/ctx" 31 "github.com/cs3org/reva/v2/pkg/utils" 32 ) 33 34 type mockTokenManager struct{} 35 36 func (m *mockTokenManager) MintToken(ctx context.Context, u *user.User, scope map[string]*auth.Scope) (string, error) { 37 return "mockToken", nil 38 } 39 40 func (m *mockTokenManager) DismantleToken(ctx context.Context, token string) (*user.User, map[string]*auth.Scope, error) { 41 return nil, nil, nil 42 } 43 44 func TestBuildOpenInAppRequest(t *testing.T) { 45 tokenmgr := &mockTokenManager{} 46 t.Run("Write mode", func(t *testing.T) { 47 ri := &providerv1beta1.ResourceInfo{} 48 req, err := buildOpenInAppRequest(context.Background(), ri, gatewayv1beta1.OpenInAppRequest_VIEW_MODE_READ_WRITE, tokenmgr, "accessToken", nil) 49 if err != nil { 50 t.Errorf("Unexpected error: %v", err) 51 } 52 if req.ViewMode != providerpb.ViewMode(gatewayv1beta1.OpenInAppRequest_VIEW_MODE_READ_WRITE) { 53 t.Errorf("Unexpected view mode. Got: %v, want: %v", req.ViewMode, providerpb.ViewMode(gatewayv1beta1.OpenInAppRequest_VIEW_MODE_READ_WRITE)) 54 } 55 if req.AccessToken != "accessToken" { 56 t.Errorf("Unexpected access token. Got: %v, want: %v", req.AccessToken, "accessToken") 57 } 58 if req.ResourceInfo != ri { 59 t.Errorf("Unexpected resource info. Got: %v, want: %v", req.ResourceInfo, ri) 60 } 61 if utils.ReadPlainFromOpaque(req.Opaque, "viewOnlyToken") != "" { 62 t.Errorf("Unexpected opaque. Got: %v, want: %v", req.Opaque, "") 63 } 64 }) 65 66 t.Run("View only mode without stat permission will not mint a viewOnlyToken", func(t *testing.T) { 67 ri := &providerv1beta1.ResourceInfo{} 68 req, err := buildOpenInAppRequest(context.Background(), ri, gatewayv1beta1.OpenInAppRequest_VIEW_MODE_VIEW_ONLY, tokenmgr, "accessToken", nil) 69 if err != nil { 70 t.Errorf("Unexpected error: %v", err) 71 } 72 if req.ViewMode != providerpb.ViewMode(gatewayv1beta1.OpenInAppRequest_VIEW_MODE_VIEW_ONLY) { 73 t.Errorf("Unexpected view mode. Got: %v, want: %v", req.ViewMode, providerpb.ViewMode(gatewayv1beta1.OpenInAppRequest_VIEW_MODE_VIEW_ONLY)) 74 } 75 if req.AccessToken != "accessToken" { 76 t.Errorf("Unexpected access token. Got: %v, want: %v", req.AccessToken, "accessToken") 77 } 78 if req.ResourceInfo != ri { 79 t.Errorf("Unexpected resource info. Got: %v, want: %v", req.ResourceInfo, ri) 80 } 81 if utils.ReadPlainFromOpaque(req.Opaque, "viewOnlyToken") != "" { 82 t.Errorf("Unexpected opaque. Got: %v, want: %v", req.Opaque, "") 83 } 84 }) 85 t.Run("View only mode with stat permission will mint a viewOnlyToken", func(t *testing.T) { 86 ri := &providerv1beta1.ResourceInfo{ 87 PermissionSet: &providerv1beta1.ResourcePermissions{ 88 Stat: true, 89 }, 90 } 91 ctx := ctxpkg.ContextSetUser(context.Background(), &user.User{Username: "a user without download permission"}) 92 req, err := buildOpenInAppRequest(ctx, ri, gatewayv1beta1.OpenInAppRequest_VIEW_MODE_VIEW_ONLY, tokenmgr, "accessToken", nil) 93 if err != nil { 94 t.Errorf("Unexpected error: %v", err) 95 } 96 if req.ViewMode != providerpb.ViewMode(gatewayv1beta1.OpenInAppRequest_VIEW_MODE_VIEW_ONLY) { 97 t.Errorf("Unexpected view mode. Got: %v, want: %v", req.ViewMode, providerpb.ViewMode(gatewayv1beta1.OpenInAppRequest_VIEW_MODE_VIEW_ONLY)) 98 } 99 if req.AccessToken != "accessToken" { 100 t.Errorf("Unexpected access token. Got: %v, want: %v", req.AccessToken, "accessToken") 101 } 102 if req.ResourceInfo != ri { 103 t.Errorf("Unexpected resource info. Got: %v, want: %v", req.ResourceInfo, ri) 104 } 105 if utils.ReadPlainFromOpaque(req.Opaque, "viewOnlyToken") != "mockToken" { 106 t.Errorf("Unexpected opaque. Got: %v, want: %v", req.Opaque, "mockToken") 107 } 108 }) 109 }