tsk-12: Added endpoint to get message (#18)

Closes #12

Reviewed-on: phoenix/textsender-api#18
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
phoenix
2025-11-06 20:42:24 +00:00
committed by phoenix
parent acf414c876
commit 607059e7ce
6 changed files with 193 additions and 0 deletions
+31
View File
@@ -2,11 +2,13 @@ package handler
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"git.kundeng.us/phoenix/textsender-models/pkg/message"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
@@ -41,3 +43,32 @@ func TestCreateMessageWithMock(t *testing.T) {
assert.NotNil(t, response.Data[0].Id, "Id should not be nil")
}
func TestGetMessageWithMock(t *testing.T) {
mockstore := NewMockMessageStore()
testUserId := uuid.New()
testCon := message.Message{Content: "Who is the one that benefits?", UserId: testUserId}
ctx := t.Context()
if err := mockstore.CreateMessage(ctx, &testCon); err != nil {
assert.NoError(t, err, "Error creating message")
return
}
url := fmt.Sprintf("%s?user_id=%s", endpoint.GET_MESSAGE, testCon.UserId)
req, _ := http.NewRequest("GET", url, nil)
rr := httptest.NewRecorder()
messageHandler := NewMessageHandler(mockstore)
messageHandler.GetMessage(rr, req)
assert.Equal(t, http.StatusOK, rr.Code)
var response GetMessageResponse
err := json.Unmarshal(rr.Body.Bytes(), &response)
assert.NoError(t, err, "Error getting message %v", err)
assert.NotEmpty(t, response.Data, "No Message retrieved")
assert.NotNil(t, response.Data[0].Id, "Id should not be nil")
}