Files
schedtxt_api/internal/handler/message_test.go
T
phoenixandphoenix a51979e724 tsk-58: Update names of Contact endpoint (#67)
Closes #58

Reviewed-on: phoenix/textsender-api#67
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
2026-01-07 20:24:30 +00:00

75 lines
2.1 KiB
Go

package handler
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"git.kundeng.us/phoenix/textsender-models/tx0/message"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"git.kundeng.us/phoenix/textsender-api/internal/handler/endpoint"
"git.kundeng.us/phoenix/textsender-api/internal/store/mock"
)
func TestCreateMessageWithMock(t *testing.T) {
mockStore := mock.NewMockMessageStore()
apiApp, err := GetApp()
assert.NoError(t, err, "Error getting app")
handler := NewMessageHandler(apiApp, mockStore)
testUserId := uuid.New()
testBody := RequestAddMessage{Content: "Who could tell?", UserId: testUserId}
jsonValue, _ := json.Marshal(testBody)
req, _ := http.NewRequest("POST", endpoint.ADD_MESSAGE, strings.NewReader(string(jsonValue)))
rr := httptest.NewRecorder()
handler.AddMessage(rr, req)
assert.Equal(t, http.StatusCreated, rr.Code)
var response AddMessageResponse
err = json.Unmarshal(rr.Body.Bytes(), &response)
assert.NoError(t, err, "Error Creating message %v", err)
assert.NotEmpty(t, response.Data, "No Message created")
assert.NotNil(t, response.Data[0].Id, "Id should not be nil")
}
func TestGetMessageWithMock(t *testing.T) {
mockstore := mock.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()
apiApp, err := GetApp()
assert.NoError(t, err, "Error getting app")
messageHandler := NewMessageHandler(apiApp, 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")
}