2020-06-17 00:39:50 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2020-10-19 10:35:31 -05:00
|
|
|
"path/filepath"
|
2020-06-17 00:39:50 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-10-05 08:54:26 -05:00
|
|
|
"gopkg.in/macaron.v1"
|
2020-06-17 00:39:50 -05:00
|
|
|
|
2021-10-05 08:54:26 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2021-03-08 00:02:49 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/manager"
|
2021-10-05 08:54:26 -05:00
|
|
|
accesscontrolmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock"
|
2020-06-17 00:39:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/licensing"
|
2021-10-05 08:54:26 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/rendering"
|
2020-06-17 00:39:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
)
|
|
|
|
|
|
|
|
func setupTestEnvironment(t *testing.T, cfg *setting.Cfg) (*macaron.Macaron, *HTTPServer) {
|
|
|
|
t.Helper()
|
|
|
|
sqlstore.InitTestDB(t)
|
|
|
|
|
|
|
|
{
|
|
|
|
oldVersion := setting.BuildVersion
|
|
|
|
oldCommit := setting.BuildCommit
|
|
|
|
oldEnv := setting.Env
|
|
|
|
t.Cleanup(func() {
|
|
|
|
setting.BuildVersion = oldVersion
|
|
|
|
setting.BuildCommit = oldCommit
|
|
|
|
setting.Env = oldEnv
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
sqlStore := sqlstore.InitTestDB(t)
|
|
|
|
pm := &manager.PluginManager{Cfg: cfg, SQLStore: sqlStore}
|
2020-06-17 00:39:50 -05:00
|
|
|
|
2021-03-17 10:06:10 -05:00
|
|
|
r := &rendering.RenderingService{
|
|
|
|
Cfg: cfg,
|
|
|
|
PluginManager: pm,
|
|
|
|
}
|
2020-06-17 00:39:50 -05:00
|
|
|
|
|
|
|
hs := &HTTPServer{
|
|
|
|
Cfg: cfg,
|
|
|
|
Bus: bus.GetBus(),
|
2021-02-10 06:31:47 -06:00
|
|
|
License: &licensing.OSSLicensingService{Cfg: cfg},
|
2020-06-17 00:39:50 -05:00
|
|
|
RenderService: r,
|
2021-03-17 10:06:10 -05:00
|
|
|
SQLStore: sqlStore,
|
|
|
|
PluginManager: pm,
|
2021-10-05 08:54:26 -05:00
|
|
|
AccessControl: accesscontrolmock.New().WithDisabled(),
|
2020-06-17 00:39:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
m := macaron.New()
|
2020-12-15 12:09:04 -06:00
|
|
|
m.Use(getContextHandler(t, cfg).Middleware)
|
2021-08-10 06:29:46 -05:00
|
|
|
m.UseMiddleware(macaron.Renderer(filepath.Join(setting.StaticRootPath, "views"), "[[", "]]"))
|
2020-06-17 00:39:50 -05:00
|
|
|
m.Get("/api/frontend/settings/", hs.GetFrontendSettings)
|
|
|
|
|
|
|
|
return m, hs
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTPServer_GetFrontendSettings_hideVersionAnonyomus(t *testing.T) {
|
|
|
|
type buildInfo struct {
|
|
|
|
Version string `json:"version"`
|
|
|
|
Commit string `json:"commit"`
|
|
|
|
Env string `json:"env"`
|
|
|
|
}
|
|
|
|
type settings struct {
|
|
|
|
BuildInfo buildInfo `json:"buildInfo"`
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg := setting.NewCfg()
|
2021-02-10 06:31:47 -06:00
|
|
|
cfg.Env = "testing"
|
|
|
|
cfg.BuildVersion = "7.8.9"
|
|
|
|
cfg.BuildCommit = "01234567"
|
2020-06-17 00:39:50 -05:00
|
|
|
m, hs := setupTestEnvironment(t, cfg)
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/frontend/settings", nil)
|
|
|
|
|
2021-02-10 06:31:47 -06:00
|
|
|
// TODO: Remove
|
|
|
|
setting.BuildVersion = cfg.BuildVersion
|
|
|
|
setting.BuildCommit = cfg.BuildCommit
|
|
|
|
setting.Env = cfg.Env
|
2020-06-17 00:39:50 -05:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-12-11 04:44:44 -06:00
|
|
|
desc string
|
2020-06-17 00:39:50 -05:00
|
|
|
hideVersion bool
|
|
|
|
expected settings
|
|
|
|
}{
|
|
|
|
{
|
2020-12-11 04:44:44 -06:00
|
|
|
desc: "Not hiding version",
|
2020-06-17 00:39:50 -05:00
|
|
|
hideVersion: false,
|
|
|
|
expected: settings{
|
|
|
|
BuildInfo: buildInfo{
|
|
|
|
Version: setting.BuildVersion,
|
|
|
|
Commit: setting.BuildCommit,
|
|
|
|
Env: setting.Env,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-12-11 04:44:44 -06:00
|
|
|
desc: "Hiding version",
|
2020-06-17 00:39:50 -05:00
|
|
|
hideVersion: true,
|
|
|
|
expected: settings{
|
|
|
|
BuildInfo: buildInfo{
|
|
|
|
Version: "",
|
|
|
|
Commit: "",
|
|
|
|
Env: setting.Env,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2020-12-11 04:44:44 -06:00
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
hs.Cfg.AnonymousHideVersion = test.hideVersion
|
|
|
|
expected := test.expected
|
|
|
|
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
m.ServeHTTP(recorder, req)
|
|
|
|
got := settings{}
|
|
|
|
err := json.Unmarshal(recorder.Body.Bytes(), &got)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.GreaterOrEqual(t, 400, recorder.Code, "status codes higher than 400 indicate a failure")
|
|
|
|
|
|
|
|
assert.EqualValues(t, expected, got)
|
|
|
|
})
|
2020-06-17 00:39:50 -05:00
|
|
|
}
|
|
|
|
}
|