Skip to content

Commit de9b121

Browse files
authored
Fix execute command request payload placement and add payload structure test (#81)
1 parent b4b1519 commit de9b121

2 files changed

Lines changed: 116 additions & 3 deletions

File tree

functions/itsmhelper/internal/handler/handlers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ func (h *Handler) createIncident(
249249
DefinitionID: &pluginDefIDServiceNow,
250250
OperationID: &operationID,
251251
ConfigID: &configID,
252-
Config: &models.DomainConfigData{
252+
Request: &models.DomainRequest{
253253
JSON: requestPayload,
254254
},
255255
},

functions/itsmhelper/internal/handler/handlers_test.go

Lines changed: 115 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,7 @@ func (s *HandlerTestSuite) TestHandleCreateIncident() {
834834
setupMockAPIIntegrations: func(mockAPIIntegrations *MockAPIIntegrationsService) {
835835
mockAPIIntegrations.ExecuteCommandFunc = func(params *api_integrations.ExecuteCommandParams, opts ...api_integrations.ClientOption) (*api_integrations.ExecuteCommandOK, error) {
836836
// Verify that custom fields are included in the request payload
837-
requestJSON, ok := params.Body.Resources[0].Config.JSON.(map[string]interface{})
837+
requestJSON, ok := params.Body.Resources[0].Request.JSON.(map[string]interface{})
838838
if !ok {
839839
return nil, fmt.Errorf("expected request JSON to be a map[string]interface{}")
840840
}
@@ -1437,7 +1437,7 @@ func (s *HandlerTestSuite) TestHandleCreateSIRIncident() {
14371437
}
14381438

14391439
// Verify that custom fields are included in the request payload
1440-
requestJSON, ok := params.Body.Resources[0].Config.JSON.(map[string]interface{})
1440+
requestJSON, ok := params.Body.Resources[0].Request.JSON.(map[string]interface{})
14411441
if !ok {
14421442
return nil, fmt.Errorf("expected request JSON to be a map[string]interface{}")
14431443
}
@@ -1741,6 +1741,119 @@ func (s *HandlerTestSuite) TestHandleCreateSIRIncident() {
17411741
}
17421742
}
17431743

1744+
// TestExecuteCommandPayloadJSON verifies the JSON wire format of the execute command request.
1745+
func (s *HandlerTestSuite) TestExecuteCommandPayloadJSON() {
1746+
tests := []struct {
1747+
name string
1748+
request CreateIncidentRequest
1749+
handler func(h *Handler, req fdk.RequestOf[CreateIncidentRequest]) fdk.Response
1750+
expectedJSON string
1751+
}{
1752+
{
1753+
name: "CreateIncident",
1754+
request: CreateIncidentRequest{
1755+
ConfigID: "config123",
1756+
EntityID: "entity123",
1757+
ShortDescription: "Test incident",
1758+
},
1759+
handler: func(h *Handler, req fdk.RequestOf[CreateIncidentRequest]) fdk.Response {
1760+
return h.HandleCreateIncident(context.Background(), req, fdk.WorkflowCtx{})
1761+
},
1762+
expectedJSON: `{
1763+
"definition_id": "` + pluginDefIDServiceNow + `",
1764+
"operation_id": "` + pluginOpIDServiceNowCreateIncident + `",
1765+
"config_id": "config123",
1766+
"config_auth_type": null,
1767+
"id": null,
1768+
"version": null,
1769+
"request": {
1770+
"json": {
1771+
"short_description": "Test incident"
1772+
}
1773+
}
1774+
}`,
1775+
},
1776+
{
1777+
name: "CreateSIRIncident",
1778+
request: CreateIncidentRequest{
1779+
ConfigID: "config456",
1780+
EntityID: "entity456",
1781+
ShortDescription: "Test SIR incident",
1782+
},
1783+
handler: func(h *Handler, req fdk.RequestOf[CreateIncidentRequest]) fdk.Response {
1784+
return h.HandleCreateSIRIncident(context.Background(), req, fdk.WorkflowCtx{})
1785+
},
1786+
expectedJSON: `{
1787+
"definition_id": "` + pluginDefIDServiceNow + `",
1788+
"operation_id": "` + pluginOpIDServiceNowCreateSIRIncident + `",
1789+
"config_id": "config456",
1790+
"config_auth_type": null,
1791+
"id": null,
1792+
"version": null,
1793+
"request": {
1794+
"json": {
1795+
"short_description": "Test SIR incident"
1796+
}
1797+
}
1798+
}`,
1799+
},
1800+
}
1801+
1802+
for _, tc := range tests {
1803+
s.Run(tc.name, func() {
1804+
s.mockStorage.GetObjectFunc = func(params *custom_storage.GetObjectParams, writer io.Writer, opts ...custom_storage.ClientOption) (*custom_storage.GetObjectOK, error) {
1805+
return nil, fmt.Errorf("status 404")
1806+
}
1807+
s.mockStorage.PutObjectFunc = func(params *custom_storage.PutObjectParams, opts ...custom_storage.ClientOption) (*custom_storage.PutObjectOK, error) {
1808+
return &custom_storage.PutObjectOK{}, nil
1809+
}
1810+
1811+
s.mockAPIIntegrations.ExecuteCommandFunc = func(params *api_integrations.ExecuteCommandParams, opts ...api_integrations.ClientOption) (*api_integrations.ExecuteCommandOK, error) {
1812+
resource := params.Body.Resources[0]
1813+
actualJSON, err := json.Marshal(resource)
1814+
s.Require().NoError(err)
1815+
1816+
var expected, actual interface{}
1817+
s.Require().NoError(json.Unmarshal([]byte(tc.expectedJSON), &expected))
1818+
s.Require().NoError(json.Unmarshal(actualJSON, &actual))
1819+
s.Equal(expected, actual, "execute command payload JSON must match expected structure")
1820+
1821+
result := map[string]interface{}{
1822+
"sys_id": "abc123",
1823+
"number": "INC0010099",
1824+
"sys_class_name": "incident",
1825+
}
1826+
return &api_integrations.ExecuteCommandOK{
1827+
Payload: &models.DomainExecuteCommandResultsV1{
1828+
Resources: []*models.DomainExecuteCommandResultV1{
1829+
{ResponseBody: map[string]interface{}{"result": result}},
1830+
},
1831+
},
1832+
}, nil
1833+
}
1834+
1835+
mockClientBuilder := func(token string, logger *slog.Logger) (*client.CrowdStrikeAPISpecification, string, error) {
1836+
mockClient := &client.CrowdStrikeAPISpecification{}
1837+
mockClient.CustomStorage = s.mockStorage
1838+
mockClient.APIIntegrations = s.mockAPIIntegrations
1839+
return mockClient, "us-1", nil
1840+
}
1841+
1842+
handler := &Handler{
1843+
logger: s.logger,
1844+
falconClientFunc: mockClientBuilder,
1845+
}
1846+
1847+
req := fdk.RequestOf[CreateIncidentRequest]{
1848+
Body: tc.request,
1849+
AccessToken: "test-token",
1850+
}
1851+
response := tc.handler(handler, req)
1852+
s.Equal(201, response.Code)
1853+
})
1854+
}
1855+
}
1856+
17441857
// TestHandlerSuite runs the handler test suite
17451858
func TestHandlerSuite(t *testing.T) {
17461859
suite.Run(t, new(HandlerTestSuite))

0 commit comments

Comments
 (0)