From fc84556c59124ecdd44340dae7e7339dd2433fab Mon Sep 17 00:00:00 2001 From: MahtabBukhari Date: Tue, 19 Nov 2024 13:46:59 +0500 Subject: [PATCH] workflow request unit test --- handlers/workflow_test.go | 70 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 handlers/workflow_test.go diff --git a/handlers/workflow_test.go b/handlers/workflow_test.go new file mode 100644 index 000000000..3fe326ed8 --- /dev/null +++ b/handlers/workflow_test.go @@ -0,0 +1,70 @@ +package handlers + +import ( + "bytes" + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/google/uuid" + "github.com/stakwork/sphinx-tribes/db" + "github.com/stretchr/testify/assert" +) + +func TestHandleWorkflowRequest(t *testing.T) { + teardownSuite := SetupSuite(t) + defer teardownSuite(t) + + wh := NewWorkFlowHandler(db.TestDB) + + t.Run("successful workflow request", func(t *testing.T) { + request := db.WfRequest{ + WorkflowID: uuid.New().String(), + Source: "test_source", + RequestID: uuid.New().String(), + } + body, _ := json.Marshal(request) + + req := httptest.NewRequest(http.MethodPost, "/workflow/request", bytes.NewBuffer(body)) + req.Header.Set("Content-Type", "application/json") + w := httptest.NewRecorder() + + wh.HandleWorkflowRequest(w, req) + + assert.Equal(t, http.StatusCreated, w.Code) + var respBody map[string]string + err := json.NewDecoder(w.Body).Decode(&respBody) + assert.NoError(t, err) + assert.Equal(t, "success", respBody["status"]) + assert.NotEmpty(t, respBody["request_id"]) + }) + + t.Run("invalid JSON format", func(t *testing.T) { + req := httptest.NewRequest(http.MethodPost, "/workflow/request", bytes.NewBuffer([]byte("invalid-json"))) + req.Header.Set("Content-Type", "application/json") + w := httptest.NewRecorder() + + wh.HandleWorkflowRequest(w, req) + + assert.Equal(t, http.StatusBadRequest, w.Code) + assert.Contains(t, w.Body.String(), "Invalid request format") + }) + + t.Run("missing required fields", func(t *testing.T) { + request := db.WfRequest{ + Source: "test_source", + } + body, _ := json.Marshal(request) + + req := httptest.NewRequest(http.MethodPost, "/workflow/request", bytes.NewBuffer(body)) + req.Header.Set("Content-Type", "application/json") + w := httptest.NewRecorder() + + wh.HandleWorkflowRequest(w, req) + + assert.Equal(t, http.StatusBadRequest, w.Code) + assert.Contains(t, w.Body.String(), "Missing required fields: workflow_id or source") + }) + +}