databag/net/server/internal/ucProfileNotification_test.go

75 lines
1.9 KiB
Go
Raw Normal View History

2022-01-21 23:08:52 +00:00
package databag
import (
2022-01-23 04:37:14 +00:00
"time"
2022-01-21 23:08:52 +00:00
"testing"
2022-01-22 07:00:47 +00:00
"encoding/json"
"github.com/gorilla/websocket"
"github.com/stretchr/testify/assert"
2022-01-21 23:08:52 +00:00
)
func TestProfileNotification(t *testing.T) {
2022-01-22 07:00:47 +00:00
var views []CardView
var revision Revision
var data []byte
2022-01-21 23:08:52 +00:00
// start notifcation thread
go SendNotifications()
2022-01-22 07:00:47 +00:00
// connect contacts
2022-01-27 08:07:42 +00:00
_, a, _ := AddTestAccount("profilenotification0")
_, b, _ := AddTestAccount("profilenotification1")
aCard, _ := AddTestCard(a, b)
bCard, _ := AddTestCard(b, a)
OpenTestCard(a, aCard)
OpenTestCard(b, bCard)
2022-01-21 23:08:52 +00:00
2022-01-22 07:00:47 +00:00
// get views list of cards
r, w, _ := NewRequest("GET", "/contact/cards/view", nil)
2022-01-27 08:07:42 +00:00
SetBearerAuth(r, a)
2022-01-22 07:00:47 +00:00
GetCardView(w, r)
assert.NoError(t, ReadResponse(w, &views))
assert.Equal(t, len(views), 1)
profileRevision := views[0].RemoteProfile
2022-01-21 23:08:52 +00:00
2022-01-22 07:00:47 +00:00
// app connects websocket
ws := getTestWebsocket()
2022-01-27 08:07:42 +00:00
announce := Announce{ AppToken: a }
2022-01-22 07:00:47 +00:00
data, _ = json.Marshal(&announce)
ws.WriteMessage(websocket.TextMessage, data)
2022-01-21 23:08:52 +00:00
2022-01-22 07:00:47 +00:00
// receive revision
2022-01-23 04:37:14 +00:00
ws.SetReadDeadline(time.Now().Add(2 * time.Second))
2022-01-22 07:00:47 +00:00
_, data, _ = ws.ReadMessage()
assert.NoError(t, json.Unmarshal(data, &revision))
cardRevision := revision.Card
2022-01-21 23:08:52 +00:00
2022-01-22 07:00:47 +00:00
// update B profile
profileData := ProfileData{
Name: "Namer",
Location: "San Francisco",
Description: "databaggerr",
};
r, w, _ = NewRequest("PUT", "/profile/data", &profileData)
2022-01-27 08:07:42 +00:00
SetBearerAuth(r, b)
2022-01-22 07:00:47 +00:00
SetProfile(w, r)
assert.NoError(t, ReadResponse(w, nil))
2022-01-21 23:08:52 +00:00
2022-01-22 07:00:47 +00:00
// receive revision
2022-01-23 04:37:14 +00:00
ws.SetReadDeadline(time.Now().Add(2 * time.Second))
2022-01-22 07:00:47 +00:00
_, data, _ = ws.ReadMessage()
assert.NoError(t, json.Unmarshal(data, &revision))
assert.NotEqual(t, cardRevision, revision.Card)
2022-01-21 23:08:52 +00:00
2022-01-22 07:00:47 +00:00
// get views list of cards
r, w, _ = NewRequest("GET", "/contact/cards/view", nil)
2022-01-27 08:07:42 +00:00
SetBearerAuth(r, a)
2022-01-22 07:00:47 +00:00
GetCardView(w, r)
assert.NoError(t, ReadResponse(w, &views))
assert.Equal(t, len(views), 1)
assert.NotEqual(t, profileRevision, views[0].RemoteProfile)
2022-01-21 23:08:52 +00:00
// stop notification thread
ExitNotifications()
}