github.com/cs3org/reva/v2@v2.27.7/pkg/token/manager/demo/demo_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 demo
    20  
    21  import (
    22  	"context"
    23  	"encoding/json"
    24  	"testing"
    25  
    26  	auth "github.com/cs3org/go-cs3apis/cs3/auth/provider/v1beta1"
    27  	user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
    28  	provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
    29  	types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
    30  )
    31  
    32  var ctx = context.Background()
    33  
    34  func TestEncodeDecode(t *testing.T) {
    35  	m, _ := New(nil)
    36  	u := &user.User{
    37  		Username: "marie",
    38  	}
    39  
    40  	ref := &provider.Reference{Path: "/"}
    41  	val, err := json.Marshal(ref)
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  	scope := map[string]*auth.Scope{
    46  		"user": {
    47  			Resource: &types.OpaqueEntry{
    48  				Decoder: "json",
    49  				Value:   val,
    50  			},
    51  			Role: auth.Role_ROLE_OWNER,
    52  		},
    53  	}
    54  
    55  	encoded, err := m.MintToken(ctx, u, scope)
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  
    60  	decodedUser, decodedScope, err := m.DismantleToken(ctx, encoded)
    61  	if err != nil {
    62  		t.Fatal(err)
    63  	}
    64  
    65  	if u.Username != decodedUser.Username {
    66  		t.Fatalf("mail claims differ: expected=%s got=%s", u.Username, decodedUser.Username)
    67  	}
    68  
    69  	if s, ok := decodedScope["user"]; !ok || s.Role != auth.Role_ROLE_OWNER {
    70  		t.Fatalf("scope claims differ: expected=%s got=%s", scope, decodedScope)
    71  	}
    72  }