-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move /send_leave
to GMSL
#387
Open
S7evinK
wants to merge
13
commits into
main
Choose a base branch
from
s7evink/sendleave
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
bc7260c
Add send_leave
S7evinK 9e1377a
Add HandleSendLeave tests
S7evinK 2640a7e
Update interface
S7evinK 645785d
Remove dupe test
S7evinK b7a785d
Use version consts
S7evinK 5d099f9
Merge branch 'main' of github.com:matrix-org/gomatrixserverlib into s…
S7evinK 540062a
PR review comments, let IRoomVersion handle send leave
S7evinK 1f82150
Revert "PR review comments, let IRoomVersion handle send leave"
S7evinK 717c829
Merge branch 'main' of github.com:matrix-org/gomatrixserverlib into s…
S7evinK e3b02c9
Make tests/lint happy for now
S7evinK 2369def
Some tweaks
S7evinK 498c486
Fix oops
S7evinK 185a49a
Move membership check further to the top, so we can avoid unneeded DB
S7evinK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package gomatrixserverlib | ||
|
||
import ( | ||
"context" | ||
"crypto/rand" | ||
"fmt" | ||
"testing" | ||
|
@@ -228,3 +229,177 @@ func TestHandleMakeLeave(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
type dummyQuerier struct { | ||
pdu PDU | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to assert the type/state key are correct when queried via |
||
} | ||
|
||
func (d dummyQuerier) CurrentStateEvent(ctx context.Context, roomID spec.RoomID, eventType string, stateKey string) (PDU, error) { | ||
return d.pdu, nil | ||
} | ||
|
||
type noopJSONVerifier struct { | ||
err error | ||
results []VerifyJSONResult | ||
} | ||
|
||
func (v *noopJSONVerifier) VerifyJSONs(ctx context.Context, requests []VerifyJSONRequest) ([]VerifyJSONResult, error) { | ||
return v.results, v.err | ||
} | ||
|
||
func TestHandleSendLeave(t *testing.T) { | ||
type args struct { | ||
ctx context.Context | ||
requestContent []byte | ||
origin spec.ServerName | ||
roomVersion RoomVersion | ||
eventID string | ||
roomID string | ||
querier CurrentStateQuerier | ||
verifier JSONVerifier | ||
} | ||
|
||
_, sk, err := ed25519.GenerateKey(rand.Reader) | ||
if err != nil { | ||
t.Fatalf("Failed generating key: %v", err) | ||
} | ||
keyID := KeyID("ed25519:1234") | ||
|
||
validUser, _ := spec.NewUserID("@valid:localhost", true) | ||
|
||
stateKey := "" | ||
eb := MustGetRoomVersion(RoomVersionV10).NewEventBuilderFromProtoEvent(&ProtoEvent{ | ||
SenderID: validUser.String(), | ||
RoomID: "!valid:localhost", | ||
Type: spec.MRoomCreate, | ||
StateKey: &stateKey, | ||
PrevEvents: []interface{}{}, | ||
AuthEvents: []interface{}{}, | ||
Depth: 0, | ||
Content: spec.RawJSON(`{"creator":"@user:local","m.federate":true,"room_version":"10"}`), | ||
Unsigned: spec.RawJSON(""), | ||
}) | ||
createEvent, err := eb.Build(time.Now(), "localhost", keyID, sk) | ||
if err != nil { | ||
t.Fatalf("Failed building create event: %v", err) | ||
} | ||
|
||
stateKey = validUser.String() | ||
eb = MustGetRoomVersion(RoomVersionV10).NewEventBuilderFromProtoEvent(&ProtoEvent{ | ||
SenderID: validUser.String(), | ||
RoomID: "!valid:localhost", | ||
Type: spec.MRoomMember, | ||
StateKey: &stateKey, | ||
PrevEvents: []interface{}{}, | ||
AuthEvents: []interface{}{}, | ||
Depth: 0, | ||
Content: spec.RawJSON(`{"membership":"leave"}`), | ||
Unsigned: spec.RawJSON(""), | ||
}) | ||
leaveEvent, err := eb.Build(time.Now(), "localhost", keyID, sk) | ||
if err != nil { | ||
t.Fatalf("Failed building create event: %v", err) | ||
} | ||
|
||
eb = MustGetRoomVersion(RoomVersionV10).NewEventBuilderFromProtoEvent(&ProtoEvent{ | ||
SenderID: validUser.String(), | ||
RoomID: "!valid:localhost", | ||
Type: spec.MRoomMember, | ||
StateKey: &stateKey, | ||
PrevEvents: []interface{}{}, | ||
AuthEvents: []interface{}{}, | ||
Depth: 0, | ||
Content: spec.RawJSON(`{"membership":"join"}`), | ||
Unsigned: spec.RawJSON(""), | ||
}) | ||
joinEvent, err := eb.Build(time.Now(), "localhost", keyID, sk) | ||
if err != nil { | ||
t.Fatalf("Failed building create event: %v", err) | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
want PDU | ||
wantErr assert.ErrorAssertionFunc | ||
}{ | ||
{ | ||
name: "invalid roomID", | ||
args: args{roomID: "@notvalid:localhost"}, | ||
wantErr: assert.Error, | ||
}, | ||
{ | ||
name: "invalid room version", | ||
args: args{roomID: "!notvalid:localhost", roomVersion: "-1"}, | ||
wantErr: assert.Error, | ||
}, | ||
{ | ||
name: "invalid content body", | ||
args: args{roomID: "!notvalid:localhost", roomVersion: RoomVersionV1, requestContent: []byte("{")}, | ||
wantErr: assert.Error, | ||
}, | ||
{ | ||
name: "not canonical JSON", | ||
args: args{roomID: "!notvalid:localhost", roomVersion: RoomVersionV10, requestContent: []byte(`{"int":9007199254740992}`)}, // number to large, not canonical json | ||
wantErr: assert.Error, | ||
}, | ||
{ | ||
name: "wrong roomID in request", | ||
args: args{roomID: "!notvalid:localhost", roomVersion: RoomVersionV10, requestContent: createEvent.JSON()}, | ||
wantErr: assert.Error, | ||
}, | ||
{ | ||
name: "wrong eventID in request", | ||
args: args{roomID: "!valid:localhost", roomVersion: RoomVersionV10, requestContent: createEvent.JSON()}, | ||
wantErr: assert.Error, | ||
}, | ||
{ | ||
name: "empty statekey", | ||
args: args{roomID: "!valid:localhost", roomVersion: RoomVersionV10, eventID: createEvent.EventID(), requestContent: createEvent.JSON()}, | ||
wantErr: assert.Error, | ||
}, | ||
{ | ||
name: "wrong request origin", | ||
args: args{roomID: "!valid:localhost", roomVersion: RoomVersionV10, eventID: leaveEvent.EventID(), requestContent: leaveEvent.JSON()}, | ||
wantErr: assert.Error, | ||
}, | ||
{ | ||
name: "never joined the room no-ops", | ||
args: args{roomID: "!valid:localhost", querier: dummyQuerier{}, origin: validUser.Domain(), roomVersion: RoomVersionV10, eventID: leaveEvent.EventID(), requestContent: leaveEvent.JSON()}, | ||
wantErr: assert.NoError, | ||
}, | ||
{ | ||
name: "already left the room no-ops", | ||
args: args{roomID: "!valid:localhost", querier: dummyQuerier{pdu: leaveEvent}, origin: validUser.Domain(), roomVersion: RoomVersionV10, eventID: leaveEvent.EventID(), requestContent: leaveEvent.JSON()}, | ||
wantErr: assert.NoError, | ||
}, | ||
{ | ||
name: "JSON validation fails", | ||
args: args{ctx: context.Background(), roomID: "!valid:localhost", querier: dummyQuerier{pdu: createEvent}, verifier: &noopJSONVerifier{err: fmt.Errorf("err")}, origin: validUser.Domain(), roomVersion: RoomVersionV10, eventID: leaveEvent.EventID(), requestContent: leaveEvent.JSON()}, | ||
wantErr: assert.Error, | ||
}, | ||
{ | ||
name: "JSON validation fails 2", | ||
args: args{ctx: context.Background(), roomID: "!valid:localhost", querier: dummyQuerier{pdu: createEvent}, verifier: &noopJSONVerifier{results: []VerifyJSONResult{{Error: fmt.Errorf("err")}}}, origin: validUser.Domain(), roomVersion: RoomVersionV10, eventID: leaveEvent.EventID(), requestContent: leaveEvent.JSON()}, | ||
wantErr: assert.Error, | ||
}, | ||
{ | ||
name: "membership not set to leave", | ||
args: args{ctx: context.Background(), roomID: "!valid:localhost", querier: dummyQuerier{pdu: createEvent}, verifier: &noopJSONVerifier{results: []VerifyJSONResult{{}}}, origin: validUser.Domain(), roomVersion: RoomVersionV10, eventID: joinEvent.EventID(), requestContent: joinEvent.JSON()}, | ||
wantErr: assert.Error, | ||
}, | ||
{ | ||
name: "membership set to leave", | ||
args: args{ctx: context.Background(), roomID: "!valid:localhost", querier: dummyQuerier{pdu: createEvent}, verifier: &noopJSONVerifier{results: []VerifyJSONResult{{}}}, origin: validUser.Domain(), roomVersion: RoomVersionV10, eventID: leaveEvent.EventID(), requestContent: leaveEvent.JSON()}, | ||
wantErr: assert.NoError, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
_, err := HandleSendLeave(tt.args.ctx, tt.args.requestContent, tt.args.origin, tt.args.roomVersion, tt.args.eventID, tt.args.roomID, tt.args.querier, tt.args.verifier) | ||
if !tt.wantErr(t, err, fmt.Sprintf("HandleSendLeave(%v, %v, %v, %v, %v, %v, %v, %v)", tt.args.ctx, tt.args.requestContent, tt.args.origin, tt.args.roomVersion, tt.args.eventID, tt.args.roomID, tt.args.querier, tt.args.verifier)) { | ||
return | ||
} | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sidenote: this function and others like it will likely need to be modified to support pseudo IDs, as in many cases we pull out the state key and check that it is a user ID, and then check that the domain is correct on it. I would probably structure this as:
HandleSendLeave
as it is in this PRroomVer.HandleSendLeave(args...)
to let room versions decide how to implement this.Whilst the caller could call
roomVer.HandleSendLeave
this then creates bad symmetry, as there are cases where you don't know the room version at this point (e.g invites).