|
| 1 | +package syncapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + keyapi "github.com/matrix-org/dendrite/keyserver/api" |
| 12 | + "github.com/matrix-org/dendrite/roomserver/api" |
| 13 | + rsapi "github.com/matrix-org/dendrite/roomserver/api" |
| 14 | + "github.com/matrix-org/dendrite/setup/jetstream" |
| 15 | + "github.com/matrix-org/dendrite/syncapi/types" |
| 16 | + "github.com/matrix-org/dendrite/test" |
| 17 | + userapi "github.com/matrix-org/dendrite/userapi/api" |
| 18 | + "github.com/nats-io/nats.go" |
| 19 | +) |
| 20 | + |
| 21 | +type syncRoomserverAPI struct { |
| 22 | + rsapi.SyncRoomserverAPI |
| 23 | + rooms []*test.Room |
| 24 | +} |
| 25 | + |
| 26 | +func (s *syncRoomserverAPI) QueryLatestEventsAndState(ctx context.Context, req *rsapi.QueryLatestEventsAndStateRequest, res *rsapi.QueryLatestEventsAndStateResponse) error { |
| 27 | + var room *test.Room |
| 28 | + for _, r := range s.rooms { |
| 29 | + if r.ID == req.RoomID { |
| 30 | + room = r |
| 31 | + break |
| 32 | + } |
| 33 | + } |
| 34 | + if room == nil { |
| 35 | + res.RoomExists = false |
| 36 | + return nil |
| 37 | + } |
| 38 | + res.RoomVersion = room.Version |
| 39 | + return nil // TODO: return state |
| 40 | +} |
| 41 | + |
| 42 | +type syncUserAPI struct { |
| 43 | + userapi.SyncUserAPI |
| 44 | + accounts []userapi.Device |
| 45 | +} |
| 46 | + |
| 47 | +func (s *syncUserAPI) QueryAccessToken(ctx context.Context, req *userapi.QueryAccessTokenRequest, res *userapi.QueryAccessTokenResponse) error { |
| 48 | + for _, acc := range s.accounts { |
| 49 | + if acc.AccessToken == req.AccessToken { |
| 50 | + res.Device = &acc |
| 51 | + return nil |
| 52 | + } |
| 53 | + } |
| 54 | + res.Err = "unknown user" |
| 55 | + return nil |
| 56 | +} |
| 57 | + |
| 58 | +func (s *syncUserAPI) PerformLastSeenUpdate(ctx context.Context, req *userapi.PerformLastSeenUpdateRequest, res *userapi.PerformLastSeenUpdateResponse) error { |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +type syncKeyAPI struct { |
| 63 | + keyapi.KeyInternalAPI |
| 64 | +} |
| 65 | + |
| 66 | +func TestSyncAPI(t *testing.T) { |
| 67 | + test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) { |
| 68 | + testSync(t, dbType) |
| 69 | + }) |
| 70 | +} |
| 71 | + |
| 72 | +func testSync(t *testing.T, dbType test.DBType) { |
| 73 | + user := test.NewUser() |
| 74 | + room := test.NewRoom(t, user) |
| 75 | + alice := userapi.Device{ |
| 76 | + ID: "ALICEID", |
| 77 | + UserID: user.ID, |
| 78 | + AccessToken: "ALICE_BEARER_TOKEN", |
| 79 | + DisplayName: "Alice", |
| 80 | + AccountType: userapi.AccountTypeUser, |
| 81 | + } |
| 82 | + |
| 83 | + base, close := test.CreateBaseDendrite(t, dbType) |
| 84 | + defer close() |
| 85 | + |
| 86 | + jsctx, _ := base.NATS.Prepare(base.ProcessContext, &base.Cfg.Global.JetStream) |
| 87 | + defer jetstream.DeleteAllStreams(jsctx, &base.Cfg.Global.JetStream) |
| 88 | + var msgs []*nats.Msg |
| 89 | + for _, ev := range room.Events() { |
| 90 | + var addsStateIDs []string |
| 91 | + if ev.StateKey() != nil { |
| 92 | + addsStateIDs = append(addsStateIDs, ev.EventID()) |
| 93 | + } |
| 94 | + msgs = append(msgs, test.NewOutputEventMsg(t, base, room.ID, api.OutputEvent{ |
| 95 | + Type: rsapi.OutputTypeNewRoomEvent, |
| 96 | + NewRoomEvent: &rsapi.OutputNewRoomEvent{ |
| 97 | + Event: ev, |
| 98 | + AddsStateEventIDs: addsStateIDs, |
| 99 | + }, |
| 100 | + })) |
| 101 | + } |
| 102 | + AddPublicRoutes(base, &syncUserAPI{accounts: []userapi.Device{alice}}, &syncRoomserverAPI{rooms: []*test.Room{room}}, &syncKeyAPI{}) |
| 103 | + test.MustPublishMsgs(t, jsctx, msgs...) |
| 104 | + |
| 105 | + testCases := []struct { |
| 106 | + name string |
| 107 | + req *http.Request |
| 108 | + wantCode int |
| 109 | + wantJoinedRooms []string |
| 110 | + }{ |
| 111 | + { |
| 112 | + name: "missing access token", |
| 113 | + req: test.NewRequest(t, "GET", "/_matrix/client/v3/sync", test.WithQueryParams(map[string]string{ |
| 114 | + "timeout": "0", |
| 115 | + })), |
| 116 | + wantCode: 401, |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "unknown access token", |
| 120 | + req: test.NewRequest(t, "GET", "/_matrix/client/v3/sync", test.WithQueryParams(map[string]string{ |
| 121 | + "access_token": "foo", |
| 122 | + "timeout": "0", |
| 123 | + })), |
| 124 | + wantCode: 401, |
| 125 | + }, |
| 126 | + { |
| 127 | + name: "valid access token", |
| 128 | + req: test.NewRequest(t, "GET", "/_matrix/client/v3/sync", test.WithQueryParams(map[string]string{ |
| 129 | + "access_token": alice.AccessToken, |
| 130 | + "timeout": "0", |
| 131 | + })), |
| 132 | + wantCode: 200, |
| 133 | + wantJoinedRooms: []string{room.ID}, |
| 134 | + }, |
| 135 | + } |
| 136 | + // TODO: find a better way |
| 137 | + time.Sleep(500 * time.Millisecond) |
| 138 | + |
| 139 | + for _, tc := range testCases { |
| 140 | + w := httptest.NewRecorder() |
| 141 | + base.PublicClientAPIMux.ServeHTTP(w, tc.req) |
| 142 | + if w.Code != tc.wantCode { |
| 143 | + t.Fatalf("%s: got HTTP %d want %d", tc.name, w.Code, tc.wantCode) |
| 144 | + } |
| 145 | + if tc.wantJoinedRooms != nil { |
| 146 | + var res types.Response |
| 147 | + if err := json.NewDecoder(w.Body).Decode(&res); err != nil { |
| 148 | + t.Fatalf("%s: failed to decode response body: %s", tc.name, err) |
| 149 | + } |
| 150 | + if len(res.Rooms.Join) != len(tc.wantJoinedRooms) { |
| 151 | + t.Errorf("%s: got %v joined rooms, want %v.\nResponse: %+v", tc.name, len(res.Rooms.Join), len(tc.wantJoinedRooms), res) |
| 152 | + } |
| 153 | + t.Logf("res: %+v", res.Rooms.Join[room.ID]) |
| 154 | + |
| 155 | + gotEventIDs := make([]string, len(res.Rooms.Join[room.ID].Timeline.Events)) |
| 156 | + for i, ev := range res.Rooms.Join[room.ID].Timeline.Events { |
| 157 | + gotEventIDs[i] = ev.EventID |
| 158 | + } |
| 159 | + test.AssertEventIDsEqual(t, gotEventIDs, room.Events()) |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments