mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-20 12:33:09 -06:00
Verify request for notifications
This commit is contained in:
parent
b455c9fca4
commit
e293d82151
|
|
@ -2,6 +2,7 @@ package util_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -32,15 +33,44 @@ func TestNotifyUserCountsAsync(t *testing.T) {
|
||||||
room := test.NewRoom(t, alice)
|
room := test.NewRoom(t, alice)
|
||||||
dummyEvent := room.Events()[len(room.Events())-1]
|
dummyEvent := room.Events()[len(room.Events())-1]
|
||||||
|
|
||||||
|
appID := util.RandomString(8)
|
||||||
|
pushKey := util.RandomString(8)
|
||||||
|
|
||||||
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
||||||
receivedRequest := make(chan bool, 1)
|
receivedRequest := make(chan bool, 1)
|
||||||
// create a test server which responds to our /notify call
|
// create a test server which responds to our /notify call
|
||||||
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var data pushgateway.NotifyRequest
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
notification := data.Notification
|
||||||
|
// Validate the request
|
||||||
|
if notification.Counts == nil {
|
||||||
|
t.Fatal("no unread notification counts in request")
|
||||||
|
}
|
||||||
|
if unread := notification.Counts.Unread; unread != 1 {
|
||||||
|
t.Errorf("expected one unread notification, got %d", unread)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(notification.Devices) == 0 {
|
||||||
|
t.Fatal("expected devices in request")
|
||||||
|
}
|
||||||
|
|
||||||
|
// We only created one push device, so access it directly
|
||||||
|
device := notification.Devices[0]
|
||||||
|
if device.AppID != appID {
|
||||||
|
t.Errorf("unexpected app_id: %s, want %s", device.AppID, appID)
|
||||||
|
}
|
||||||
|
if device.PushKey != pushKey {
|
||||||
|
t.Errorf("unexpected push_key: %s, want %s", device.PushKey, pushKey)
|
||||||
|
}
|
||||||
|
|
||||||
// Return empty result, otherwise the call is handled as failed
|
// Return empty result, otherwise the call is handled as failed
|
||||||
if _, err := w.Write([]byte("{}")); err != nil {
|
if _, err := w.Write([]byte("{}")); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
receivedRequest <- true
|
close(receivedRequest)
|
||||||
}))
|
}))
|
||||||
defer srv.Close()
|
defer srv.Close()
|
||||||
|
|
||||||
|
|
@ -59,11 +89,10 @@ func TestNotifyUserCountsAsync(t *testing.T) {
|
||||||
// Prepare pusher with our test server URL
|
// Prepare pusher with our test server URL
|
||||||
if err := db.UpsertPusher(ctx, api.Pusher{
|
if err := db.UpsertPusher(ctx, api.Pusher{
|
||||||
Kind: api.HTTPKind,
|
Kind: api.HTTPKind,
|
||||||
AppID: util.RandomString(8),
|
AppID: appID,
|
||||||
PushKey: util.RandomString(8),
|
PushKey: pushKey,
|
||||||
Data: map[string]interface{}{
|
Data: map[string]interface{}{
|
||||||
"url": srv.URL,
|
"url": srv.URL,
|
||||||
"event_id": dummyEvent.EventID(),
|
|
||||||
},
|
},
|
||||||
}, aliceLocalpart, serverName); err != nil {
|
}, aliceLocalpart, serverName); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
|
|
|
||||||
|
|
@ -63,9 +63,7 @@ func StartPhoneHomeCollector(startTime time.Time, cfg *config.Dendrite, statsDB
|
||||||
}
|
}
|
||||||
|
|
||||||
// start initial run after 5min
|
// start initial run after 5min
|
||||||
time.AfterFunc(time.Minute*5, func() {
|
time.AfterFunc(time.Minute*5, p.collect)
|
||||||
p.collect()
|
|
||||||
})
|
|
||||||
|
|
||||||
// run every 3 hours
|
// run every 3 hours
|
||||||
ticker := time.NewTicker(time.Hour * 3)
|
ticker := time.NewTicker(time.Hour * 3)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue