Update sendMessage to return inserted guid for new protocol
This commit is contained in:
@@ -534,3 +534,71 @@ func TestMessageQueries(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestSendMessage(t *testing.T) {
|
||||
s := web.NewMockHTTPServer(web.MockHTTPServerConfiguration{AuthEnabled: true})
|
||||
httpServer := httptest.NewServer(s)
|
||||
|
||||
// Mock conversation
|
||||
guid := "1234567890"
|
||||
conversation := model.Conversation{
|
||||
Date: time.Now(),
|
||||
Participants: []string{"Alice"},
|
||||
UnreadCount: 0,
|
||||
Guid: guid,
|
||||
}
|
||||
|
||||
s.Server.AddConversation(conversation)
|
||||
|
||||
request := web.SendMessageRequest{
|
||||
ConversationGUID: "1234567890",
|
||||
Body: "This is a test.",
|
||||
TransferGUIDs: []string{},
|
||||
}
|
||||
|
||||
// Encode as json
|
||||
requestJSON, err := json.Marshal(request)
|
||||
if err != nil {
|
||||
t.Fatalf("Error marshalling JSON: %s", err)
|
||||
}
|
||||
|
||||
// Send request
|
||||
resp, err := http.Post(httpServer.URL+"/sendMessage", "application/json", io.NopCloser(bytes.NewReader(requestJSON)))
|
||||
if err != nil {
|
||||
t.Fatalf("TestSendMessage error: %s", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("Unexpected status code: %d (expected %d)", resp.StatusCode, http.StatusOK)
|
||||
}
|
||||
|
||||
// Decode response
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("Error decoding body: %s", body)
|
||||
}
|
||||
|
||||
var response web.SendMessageResponse
|
||||
err = json.Unmarshal(body, &response)
|
||||
if err != nil {
|
||||
t.Fatalf("Error unmarshalling JSON: %s", err)
|
||||
}
|
||||
|
||||
if response.Guid == "" {
|
||||
t.Fatalf("Unexpected empty guid")
|
||||
}
|
||||
|
||||
// Make sure message is present
|
||||
messages := s.Server.MessagesForConversation(&conversation)
|
||||
found := false
|
||||
for _, message := range messages {
|
||||
if message.Guid == response.Guid {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if found != true {
|
||||
t.Fatalf("Message not found in conversation")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user