File size: 1,718 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package smoke

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/target/goalert/expflag"
	"github.com/target/goalert/test/smoke/harness"
)

const actionValidQuery = `
query TestActionValid($input: ActionInput!) {
	actionInputValidate(input: $input)
}
`

// TestActionValid tests the action validation query.
func TestActionValid(t *testing.T) {
	t.Parallel()

	h := harness.NewHarnessWithFlags(t, "", "", expflag.FlagSet{expflag.UnivKeys})
	defer h.Close()

	type params map[string]string
	check := func(destType string, dest, dyn params) harness.QLResponse {
		t.Helper()
		var vars struct {
			Input struct {
				Dest struct {
					Type string `json:"type"`
					Args params `json:"args"`
				} `json:"dest"`
				Params params `json:"params"`
			} `json:"input"`
		}
		vars.Input.Params = dyn
		vars.Input.Dest.Type = destType
		vars.Input.Dest.Args = dest

		return *h.GraphQLQueryUserVarsT(t, harness.DefaultGraphQLAdminUserID, actionValidQuery, "TestActionValid", vars)
	}

	res := check("invalid", params{}, params{})
	if assert.Len(t, res.Errors, 1) {
		assert.EqualValues(t, "actionInputValidate.input.dest.type", res.Errors[0].Path)
		assert.Equal(t, "unsupported destination type: invalid", res.Errors[0].Message)
		assert.Equal(t, "INVALID_INPUT_VALUE", res.Errors[0].Extensions.Code)
	}

	res = check("builtin-alert", params{}, params{"invalid-expr": `foo+`})
	if assert.Len(t, res.Errors, 1) {
		assert.EqualValues(t, "actionInputValidate.input.params", res.Errors[0].Path)
		assert.Contains(t, res.Errors[0].Message, "unexpected token")
		assert.Equal(t, "INVALID_MAP_FIELD_VALUE", res.Errors[0].Extensions.Code)
		assert.Equal(t, "invalid-expr", res.Errors[0].Extensions.Key)
	}
}