1 package msgdata
2
3 import (
4 "fmt"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8
9 "edge-infra.dev/pkg/sds/emergencyaccess/apierror"
10 "edge-infra.dev/pkg/sds/emergencyaccess/eaconst"
11 )
12
13 const (
14 defaultKey = "hello"
15 defaultVal = "world"
16 updatedVal = "goodbye"
17 )
18
19 var (
20 defaultV1_0Req = v1_0Request{
21 ReqData: v1Data{
22 Command: "echo hello world",
23 },
24 ReqAttr: map[string]string{
25 eaconst.VersionKey: string(eaconst.MessageVersion1_0),
26 eaconst.RequestTypeKey: string(eaconst.Command),
27 },
28 command: "echo",
29 }
30 )
31
32 func TestNewV1_0Request(t *testing.T) {
33 t.Parallel()
34
35 tests := map[string]struct {
36 payload string
37 expected Request
38 errAssert assert.ErrorAssertionFunc
39 }{
40 "Success": {
41 payload: "echo hello world",
42 expected: v1_0Request{
43 ReqData: v1Data{
44 Command: "echo hello world",
45 },
46 ReqAttr: map[string]string{
47 eaconst.VersionKey: string(eaconst.MessageVersion1_0),
48 eaconst.RequestTypeKey: string(eaconst.Command),
49 },
50 command: "echo",
51 },
52 errAssert: assert.NoError,
53 },
54 "Can't Parse Payload": {
55 errAssert: APIError(apierror.ErrInvalidCommand, "payload cannot be empty"),
56 },
57 "No Command Identified": {
58 payload: "\"\" echo hello world",
59 errAssert: APIError(apierror.ErrInvalidCommand, "command is empty", "No command identified"),
60 },
61 }
62
63 for name, tc := range tests {
64 tc := tc
65 t.Run(name, func(t *testing.T) {
66 t.Parallel()
67
68 actual, err := NewV1_0Request(tc.payload)
69 tc.errAssert(t, err)
70 assert.Equal(t, tc.expected, actual)
71 })
72 }
73 }
74
75 func TestV1_0RequestData(t *testing.T) {
76 t.Parallel()
77
78 req := defaultV1_0Req
79 expectedJSON := fmt.Sprintf(`{
80 "command":"%s"
81 }`, req.ReqData.Command)
82
83 actualJSON, err := req.Data()
84 assert.NoError(t, err)
85 assert.JSONEq(t, expectedJSON, string(actualJSON))
86 }
87
88 func TestV1_0RequestAttributes(t *testing.T) {
89 t.Parallel()
90
91 req := defaultV1_0Req
92 assert.Equal(t, req.ReqAttr, req.Attributes())
93 }
94
95 func TestV1_0RequestAddAttributes(t *testing.T) {
96 t.Parallel()
97
98 req := v1_0Request{
99 ReqAttr: deepCopyMap(defaultV1_0Req.ReqAttr),
100 ReqData: defaultV1_0Req.ReqData,
101 }
102 attr := req.Attributes()
103
104 req.AddAttribute(defaultKey, defaultVal)
105 assert.NotEqual(t, attr, req.Attributes())
106 attr[defaultKey] = defaultVal
107 assert.Equal(t, attr, req.Attributes())
108
109
110 req.AddAttribute(defaultKey, updatedVal)
111 assert.Equal(t, attr, req.Attributes())
112 attr[defaultKey] = updatedVal
113 assert.NotEqual(t, attr, req.Attributes())
114 }
115
116 func TestV1_0RequestCommandToBeAuthorized(t *testing.T) {
117 t.Parallel()
118
119 req := defaultV1_0Req
120 assert.Equal(t, req.command, req.CommandToBeAuthorized())
121 }
122
123 func TestV1_0RequestType(t *testing.T) {
124 t.Parallel()
125
126 req := defaultV1_0Req
127 assert.Equal(t, eaconst.Command, req.RequestType())
128 }
129
130 func TestV1_0RequestValidate(t *testing.T) {
131 t.Parallel()
132
133 tests := map[string]struct {
134 req v1_0Request
135 errAssert assert.ErrorAssertionFunc
136 }{
137 "Valid": {
138 req: v1_0Request{
139 ReqData: v1Data{
140 Command: "echo hello world",
141 },
142 ReqAttr: map[string]string{
143 eaconst.VersionKey: string(eaconst.MessageVersion1_0),
144 eaconst.RequestTypeKey: string(eaconst.Command),
145 },
146 command: "echo",
147 },
148 errAssert: assert.NoError,
149 },
150 "All Invalid": {
151 errAssert: APIError(
152 apierror.ErrInvalidCommand,
153 "command and args are empty\ncommand is empty\nversion attribute \"\" should be \"1.0\"\ntype attribute \"\" should be \"command\"",
154 "No command identified",
155 ),
156 },
157 }
158
159 for name, tc := range tests {
160 tc := tc
161 t.Run(name, func(t *testing.T) {
162 t.Parallel()
163
164 tc.errAssert(t, tc.req.validate())
165 })
166 }
167 }
168
View as plain text