11package tests
22
33import (
4+ "io/ioutil"
45 "testing"
56
67 "github.com/matrix-org/complement/internal/b"
78 "github.com/matrix-org/complement/internal/client"
9+ "github.com/matrix-org/complement/internal/docker"
810 "github.com/matrix-org/complement/internal/match"
911 "github.com/matrix-org/complement/internal/must"
12+
13+ "github.com/tidwall/gjson"
1014)
1115
1216func TestChangePassword (t * testing.T ) {
@@ -16,6 +20,7 @@ func TestChangePassword(t *testing.T) {
1620 newPassword := "my_new_password"
1721 passwordClient := deployment .RegisterUser (t , "hs1" , "test_change_password_user" , oldPassword )
1822 unauthedClient := deployment .Client (t , "hs1" , "" )
23+ sessionTest := createSession (t , deployment , "test_change_password_user" , "superuser" )
1924 // sytest: After changing password, can't log in with old password
2025 t .Run ("After changing password, can't log in with old password" , func (t * testing.T ) {
2126
@@ -49,6 +54,48 @@ func TestChangePassword(t *testing.T) {
4954 "password" : newPassword ,
5055 })
5156 res := unauthedClient .DoFunc (t , "POST" , []string {"_matrix" , "client" , "r0" , "login" }, reqBody )
57+ must .MatchResponse (t , res , match.HTTPResponse {
58+ StatusCode : 200 ,
59+ JSON : []match.JSON {
60+ match .JSONKeyEqual ("user_id" , passwordClient .UserID ),
61+ },
62+ })
63+ })
64+ // sytest: After changing password, existing session still works
65+ t .Run ("After changing password, existing session still works" , func (t * testing.T ) {
66+ res := passwordClient .DoFunc (t , "GET" , []string {"_matrix" , "client" , "r0" , "account" , "whoami" })
67+ must .MatchResponse (t , res , match.HTTPResponse {
68+ StatusCode : 200 ,
69+ })
70+ })
71+ // sytest: After changing password, a different session no longer works by default
72+ t .Run ("After changing password, a different session no longer works by default" , func (t * testing.T ) {
73+ res := sessionTest .DoFunc (t , "GET" , []string {"_matrix" , "client" , "r0" , "account" , "whoami" })
74+ must .MatchResponse (t , res , match.HTTPResponse {
75+ StatusCode : 401 ,
76+ })
77+ })
78+
79+ // sytest: After changing password, different sessions can optionally be kept
80+ t .Run ("After changing password, different sessions can optionally be kept" , func (t * testing.T ) {
81+ sessionOptional := createSession (t , deployment , "test_change_password_user" , newPassword )
82+ reqBody := client .WithJSONBody (t , map [string ]interface {}{
83+ "auth" : map [string ]interface {}{
84+ "type" : "m.login.password" ,
85+ "user" : passwordClient .UserID ,
86+ "password" : newPassword ,
87+ },
88+ "new_password" : "new_optional_password" ,
89+ "logout_devices" : false ,
90+ })
91+
92+ res := passwordClient .DoFunc (t , "POST" , []string {"_matrix" , "client" , "r0" , "account" , "password" }, reqBody )
93+
94+ must .MatchResponse (t , res , match.HTTPResponse {
95+ StatusCode : 200 ,
96+ })
97+ res = sessionOptional .DoFunc (t , "GET" , []string {"_matrix" , "client" , "r0" , "account" , "whoami" })
98+
5299 must .MatchResponse (t , res , match.HTTPResponse {
53100 StatusCode : 200 ,
54101 })
@@ -72,3 +119,24 @@ func changePassword(t *testing.T, passwordClient *client.CSAPI, oldPassword stri
72119 StatusCode : 200 ,
73120 })
74121}
122+
123+ func createSession (t * testing.T , deployment * docker.Deployment , userID , password string ) * client.CSAPI {
124+ authedClient := deployment .Client (t , "hs1" , "" )
125+ reqBody := client .WithJSONBody (t , map [string ]interface {}{
126+ "identifier" : map [string ]interface {}{
127+ "type" : "m.id.user" ,
128+ "user" : userID ,
129+ },
130+ "type" : "m.login.password" ,
131+ "password" : password ,
132+ })
133+ res := authedClient .DoFunc (t , "POST" , []string {"_matrix" , "client" , "r0" , "login" }, reqBody )
134+ body , err := ioutil .ReadAll (res .Body )
135+ if err != nil {
136+ t .Fatalf ("unable to read response body: %v" , err )
137+ }
138+
139+ authedClient .UserID = gjson .GetBytes (body , "user_id" ).Str
140+ authedClient .AccessToken = gjson .GetBytes (body , "access_token" ).Str
141+ return authedClient
142+ }
0 commit comments