Fix kiosk bug (#43152)

* fix_kiosk_bug

Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com>
Co-authored-by: Sofia Papagiannaki <sofia@grafana.com>
This commit is contained in:
Dessen Xu 2022-01-19 16:58:22 +08:00 committed by GitHub
parent 6ae7b0c3b2
commit 8917e66eb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 25 deletions

View File

@ -44,7 +44,16 @@ func OrgRedirect(cfg *setting.Cfg) web.Handler {
return
}
newURL := fmt.Sprintf("%s%s?%s", cfg.AppURL, strings.TrimPrefix(c.Req.URL.Path, "/"), c.Req.URL.Query().Encode())
urlParams := c.Req.URL.Query()
qs := urlParams.Encode()
if urlParams.Has("kiosk") && urlParams.Get("kiosk") == "" {
urlParams.Del("kiosk")
qs = fmt.Sprintf("%s&kiosk", urlParams.Encode())
}
newURL := fmt.Sprintf("%s%s?%s", cfg.AppURL, strings.TrimPrefix(c.Req.URL.Path, "/"), qs)
c.Redirect(newURL, 302)
}
}

View File

@ -7,33 +7,68 @@ import (
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestOrgRedirectMiddleware(t *testing.T) {
middlewareScenario(t, "when setting a correct org for the user", func(t *testing.T, sc *scenarioContext) {
sc.withTokenSessionCookie("token")
bus.AddHandler("test", func(ctx context.Context, query *models.SetUsingOrgCommand) error {
return nil
testCases := []struct {
desc string
input string
expStatus int
expLocation string
}{
{
desc: "when setting a correct org for the user",
input: "/?orgId=3",
expStatus: 302,
expLocation: "/?orgId=3",
},
{
desc: "when setting a correct org for the user with '&kiosk'",
input: "/?orgId=3&kiosk",
expStatus: 302,
expLocation: "/?orgId=3&kiosk",
},
{
desc: "when setting a correct org for the user with '&kiosk=",
input: "/?kiosk=&orgId=3",
expStatus: 302,
expLocation: "/?orgId=3&kiosk",
},
{
desc: "when setting a correct org for the user with '&kiosk=tv'",
input: "/?kiosk=tv&orgId=3",
expStatus: 302,
expLocation: "/?kiosk=tv&orgId=3",
},
}
for _, tc := range testCases {
middlewareScenario(t, tc.desc, func(t *testing.T, sc *scenarioContext) {
sc.withTokenSessionCookie("token")
bus.AddHandler("test", func(ctx context.Context, query *models.SetUsingOrgCommand) error {
return nil
})
bus.AddHandler("test", func(ctx context.Context, query *models.GetSignedInUserQuery) error {
query.Result = &models.SignedInUser{OrgId: 1, UserId: 12}
return nil
})
sc.userAuthTokenService.LookupTokenProvider = func(ctx context.Context, unhashedToken string) (*models.UserToken, error) {
return &models.UserToken{
UserId: 0,
UnhashedToken: "",
}, nil
}
sc.m.Get("/", sc.defaultHandler)
sc.fakeReq("GET", tc.input).exec()
require.Equal(t, tc.expStatus, sc.resp.Code)
require.Equal(t, tc.expLocation, sc.resp.Header().Get("Location"))
})
bus.AddHandler("test", func(ctx context.Context, query *models.GetSignedInUserQuery) error {
query.Result = &models.SignedInUser{OrgId: 1, UserId: 12}
return nil
})
sc.userAuthTokenService.LookupTokenProvider = func(ctx context.Context, unhashedToken string) (*models.UserToken, error) {
return &models.UserToken{
UserId: 0,
UnhashedToken: "",
}, nil
}
sc.m.Get("/", sc.defaultHandler)
sc.fakeReq("GET", "/?orgId=3").exec()
assert.Equal(t, 302, sc.resp.Code)
})
}
middlewareScenario(t, "when setting an invalid org for user", func(t *testing.T, sc *scenarioContext) {
sc.withTokenSessionCookie("token")
@ -56,6 +91,6 @@ func TestOrgRedirectMiddleware(t *testing.T) {
sc.m.Get("/", sc.defaultHandler)
sc.fakeReq("GET", "/?orgId=3").exec()
assert.Equal(t, 404, sc.resp.Code)
require.Equal(t, 404, sc.resp.Code)
})
}