Alerting: Encode query model map to string in rule export to avoid html escape sequences (#87663)

* Encode query model map to string to avoid html escape sequences

* Remove insignificant whitespace in test request
This commit is contained in:
William Wernert
2024-05-14 09:29:50 -04:00
committed by GitHub
parent 62d326cf04
commit 563fcb8bf4
3 changed files with 37 additions and 7 deletions
@@ -1,6 +1,7 @@
package api
import (
"bytes"
"context"
"embed"
"encoding/json"
@@ -42,8 +43,12 @@ func TestExportFromPayload(t *testing.T) {
requestFile := "post-rulegroup-101.json"
rawBody, err := testData.ReadFile(path.Join("test-data", requestFile))
require.NoError(t, err)
// compact the json to remove any extra whitespace
var buf bytes.Buffer
require.NoError(t, json.Compact(&buf, rawBody))
// unmarshal the compacted json
var body apimodels.PostableRuleGroupConfig
require.NoError(t, json.Unmarshal(rawBody, &body))
require.NoError(t, json.Unmarshal(buf.Bytes(), &body))
createRequest := func() *contextmodel.ReqContext {
return createRequestContextWithPerms(orgID, map[int64]map[string][]string{}, nil)
@@ -212,6 +217,13 @@ func TestExportRules(t *testing.T) {
gen := ngmodels.RuleGen
accessQuery := gen.GenerateQuery()
noAccessQuery := gen.GenerateQuery()
mdl := map[string]any{
"foo": "bar",
"baz": "a <=> b", // explicitly check greater/less than characters
}
model, err := json.Marshal(mdl)
require.NoError(t, err)
accessQuery.Model = model
hasAccess1 := gen.With(gen.WithGroupKey(hasAccessKey1), gen.WithQuery(accessQuery), gen.WithUniqueGroupIndex()).GenerateManyRef(5)
ruleStore.PutRule(context.Background(), hasAccess1...)