Skip to content

Commit

Permalink
Add test for nil senderID check
Browse files Browse the repository at this point in the history
  • Loading branch information
devonh committed Dec 14, 2023
1 parent 436aee5 commit 05f9321
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
34 changes: 34 additions & 0 deletions eventauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

"github.com/matrix-org/gomatrixserverlib/spec"
"github.com/stretchr/testify/assert"
)

func stateNeededEquals(a, b StateNeeded) bool {
Expand Down Expand Up @@ -1116,6 +1117,39 @@ func TestDemoteUserDefaultPowerLevelBelowOwn(t *testing.T) {
}
}

func NilUserIDForBadSenderTest(roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
if senderID == "@baduser" {
return nil, nil
}

return spec.NewUserID(string(senderID), true)
}

func TestPowerLevelCheckShouldNotPanic(t *testing.T) {
powerChangeShouldSucceed, err := MustGetRoomVersion(RoomVersionV1).NewEventFromTrustedJSON(spec.RawJSON(`{
"type": "m.room.power_levels",
"state_key": "",
"sender": "@u1:a",
"room_id": "!r1:a",
"event_id": "$e5:a",
"content": {
"users_default": 50,
"users": {
"@baduser": 0
},
"redact": 100
}
}`), false)
if err != nil {
t.Fatal(err)
}
assert.NotPanics(t, func() {
if err := Allowed(powerChangeShouldSucceed, powerLevelTestRoom, NilUserIDForBadSenderTest); err == nil {
panic("Event should not be allowed")
}
}, "")
}

func TestPromoteUserDefaultLevelAboveOwn(t *testing.T) {
// User shouldn't be able to promote the user default
// level above their own effective level.
Expand Down
4 changes: 4 additions & 0 deletions eventcontent.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func NewCreateContentFromAuthEvents(authEvents AuthEventProvider, userIDForSende
err = errorf("invalid sender userID: %s", err.Error())
return
}
if sender == nil {
err = errorf("userID not found for sender: %s in room %s", createEvent.SenderID(), createEvent.RoomID().String())
return
}

Check warning on line 82 in eventcontent.go

View check run for this annotation

Codecov / codecov/patch

eventcontent.go#L80-L82

Added lines #L80 - L82 were not covered by tests
c.senderDomain = string(sender.Domain())
return
}
Expand Down

0 comments on commit 05f9321

Please sign in to comment.