|
| 1 | +//go:build e2e |
| 2 | + |
| 3 | +package e2e |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/harness/harness-mcp/client/dto" |
| 12 | + "github.com/mark3labs/mcp-go/mcp" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +// TestGetAuditYamlTool verifies that the get_audit_yaml tool is available |
| 17 | +func TestGetAuditYamlTool(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + |
| 20 | + mcpClient := setupMCPClient(t) |
| 21 | + ctx := context.Background() |
| 22 | + |
| 23 | + // List available tools |
| 24 | + request := mcp.ListToolsRequest{} |
| 25 | + response, err := mcpClient.ListTools(ctx, request) |
| 26 | + require.NoError(t, err, "expected to list tools successfully") |
| 27 | + |
| 28 | + // Check that audit yaml tool is available |
| 29 | + var foundAuditYamlTool bool |
| 30 | + |
| 31 | + // Print all available tools |
| 32 | + fmt.Println("Available tools in TestGetAuditYamlTool:") |
| 33 | + for _, tool := range response.Tools { |
| 34 | + fmt.Printf("- %s\n", tool.Name) |
| 35 | + |
| 36 | + // Check if this is our get_audit_yaml tool |
| 37 | + if tool.Name == "get_audit_yaml" { |
| 38 | + foundAuditYamlTool = true |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // Check if we found the get_audit_yaml tool |
| 43 | + require.True(t, foundAuditYamlTool, "expected to find get_audit_yaml tool") |
| 44 | +} |
| 45 | + |
| 46 | +// TestGetAuditYaml verifies that the get_audit_yaml tool works correctly |
| 47 | +func TestGetAuditYaml(t *testing.T) { |
| 48 | + t.Skip("Skipping TestGetAuditYaml as it requires specific audit events with YAML content") |
| 49 | + t.Parallel() |
| 50 | + |
| 51 | + mcpClient := setupMCPClient(t) |
| 52 | + ctx := context.Background() |
| 53 | + accountID := getE2EAccountID(t) |
| 54 | + |
| 55 | + // First, get an audit ID by listing audit events |
| 56 | + listRequest := mcp.CallToolRequest{} |
| 57 | + listRequest.Params.Name = "list_user_audits" |
| 58 | + listRequest.Params.Arguments = map[string]any{ |
| 59 | + "accountIdentifier": accountID, |
| 60 | + "orgIdentifier": getE2EOrgID(), |
| 61 | + "projectIdentifier": getE2EProjectID(), |
| 62 | + "page": 0, |
| 63 | + "size": 1, |
| 64 | + "resource_type": "PIPELINE", // Filter for pipeline events which are likely to have YAML |
| 65 | + } |
| 66 | + |
| 67 | + listResponse, err := mcpClient.CallTool(ctx, listRequest) |
| 68 | + require.NoError(t, err, "expected to call 'list_user_audits' tool successfully") |
| 69 | + if listResponse.IsError { |
| 70 | + t.Logf("Error response: %v", listResponse.Content) |
| 71 | + t.Log("list_user_audits tool returned an error") |
| 72 | + t.FailNow() |
| 73 | + } |
| 74 | + |
| 75 | + // Parse the response to extract audit events |
| 76 | + if len(listResponse.Content) == 0 { |
| 77 | + t.Skip("No content returned from list_user_audits") |
| 78 | + } |
| 79 | + |
| 80 | + textContent, ok := listResponse.Content[0].(mcp.TextContent) |
| 81 | + if !ok { |
| 82 | + t.Skip("Content is not of type TextContent") |
| 83 | + } |
| 84 | + |
| 85 | + var auditResponse dto.AuditOutput[dto.AuditListItem] |
| 86 | + err = json.Unmarshal([]byte(textContent.Text), &auditResponse) |
| 87 | + if err != nil { |
| 88 | + t.Logf("Failed to unmarshal response: %v", err) |
| 89 | + t.Skip("Could not unmarshal audit response") |
| 90 | + } |
| 91 | + |
| 92 | + // Skip test if no audit events found |
| 93 | + if len(auditResponse.Data.Content) == 0 { |
| 94 | + t.Skip("No audit events found to test get_audit_yaml") |
| 95 | + } |
| 96 | + |
| 97 | + // Get the first audit ID |
| 98 | + auditID := auditResponse.Data.Content[0].AuditID |
| 99 | + if auditID == "" { |
| 100 | + t.Skip("Audit ID is empty") |
| 101 | + } |
| 102 | + t.Logf("Using audit ID: %s", auditID) |
| 103 | + |
| 104 | + // Now call the get_audit_yaml tool with this audit ID |
| 105 | + yamlRequest := mcp.CallToolRequest{} |
| 106 | + yamlRequest.Params.Name = "get_audit_yaml" |
| 107 | + yamlRequest.Params.Arguments = map[string]any{ |
| 108 | + "accountIdentifier": accountID, |
| 109 | + "orgIdentifier": getE2EOrgID(), |
| 110 | + "projectIdentifier": getE2EProjectID(), |
| 111 | + "audit_id": auditID, |
| 112 | + } |
| 113 | + |
| 114 | + yamlResponse, err := mcpClient.CallTool(ctx, yamlRequest) |
| 115 | + require.NoError(t, err, "expected to call 'get_audit_yaml' tool successfully") |
| 116 | + |
| 117 | + // Note: Some audit events might not have YAML content, so we don't strictly require success |
| 118 | + if yamlResponse.IsError { |
| 119 | + t.Logf("Error response from get_audit_yaml: %v", yamlResponse.Content) |
| 120 | + t.Skip("Skipping YAML content verification as the audit event might not have YAML content") |
| 121 | + } |
| 122 | + |
| 123 | + // If we got a successful response, verify the structure |
| 124 | + require.NotEmpty(t, yamlResponse.Content, "expected content to not be empty") |
| 125 | + |
| 126 | + // Parse the response to extract YAML content |
| 127 | + yamlTextContent, ok := yamlResponse.Content[0].(mcp.TextContent) |
| 128 | + require.True(t, ok, "expected content to be of type TextContent") |
| 129 | + |
| 130 | + var auditYamlResponse dto.AuditYamlResponse |
| 131 | + err = json.Unmarshal([]byte(yamlTextContent.Text), &auditYamlResponse) |
| 132 | + require.NoError(t, err, "expected to unmarshal response successfully") |
| 133 | + |
| 134 | + // Verify the response structure |
| 135 | + require.Equal(t, "SUCCESS", auditYamlResponse.Status, "expected status to be SUCCESS") |
| 136 | + |
| 137 | + // Log the YAML content lengths for debugging |
| 138 | + oldYamlLen := len(auditYamlResponse.Data.OldYaml) |
| 139 | + newYamlLen := len(auditYamlResponse.Data.NewYaml) |
| 140 | + t.Logf("Old YAML length: %d, New YAML length: %d", oldYamlLen, newYamlLen) |
| 141 | + |
| 142 | + // At least one of the YAMLs should be non-empty |
| 143 | + require.True(t, oldYamlLen > 0 || newYamlLen > 0, |
| 144 | + "expected at least one of old or new YAML to be non-empty") |
| 145 | +} |
0 commit comments