mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
parent
6ae7b0c3b2
commit
8917e66eb4
@ -44,7 +44,16 @@ func OrgRedirect(cfg *setting.Cfg) web.Handler {
|
|||||||
return
|
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)
|
c.Redirect(newURL, 302)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,33 +7,68 @@ import (
|
|||||||
|
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestOrgRedirectMiddleware(t *testing.T) {
|
func TestOrgRedirectMiddleware(t *testing.T) {
|
||||||
middlewareScenario(t, "when setting a correct org for the user", func(t *testing.T, sc *scenarioContext) {
|
testCases := []struct {
|
||||||
sc.withTokenSessionCookie("token")
|
desc string
|
||||||
bus.AddHandler("test", func(ctx context.Context, query *models.SetUsingOrgCommand) error {
|
input string
|
||||||
return nil
|
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) {
|
middlewareScenario(t, "when setting an invalid org for user", func(t *testing.T, sc *scenarioContext) {
|
||||||
sc.withTokenSessionCookie("token")
|
sc.withTokenSessionCookie("token")
|
||||||
@ -56,6 +91,6 @@ func TestOrgRedirectMiddleware(t *testing.T) {
|
|||||||
sc.m.Get("/", sc.defaultHandler)
|
sc.m.Get("/", sc.defaultHandler)
|
||||||
sc.fakeReq("GET", "/?orgId=3").exec()
|
sc.fakeReq("GET", "/?orgId=3").exec()
|
||||||
|
|
||||||
assert.Equal(t, 404, sc.resp.Code)
|
require.Equal(t, 404, sc.resp.Code)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user