Private
Public Access
1
0

server: implements /markConversation

This commit is contained in:
2023-07-19 12:10:25 -06:00
parent 1fce2c7cb3
commit b77020e23c
3 changed files with 88 additions and 6 deletions

View File

@@ -229,6 +229,29 @@ func (m *MockHTTPServer) handlePollUpdates(w http.ResponseWriter, r *http.Reques
}
}
func (m *MockHTTPServer) handleMarkConversation(w http.ResponseWriter, r *http.Request) {
guid := r.URL.Query().Get("guid")
if len(guid) == 0 {
log.Error().Msg("handleMarkConversation: Got empty guid parameter")
http.Error(w, "no guid parameter specified", http.StatusBadRequest)
return
}
// Find conversation
convo, err := m.Server.ConversationForGUID(guid)
if err != nil {
log.Error().Err(err).Msgf("handleMarkConversation: Error finding conversation (%s)", guid)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Mark conversation
m.Server.MarkConversationAsRead(convo)
// Respond 200
w.WriteHeader(http.StatusOK)
}
func (m *MockHTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
m.logRequest(r, r.URL.Query().Encode())
m.mux.ServeHTTP(w, r)
@@ -248,6 +271,7 @@ func NewMockHTTPServer(config MockHTTPServerConfiguration) *MockHTTPServer {
this.mux.Handle("/messages", http.HandlerFunc(this.handleMessages))
this.mux.Handle("/pollUpdates", http.HandlerFunc(this.handlePollUpdates))
this.mux.Handle("/sendMessage", http.HandlerFunc(this.handleSendMessage))
this.mux.Handle("/markConversation", http.HandlerFunc(this.handleMarkConversation))
this.mux.Handle("/", http.HandlerFunc(this.handleNotFound))

View File

@@ -299,3 +299,46 @@ func TestUpdates(t *testing.T) {
t.Fatalf("Unexpected message text: %s (expected %s)", update.Message.Text, message.Text)
}
}
func TestMarkConversation(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)
// Receive message to mark as unread
message := model.Message{
Text: "This is a test.",
Sender: &conversation.Participants[0],
Date: time.Now(),
}
s.Server.ReceiveMessage(&conversation, message)
if convo, _ := s.Server.ConversationForGUID(guid); convo.UnreadCount != 1 {
t.Fatalf("Unexpected unread count: %d (expected %d)", convo.UnreadCount, 1)
}
// Mark conversation as read
resp, err := http.Post(httpServer.URL+"/markConversation?guid="+guid, "", nil)
if err != nil {
t.Fatalf("TestMarkConversation error: %s", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("Unexpected status code: %d (expected %d)", resp.StatusCode, http.StatusOK)
}
if convo, _ := s.Server.ConversationForGUID(guid); convo.UnreadCount != 0 {
t.Fatalf("Unexpected unread count: %d (expected %d)", convo.UnreadCount, 0)
}
}