Skip to content

Commit ab4304a

Browse files
authored
feat: session related change-password tests (#126)
* feat: session related change-password tests Signed-off-by: Meenal Trivedi <[email protected]> * add optinally keep tests Signed-off-by: Meenal Trivedi <[email protected]> * fix Signed-off-by: Meenal Trivedi <[email protected]> * requested changes Signed-off-by: Meenal Trivedi <[email protected]> * update sytesT_coverage Signed-off-by: Meenal Trivedi <[email protected]>
1 parent 1babb4a commit ab4304a

File tree

2 files changed

+99
-10
lines changed

2 files changed

+99
-10
lines changed

README.md

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,26 @@ $ go run sytest_coverage.go -v
132132
10apidoc/01request-encoding 1/1 tests
133133
✓ POST rejects invalid utf-8 in JSON
134134
135-
10apidoc/02login 3/6 tests
135+
10apidoc/02login 6/6 tests
136136
✓ GET /login yields a set of flows
137137
✓ POST /login can log in as a user
138138
✓ POST /login returns the same device_id as that in the request
139-
× POST /login can log in as a user with just the local part of the id
140-
× POST /login as non-existing user is rejected
141-
× POST /login wrong password is rejected
139+
POST /login can log in as a user with just the local part of the id
140+
POST /login as non-existing user is rejected
141+
POST /login wrong password is rejected
142142
143143
10apidoc/03events-initial 0/2 tests
144-
10apidoc/04version 0/1 tests
144+
10apidoc/04version 1/1 tests
145+
✓ Version responds 200 OK with valid structure
146+
145147
10apidoc/10profile-displayname 0/2 tests
146148
10apidoc/11profile-avatar_url 0/2 tests
147149
10apidoc/12device_management 0/8 tests
148150
10apidoc/13ui-auth 0/4 tests
149-
10apidoc/20presence 0/2 tests
151+
10apidoc/20presence 2/2 tests
152+
✓ GET /presence/:user_id/status fetches initial status
153+
✓ PUT /presence/:user_id/status updates my presence
154+
150155
10apidoc/30room-create 0/10 tests
151156
10apidoc/31room-state 0/14 tests
152157
10apidoc/32room-alias 0/2 tests
@@ -162,8 +167,21 @@ $ go run sytest_coverage.go -v
162167
12login/01threepid-and-password 0/1 tests
163168
12login/02cas 0/3 tests
164169
13logout 0/4 tests
165-
14account/01change-password 0/7 tests
166-
14account/02deactivate 0/4 tests
170+
14account/01change-password 5/7 tests
171+
✓ After changing password, can't log in with old password
172+
✓ After changing password, can log in with new password
173+
✓ After changing password, existing session still works
174+
✓ After changing password, a different session no longer works by default
175+
✓ After changing password, different sessions can optionally be kept
176+
× Pushers created with a different access token are deleted on password change
177+
× Pushers created with a the same access token are not deleted on password change
178+
179+
14account/02deactivate 3/4 tests
180+
✓ Can deactivate account
181+
✓ Can't deactivate account with wrong password
182+
✓ After deactivating account, can't log in with password
183+
× After deactivating account, can't log in with an email
184+
167185
21presence-events 0/3 tests
168186
30rooms/01state 2/9 tests
169187
✓ Room creation reports m.room.create to myself
@@ -202,7 +220,10 @@ $ go run sytest_coverage.go -v
202220
30rooms/52members 0/3 tests
203221
30rooms/60version_upgrade 0/19 tests
204222
30rooms/70publicroomslist 0/5 tests
205-
31sync/01filter 0/2 tests
223+
31sync/01filter 2/2 tests
224+
✓ Can create filter
225+
✓ Can download filter
226+
206227
31sync/02sync 0/1 tests
207228
31sync/03joined 0/6 tests
208229
31sync/04timeline 0/8 tests
@@ -329,5 +350,5 @@ $ go run sytest_coverage.go -v
329350
90jira/SYN-516 0/1 tests
330351
90jira/SYN-627 0/1 tests
331352
332-
TOTAL: 15/690 tests converted
353+
TOTAL: 31/690 tests converted
333354
```

tests/account_change_password_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package tests
22

33
import (
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

1216
func 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

Comments
 (0)