Skip to content

Commit

Permalink
workflow request unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
MahtabBukhari committed Nov 19, 2024
1 parent c09d827 commit fc84556
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions handlers/workflow_test.go
Original file line number Diff line number Diff line change
@@ -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")
})

}

0 comments on commit fc84556

Please sign in to comment.