2022-11-11 15:44:59 +00:00
|
|
|
package helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
2023-03-17 11:09:45 +00:00
|
|
|
"time"
|
2022-11-11 15:44:59 +00:00
|
|
|
|
2023-03-17 11:09:45 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2022-11-11 15:44:59 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2023-03-01 16:06:47 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
|
|
|
|
2022-11-11 15:44:59 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage"
|
2023-03-17 11:09:45 +00:00
|
|
|
"github.com/matrix-org/dendrite/test"
|
2022-11-11 15:44:59 +00:00
|
|
|
)
|
|
|
|
|
2023-03-17 11:09:45 +00:00
|
|
|
func mustCreateDatabase(t *testing.T, dbType test.DBType) (storage.Database, func()) {
|
|
|
|
conStr, close := test.PrepareDBConnectionString(t, dbType)
|
|
|
|
caches := caching.NewRistrettoCache(8*1024*1024, time.Hour, caching.DisableMetrics)
|
2023-03-22 08:21:32 +00:00
|
|
|
cm := sqlutil.NewConnectionManager(nil, config.DatabaseOptions{})
|
2023-03-17 11:09:45 +00:00
|
|
|
db, err := storage.Open(context.Background(), cm, &config.DatabaseOptions{ConnectionString: config.DataSource(conStr)}, caches)
|
2022-11-11 15:44:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create Database: %v", err)
|
|
|
|
}
|
2023-03-17 11:09:45 +00:00
|
|
|
return db, close
|
2022-11-11 15:44:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsInvitePendingWithoutNID(t *testing.T) {
|
|
|
|
|
|
|
|
alice := test.NewUser(t)
|
|
|
|
bob := test.NewUser(t)
|
|
|
|
room := test.NewRoom(t, alice, test.RoomPreset(test.PresetPublicChat))
|
|
|
|
_ = bob
|
|
|
|
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
2023-03-17 11:09:45 +00:00
|
|
|
db, close := mustCreateDatabase(t, dbType)
|
2022-11-11 15:44:59 +00:00
|
|
|
defer close()
|
|
|
|
|
|
|
|
// store all events
|
|
|
|
var authNIDs []types.EventNID
|
|
|
|
for _, x := range room.Events() {
|
|
|
|
|
2023-04-27 11:54:20 +00:00
|
|
|
roomInfo, err := db.GetOrCreateRoomInfo(context.Background(), x.Event)
|
2023-02-24 08:40:20 +00:00
|
|
|
assert.NoError(t, err)
|
2023-03-01 16:06:47 +00:00
|
|
|
assert.NotNil(t, roomInfo)
|
2023-02-24 08:40:20 +00:00
|
|
|
|
|
|
|
eventTypeNID, err := db.GetOrCreateEventTypeNID(context.Background(), x.Type())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Greater(t, eventTypeNID, types.EventTypeNID(0))
|
|
|
|
|
|
|
|
eventStateKeyNID, err := db.GetOrCreateEventStateKeyNID(context.Background(), x.StateKey())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2023-03-01 16:06:47 +00:00
|
|
|
evNID, _, err := db.StoreEvent(context.Background(), x.Event, roomInfo, eventTypeNID, eventStateKeyNID, authNIDs, false)
|
2022-11-11 15:44:59 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
authNIDs = append(authNIDs, evNID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alice should have no pending invites and should have a NID
|
|
|
|
pendingInvite, _, _, _, err := IsInvitePending(context.Background(), db, room.ID, alice.ID)
|
|
|
|
assert.NoError(t, err, "failed to get pending invites")
|
|
|
|
assert.False(t, pendingInvite, "unexpected pending invite")
|
|
|
|
|
|
|
|
// Bob should have no pending invites and receive a new NID
|
|
|
|
pendingInvite, _, _, _, err = IsInvitePending(context.Background(), db, room.ID, bob.ID)
|
|
|
|
assert.NoError(t, err, "failed to get pending invites")
|
|
|
|
assert.False(t, pendingInvite, "unexpected pending invite")
|
|
|
|
})
|
|
|
|
}
|