Extract check function

This commit is contained in:
Till Faelligen 2022-06-02 12:56:03 +02:00
parent 239dcb83b3
commit 131d8ea319

View file

@ -14,7 +14,6 @@ import (
rsapi "github.com/matrix-org/dendrite/roomserver/api" rsapi "github.com/matrix-org/dendrite/roomserver/api"
"github.com/matrix-org/dendrite/setup/base" "github.com/matrix-org/dendrite/setup/base"
"github.com/matrix-org/dendrite/setup/jetstream" "github.com/matrix-org/dendrite/setup/jetstream"
"github.com/matrix-org/dendrite/syncapi/routing"
"github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/dendrite/syncapi/types"
"github.com/matrix-org/dendrite/test" "github.com/matrix-org/dendrite/test"
"github.com/matrix-org/dendrite/test/testrig" "github.com/matrix-org/dendrite/test/testrig"
@ -343,7 +342,7 @@ func testHistoryVisibility(t *testing.T, dbType test.DBType) {
AccessToken: "BOD_BEARER_TOKEN", AccessToken: "BOD_BEARER_TOKEN",
DisplayName: "BOB", DisplayName: "BOB",
} }
// check guest and normaler user accounts // check guest and normal user accounts
for _, accType := range []userapi.AccountType{userapi.AccountTypeGuest, userapi.AccountTypeUser} { for _, accType := range []userapi.AccountType{userapi.AccountTypeGuest, userapi.AccountTypeUser} {
testCases := []struct { testCases := []struct {
historyVisibility string historyVisibility string
@ -427,29 +426,15 @@ func testHistoryVisibility(t *testing.T, dbType test.DBType) {
t.Logf("%s", w.Body.String()) t.Logf("%s", w.Body.String())
t.Fatalf("got HTTP %d want %d", w.Code, 200) t.Fatalf("got HTTP %d want %d", w.Code, 200)
} }
var res routing.MessageResp // We only care about the returned events at this point
var res struct {
Chunk []gomatrixserverlib.ClientEvent `json:"chunk"`
}
if err := json.NewDecoder(w.Body).Decode(&res); err != nil { if err := json.NewDecoder(w.Body).Decode(&res); err != nil {
t.Errorf("failed to decode response body: %s", err) t.Errorf("failed to decode response body: %s", err)
} }
if tc.wantResult.seeWithoutJoin { verifyEventVisible(t, tc.wantResult.seeWithoutJoin, beforeJoinEv, res.Chunk)
found := false
for _, ev := range res.Chunk {
if ev.EventID == beforeJoinEv.EventID() {
found = true
break
}
}
if !found {
t.Fatalf("expected to see event %s without joining but didn't: %+v", beforeJoinEv.EventID(), res.Chunk)
}
} else {
for _, ev := range res.Chunk {
if ev.EventID == beforeJoinEv.EventID() {
t.Fatalf("expected not to see event %s without joining: %+v", beforeJoinEv.EventID(), string(ev.Content))
}
}
}
// Create invite, a message, join the room and create another message. // Create invite, a message, join the room and create another message.
msgs := toNATSMsgs(t, base, room.CreateAndInsert(t, alice, "m.room.member", map[string]interface{}{"membership": "invite"}, test.WithStateKey(bob.ID))) msgs := toNATSMsgs(t, base, room.CreateAndInsert(t, alice, "m.room.member", map[string]interface{}{"membership": "invite"}, test.WithStateKey(bob.ID)))
@ -476,48 +461,34 @@ func testHistoryVisibility(t *testing.T, dbType test.DBType) {
if err := json.NewDecoder(w.Body).Decode(&res); err != nil { if err := json.NewDecoder(w.Body).Decode(&res); err != nil {
t.Errorf("failed to decode response body: %s", err) t.Errorf("failed to decode response body: %s", err)
} }
// verify result for seeBeforeJoin // verify results
if tc.wantResult.seeBeforeJoin { verifyEventVisible(t, tc.wantResult.seeBeforeJoin, beforeJoinEv, res.Chunk)
found := false verifyEventVisible(t, tc.wantResult.seeAfterInvite, afterInviteEv, res.Chunk)
for _, ev := range res.Chunk { })
if ev.EventID == beforeJoinEv.EventID() {
found = true
break
}
}
if !found {
t.Fatalf("expected to see event %s before joining but didn't: %+v", beforeJoinEv.EventID(), res.Chunk)
}
} else {
for _, ev := range res.Chunk {
if ev.EventID == beforeJoinEv.EventID() {
t.Fatalf("expected not to see event %s before joining: %+v", beforeJoinEv.EventID(), string(ev.Content))
} }
} }
} }
// verify result for seeAfterInvite func verifyEventVisible(t *testing.T, wantVisible bool, wantVisibleEvent *gomatrixserverlib.HeaderedEvent, chunk []gomatrixserverlib.ClientEvent) {
if tc.wantResult.seeAfterInvite { t.Helper()
if wantVisible {
found := false found := false
for _, ev := range res.Chunk { for _, ev := range chunk {
if ev.EventID == afterInviteEv.EventID() { if ev.EventID == wantVisibleEvent.EventID() {
found = true found = true
break break
} }
} }
if !found { if !found {
t.Fatalf("expected to see event %s after invite but didn't: %+v", afterInviteEv.EventID(), res.Chunk) t.Fatalf("expected to see event %s but didn't: %+v", wantVisibleEvent.EventID(), chunk)
} }
} else { } else {
for _, ev := range res.Chunk { for _, ev := range chunk {
if ev.EventID == afterInviteEv.EventID() { if ev.EventID == wantVisibleEvent.EventID() {
t.Fatalf("expected not to see event %s after invite: %+v", afterInviteEv.EventID(), string(ev.Content)) t.Fatalf("expected not to see event %s: %+v", wantVisibleEvent.EventID(), string(ev.Content))
} }
} }
} }
})
}
}
} }
func toNATSMsgs(t *testing.T, base *base.BaseDendrite, input ...*gomatrixserverlib.HeaderedEvent) []*nats.Msg { func toNATSMsgs(t *testing.T, base *base.BaseDendrite, input ...*gomatrixserverlib.HeaderedEvent) []*nats.Msg {