Skip to content

Commit 3049413

Browse files
sandhosehughns
authored andcommitted
Test the scope of a transaction IDs after using refresh token
1 parent 9f43aa2 commit 3049413

File tree

2 files changed

+1150
-150
lines changed

2 files changed

+1150
-150
lines changed

internal/client/client.go

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ func (c *CSAPI) SetPushRule(t *testing.T, scope string, kind string, ruleID stri
264264
// SendEventUnsynced sends `e` into the room.
265265
// Returns the event ID of the sent event.
266266
func (c *CSAPI) SendEventUnsynced(t *testing.T, roomID string, e b.Event) string {
267-
t.Helper()
268267
txnID := int(atomic.AddInt64(&c.txnID, 1))
269268
return c.SendEventUnsyncedWithTxnID(t, roomID, e, strconv.Itoa(txnID))
270269
}
@@ -459,6 +458,80 @@ func (c *CSAPI) LoginUser(t *testing.T, localpart, password string, opts ...Logi
459458
return userID, accessToken, deviceID
460459
}
461460

461+
// LoginUserWithDeviceID will log in to a homeserver on an existing device
462+
func (c *CSAPI) LoginUserWithDeviceID(t *testing.T, localpart, password, deviceID string) (userID, accessToken string) {
463+
t.Helper()
464+
reqBody := map[string]interface{}{
465+
"identifier": map[string]interface{}{
466+
"type": "m.id.user",
467+
"user": localpart,
468+
},
469+
"device_id": deviceID,
470+
"password": password,
471+
"type": "m.login.password",
472+
}
473+
res := c.MustDoFunc(t, "POST", []string{"_matrix", "client", "v3", "login"}, WithJSONBody(t, reqBody))
474+
475+
body, err := ioutil.ReadAll(res.Body)
476+
if err != nil {
477+
t.Fatalf("unable to read response body: %v", err)
478+
}
479+
480+
userID = gjson.GetBytes(body, "user_id").Str
481+
accessToken = gjson.GetBytes(body, "access_token").Str
482+
if gjson.GetBytes(body, "device_id").Str != deviceID {
483+
t.Fatalf("device_id returned by login does not match the one requested")
484+
}
485+
return userID, accessToken
486+
}
487+
488+
// LoginUserWithRefreshToken will log in to a homeserver, with refresh token enabled,
489+
// and create a new device on an existing user.
490+
func (c *CSAPI) LoginUserWithRefreshToken(t *testing.T, localpart, password string) (userID, accessToken, refreshToken, deviceID string, expiresInMs int64) {
491+
t.Helper()
492+
reqBody := map[string]interface{}{
493+
"identifier": map[string]interface{}{
494+
"type": "m.id.user",
495+
"user": localpart,
496+
},
497+
"password": password,
498+
"type": "m.login.password",
499+
"refresh_token": true,
500+
}
501+
res := c.MustDoFunc(t, "POST", []string{"_matrix", "client", "v3", "login"}, WithJSONBody(t, reqBody))
502+
503+
body, err := ioutil.ReadAll(res.Body)
504+
if err != nil {
505+
t.Fatalf("unable to read response body: %v", err)
506+
}
507+
508+
userID = gjson.GetBytes(body, "user_id").Str
509+
accessToken = gjson.GetBytes(body, "access_token").Str
510+
deviceID = gjson.GetBytes(body, "device_id").Str
511+
refreshToken = gjson.GetBytes(body, "refresh_token").Str
512+
expiresInMs = gjson.GetBytes(body, "expires_in_ms").Int()
513+
return userID, accessToken, refreshToken, deviceID, expiresInMs
514+
}
515+
516+
// RefreshToken will consume a refresh token and return a new access token and refresh token.
517+
func (c *CSAPI) ConsumeRefreshToken(t *testing.T, refreshToken string) (newAccessToken, newRefreshToken string, expiresInMs int64) {
518+
t.Helper()
519+
reqBody := map[string]interface{}{
520+
"refresh_token": refreshToken,
521+
}
522+
res := c.MustDoFunc(t, "POST", []string{"_matrix", "client", "v3", "refresh"}, WithJSONBody(t, reqBody))
523+
524+
body, err := ioutil.ReadAll(res.Body)
525+
if err != nil {
526+
t.Fatalf("unable to read response body: %v", err)
527+
}
528+
529+
newAccessToken = gjson.GetBytes(body, "access_token").Str
530+
newRefreshToken = gjson.GetBytes(body, "refresh_token").Str
531+
expiresInMs = gjson.GetBytes(body, "expires_in_ms").Int()
532+
return newAccessToken, newRefreshToken, expiresInMs
533+
}
534+
462535
// RegisterUser will register the user with given parameters and
463536
// return user ID, access token and device ID. It fails the test on network error.
464537
func (c *CSAPI) RegisterUser(t *testing.T, localpart, password string) (userID, accessToken, deviceID string) {

0 commit comments

Comments
 (0)