mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-08-01 13:52:46 +00:00
Add some roomserver UTs (#3067)
Adds tests for `QueryRestrictedJoinAllowed`, `IsServerAllowed` and `PerformRoomUpgrade`. Refactors the `QueryRoomVersionForRoom` method to accept a string and return a `gmsl.RoomVersion` instead of req/resp structs. Adds some more caching for `GetStateEvent` This should also fix #2912 by ignoring state events belonging to other users.
This commit is contained in:
parent
dd5e47a9a7
commit
2475cf4b61
20 changed files with 705 additions and 135 deletions
|
@ -478,7 +478,7 @@ func (r *Inputer) processRoomEvent(
|
|||
|
||||
// If guest_access changed and is not can_join, kick all guest users.
|
||||
if event.Type() == spec.MRoomGuestAccess && gjson.GetBytes(event.Content(), "guest_access").Str != "can_join" {
|
||||
if err = r.kickGuests(ctx, event, roomInfo); err != nil {
|
||||
if err = r.kickGuests(ctx, event, roomInfo); err != nil && err != sql.ErrNoRows {
|
||||
logrus.WithError(err).Error("failed to kick guest users on m.room.guest_access revocation")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -319,9 +319,7 @@ func publishNewRoomAndUnpublishOldRoom(
|
|||
}
|
||||
|
||||
func (r *Upgrader) validateRoomExists(ctx context.Context, roomID string) error {
|
||||
verReq := api.QueryRoomVersionForRoomRequest{RoomID: roomID}
|
||||
verRes := api.QueryRoomVersionForRoomResponse{}
|
||||
if err := r.URSAPI.QueryRoomVersionForRoom(ctx, &verReq, &verRes); err != nil {
|
||||
if _, err := r.URSAPI.QueryRoomVersionForRoom(ctx, roomID); err != nil {
|
||||
return &api.PerformError{
|
||||
Code: api.PerformErrorNoRoom,
|
||||
Msg: "Room does not exist",
|
||||
|
@ -357,7 +355,7 @@ func (r *Upgrader) generateInitialEvents(ctx context.Context, oldRoom *api.Query
|
|||
continue
|
||||
}
|
||||
if event.Type() == spec.MRoomMember && !event.StateKeyEquals(userID) {
|
||||
// With the exception of bans and invites which we do want to copy, we
|
||||
// With the exception of bans which we do want to copy, we
|
||||
// should ignore membership events that aren't our own, as event auth will
|
||||
// prevent us from being able to create membership events on behalf of other
|
||||
// users anyway unless they are invites or bans.
|
||||
|
@ -367,11 +365,15 @@ func (r *Upgrader) generateInitialEvents(ctx context.Context, oldRoom *api.Query
|
|||
}
|
||||
switch membership {
|
||||
case spec.Ban:
|
||||
case spec.Invite:
|
||||
default:
|
||||
continue
|
||||
}
|
||||
}
|
||||
// skip events that rely on a specific user being present
|
||||
sKey := *event.StateKey()
|
||||
if event.Type() != spec.MRoomMember && len(sKey) > 0 && sKey[:1] == "@" {
|
||||
continue
|
||||
}
|
||||
state[gomatrixserverlib.StateKeyTuple{EventType: event.Type(), StateKey: *event.StateKey()}] = event
|
||||
}
|
||||
|
||||
|
|
|
@ -521,14 +521,10 @@ func (r *Queryer) QueryMissingEvents(
|
|||
response.Events = make([]*gomatrixserverlib.HeaderedEvent, 0, len(loadedEvents)-len(eventsToFilter))
|
||||
for _, event := range loadedEvents {
|
||||
if !eventsToFilter[event.EventID()] {
|
||||
roomVersion, verr := r.roomVersion(event.RoomID())
|
||||
if verr != nil {
|
||||
return verr
|
||||
}
|
||||
if _, ok := redactEventIDs[event.EventID()]; ok {
|
||||
event.Redact()
|
||||
}
|
||||
response.Events = append(response.Events, event.Headered(roomVersion))
|
||||
response.Events = append(response.Events, event.Headered(info.RoomVersion))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -696,34 +692,20 @@ func GetAuthChain(
|
|||
}
|
||||
|
||||
// QueryRoomVersionForRoom implements api.RoomserverInternalAPI
|
||||
func (r *Queryer) QueryRoomVersionForRoom(
|
||||
ctx context.Context,
|
||||
request *api.QueryRoomVersionForRoomRequest,
|
||||
response *api.QueryRoomVersionForRoomResponse,
|
||||
) error {
|
||||
if roomVersion, ok := r.Cache.GetRoomVersion(request.RoomID); ok {
|
||||
response.RoomVersion = roomVersion
|
||||
return nil
|
||||
func (r *Queryer) QueryRoomVersionForRoom(ctx context.Context, roomID string) (gomatrixserverlib.RoomVersion, error) {
|
||||
if roomVersion, ok := r.Cache.GetRoomVersion(roomID); ok {
|
||||
return roomVersion, nil
|
||||
}
|
||||
|
||||
info, err := r.DB.RoomInfo(ctx, request.RoomID)
|
||||
info, err := r.DB.RoomInfo(ctx, roomID)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
if info == nil {
|
||||
return fmt.Errorf("QueryRoomVersionForRoom: missing room info for room %s", request.RoomID)
|
||||
return "", fmt.Errorf("QueryRoomVersionForRoom: missing room info for room %s", roomID)
|
||||
}
|
||||
response.RoomVersion = info.RoomVersion
|
||||
r.Cache.StoreRoomVersion(request.RoomID, response.RoomVersion)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Queryer) roomVersion(roomID string) (gomatrixserverlib.RoomVersion, error) {
|
||||
var res api.QueryRoomVersionForRoomResponse
|
||||
err := r.QueryRoomVersionForRoom(context.Background(), &api.QueryRoomVersionForRoomRequest{
|
||||
RoomID: roomID,
|
||||
}, &res)
|
||||
return res.RoomVersion, err
|
||||
r.Cache.StoreRoomVersion(roomID, info.RoomVersion)
|
||||
return info.RoomVersion, nil
|
||||
}
|
||||
|
||||
func (r *Queryer) QueryPublishedRooms(
|
||||
|
@ -910,8 +892,8 @@ func (r *Queryer) QueryRestrictedJoinAllowed(ctx context.Context, req *api.Query
|
|||
if err = json.Unmarshal(joinRulesEvent.Content(), &joinRules); err != nil {
|
||||
return fmt.Errorf("json.Unmarshal: %w", err)
|
||||
}
|
||||
// If the join rule isn't "restricted" then there's nothing more to do.
|
||||
res.Restricted = joinRules.JoinRule == spec.Restricted
|
||||
// If the join rule isn't "restricted" or "knock_restricted" then there's nothing more to do.
|
||||
res.Restricted = joinRules.JoinRule == spec.Restricted || joinRules.JoinRule == spec.KnockRestricted
|
||||
if !res.Restricted {
|
||||
return nil
|
||||
}
|
||||
|
@ -932,9 +914,9 @@ func (r *Queryer) QueryRestrictedJoinAllowed(ctx context.Context, req *api.Query
|
|||
if err != nil {
|
||||
return fmt.Errorf("r.DB.GetStateEvent: %w", err)
|
||||
}
|
||||
var powerLevels gomatrixserverlib.PowerLevelContent
|
||||
if err = json.Unmarshal(powerLevelsEvent.Content(), &powerLevels); err != nil {
|
||||
return fmt.Errorf("json.Unmarshal: %w", err)
|
||||
powerLevels, err := powerLevelsEvent.PowerLevels()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to get powerlevels: %w", err)
|
||||
}
|
||||
// Step through the join rules and see if the user matches any of them.
|
||||
for _, rule := range joinRules.Allow {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue