github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/config/scopes_test.go (about) 1 package config_test 2 3 import ( 4 "testing" 5 6 "github.com/kyma-incubator/compass/components/director/pkg/config" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestProvider_GetRequiredScopes(t *testing.T) { 13 t.Run("requires Load", func(t *testing.T) { 14 sut := config.NewProvider("anything") 15 _, err := sut.GetRequiredScopes("graphql.query.runtime") 16 require.Error(t, err, "required scopes configuration not loaded") 17 }) 18 19 // GIVEN 20 sut := config.NewProvider("testdata/valid.yaml") 21 require.NoError(t, sut.Load()) 22 23 t.Run("returns single scope", func(t *testing.T) { 24 // WHEN 25 actual, err := sut.GetRequiredScopes("graphql.query.runtime") 26 // THEN 27 require.NoError(t, err) 28 assert.Equal(t, actual, []string{"runtime:get"}) 29 }) 30 31 t.Run("returns many scopes", func(t *testing.T) { 32 // WHEN 33 actual, err := sut.GetRequiredScopes("graphql.mutation.createApplication") 34 // THEN 35 require.NoError(t, err) 36 assert.Equal(t, actual, []string{"application:create", "global:create"}) 37 }) 38 39 t.Run("returns error if required scopes are empty", func(t *testing.T) { 40 // WHEN 41 _, err := sut.GetRequiredScopes("graphql.mutation.empty") 42 // THEN 43 require.Error(t, err) 44 assert.EqualError(t, err, "required scopes are not defined") 45 }) 46 47 t.Run("returns error if path not found", func(t *testing.T) { 48 // WHEN 49 _, err := sut.GetRequiredScopes("does.not.exist") 50 // THEN 51 require.EqualError(t, err, "while searching configuration using path $.does.not.exist: key error: does not found in object") 52 }) 53 54 t.Run("return error if path is invalid", func(t *testing.T) { 55 // WHEN 56 _, err := sut.GetRequiredScopes("...graphql") 57 // THEN 58 require.Error(t, err, "while searching configuration using path $....graphql: expression don't support in filter") 59 }) 60 61 t.Run("returns error if path points to invalid type", func(t *testing.T) { 62 // WHEN 63 _, err := sut.GetRequiredScopes("graphql.query") 64 // THEN 65 require.EqualError(t, err, "unexpected scopes definition, should be string or list of strings, but was map[string]interface {}") 66 }) 67 68 t.Run("returns error if path points to list with invalid types", func(t *testing.T) { 69 // WHEN 70 _, err := sut.GetRequiredScopes("graphql.mutation.updateApplication") 71 // THEN 72 require.EqualError(t, err, "unexpected float64 value in a string list") 73 }) 74 } 75 76 func TestProvider_GetRequiredGrantTypes(t *testing.T) { 77 const grantTypesPath = "clientCredentialsRegistrationGrantTypes" 78 var expectedGrantTypes = []string{"client_credentials"} 79 80 t.Run("requires Load", func(t *testing.T) { 81 sut := config.NewProvider("anything") 82 _, err := sut.GetRequiredGrantTypes(grantTypesPath) 83 require.Error(t, err, "required scopes configuration not loaded") 84 }) 85 86 // GIVEN 87 sut := config.NewProvider("testdata/valid.yaml") 88 require.NoError(t, sut.Load()) 89 90 t.Run("returns grant types", func(t *testing.T) { 91 // WHEN 92 actual, err := sut.GetRequiredGrantTypes(grantTypesPath) 93 // THEN 94 require.NoError(t, err) 95 assert.Equal(t, actual, expectedGrantTypes) 96 }) 97 98 t.Run("returns error when single value is provided instead of a list", func(t *testing.T) { 99 // WHEN 100 _, err := sut.GetRequiredGrantTypes("graphql.query.runtime") 101 // THEN 102 require.EqualError(t, err, "unexpected grant_types definition, should be a list of strings, but was string") 103 }) 104 105 t.Run("returns error if required scopes are empty", func(t *testing.T) { 106 // WHEN 107 _, err := sut.GetRequiredGrantTypes("graphql.mutation.empty") 108 // THEN 109 require.Error(t, err) 110 assert.EqualError(t, err, "required scopes are not defined") 111 }) 112 113 t.Run("returns error if path not found", func(t *testing.T) { 114 // WHEN 115 _, err := sut.GetRequiredGrantTypes("does.not.exist") 116 // THEN 117 require.EqualError(t, err, "while searching configuration using path $.does.not.exist: key error: does not found in object") 118 }) 119 120 t.Run("return error if path is invalid", func(t *testing.T) { 121 // WHEN 122 _, err := sut.GetRequiredGrantTypes("...graphql") 123 // THEN 124 require.Error(t, err, "while searching configuration using path $....graphql: expression don't support in filter") 125 }) 126 127 t.Run("returns error if path points to invalid type", func(t *testing.T) { 128 // WHEN 129 _, err := sut.GetRequiredGrantTypes("graphql.query") 130 // THEN 131 require.EqualError(t, err, "unexpected grant_types definition, should be a list of strings, but was map[string]interface {}") 132 }) 133 134 t.Run("returns error if path points to list with invalid types", func(t *testing.T) { 135 // WHEN 136 _, err := sut.GetRequiredScopes("graphql.mutation.updateApplication") 137 // THEN 138 require.EqualError(t, err, "unexpected float64 value in a string list") 139 }) 140 }