Skip to content

Commit

Permalink
Add tests to ensure MSC3030 functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
MadLittleMods committed Jul 28, 2021
1 parent fd752eb commit 1889d0a
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
41 changes: 41 additions & 0 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,27 @@ func (c *CSAPI) RegisterUser(t *testing.T, localpart, password string) (userID,
return userID, accessToken
}

// GetEvent fetches the given event from the specified room and returns a typed Event
func (c *CSAPI) GetEvent(t *testing.T, roomID, eventId string) b.Event {
t.Helper()
res := c.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "event", eventId})
body := ParseJSON(t, res)

statKeyRes := gjson.GetBytes(body, "state_key")
var stateKey *string = nil
if statKeyRes.Exists() {
stateKey = b.Ptr(statKeyRes.Str)
}

return b.Event{
Type: GetJSONFieldStr(t, body, "type"),
Sender: GetJSONFieldStr(t, body, "sender"),
StateKey: stateKey,
Content: GetJSONFieldStringMap(t, body, "content"),
Unsigned: GetJSONFieldStringMap(t, body, "unsigned"),
}
}

// MustDo will do the HTTP request and fail the test if the response is not 2xx
func (c *CSAPI) MustDo(t *testing.T, method string, paths []string, jsonBody interface{}) *http.Response {
t.Helper()
Expand Down Expand Up @@ -406,6 +427,26 @@ func GetJSONFieldStringArray(t *testing.T, body []byte, wantKey string) []string
return arr
}

func GetJSONFieldStringMap(t *testing.T, body []byte, wantKey string) map[string]interface{} {
t.Helper()

res := gjson.GetBytes(body, wantKey)

if !res.Exists() {
t.Fatalf("GetJSONFieldStringMap: key '%s' missing from %s", wantKey, string(body))
}

jsonMap := map[string]interface{}{}
res.ForEach(func(key, value gjson.Result) bool {
jsonMap[key.Str] = value.Str

// Keep iterating
return true
})

return jsonMap
}

// ParseJSON parses a JSON-encoded HTTP Response body into a byte slice
func ParseJSON(t *testing.T, res *http.Response) []byte {
t.Helper()
Expand Down
86 changes: 86 additions & 0 deletions tests/msc3030_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// +build msc3030

// This file contains tests for a jump to date API endpoint,
// currently experimental feature defined by MSC3030, which you can read here:
// https://github.com/matrix-org/matrix-doc/pull/3030

package tests

import (
"net/url"
"strconv"
"testing"
"time"

"github.com/matrix-org/complement/internal/b"
"github.com/matrix-org/complement/internal/client"
"github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
)

func TestJumpToDateEndpoint(t *testing.T) {
deployment := Deploy(t, b.BlueprintFederationTwoLocalOneRemote)
defer deployment.Destroy(t)

// Create the normal user which will send messages in the room
userID := "@alice:hs1"
alice := deployment.Client(t, "hs1", userID)

roomID := alice.CreateRoom(t, map[string]interface{}{})
alice.JoinRoom(t, roomID, nil)

timeBeforeEventA := time.Now()
eventAID := alice.SendEventSynced(t, roomID, b.Event{
Type: "m.room.message",
Content: map[string]interface{}{
"msgtype": "m.text",
"body": "Message A",
},
})
eventBID := alice.SendEventSynced(t, roomID, b.Event{
Type: "m.room.message",
Content: map[string]interface{}{
"msgtype": "m.text",
"body": "Message A",
},
})
timeAfterEventB := time.Now()

logrus.WithFields(logrus.Fields{
"eventAID": eventAID,
"eventBID": eventBID,
}).Error("see messages")

t.Run("parallel", func(t *testing.T) {
t.Run("should find event after given timestmap", func(t *testing.T) {
checkEventisReturnedForTime(t, alice, roomID, timeBeforeEventA, eventAID)
})

t.Run("should find event before given timestmap", func(t *testing.T) {
checkEventisReturnedForTime(t, alice, roomID, timeAfterEventB, eventBID)
})

})
}

func makeTimestampFromTime(t time.Time) int64 {
return t.UnixNano() / int64(time.Millisecond)
}

func checkEventisReturnedForTime(t *testing.T, c *client.CSAPI, roomID string, givenTime time.Time, expectedEventId string) {
t.Helper()

timestampString := strconv.FormatInt(makeTimestampFromTime(givenTime), 10)
timestampToEventRes := c.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "timestamp_to_event"}, client.WithContentType("application/json"), client.WithQueries(url.Values{
"ts": []string{timestampString},
}))
timestampToEventResBody := client.ParseJSON(t, timestampToEventRes)

actualEventIdRes := gjson.GetBytes(timestampToEventResBody, "event_id")
actualEventId := actualEventIdRes.Str

if actualEventId != expectedEventId {
actualEvent := c.GetEvent(t, roomID, actualEventId)
t.Fatalf("Expected to see %s but received %s\n%+v", expectedEventId, actualEventId, actualEvent)
}
}

0 comments on commit 1889d0a

Please sign in to comment.