| 
 | 1 | +package tests  | 
 | 2 | + | 
 | 3 | +import (  | 
 | 4 | +	"github.com/matrix-org/complement/internal/b"  | 
 | 5 | +	"github.com/matrix-org/complement/internal/federation"  | 
 | 6 | +	"github.com/matrix-org/complement/runtime"  | 
 | 7 | +	"github.com/matrix-org/gomatrixserverlib"  | 
 | 8 | +	"testing"  | 
 | 9 | +	"time"  | 
 | 10 | +)  | 
 | 11 | + | 
 | 12 | +// test that a redaction is sent out over federation even if we don't have the original event  | 
 | 13 | +func TestFederationRedactSendsWithoutEvent(t *testing.T) {  | 
 | 14 | +	runtime.SkipIf(t, runtime.Dendrite)  | 
 | 15 | + | 
 | 16 | +	deployment := Deploy(t, b.BlueprintAlice)  | 
 | 17 | +	defer deployment.Destroy(t)  | 
 | 18 | + | 
 | 19 | +	alice := deployment.Client(t, "hs1", "@alice:hs1")  | 
 | 20 | + | 
 | 21 | +	waiter := NewWaiter()  | 
 | 22 | +	wantEventType := "m.room.redaction"  | 
 | 23 | + | 
 | 24 | +	// create a remote homeserver  | 
 | 25 | +	srv := federation.NewServer(t, deployment,  | 
 | 26 | +		federation.HandleKeyRequests(),  | 
 | 27 | +		federation.HandleMakeSendJoinRequests(),  | 
 | 28 | +		federation.HandleTransactionRequests(  | 
 | 29 | +			// listen for PDU events in transactions  | 
 | 30 | +			func(ev *gomatrixserverlib.Event) {  | 
 | 31 | +				defer waiter.Finish()  | 
 | 32 | + | 
 | 33 | +				if ev.Type() != wantEventType {  | 
 | 34 | +					t.Errorf("Wrong event type, got %s want %s", ev.Type(), wantEventType)  | 
 | 35 | +				}  | 
 | 36 | +			},  | 
 | 37 | +			nil,  | 
 | 38 | +		),  | 
 | 39 | +	)  | 
 | 40 | +	cancel := srv.Listen()  | 
 | 41 | +	defer cancel()  | 
 | 42 | + | 
 | 43 | +	// create username  | 
 | 44 | +	charlie := srv.UserID("charlie")  | 
 | 45 | + | 
 | 46 | +	ver := alice.GetDefaultRoomVersion(t)  | 
 | 47 | + | 
 | 48 | +	// the remote homeserver creates a public room allowing anyone to redact  | 
 | 49 | +	initalEvents := federation.InitialRoomEvents(ver, charlie)  | 
 | 50 | +	plEvent := initalEvents[2]  | 
 | 51 | +	plEvent.Content["redact"] = 0  | 
 | 52 | +	serverRoom := srv.MustMakeRoom(t, ver, initalEvents)  | 
 | 53 | +	roomAlias := srv.MakeAliasMapping("flibble", serverRoom.RoomID)  | 
 | 54 | + | 
 | 55 | +	// the local homeserver joins the room  | 
 | 56 | +	alice.JoinRoom(t, roomAlias, []string{srv.ServerName()})  | 
 | 57 | + | 
 | 58 | +	// inject event to redact in the room  | 
 | 59 | +	badEvent := srv.MustCreateEvent(t, serverRoom, b.Event{  | 
 | 60 | +		Type:   "m.room.message",  | 
 | 61 | +		Sender: charlie,  | 
 | 62 | +		Content: map[string]interface{}{  | 
 | 63 | +			"body": "666",  | 
 | 64 | +		},  | 
 | 65 | +	})  | 
 | 66 | +	serverRoom.AddEvent(badEvent)  | 
 | 67 | + | 
 | 68 | +	eventID := badEvent.EventID()  | 
 | 69 | +	fullServerName := srv.ServerName()  | 
 | 70 | +	eventToRedact := eventID + ":" + fullServerName  | 
 | 71 | + | 
 | 72 | +	// the client sends a request to the local homeserver to send the redaction  | 
 | 73 | +	res := alice.SendRedaction(t, serverRoom.RoomID, b.Event{Type: wantEventType, Content: map[string]interface{}{  | 
 | 74 | +		"msgtype": "m.room.redaction"},  | 
 | 75 | +		Redacts: eventToRedact}, eventToRedact)  | 
 | 76 | + | 
 | 77 | +	// wait for redaction to arrive at remote homeserver  | 
 | 78 | +	waiter.Wait(t, 1*time.Second)  | 
 | 79 | + | 
 | 80 | +	// Check that the last event in the room is now the redaction  | 
 | 81 | +	lastEvent := serverRoom.Timeline[len(serverRoom.Timeline)-1]  | 
 | 82 | +	lastEventType := lastEvent.Type()  | 
 | 83 | +	wantedType := "m.room.redaction"  | 
 | 84 | +	if lastEventType != wantedType {  | 
 | 85 | +		t.Fatalf("Incorrent event type %s, wanted m.room.redaction.", lastEventType)  | 
 | 86 | +	}  | 
 | 87 | + | 
 | 88 | +	// check that the event id of the redaction sent by alice is the same as the redaction event in the room  | 
 | 89 | +	if res != lastEvent.EventID() {  | 
 | 90 | +		t.Fatalf("Incorrent event id %s, wanted %s.", res, lastEvent.EventID())  | 
 | 91 | +	}  | 
 | 92 | + | 
 | 93 | +}  | 
0 commit comments