package msgdata import ( "fmt" "testing" "github.com/stretchr/testify/assert" "edge-infra.dev/pkg/sds/emergencyaccess/apierror" "edge-infra.dev/pkg/sds/emergencyaccess/eaconst" ) const ( defaultKey = "hello" defaultVal = "world" updatedVal = "goodbye" ) var ( defaultV1_0Req = v1_0Request{ ReqData: v1Data{ Command: "echo hello world", }, ReqAttr: map[string]string{ eaconst.VersionKey: string(eaconst.MessageVersion1_0), eaconst.RequestTypeKey: string(eaconst.Command), }, command: "echo", } ) func TestNewV1_0Request(t *testing.T) { t.Parallel() tests := map[string]struct { payload string expected Request errAssert assert.ErrorAssertionFunc }{ "Success": { payload: "echo hello world", expected: v1_0Request{ ReqData: v1Data{ Command: "echo hello world", }, ReqAttr: map[string]string{ eaconst.VersionKey: string(eaconst.MessageVersion1_0), eaconst.RequestTypeKey: string(eaconst.Command), }, command: "echo", }, errAssert: assert.NoError, }, "Can't Parse Payload": { errAssert: APIError(apierror.ErrInvalidCommand, "payload cannot be empty"), }, "No Command Identified": { payload: "\"\" echo hello world", errAssert: APIError(apierror.ErrInvalidCommand, "command is empty", "No command identified"), }, } for name, tc := range tests { tc := tc t.Run(name, func(t *testing.T) { t.Parallel() actual, err := NewV1_0Request(tc.payload) tc.errAssert(t, err) assert.Equal(t, tc.expected, actual) }) } } func TestV1_0RequestData(t *testing.T) { t.Parallel() req := defaultV1_0Req expectedJSON := fmt.Sprintf(`{ "command":"%s" }`, req.ReqData.Command) actualJSON, err := req.Data() assert.NoError(t, err) assert.JSONEq(t, expectedJSON, string(actualJSON)) } func TestV1_0RequestAttributes(t *testing.T) { t.Parallel() req := defaultV1_0Req assert.Equal(t, req.ReqAttr, req.Attributes()) } func TestV1_0RequestAddAttributes(t *testing.T) { t.Parallel() req := v1_0Request{ ReqAttr: deepCopyMap(defaultV1_0Req.ReqAttr), ReqData: defaultV1_0Req.ReqData, } attr := req.Attributes() req.AddAttribute(defaultKey, defaultVal) assert.NotEqual(t, attr, req.Attributes()) attr[defaultKey] = defaultVal assert.Equal(t, attr, req.Attributes()) // Test Attributes do not get updated with a changed value req.AddAttribute(defaultKey, updatedVal) assert.Equal(t, attr, req.Attributes()) attr[defaultKey] = updatedVal assert.NotEqual(t, attr, req.Attributes()) } func TestV1_0RequestCommandToBeAuthorized(t *testing.T) { t.Parallel() req := defaultV1_0Req assert.Equal(t, req.command, req.CommandToBeAuthorized()) } func TestV1_0RequestType(t *testing.T) { t.Parallel() req := defaultV1_0Req assert.Equal(t, eaconst.Command, req.RequestType()) } func TestV1_0RequestValidate(t *testing.T) { t.Parallel() tests := map[string]struct { req v1_0Request errAssert assert.ErrorAssertionFunc }{ "Valid": { req: v1_0Request{ ReqData: v1Data{ Command: "echo hello world", }, ReqAttr: map[string]string{ eaconst.VersionKey: string(eaconst.MessageVersion1_0), eaconst.RequestTypeKey: string(eaconst.Command), }, command: "echo", }, errAssert: assert.NoError, }, "All Invalid": { errAssert: APIError( apierror.ErrInvalidCommand, "command and args are empty\ncommand is empty\nversion attribute \"\" should be \"1.0\"\ntype attribute \"\" should be \"command\"", "No command identified", ), }, } for name, tc := range tests { tc := tc t.Run(name, func(t *testing.T) { t.Parallel() tc.errAssert(t, tc.req.validate()) }) } }