|
| 1 | +package csapi_tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/tidwall/gjson" |
| 9 | + |
| 10 | + "github.com/matrix-org/complement" |
| 11 | + "github.com/matrix-org/complement/client" |
| 12 | + "github.com/matrix-org/complement/helpers" |
| 13 | + "github.com/matrix-org/complement/match" |
| 14 | + "github.com/matrix-org/complement/must" |
| 15 | +) |
| 16 | + |
| 17 | +func TestPublicRooms(t *testing.T) { |
| 18 | + deployment := complement.Deploy(t, 1) |
| 19 | + defer deployment.Destroy(t) |
| 20 | + |
| 21 | + t.Run("parallel", func(t *testing.T) { |
| 22 | + // sytest: Name/topic keys are correct |
| 23 | + t.Run("Name/topic keys are correct", func(t *testing.T) { |
| 24 | + t.Parallel() |
| 25 | + authedClient := deployment.Register(t, "hs1", helpers.RegistrationOpts{}) |
| 26 | + |
| 27 | + // Define room configurations matching the Sytest |
| 28 | + roomConfigs := []struct { |
| 29 | + alias string |
| 30 | + name string |
| 31 | + topic string |
| 32 | + }{ |
| 33 | + {"publicroomalias_no_name", "", ""}, |
| 34 | + {"publicroomalias_with_name", "name_1", ""}, |
| 35 | + {"publicroomalias_with_topic", "", "topic_1"}, |
| 36 | + {"publicroomalias_with_name_topic", "name_2", "topic_2"}, |
| 37 | + {"publicroom_with_unicode_chars_name", "un nom français", ""}, |
| 38 | + {"publicroom_with_unicode_chars_topic", "", "un topic à la française"}, |
| 39 | + {"publicroom_with_unicode_chars_name_topic", "un nom français", "un topic à la française"}, |
| 40 | + } |
| 41 | + |
| 42 | + // Create all rooms with their configurations |
| 43 | + createdRooms := make(map[string]struct { |
| 44 | + roomID string |
| 45 | + name string |
| 46 | + topic string |
| 47 | + }) |
| 48 | + |
| 49 | + for _, config := range roomConfigs { |
| 50 | + roomOptions := map[string]interface{}{ |
| 51 | + "visibility": "public", |
| 52 | + "room_alias_name": config.alias, |
| 53 | + } |
| 54 | + |
| 55 | + if config.name != "" { |
| 56 | + roomOptions["name"] = config.name |
| 57 | + } |
| 58 | + if config.topic != "" { |
| 59 | + roomOptions["topic"] = config.topic |
| 60 | + } |
| 61 | + |
| 62 | + roomID := authedClient.MustCreateRoom(t, roomOptions) |
| 63 | + createdRooms[config.alias] = struct { |
| 64 | + roomID string |
| 65 | + name string |
| 66 | + topic string |
| 67 | + }{ |
| 68 | + roomID: roomID, |
| 69 | + name: config.name, |
| 70 | + topic: config.topic, |
| 71 | + } |
| 72 | + t.Logf("Created room %s with alias %s", roomID, config.alias) |
| 73 | + } |
| 74 | + |
| 75 | + // Poll /publicRooms until all our rooms appear with correct data |
| 76 | + authedClient.MustDo(t, "GET", []string{"_matrix", "client", "v3", "publicRooms"}, |
| 77 | + client.WithRetryUntil(15*time.Second, func(res *http.Response) bool { |
| 78 | + body := must.ParseJSON(t, res.Body) |
| 79 | + |
| 80 | + must.MatchGJSON( |
| 81 | + t, |
| 82 | + body, |
| 83 | + match.JSONKeyPresent("chunk"), |
| 84 | + match.JSONKeyTypeEqual("chunk", gjson.JSON), |
| 85 | + ) |
| 86 | + |
| 87 | + chunk := body.Get("chunk") |
| 88 | + if !chunk.IsArray() { |
| 89 | + t.Logf("chunk is not an array") |
| 90 | + return false |
| 91 | + } |
| 92 | + |
| 93 | + // Track which rooms we've correctly found |
| 94 | + foundRooms := make(map[string]bool) |
| 95 | + |
| 96 | + // Check each room in the public rooms list |
| 97 | + for _, roomData := range chunk.Array() { |
| 98 | + // Verify required keys are present |
| 99 | + must.MatchGJSON( |
| 100 | + t, |
| 101 | + roomData, |
| 102 | + match.JSONKeyPresent("world_readable"), |
| 103 | + match.JSONKeyPresent("guest_can_join"), |
| 104 | + match.JSONKeyPresent("num_joined_members"), |
| 105 | + ) |
| 106 | + |
| 107 | + canonicalAlias := roomData.Get("canonical_alias").Str |
| 108 | + name := roomData.Get("name").Str |
| 109 | + topic := roomData.Get("topic").Str |
| 110 | + numMembers := roomData.Get("num_joined_members").Int() |
| 111 | + |
| 112 | + // Skip rooms that aren't ours |
| 113 | + if canonicalAlias == "" { |
| 114 | + continue |
| 115 | + } |
| 116 | + |
| 117 | + // Find which of our rooms this matches |
| 118 | + var matchedAlias string |
| 119 | + for alias := range createdRooms { |
| 120 | + expectedAlias := "#" + alias + ":hs1" |
| 121 | + if canonicalAlias == expectedAlias { |
| 122 | + matchedAlias = alias |
| 123 | + break |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if matchedAlias == "" { |
| 128 | + continue // Not one of our rooms |
| 129 | + } |
| 130 | + |
| 131 | + roomConfig := createdRooms[matchedAlias] |
| 132 | + |
| 133 | + // Verify member count |
| 134 | + if numMembers != 1 { |
| 135 | + t.Logf("Room %s has %d members, expected 1", matchedAlias, numMembers) |
| 136 | + return false |
| 137 | + } |
| 138 | + |
| 139 | + // Verify name field |
| 140 | + if roomConfig.name != "" { |
| 141 | + if name != roomConfig.name { |
| 142 | + t.Logf("Room %s has name '%s', expected '%s'", matchedAlias, name, roomConfig.name) |
| 143 | + return false |
| 144 | + } |
| 145 | + } else { |
| 146 | + if name != "" { |
| 147 | + t.Logf("Room %s has unexpected name '%s', expected no name", matchedAlias, name) |
| 148 | + return false |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + // Verify topic field |
| 153 | + if roomConfig.topic != "" { |
| 154 | + if topic != roomConfig.topic { |
| 155 | + t.Logf("Room %s has topic '%s', expected '%s'", matchedAlias, topic, roomConfig.topic) |
| 156 | + return false |
| 157 | + } |
| 158 | + } else { |
| 159 | + if topic != "" { |
| 160 | + t.Logf("Room %s has unexpected topic '%s', expected no topic", matchedAlias, topic) |
| 161 | + return false |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + // Mark this room as correctly found |
| 166 | + foundRooms[matchedAlias] = true |
| 167 | + t.Logf("Successfully validated room %s", matchedAlias) |
| 168 | + } |
| 169 | + |
| 170 | + // Check if we found all our rooms |
| 171 | + if len(foundRooms) != len(createdRooms) { |
| 172 | + missing := []string{} |
| 173 | + for alias := range createdRooms { |
| 174 | + if !foundRooms[alias] { |
| 175 | + missing = append(missing, alias) |
| 176 | + } |
| 177 | + } |
| 178 | + t.Logf("Missing rooms in public list: %v (found %d/%d)", missing, len(foundRooms), len(createdRooms)) |
| 179 | + return false |
| 180 | + } |
| 181 | + |
| 182 | + t.Logf("All %d rooms found with correct name/topic data", len(foundRooms)) |
| 183 | + return true |
| 184 | + }), |
| 185 | + ) |
| 186 | + }) |
| 187 | + }) |
| 188 | +} |
0 commit comments