grafana/pkg/services/datasources/models_test.go
Eric Leijonmarck be5ba68132
Team LBAC: Add teamHeaders for datasource proxy requests (#76339)
* Add teamHeaders for datasource proxy requests

* adds validation for the teamHeaders

* added tests for applying teamHeaders

* remove previous implementation

* validation for header values being set to authproxy

* removed unnecessary checks

* newline

* Add middleware for injecting headers on the data source backend

* renamed feature toggle

* Get user teams from context

* Fix feature toggle name

* added test for validation of the auth headers and fixed evaluation to cover headers

* renaming of teamHeaders to teamHTTPHeaders

* use of header set for non-existing header and add for existing headers

* moves types into datasources

* fixed unchecked errors

* Refactor

* Add tests for data model

* Update pkg/api/datasources.go

Co-authored-by: Victor Cinaglia <victor@grafana.com>

* Update pkg/api/datasources.go

Co-authored-by: Victor Cinaglia <victor@grafana.com>

---------

Co-authored-by: Alexander Zobnin <alexanderzobnin@gmail.com>
Co-authored-by: Victor Cinaglia <victor@grafana.com>
2023-10-17 11:23:54 +01:00

103 lines
2.2 KiB
Go

package datasources
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/components/simplejson"
)
func TestAllowedCookies(t *testing.T) {
testCases := []struct {
desc string
given map[string]any
want []string
}{
{
desc: "Usual json data with keepCookies",
given: map[string]any{
"keepCookies": []string{"cookie2"},
},
want: []string{"cookie2"},
},
{
desc: "Usual json data without kepCookies",
given: map[string]any{
"something": "somethingelse",
},
want: []string(nil),
},
{
desc: "Usual json data that has multiple values in keepCookies",
given: map[string]any{
"keepCookies": []string{"cookie1", "cookie2", "special[]"},
},
want: []string{"cookie1", "cookie2", "special[]"},
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
jsonDataBytes, err := json.Marshal(&test.given)
require.NoError(t, err)
jsonData, err := simplejson.NewJson(jsonDataBytes)
require.NoError(t, err)
ds := DataSource{
ID: 1235,
JsonData: jsonData,
UID: "test",
}
actual := ds.AllowedCookies()
assert.Equal(t, test.want, actual)
assert.EqualValues(t, test.want, actual)
})
}
}
func TestTeamHTTPHeaders(t *testing.T) {
testCases := []struct {
desc string
given string
want TeamHTTPHeaders
}{
{
desc: "Usual json data with teamHttpHeaders",
given: `{"teamHttpHeaders": {"101": [{"header": "X-CUSTOM-HEADER", "value": "foo"}]}}`,
want: TeamHTTPHeaders{
"101": {
{Header: "X-CUSTOM-HEADER", Value: "foo"},
},
},
},
{
desc: "Json data without teamHttpHeaders",
given: `{"foo": "bar"}`,
want: nil,
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
jsonDataBytes := []byte(test.given)
jsonData, err := simplejson.NewJson(jsonDataBytes)
require.NoError(t, err)
ds := DataSource{
ID: 1235,
JsonData: jsonData,
UID: "test",
}
actual, err := ds.TeamHTTPHeaders()
assert.NoError(t, err)
assert.Equal(t, test.want, actual)
assert.EqualValues(t, test.want, actual)
})
}
}