2022-05-10 14:41:12 +02:00
|
|
|
package tables_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-27 14:40:35 +02:00
|
|
|
"fmt"
|
2022-05-10 14:41:12 +02:00
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
2022-10-27 14:40:35 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2022-05-10 14:41:12 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/postgres"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/sqlite3"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
|
|
|
"github.com/matrix-org/dendrite/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func mustCreatePublishedTable(t *testing.T, dbType test.DBType) (tab tables.Published, close func()) {
|
|
|
|
t.Helper()
|
|
|
|
connStr, close := test.PrepareDBConnectionString(t, dbType)
|
|
|
|
db, err := sqlutil.Open(&config.DatabaseOptions{
|
|
|
|
ConnectionString: config.DataSource(connStr),
|
|
|
|
}, sqlutil.NewExclusiveWriter())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
switch dbType {
|
|
|
|
case test.DBTypePostgres:
|
|
|
|
err = postgres.CreatePublishedTable(db)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
tab, err = postgres.PreparePublishedTable(db)
|
|
|
|
case test.DBTypeSQLite:
|
|
|
|
err = sqlite3.CreatePublishedTable(db)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
tab, err = sqlite3.PreparePublishedTable(db)
|
|
|
|
}
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
return tab, close
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPublishedTable(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2022-05-17 14:23:35 +02:00
|
|
|
alice := test.NewUser(t)
|
2022-05-10 14:41:12 +02:00
|
|
|
|
|
|
|
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
|
|
|
tab, close := mustCreatePublishedTable(t, dbType)
|
|
|
|
defer close()
|
|
|
|
|
|
|
|
// Publish some rooms
|
|
|
|
publishedRooms := []string{}
|
2022-10-27 14:40:35 +02:00
|
|
|
asID := ""
|
|
|
|
nwID := ""
|
2022-05-10 14:41:12 +02:00
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
room := test.NewRoom(t, alice)
|
|
|
|
published := i%2 == 0
|
2022-10-27 14:40:35 +02:00
|
|
|
err := tab.UpsertRoomPublished(ctx, nil, room.ID, asID, nwID, published)
|
2022-05-10 14:41:12 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
if published {
|
|
|
|
publishedRooms = append(publishedRooms, room.ID)
|
|
|
|
}
|
|
|
|
publishedRes, err := tab.SelectPublishedFromRoomID(ctx, nil, room.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, published, publishedRes)
|
|
|
|
}
|
|
|
|
sort.Strings(publishedRooms)
|
|
|
|
|
|
|
|
// check that we get the expected published rooms
|
2022-10-27 14:40:35 +02:00
|
|
|
roomIDs, err := tab.SelectAllPublishedRooms(ctx, nil, "", true, true)
|
2022-05-10 14:41:12 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, publishedRooms, roomIDs)
|
|
|
|
|
|
|
|
// test an actual upsert
|
|
|
|
room := test.NewRoom(t, alice)
|
2022-10-27 14:40:35 +02:00
|
|
|
err = tab.UpsertRoomPublished(ctx, nil, room.ID, asID, nwID, true)
|
2022-05-10 14:41:12 +02:00
|
|
|
assert.NoError(t, err)
|
2022-10-27 14:40:35 +02:00
|
|
|
err = tab.UpsertRoomPublished(ctx, nil, room.ID, asID, nwID, false)
|
2022-05-10 14:41:12 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
// should now be false, due to the upsert
|
|
|
|
publishedRes, err := tab.SelectPublishedFromRoomID(ctx, nil, room.ID)
|
|
|
|
assert.NoError(t, err)
|
2022-10-27 14:40:35 +02:00
|
|
|
assert.False(t, publishedRes, fmt.Sprintf("expected room %s to be unpublished", room.ID))
|
|
|
|
|
|
|
|
// network specific test
|
|
|
|
nwID = "irc"
|
|
|
|
room = test.NewRoom(t, alice)
|
|
|
|
err = tab.UpsertRoomPublished(ctx, nil, room.ID, asID, nwID, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
publishedRooms = append(publishedRooms, room.ID)
|
|
|
|
sort.Strings(publishedRooms)
|
|
|
|
// should only return the room for network "irc"
|
|
|
|
allNWPublished, err := tab.SelectAllPublishedRooms(ctx, nil, nwID, true, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, []string{room.ID}, allNWPublished)
|
|
|
|
|
|
|
|
// check that we still get all published rooms regardless networkID
|
|
|
|
roomIDs, err = tab.SelectAllPublishedRooms(ctx, nil, "", true, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, publishedRooms, roomIDs)
|
2022-05-10 14:41:12 +02:00
|
|
|
})
|
|
|
|
}
|