forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels_test.go
104 lines (93 loc) · 2.23 KB
/
models_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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": {"headers": {"101": [{"header": "X-CUSTOM-HEADER", "value": "foo"}]}}}`,
want: &TeamHTTPHeaders{
Headers: TeamHeaders{
"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)
})
}
}