API: Rewrite tests from goconvey (#29091)

* API: Rewrite tests from goconvey

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Fix test

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

* Fix tests

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
Arve Knudsen
2020-11-17 11:31:35 +01:00
committed by GitHub
parent 2bf964c61e
commit 8c765e8068
6 changed files with 283 additions and 264 deletions

View File

@@ -9,11 +9,12 @@ import (
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPluginProxy(t *testing.T) {
Convey("When getting proxy headers", t, func() {
t.Run("When getting proxy headers", func(t *testing.T) {
route := &plugins.AppPluginRoute{
Headers: []plugins.AppPluginRouteHeader{
{Name: "x-header", Content: "my secret {{.SecureJsonData.key}}"},
@@ -37,6 +38,7 @@ func TestPluginProxy(t *testing.T) {
})
req := getPluginProxiedRequest(
t,
&models.ReqContext{
SignedInUser: &models.SignedInUser{
Login: "test_user",
@@ -46,13 +48,12 @@ func TestPluginProxy(t *testing.T) {
route,
)
Convey("Should render header template", func() {
So(req.Header.Get("x-header"), ShouldEqual, "my secret 123")
})
assert.Equal(t, "my secret 123", req.Header.Get("x-header"))
})
Convey("When SendUserHeader config is enabled", t, func() {
t.Run("When SendUserHeader config is enabled", func(t *testing.T) {
req := getPluginProxiedRequest(
t,
&models.ReqContext{
SignedInUser: &models.SignedInUser{
Login: "test_user",
@@ -62,14 +63,13 @@ func TestPluginProxy(t *testing.T) {
nil,
)
Convey("Should add header with username", func() {
// Get will return empty string even if header is not set
So(req.Header.Get("X-Grafana-User"), ShouldEqual, "test_user")
})
// Get will return empty string even if header is not set
assert.Equal(t, "test_user", req.Header.Get("X-Grafana-User"))
})
Convey("When SendUserHeader config is disabled", t, func() {
t.Run("When SendUserHeader config is disabled", func(t *testing.T) {
req := getPluginProxiedRequest(
t,
&models.ReqContext{
SignedInUser: &models.SignedInUser{
Login: "test_user",
@@ -78,14 +78,13 @@ func TestPluginProxy(t *testing.T) {
&setting.Cfg{SendUserHeader: false},
nil,
)
Convey("Should not add header with username", func() {
// Get will return empty string even if header is not set
So(req.Header.Get("X-Grafana-User"), ShouldEqual, "")
})
// Get will return empty string even if header is not set
assert.Equal(t, "", req.Header.Get("X-Grafana-User"))
})
Convey("When SendUserHeader config is enabled but user is anonymous", t, func() {
t.Run("When SendUserHeader config is enabled but user is anonymous", func(t *testing.T) {
req := getPluginProxiedRequest(
t,
&models.ReqContext{
SignedInUser: &models.SignedInUser{IsAnonymous: true},
},
@@ -93,13 +92,11 @@ func TestPluginProxy(t *testing.T) {
nil,
)
Convey("Should not add header with username", func() {
// Get will return empty string even if header is not set
So(req.Header.Get("X-Grafana-User"), ShouldEqual, "")
})
// Get will return empty string even if header is not set
assert.Equal(t, "", req.Header.Get("X-Grafana-User"))
})
Convey("When getting templated url", t, func() {
t.Run("When getting templated url", func(t *testing.T) {
route := &plugins.AppPluginRoute{
URL: "{{.JsonData.dynamicUrl}}",
Method: "GET",
@@ -115,6 +112,7 @@ func TestPluginProxy(t *testing.T) {
})
req := getPluginProxiedRequest(
t,
&models.ReqContext{
SignedInUser: &models.SignedInUser{
Login: "test_user",
@@ -123,15 +121,11 @@ func TestPluginProxy(t *testing.T) {
&setting.Cfg{SendUserHeader: true},
route,
)
Convey("Should set req.URL to be interpolated value from jsonData", func() {
So(req.URL.String(), ShouldEqual, "https://dynamic.grafana.com")
})
Convey("Route url should not be modified", func() {
So(route.URL, ShouldEqual, "{{.JsonData.dynamicUrl}}")
})
assert.Equal(t, "https://dynamic.grafana.com", req.URL.String())
assert.Equal(t, "{{.JsonData.dynamicUrl}}", route.URL)
})
Convey("When getting complex templated url", t, func() {
t.Run("When getting complex templated url", func(t *testing.T) {
route := &plugins.AppPluginRoute{
URL: "{{if .JsonData.apiHost}}{{.JsonData.apiHost}}{{else}}https://example.com{{end}}",
Method: "GET",
@@ -143,6 +137,7 @@ func TestPluginProxy(t *testing.T) {
})
req := getPluginProxiedRequest(
t,
&models.ReqContext{
SignedInUser: &models.SignedInUser{
Login: "test_user",
@@ -151,14 +146,12 @@ func TestPluginProxy(t *testing.T) {
&setting.Cfg{SendUserHeader: true},
route,
)
Convey("Should set req.URL to be interpolated default value", func() {
So(req.URL.String(), ShouldEqual, "https://example.com")
})
assert.Equal(t, "https://example.com", req.URL.String())
})
}
// getPluginProxiedRequest is a helper for easier setup of tests based on global config and ReqContext.
func getPluginProxiedRequest(ctx *models.ReqContext, cfg *setting.Cfg, route *plugins.AppPluginRoute) *http.Request {
func getPluginProxiedRequest(t *testing.T, ctx *models.ReqContext, cfg *setting.Cfg, route *plugins.AppPluginRoute) *http.Request {
// insert dummy route if none is specified
if route == nil {
route = &plugins.AppPluginRoute{
@@ -170,7 +163,7 @@ func getPluginProxiedRequest(ctx *models.ReqContext, cfg *setting.Cfg, route *pl
proxy := NewApiPluginProxy(ctx, "", route, "", cfg)
req, err := http.NewRequest(http.MethodGet, "/api/plugin-proxy/grafana-simple-app/api/v4/alerts", nil)
So(err, ShouldBeNil)
require.NoError(t, err)
proxy.Director(req)
return req
}