tsk-19: Update status of scheduled message endpoint (#32)
Closes #19 Reviewed-on: phoenix/textsender-api#32 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.kundeng.us/phoenix/textsender-models/pkg/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()
|
||||
|
||||
handler := NewScheduledMessageStatusHandler(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")
|
||||
}
|
||||
Reference in New Issue
Block a user