Files
schedtxt_api/internal/handler/schedule_message_status_test.go
T
2026-01-01 00:59:14 +00:00

76 lines
2.7 KiB
Go

package handler
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"git.kundeng.us/phoenix/textsender-models/tx0/message/scheduling"
"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 TestUpdateScheduledMessageStatusWithMock(t *testing.T) {
now := time.Now()
schMsgEventStore := mock.NewMockScheduledMessageEventStore()
contactStore := mock.NewMockContactStore()
messageStore := mock.NewMockMessageStore()
schMsgStore := mock.NewMockScheduledMessageStore()
recipientId := uuid.New()
messageId := uuid.New()
scheduledMessageId := uuid.New()
testUserId := uuid.New()
con := TestContact(recipientId, testUserId)
msg := TestMessage(messageId, testUserId)
schMsg := TestScheduledMessage(scheduledMessageId, testUserId, now)
event := TestScheduledMessageEvent(msg.Id, *con.Id, schMsg.Id)
ctx := t.Context()
if err := contactStore.CreateContact(ctx, &con); err != nil {
assert.NoError(t, err, "Error creating contact: %v", err)
} else if err = messageStore.CreateMessage(ctx, &msg); err != nil {
assert.NoError(t, err, "Error creating message: %v", err)
} else if err = schMsgStore.CreateScheduledMessage(ctx, &schMsg); err != nil {
assert.NoError(t, err, "Error creating scheduled message: %v", err)
} else if err = schMsgEventStore.CreateScheduledMessageEvent(ctx, &event); err != nil {
assert.NoError(t, err, "Error creating scheduled message event: %v", err)
}
testReq := RequestScheduledMessageStatus{}
testReq.Status = scheduling.Ready
testReq.ScheduledMessageId = schMsg.Id
jsonValue, _ := json.Marshal(testReq)
jsonBody := strings.NewReader(string(jsonValue))
req, _ := http.NewRequest("PATCH", endpoint.UpdateScheduledMessageStatusEndpoint, jsonBody)
rr := httptest.NewRecorder()
apiApp, err := GetApp()
assert.NoError(t, err, "Error getting app")
handler := NewScheduledMessageStatusHandler(apiApp, schMsgEventStore, schMsgStore)
handler.UpdateStatus(rr, req)
assert.Equal(t, http.StatusOK, rr.Code)
var response ScheduledMessageStatusResponse
err = json.Unmarshal(rr.Body.Bytes(), &response)
assert.NoError(t, err, "Error creating event %v", err)
assert.NotEmpty(t, response.Data, "Data should not be empty")
changes := response.Data[0]
assert.NotEqual(t, changes.OldStatus, changes.ScheduledMessage.Status, "The status should not match")
assert.Equal(t, scheduling.Pending, changes.OldStatus, "The Old status does not match Old %s New %s", changes.OldStatus, scheduling.Pending)
assert.Equal(t, scheduling.Ready, changes.ScheduledMessage.Status, "Status has not been updated")
}