Skip to content

Commit

Permalink
fix tests after models change
Browse files Browse the repository at this point in the history
  • Loading branch information
lucamrgs committed Jan 17, 2025
1 parent 4c14bbb commit a976039
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 77 deletions.
29 changes: 23 additions & 6 deletions pkg/api/manual/manual_api.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package manual

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"reflect"
Expand Down Expand Up @@ -66,7 +68,7 @@ func (manualHandler *ManualHandler) GetPendingCommands(g *gin.Context) {
log.Error(err)
apiError.SendErrorResponse(g, http.StatusInternalServerError,
"Failed get pending manual commands",
"GET /manual/", err.Error())
"GET /manual/", "")
return
}
g.JSON(status,
Expand Down Expand Up @@ -94,7 +96,7 @@ func (manualHandler *ManualHandler) GetPendingCommand(g *gin.Context) {
log.Error(err)
apiError.SendErrorResponse(g, http.StatusBadRequest,
"Failed to parse execution ID",
"GET /manual/"+execution_id+"/"+step_id, err.Error())
"GET /manual/"+execution_id+"/"+step_id, "")
return
}

Expand All @@ -104,7 +106,7 @@ func (manualHandler *ManualHandler) GetPendingCommand(g *gin.Context) {
log.Error(err)
apiError.SendErrorResponse(g, http.StatusInternalServerError,
"Failed to provide pending manual command",
"GET /manual/"+execution_id+"/"+step_id, err.Error())
"GET /manual/"+execution_id+"/"+step_id, "")
return
}
g.JSON(status,
Expand Down Expand Up @@ -143,7 +145,9 @@ func (manualHandler *ManualHandler) PostContinue(g *gin.Context) {
}

var outArgsUpdate api.ManualOutArgsUpdatePayload
err = json.Unmarshal(jsonData, &outArgsUpdate)
decoder := json.NewDecoder(bytes.NewReader(jsonData))
decoder.DisallowUnknownFields()
err = decoder.Decode(&outArgsUpdate)
if err != nil {
log.Error("failed to unmarshal JSON")
apiError.SendErrorResponse(g, http.StatusBadRequest,
Expand All @@ -152,13 +156,26 @@ func (manualHandler *ManualHandler) PostContinue(g *gin.Context) {
return
}

fmt.Println("Printing parsed body!")
fmt.Println(outArgsUpdate)

for varName, variable := range outArgsUpdate.ResponseOutArgs {
if varName != variable.Name {
log.Error("variable name mismatch")
apiError.SendErrorResponse(g, http.StatusBadRequest,
"Variable name mismatch",
"POST /manual/continue", "")
return
}
}

// Create object to pass to interaction capability
executionId, err := uuid.Parse(outArgsUpdate.ExecutionId)
if err != nil {
log.Error(err)
apiError.SendErrorResponse(g, http.StatusBadRequest,
"Failed to parse execution ID",
"POST /manual/continue", err.Error())
"POST /manual/continue", "")
return
}

Expand All @@ -178,7 +195,7 @@ func (manualHandler *ManualHandler) PostContinue(g *gin.Context) {
log.Error(err)
apiError.SendErrorResponse(g, http.StatusInternalServerError,
"Failed to post continue ID",
"POST /manual/continue", err.Error())
"POST /manual/continue", "")
return
}
g.JSON(
Expand Down
97 changes: 28 additions & 69 deletions pkg/core/capability/manual/interaction/interaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,76 +163,34 @@ func TestGetAllPendingInteractions(t *testing.T) {
t.Fail()
}

expectedInteractionsJson := `
[
{
"type": "test_type",
"execution_id": "61a6c41e-6efc-4516-a242-dfbc5c89d562",
"playbook_id": "test_playbook_id",
"step_id": "test_step_id",
"description": "test_description",
"command": "test_command",
"commandb64": "test_command_b64",
"target": {
"id": "test_id",
"type": "test_type",
"name": "test_name",
"description": "test_description",
"location": {},
"contact": {}
},
"out_args": {
"var2": {
"type": "string",
"name": "var2",
"description": "test variable",
"value": "test_value_2"
}
}
},
{
"type": "test_type",
"execution_id": "50b6d52c-6efc-4516-a242-dfbc5c89d421",
"playbook_id": "test_playbook_id",
"step_id": "test_step_id",
"description": "test_description",
"command": "test_command",
"commandb64": "test_command_b64",
"target": {
"id": "test_id",
"type": "test_type",
"name": "test_name",
"description": "test_description",
"location": {},
"contact": {}
},
"out_args": {
"var2": {
"type": "string",
"name": "var2",
"description": "test variable",
"value": "test_value_2"
}
}
}
]
`
var expectedInteractions []manualModel.InteractionResponse
err = json.Unmarshal([]byte(expectedInteractionsJson), &expectedInteractions)
expectedInteractions := []manualModel.CommandInfo{testInteractionCommand, testNewInteractionCommand}

receivedInteractions := interaction.getAllPendingInteractions()
receivedInteractionsJson, err := json.MarshalIndent(receivedInteractions, "", " ")
if err != nil {
t.Log("failed to marshal received interactions")
t.Log(err)
t.Fail()
}
t.Log("expected interactions")
t.Log(expectedInteractions)
fmt.Println("received interactions")
fmt.Println(string(receivedInteractionsJson))

receivedInteractions := interaction.getAllPendingInteractions()
fmt.Println(receivedInteractions)

if !reflect.DeepEqual(expectedInteractions, receivedInteractions) {
err = fmt.Errorf("expected %v, but got %v", expectedInteractions, receivedInteractions)
t.Log(err)
t.Fail()
for i, receivedInteraction := range receivedInteractions {
if expectedInteractions[i].Metadata != receivedInteraction.Metadata {
err = fmt.Errorf("expected %v, but got %v", expectedInteractions, receivedInteractions)
t.Log(err)
t.Fail()
}
if !reflect.DeepEqual(expectedInteractions[i].OutArgsVariables, receivedInteraction.OutArgsVariables) {
err = fmt.Errorf("expected %v, but got %v", expectedInteractions, receivedInteractions)
t.Log(err)
t.Fail()
}
if !reflect.DeepEqual(expectedInteractions[i].Context, receivedInteraction.Context) {
err = fmt.Errorf("expected %v, but got %v", expectedInteractions[i].Context, receivedInteraction.Context)
t.Log(err)
t.Fail()
}
}
}

Expand Down Expand Up @@ -332,7 +290,7 @@ func TestPostContinueWarningsRaised(t *testing.T) {
}

outArg := cacao.Variable{
Type: "string",
Type: "banana",
Name: "var2",
Description: "this description will not make it to the returned var",
Value: "now the value is bananas",
Expand All @@ -358,7 +316,8 @@ func TestPostContinueWarningsRaised(t *testing.T) {
expectedLogEntry1 := "provided out arg var2 is attempting to change 'Constant' property"
expectedLogEntry2 := "provided out arg var2 is attempting to change 'Description' property"
expectedLogEntry3 := "provided out arg var2 is attempting to change 'External' property"
expectedLogs := []string{expectedLogEntry1, expectedLogEntry2, expectedLogEntry3}
expectedLogEntry4 := "provided out arg var2 is attempting to change 'Type' property"
expectedLogs := []string{expectedLogEntry1, expectedLogEntry2, expectedLogEntry3, expectedLogEntry4}

all := true
for _, expectedMessage := range expectedLogs {
Expand Down Expand Up @@ -625,9 +584,9 @@ var testMetadata = execution.Metadata{
var testInteractionCommand = manualModel.CommandInfo{
Metadata: testMetadata,
OutArgsVariables: cacao.Variables{
"var1": {
"var2": {
Type: "string",
Name: "var1",
Name: "var2",
Description: "test variable",
Value: "",
Constant: false,
Expand Down
6 changes: 5 additions & 1 deletion pkg/core/capability/manual/manual_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ func TestManualExecution(t *testing.T) {
meta := execution.Metadata{}
commandContext := capability.Context{}

command := manualModel.CommandInfo{}
command := manualModel.CommandInfo{
Metadata: execution.Metadata{},
Context: capability.Context{},
OutArgsVariables: cacao.NewVariables(),
}

// Capture the channel passed to Queue

Expand Down
18 changes: 17 additions & 1 deletion test/integration/api/routes/manual_api/manual_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http/httptest"
api_routes "soarca/pkg/api"
manual_api "soarca/pkg/api/manual"
apiModel "soarca/pkg/models/api"
"soarca/pkg/models/cacao"
"soarca/pkg/models/execution"
"soarca/pkg/models/manual"
Expand Down Expand Up @@ -96,6 +97,21 @@ func TestPostContinueCalled(t *testing.T) {
testPlaybookId := "21a4d52c-6efc-4516-a242-dfbc5c89d312"
path := "/manual/continue"

testManualUpdatePayload := apiModel.ManualOutArgsUpdatePayload{
Type: "manual-step-response",
ExecutionId: testExecId,
StepId: testStepId,
PlaybookId: testPlaybookId,
ResponseStatus: "success",
ResponseOutArgs: cacao.Variables{
"testvar": {
Type: "string",
Name: "testvar",
Value: "testing!",
},
},
}

testManualResponse := manual.InteractionResponse{
Metadata: execution.Metadata{
ExecutionId: uuid.MustParse(testExecId),
Expand All @@ -111,7 +127,7 @@ func TestPostContinueCalled(t *testing.T) {
},
},
}
jsonData, err := json.Marshal(testManualResponse)
jsonData, err := json.Marshal(testManualUpdatePayload)
if err != nil {
t.Fatalf("Error marshalling JSON: %v", err)
}
Expand Down

0 comments on commit a976039

Please sign in to comment.