2023-06-16 23:35:41 -07:00
|
|
|
package web_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"code.severnaya.net/kordophone-mock/v2/model"
|
2023-06-16 23:38:48 -07:00
|
|
|
"code.severnaya.net/kordophone-mock/v2/server"
|
2023-06-16 23:35:41 -07:00
|
|
|
"code.severnaya.net/kordophone-mock/v2/web"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestVersion(t *testing.T) {
|
|
|
|
|
s := httptest.NewServer(web.NewMockHTTPServer())
|
|
|
|
|
|
|
|
|
|
resp, err := http.Get(s.URL + "/version")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("TestVersion error: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Error decoding body: %s", body)
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 23:38:48 -07:00
|
|
|
if string(body) != server.VERSION {
|
2023-06-16 23:35:41 -07:00
|
|
|
t.Fatalf("Unexpected return value: %s (expected %s)", body, "1.0")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestStatus(t *testing.T) {
|
|
|
|
|
s := httptest.NewServer(web.NewMockHTTPServer())
|
|
|
|
|
|
|
|
|
|
resp, err := http.Get(s.URL + "/status")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("TestStatus error: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Error decoding body: %s", body)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if string(body) != "OK" {
|
|
|
|
|
t.Fatalf("Unexpected return value: %s (expected %s)", body, "OK")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestConversations(t *testing.T) {
|
|
|
|
|
server := web.NewMockHTTPServer()
|
|
|
|
|
httpServer := httptest.NewServer(server)
|
|
|
|
|
|
|
|
|
|
conversation := model.Conversation{
|
|
|
|
|
Date: time.Now(),
|
|
|
|
|
Participants: []string{"Alice", "Bob"},
|
|
|
|
|
UnreadCount: 1,
|
|
|
|
|
LastMessagePreview: "Hello world",
|
|
|
|
|
Guid: "1234567890",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
server.Server.AddConversation(conversation)
|
|
|
|
|
|
|
|
|
|
resp, err := http.Get(httpServer.URL + "/conversations")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("TestConversations error: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Error decoding body: %s", body)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var convos []model.Conversation
|
|
|
|
|
err = json.Unmarshal(body, &convos)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Error unmarshalling JSON: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(convos) != 1 {
|
|
|
|
|
t.Fatalf("Unexpected number of conversations: %d (expected %d)", len(convos), 1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if convos[0].Guid != conversation.Guid {
|
|
|
|
|
t.Fatalf("Unexpected conversation GUID: %s (expected %s)", convos[0].Guid, conversation.Guid)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if convos[0].LastMessagePreview != conversation.LastMessagePreview {
|
|
|
|
|
t.Fatalf("Unexpected conversation LastMessagePreview: %s (expected %s)", convos[0].LastMessagePreview, conversation.LastMessagePreview)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if convos[0].UnreadCount != conversation.UnreadCount {
|
|
|
|
|
t.Fatalf("Unexpected conversation UnreadCount: %d (expected %d)", convos[0].UnreadCount, conversation.UnreadCount)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i, p := range convos[0].Participants {
|
|
|
|
|
if p != conversation.Participants[i] {
|
|
|
|
|
t.Fatalf("Unexpected conversation Participants: %s (expected %s)", p, conversation.Participants[i])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !convos[0].Date.Equal(conversation.Date) {
|
|
|
|
|
t.Fatalf("Unexpected conversation Date: %s (expected %s)", convos[0].Date, conversation.Date)
|
|
|
|
|
}
|
|
|
|
|
}
|