Skip to content

Commit

Permalink
Extend 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 05f9321 commit 72c39c8
Showing 1 changed file with 64 additions and 2 deletions.
66 changes: 64 additions & 2 deletions eventauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1125,8 +1125,47 @@ func NilUserIDForBadSenderTest(roomID spec.RoomID, senderID spec.SenderID) (*spe
return spec.NewUserID(string(senderID), true)
}

var nilPowerLevelTestRoom = &testAuthEvents{
CreateJSON: json.RawMessage(`{
"type": "m.room.create",
"state_key": "",
"sender": "@baduser",
"room_id": "!r1:a",
"event_id": "$e1:a",
"content": {
"room_version": "1"
}
}`),
PowerLevelsJSON: json.RawMessage(`{
"type": "m.room.power_levels",
"state_key": "",
"sender": "@u1:a",
"room_id": "!r1:a",
"event_id": "$e3:a",
"content": {
"users_default": 100,
"users": {
"@u1:a": 100
},
"redact": 100
}
}`),
MemberJSON: map[string]json.RawMessage{
"@u1:a": json.RawMessage(`{
"type": "m.room.member",
"state_key": "@u1:a",
"sender": "@u1:a",
"room_id": "!r1:a",
"event_id": "$e2:a",
"content": {
"membership": "join"
}
}`),
},
}

func TestPowerLevelCheckShouldNotPanic(t *testing.T) {
powerChangeShouldSucceed, err := MustGetRoomVersion(RoomVersionV1).NewEventFromTrustedJSON(spec.RawJSON(`{
powerChangeBadUser, err := MustGetRoomVersion(RoomVersionV1).NewEventFromTrustedJSON(spec.RawJSON(`{
"type": "m.room.power_levels",
"state_key": "",
"sender": "@u1:a",
Expand All @@ -1144,7 +1183,30 @@ func TestPowerLevelCheckShouldNotPanic(t *testing.T) {
t.Fatal(err)
}
assert.NotPanics(t, func() {
if err := Allowed(powerChangeShouldSucceed, powerLevelTestRoom, NilUserIDForBadSenderTest); err == nil {
if err := Allowed(powerChangeBadUser, powerLevelTestRoom, NilUserIDForBadSenderTest); err == nil {
panic("Event should not be allowed")
}
}, "")

powerChange, 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": {
"@u1:a": 0
},
"redact": 100
}
}`), false)
if err != nil {
t.Fatal(err)
}
assert.NotPanics(t, func() {
if err := Allowed(powerChange, nilPowerLevelTestRoom, NilUserIDForBadSenderTest); err == nil {
panic("Event should not be allowed")
}
}, "")
Expand Down

0 comments on commit 72c39c8

Please sign in to comment.