Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lucamrgs committed Jan 10, 2025
1 parent 6506a17 commit afbbf8a
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 9 deletions.
10 changes: 9 additions & 1 deletion pkg/core/capability/manual/interaction/interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,16 @@ func (manualController *InteractionController) PostContinue(result manual.Manual

// If it is
for varName, variable := range result.ResponseOutArgs {
// Sanity check that dictionary key matches variable name
if varName != variable.Name {
err := fmt.Errorf("provided out arg key [ %s ] does not match its name property [ %s ]", varName, variable.Name)
log.Error(err)
return http.StatusBadRequest, err
}

// first check that out args provided match the variables
if _, ok := pendingEntry.CommandData.OutVariables[varName]; !ok {
err := errors.New("provided out args do not match command-related variables")
log.Warning("provided out args do not match command-related variables")
return http.StatusBadRequest, err
}
Expand All @@ -207,7 +215,7 @@ func (manualController *InteractionController) PostContinue(result manual.Manual
//Then put outArgs back into manualCapabilityChannel
// Copy result and conversion back to interactionResponse format
returnedVars := manualController.copyOutArgsToVars(result.ResponseOutArgs)
log.Info("putting stuff in manual capability channel")
log.Trace("pushing assigned variables in manual capability channel")
pendingEntry.Channel <- manual.InteractionResponse{
ResponseError: nil,
Payload: returnedVars,
Expand Down
147 changes: 139 additions & 8 deletions pkg/core/capability/manual/interaction/interaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,13 +335,6 @@ func TestPostContinueWarningsRaised(t *testing.T) {
t.Fail()
}

pending, err := interaction.getPendingInteraction((testMetadata))
if err != nil {
t.Log(err)
t.Fail()
}
fmt.Println(pending)

outArg := manualModel.ManualOutArg{
Type: "string",
Name: "var2",
Expand Down Expand Up @@ -397,8 +390,146 @@ func TestPostContinueWarningsRaised(t *testing.T) {

}

func TestPostContinueFailOnUnmatchedOutArgs(t *testing.T) {
func TestPostContinueFailOnUnmatchedOutArgsKeyName(t *testing.T) {
interaction := New([]IInteractionIntegrationNotifier{})
timeout := 500 * time.Millisecond
testCtx, testCancel := context.WithTimeout(context.Background(), timeout)

defer testCancel()

hook := NewTestLogHook()
log.Logger.AddHook(hook)

testCapComms := manualModel.ManualCapabilityCommunication{
Channel: make(chan manualModel.InteractionResponse),
TimeoutContext: testCtx,
}
defer close(testCapComms.Channel)

err := interaction.Queue(testInteractionCommand, testCapComms)
if err != nil {
t.Log(err)
t.Fail()
}

outArg := manualModel.ManualOutArg{
Type: "string",
Name: "testNotExisting",
Value: "now the value is bananas",
}

outArgsUpdate := manualModel.ManualOutArgsUpdatePayload{
Type: "test-manual-response",
ExecutionId: testMetadata.ExecutionId.String(),
PlaybookId: testMetadata.PlaybookId,
StepId: testMetadata.StepId,
ResponseStatus: true,
ResponseOutArgs: manualModel.ManualOutArgs{"asd": outArg},
}

statusCode, err := interaction.PostContinue(outArgsUpdate)

expectedStatusCode := 400
expectedErr := errors.New("provided out arg key [ asd ] does not match its name property [ testNotExisting ]")

expectedLogEntry1 := "provided out arg key [ asd ] does not match its name property [ testNotExisting ]"
expectedLogs := []string{expectedLogEntry1}

all := true
for _, expectedMessage := range expectedLogs {
containsAll := true
for _, entry := range hook.Entries {
if strings.Contains(expectedMessage, entry.Message) {
containsAll = true
break
}
if !strings.Contains(expectedMessage, entry.Message) {
containsAll = false
}
}
if !containsAll {
t.Logf("log message: '%s' not found in logged messages", expectedMessage)
all = false
break
}
}

assert.Equal(t, statusCode, expectedStatusCode)
assert.Equal(t, err, expectedErr)

assert.NotEqual(t, len(hook.Entries), 0)
assert.Equal(t, all, true)
}

func TestPostContinueFailOnNonexistingVariable(t *testing.T) {
interaction := New([]IInteractionIntegrationNotifier{})
timeout := 500 * time.Millisecond
testCtx, testCancel := context.WithTimeout(context.Background(), timeout)

defer testCancel()

hook := NewTestLogHook()
log.Logger.AddHook(hook)

testCapComms := manualModel.ManualCapabilityCommunication{
Channel: make(chan manualModel.InteractionResponse),
TimeoutContext: testCtx,
}
defer close(testCapComms.Channel)

err := interaction.Queue(testInteractionCommand, testCapComms)
if err != nil {
t.Log(err)
t.Fail()
}

outArg := manualModel.ManualOutArg{
Type: "string",
Name: "testNotExisting",
Value: "now the value is bananas",
}

outArgsUpdate := manualModel.ManualOutArgsUpdatePayload{
Type: "test-manual-response",
ExecutionId: testMetadata.ExecutionId.String(),
PlaybookId: testMetadata.PlaybookId,
StepId: testMetadata.StepId,
ResponseStatus: true,
ResponseOutArgs: manualModel.ManualOutArgs{"testNotExisting": outArg},
}

statusCode, err := interaction.PostContinue(outArgsUpdate)

expectedStatusCode := 400
expectedErr := errors.New("provided out args do not match command-related variables")

expectedLogEntry1 := "provided out args do not match command-related variables"
expectedLogs := []string{expectedLogEntry1}

all := true
for _, expectedMessage := range expectedLogs {
containsAll := true
for _, entry := range hook.Entries {
if strings.Contains(expectedMessage, entry.Message) {
containsAll = true
break
}
if !strings.Contains(expectedMessage, entry.Message) {
containsAll = false
}
}
if !containsAll {
t.Logf("log message: '%s' not found in logged messages", expectedMessage)
all = false
break
}
}

assert.Equal(t, statusCode, expectedStatusCode)
assert.Equal(t, err, expectedErr)

assert.NotEqual(t, len(hook.Entries), 0)
assert.Equal(t, all, true)
}

func TestRegisterRetrieveNewExecutionNewPendingInteraction(t *testing.T) {
Expand Down

0 comments on commit afbbf8a

Please sign in to comment.