github.com/cs3org/reva/v2@v2.27.7/pkg/appctx/ctxmap_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 appctx
    20  
    21  import (
    22  	"context"
    23  	"testing"
    24  
    25  	userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  type ctxStringKey string
    30  type ctxIntKey int
    31  
    32  func TestGetKeyValues(t *testing.T) {
    33  	tests := []struct {
    34  		name string
    35  		ctx  context.Context
    36  		m    map[interface{}]interface{}
    37  	}{
    38  		{
    39  			"Background context",
    40  			context.Background(),
    41  			map[interface{}]interface{}{},
    42  		},
    43  		{
    44  			"Context with Values",
    45  			context.WithValue(context.Background(), ctxStringKey("key"), "value"),
    46  			map[interface{}]interface{}{
    47  				ctxStringKey("key"): "value",
    48  			},
    49  		},
    50  		{
    51  			"Context with user object",
    52  			context.WithValue(context.WithValue(context.Background(), ctxStringKey("key"), "value"), ctxStringKey("user"), &userpb.User{Username: "einstein"}),
    53  			map[interface{}]interface{}{
    54  				ctxStringKey("key"):  "value",
    55  				ctxStringKey("user"): &userpb.User{Username: "einstein"},
    56  			},
    57  		},
    58  		{
    59  			"Nested Context with Values of different types",
    60  			context.WithValue(context.WithValue(context.Background(), ctxStringKey("key"), "value"), ctxIntKey(123), "value2"),
    61  			map[interface{}]interface{}{
    62  				ctxStringKey("key"): "value",
    63  				ctxIntKey(123):      "value2",
    64  			},
    65  		},
    66  	}
    67  
    68  	for _, tt := range tests {
    69  		t.Run(tt.name, func(t *testing.T) {
    70  			kvMap := GetKeyValuesFromCtx(tt.ctx)
    71  			assert.Equal(t, tt.m, kvMap)
    72  		})
    73  	}
    74  }
    75  
    76  func TestPutKeyValues(t *testing.T) {
    77  	tests := []struct {
    78  		name string
    79  		m    map[interface{}]interface{}
    80  		ctx  context.Context
    81  	}{
    82  		{
    83  			"empty context",
    84  			map[interface{}]interface{}{},
    85  			context.Background(),
    86  		},
    87  		{
    88  			"single kv pair",
    89  			map[interface{}]interface{}{
    90  				ctxStringKey("key"): "value",
    91  			},
    92  			context.WithValue(context.Background(), ctxStringKey("key"), "value"),
    93  		},
    94  	}
    95  
    96  	for _, tt := range tests {
    97  		t.Run(tt.name, func(t *testing.T) {
    98  			ctx := PutKeyValuesToCtx(tt.m)
    99  			assert.Equal(t, tt.ctx, ctx)
   100  		})
   101  	}
   102  }