github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/scope/context_test.go (about)

     1  package scope_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/kyma-incubator/compass/components/director/pkg/scope"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestScopesContext(t *testing.T) {
    14  	t.Run("load returns scopes previously saved in context", func(t *testing.T) {
    15  		// GIVEN
    16  		givenScopes := []string{"aaa", "bbb"}
    17  		ctx := scope.SaveToContext(context.TODO(), givenScopes)
    18  		// WHEN
    19  		actual, err := scope.LoadFromContext(ctx)
    20  		// THEN
    21  		require.NoError(t, err)
    22  		assert.Equal(t, givenScopes, actual)
    23  	})
    24  	t.Run("load returns error if scopes not found in ctx", func(t *testing.T) {
    25  		// WHEN
    26  		_, err := scope.LoadFromContext(context.TODO())
    27  		// THEN
    28  		assert.EqualError(t, err, "cannot read scopes from context")
    29  	})
    30  
    31  	t.Run("cannot override scopes accidentally", func(t *testing.T) {
    32  		// GIVEN
    33  		givenScopes := []string{"aaa", "bbb"}
    34  		ctx := scope.SaveToContext(context.TODO(), givenScopes)
    35  
    36  		ctx = context.WithValue(ctx, 0, "some random value")
    37  		// WHEN
    38  		actual, err := scope.LoadFromContext(ctx)
    39  		// THEN
    40  		require.NoError(t, err)
    41  		assert.Equal(t, givenScopes, actual)
    42  	})
    43  }