-
Notifications
You must be signed in to change notification settings - Fork 61
Add tests for MSC3757 #733
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d6f55e0
Add tests for MSC3757
AndrewFerr edfcace
Allow write access only to the test state type
AndrewFerr 7f29646
Refactor test
AndrewFerr ec2d41e
Rename test file
AndrewFerr 08caccc
Rename test namespace
AndrewFerr 1c721ae
Use const for homeserver name
AndrewFerr d765a55
Lock non-owned-state tests to non-MSC room version
AndrewFerr 9060f95
Test suffixed key without separator
AndrewFerr 9f074e1
Test user ID in middle of state key
AndrewFerr f2d19d0
Rename admin to "room creator"
AndrewFerr 3d2940c
Test state with key of a user ID not in the room
AndrewFerr 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package tests | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/matrix-org/complement" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
complement.TestMain(m, "msc3757") | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package tests | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/matrix-org/complement" | ||
"github.com/matrix-org/complement/client" | ||
"github.com/matrix-org/complement/helpers" | ||
"github.com/matrix-org/complement/match" | ||
"github.com/matrix-org/complement/must" | ||
) | ||
|
||
const hsName = "hs1" | ||
const stateEventTestType = "com.example.test" | ||
|
||
// To stress-test parsing, include separator & sigil characters | ||
const stateKeySuffix = "_state_key_suffix:!@#$123" | ||
|
||
func TestWithoutOwnedState(t *testing.T) { | ||
deployment := complement.Deploy(t, 1) | ||
defer deployment.Destroy(t) | ||
|
||
admin := deployment.Register(t, hsName, helpers.RegistrationOpts{}) | ||
AndrewFerr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
user := deployment.Register(t, hsName, helpers.RegistrationOpts{}) | ||
|
||
roomID := admin.MustCreateRoom(t, map[string]interface{}{ | ||
"preset": "public_chat", | ||
"power_level_content_override": map[string]interface{}{ | ||
"events": map[string]int{ | ||
stateEventTestType: 0, | ||
}, | ||
}, | ||
}) | ||
AndrewFerr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
user.MustJoinRoom(t, roomID, nil) | ||
|
||
t.Run("parallel", func(t *testing.T) { | ||
t.Run("user can set state with their own user ID as state key", func(t *testing.T) { | ||
t.Parallel() | ||
res := sendState(t, roomID, user, user.UserID) | ||
must.MatchSuccess(t, res) | ||
}) | ||
t.Run("admin cannot set state with their own suffixed user ID as state key", func(t *testing.T) { | ||
t.Parallel() | ||
res := sendState(t, roomID, admin, admin.UserID+stateKeySuffix) | ||
mustMatchForbidden(t, res) | ||
}) | ||
t.Run("admin cannot set state with another user ID as state key", func(t *testing.T) { | ||
t.Parallel() | ||
res := sendState(t, roomID, admin, user.UserID) | ||
mustMatchForbidden(t, res) | ||
}) | ||
t.Run("admin cannot set state with another suffixed user ID as state key", func(t *testing.T) { | ||
t.Parallel() | ||
res := sendState(t, roomID, admin, user.UserID+stateKeySuffix) | ||
mustMatchForbidden(t, res) | ||
}) | ||
t.Run("admin cannot set state with malformed user ID as state key", func(t *testing.T) { | ||
AndrewFerr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
t.Parallel() | ||
res := sendState(t, roomID, admin, "@oops") | ||
mustMatchForbidden(t, res) | ||
}) | ||
}) | ||
} | ||
|
||
func TestMSC3757OwnedState(t *testing.T) { | ||
deployment := complement.Deploy(t, 1) | ||
defer deployment.Destroy(t) | ||
|
||
admin := deployment.Register(t, hsName, helpers.RegistrationOpts{}) | ||
user1 := deployment.Register(t, hsName, helpers.RegistrationOpts{}) | ||
user2 := deployment.Register(t, hsName, helpers.RegistrationOpts{}) | ||
|
||
roomID := admin.MustCreateRoom(t, map[string]interface{}{ | ||
"room_version": "org.matrix.msc3757.10", | ||
"preset": "public_chat", | ||
"power_level_content_override": map[string]interface{}{ | ||
"events": map[string]int{ | ||
stateEventTestType: 0, | ||
}, | ||
}, | ||
}) | ||
user1.MustJoinRoom(t, roomID, nil) | ||
user2.MustJoinRoom(t, roomID, nil) | ||
|
||
t.Run("parallel", func(t *testing.T) { | ||
t.Run("user can set state with their own suffixed user ID as state key", func(t *testing.T) { | ||
t.Parallel() | ||
res := sendState(t, roomID, user1, user1.UserID+stateKeySuffix) | ||
must.MatchSuccess(t, res) | ||
}) | ||
t.Run("admin can set state with another user ID as state key", func(t *testing.T) { | ||
t.Parallel() | ||
res := sendState(t, roomID, admin, user1.UserID) | ||
must.MatchSuccess(t, res) | ||
}) | ||
t.Run("admin can set state with another suffixed user ID as state key", func(t *testing.T) { | ||
t.Parallel() | ||
res := sendState(t, roomID, admin, user1.UserID+stateKeySuffix) | ||
must.MatchSuccess(t, res) | ||
}) | ||
t.Run("user cannot set state with another user ID as state key", func(t *testing.T) { | ||
t.Parallel() | ||
res := sendState(t, roomID, user1, user2.UserID) | ||
mustMatchForbidden(t, res) | ||
}) | ||
t.Run("user cannot set state with another suffixed user ID as state key", func(t *testing.T) { | ||
t.Parallel() | ||
res := sendState(t, roomID, user1, user2.UserID+stateKeySuffix) | ||
mustMatchForbidden(t, res) | ||
}) | ||
t.Run("admin cannot set state with malformed user ID as state key", func(t *testing.T) { | ||
t.Parallel() | ||
res := sendState(t, roomID, admin, "@oops") | ||
mustMatchInvalid(t, res) | ||
}) | ||
t.Run("admin cannot set state with improperly suffixed state key", func(t *testing.T) { | ||
t.Parallel() | ||
res := sendState(t, roomID, admin, admin.UserID+"@"+stateKeySuffix[1:]) | ||
mustMatchInvalid(t, res) | ||
}) | ||
}) | ||
} | ||
|
||
func sendState(t *testing.T, roomID string, user *client.CSAPI, stateKey string) *http.Response { | ||
t.Helper() | ||
return user.Do( | ||
t, | ||
"PUT", | ||
[]string{"_matrix", "client", "v3", "rooms", roomID, "state", stateEventTestType, stateKey}, | ||
client.WithJSONBody(t, map[string]interface{}{}), | ||
) | ||
} | ||
|
||
func mustMatchForbidden(t *testing.T, res *http.Response) { | ||
t.Helper() | ||
must.MatchResponse(t, res, match.HTTPResponse{ | ||
StatusCode: 403, | ||
JSON: []match.JSON{ | ||
match.JSONKeyEqual("errcode", "M_FORBIDDEN"), | ||
}, | ||
}) | ||
} | ||
|
||
func mustMatchInvalid(t *testing.T, res *http.Response) { | ||
t.Helper() | ||
must.MatchResponse(t, res, match.HTTPResponse{ | ||
StatusCode: 400, | ||
JSON: []match.JSON{ | ||
match.JSONKeyEqual("errcode", "M_BAD_JSON"), | ||
}, | ||
}) | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.