From 068fc9d2cd95bb090a37b7ec49f499eef34a9d0b Mon Sep 17 00:00:00 2001 From: elraphty Date: Tue, 24 Dec 2024 12:21:16 +0100 Subject: [PATCH] fixed merge conflicts --- handlers/chat.go | 9 ++ handlers/chat_test.go | 195 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 203 insertions(+), 1 deletion(-) diff --git a/handlers/chat.go b/handlers/chat.go index af676666c..5b49c9cd6 100644 --- a/handlers/chat.go +++ b/handlers/chat.go @@ -72,6 +72,15 @@ func (ch *ChatHandler) CreateChat(w http.ResponseWriter, r *http.Request) { return } + if request.WorkspaceID == "" || request.Title == "" { + w.WriteHeader(http.StatusBadRequest) + json.NewEncoder(w).Encode(ChatResponse{ + Success: false, + Message: "Invalid request body", + }) + return + } + chat := &db.Chat{ ID: xid.New().String(), WorkspaceID: request.WorkspaceID, diff --git a/handlers/chat_test.go b/handlers/chat_test.go index 71f17e78f..41f463dff 100644 --- a/handlers/chat_test.go +++ b/handlers/chat_test.go @@ -152,7 +152,7 @@ func TestUpdateChat(t *testing.T) { }) } -func TestGetChat(t *testing.T) { +func TestCreateChat(t *testing.T) { teardownSuite := SetupSuite(t) defer teardownSuite(t) @@ -314,4 +314,197 @@ func TestGetChat(t *testing.T) { assert.True(t, ok) assert.Equal(t, 100, len(responseChats)) }) + + t.Run("should successfully create chat when valid data is provided", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(chatHandler.CreateChat) + + requestBody := map[string]string{ + "workspaceId": "workspace123", + "title": "Test Chat", + } + bodyBytes, _ := json.Marshal(requestBody) + + req, err := http.NewRequest( + http.MethodPost, + "/hivechat", + bytes.NewReader(bodyBytes), + ) + if err != nil { + t.Fatal(err) + } + + handler.ServeHTTP(rr, req) + + var response ChatResponse + _ = json.Unmarshal(rr.Body.Bytes(), &response) + assert.Equal(t, http.StatusOK, rr.Code) + assert.True(t, response.Success) + assert.Equal(t, "Chat created successfully", response.Message) + + responseData, ok := response.Data.(map[string]interface{}) + assert.True(t, ok, "Response data should be a map") + assert.NotEmpty(t, responseData["id"]) + assert.Equal(t, "workspace123", responseData["workspaceId"]) + assert.Equal(t, "Test Chat", responseData["title"]) + }) + + t.Run("should return bad request when request body is invalid", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(chatHandler.CreateChat) + + invalidJson := []byte(`{"title": "Test Chat"`) + req, err := http.NewRequest( + http.MethodPost, + "/hivechat", + bytes.NewReader(invalidJson), + ) + if err != nil { + t.Fatal(err) + } + + handler.ServeHTTP(rr, req) + + var response ChatResponse + _ = json.Unmarshal(rr.Body.Bytes(), &response) + assert.Equal(t, http.StatusBadRequest, rr.Code) + assert.False(t, response.Success) + assert.Equal(t, "Invalid request body", response.Message) + }) + + t.Run("should return bad request when required field workspaceId is missing", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(chatHandler.CreateChat) + + requestBody := map[string]string{ + "title": "Test Chat", + } + bodyBytes, _ := json.Marshal(requestBody) + + req, err := http.NewRequest( + http.MethodPost, + "/hivechat", + bytes.NewReader(bodyBytes), + ) + if err != nil { + t.Fatal(err) + } + + handler.ServeHTTP(rr, req) + + var response ChatResponse + _ = json.Unmarshal(rr.Body.Bytes(), &response) + assert.Equal(t, http.StatusBadRequest, rr.Code) + assert.False(t, response.Success) + assert.Equal(t, "Invalid request body", response.Message) + }) + + t.Run("should return bad request when required field title is missing", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(chatHandler.CreateChat) + + requestBody := map[string]string{ + "workspaceId": "workspace123", + } + bodyBytes, _ := json.Marshal(requestBody) + + req, err := http.NewRequest( + http.MethodPost, + "/hivechat", + bytes.NewReader(bodyBytes), + ) + if err != nil { + t.Fatal(err) + } + + handler.ServeHTTP(rr, req) + + var response ChatResponse + _ = json.Unmarshal(rr.Body.Bytes(), &response) + assert.Equal(t, http.StatusBadRequest, rr.Code) + assert.False(t, response.Success) + assert.Equal(t, "Invalid request body", response.Message) + }) + + t.Run("should handle empty strings in required fields", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(chatHandler.CreateChat) + + requestBody := map[string]string{ + "workspaceId": "", + "title": "", + } + bodyBytes, _ := json.Marshal(requestBody) + + req, err := http.NewRequest( + http.MethodPost, + "/hivechat", + bytes.NewReader(bodyBytes), + ) + if err != nil { + t.Fatal(err) + } + + handler.ServeHTTP(rr, req) + + var response ChatResponse + _ = json.Unmarshal(rr.Body.Bytes(), &response) + assert.Equal(t, http.StatusBadRequest, rr.Code) + assert.False(t, response.Success) + assert.Equal(t, "Invalid request body", response.Message) + }) + + t.Run("should return bad request when non-string title is provided", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(chatHandler.CreateChat) + + rawJSON := []byte(`{ + "workspaceId": "workspace123", + "title": 12345 + }`) + + req, err := http.NewRequest( + http.MethodPost, + "/hivechat", + bytes.NewReader(rawJSON), + ) + if err != nil { + t.Fatal(err) + } + + handler.ServeHTTP(rr, req) + + var response ChatResponse + _ = json.Unmarshal(rr.Body.Bytes(), &response) + assert.Equal(t, http.StatusBadRequest, rr.Code) + assert.False(t, response.Success) + assert.Equal(t, "Invalid request body", response.Message) + }) + + t.Run("should return bad request when non-string workspaceId is provided", func(t *testing.T) { + rr := httptest.NewRecorder() + handler := http.HandlerFunc(chatHandler.CreateChat) + + rawJSON := []byte(`{ + "workspaceId": 12345, + "title": "Test Chat" + }`) + + req, err := http.NewRequest( + http.MethodPost, + "/hivechat", + bytes.NewReader(rawJSON), + ) + if err != nil { + t.Fatal(err) + } + + handler.ServeHTTP(rr, req) + + var response ChatResponse + _ = json.Unmarshal(rr.Body.Bytes(), &response) + assert.Equal(t, http.StatusBadRequest, rr.Code) + assert.False(t, response.Success) + assert.Equal(t, "Invalid request body", response.Message) + }) }