Skip to content

Commit

Permalink
account data: add test for setting and getting
Browse files Browse the repository at this point in the history
Signed-off-by: Sumner Evans <sumner@beeper.com>
  • Loading branch information
sumnerevans committed Dec 21, 2022
1 parent 46f8e84 commit c6337a0
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
8 changes: 8 additions & 0 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ func (c *CSAPI) SetGlobalAccountData(t *testing.T, eventType string, content map
return c.MustDoFunc(t, "PUT", []string{"_matrix", "client", "v3", "user", c.UserID, "account_data", eventType}, WithJSONBody(t, content))
}

func (c *CSAPI) GetRoomAccountData(t *testing.T, roomID string, eventType string) *http.Response {
return c.MustDoFunc(t, "GET", []string{"_matrix", "client", "v3", "user", c.UserID, "rooms", roomID, "account_data", eventType})
}

func (c *CSAPI) SetRoomAccountData(t *testing.T, roomID string, eventType string, content map[string]interface{}) *http.Response {
return c.MustDoFunc(t, "PUT", []string{"_matrix", "client", "v3", "user", c.UserID, "rooms", roomID, "account_data", eventType}, WithJSONBody(t, content))
}

// SendEventSynced sends `e` into the room and waits for its event ID to come down /sync.
// Returns the event ID of the sent event.
func (c *CSAPI) SendEventSynced(t *testing.T, roomID string, e b.Event) string {
Expand Down
83 changes: 83 additions & 0 deletions tests/csapi/account_data_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package csapi_tests

import (
"testing"

"github.com/matrix-org/complement/internal/b"
"github.com/matrix-org/complement/internal/match"
"github.com/matrix-org/complement/internal/must"
)

func TestAddAccountData(t *testing.T) {
deployment := Deploy(t, b.BlueprintOneToOneRoom)
defer deployment.Destroy(t)

alice := deployment.Client(t, "hs1", "@alice:hs1")

// sytest: Can add account data
// sytest: Can get account data without syncing
t.Run("Can add global account data", func(t *testing.T) {
// Set the account data entry
resp := alice.SetGlobalAccountData(t, "com.beeper.test", map[string]interface{}{"value": "first"})
must.MatchResponse(t, resp, match.HTTPResponse{
StatusCode: 200,
})

// check that getting the account data returns the correct value
must.MatchResponse(t, alice.GetGlobalAccountData(t, "com.beeper.test"), match.HTTPResponse{
StatusCode: 200,
JSON: []match.JSON{
match.JSONKeyEqual("value", "first"),
},
})

// Set it to something else
resp = alice.SetGlobalAccountData(t, "com.beeper.test", map[string]interface{}{"value": "second"})
must.MatchResponse(t, resp, match.HTTPResponse{
StatusCode: 200,
})

// check that getting the account data returns the updated value
must.MatchResponse(t, alice.GetGlobalAccountData(t, "com.beeper.test"), match.HTTPResponse{
StatusCode: 200,
JSON: []match.JSON{
match.JSONKeyEqual("value", "second"),
},
})
})

// sytest: Can add account data to room
// sytest: Can get room account data without syncing
t.Run("Can add room account data", func(t *testing.T) {
// Create a room
roomID := alice.CreateRoom(t, map[string]interface{}{})

// Set the room account data entry
resp := alice.SetRoomAccountData(t, roomID, "com.beeper.test", map[string]interface{}{"value": "room first"})
must.MatchResponse(t, resp, match.HTTPResponse{
StatusCode: 200,
})

// check that getting the account data returns the correct value
must.MatchResponse(t, alice.GetRoomAccountData(t, roomID, "com.beeper.test"), match.HTTPResponse{
StatusCode: 200,
JSON: []match.JSON{
match.JSONKeyEqual("value", "room first"),
},
})

// Set it to something else
resp = alice.SetRoomAccountData(t, roomID, "com.beeper.test", map[string]interface{}{"value": "room second"})
must.MatchResponse(t, resp, match.HTTPResponse{
StatusCode: 200,
})

// check that getting the account data returns the updated value
must.MatchResponse(t, alice.GetRoomAccountData(t, roomID, "com.beeper.test"), match.HTTPResponse{
StatusCode: 200,
JSON: []match.JSON{
match.JSONKeyEqual("value", "room second"),
},
})
})
}

0 comments on commit c6337a0

Please sign in to comment.