Skip to content

Commit c26972f

Browse files
authored
tests: add all login tests (#102)
Signed-off-by: Meenal Trivedi <[email protected]>
1 parent 785f8a6 commit c26972f

File tree

1 file changed

+67
-2
lines changed

1 file changed

+67
-2
lines changed

tests/apidoc_login_test.go

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func TestLogin(t *testing.T) {
4747
"type": "m.login.password",
4848
"identifier": {
4949
"type": "m.id.user",
50-
"user": "login_test_user"
50+
"user": "@login_test_user:hs1"
5151
},
5252
"password": "superuser"
5353
}`))
@@ -68,7 +68,7 @@ func TestLogin(t *testing.T) {
6868
"type": "m.login.password",
6969
"identifier": {
7070
"type": "m.id.user",
71-
"user": "device_id_test_user"
71+
"user": "@device_id_test_user:hs1"
7272
},
7373
"password": "superuser",
7474
"device_id": "`+deviceID+`"
@@ -81,6 +81,71 @@ func TestLogin(t *testing.T) {
8181
},
8282
})
8383
})
84+
// sytest: POST /login can log in as a user with just the local part of the id
85+
t.Run("POST /login can log in as a user with just the local part of the id", func(t *testing.T) {
86+
t.Parallel()
87+
88+
createDummyUser(t, unauthedClient, "local-login-user")
89+
90+
res := unauthedClient.MustDo(t, "POST", []string{"_matrix", "client", "r0", "login"}, json.RawMessage(`{
91+
"type": "m.login.password",
92+
"identifier": {
93+
"type": "m.id.user",
94+
"user": "local-login-user"
95+
},
96+
"password": "superuser"
97+
}`))
98+
99+
must.MatchResponse(t, res, match.HTTPResponse{
100+
JSON: []match.JSON{
101+
match.JSONKeyTypeEqual("access_token", gjson.String),
102+
match.JSONKeyEqual("home_server", "hs1"),
103+
},
104+
})
105+
})
106+
// sytest: POST /login as non-existing user is rejected
107+
t.Run("POST /login as non-existing user is rejected", func(t *testing.T) {
108+
t.Parallel()
109+
res, err := unauthedClient.Do(t, "POST", []string{"_matrix", "client", "r0", "login"}, json.RawMessage(`{
110+
"type": "m.login.password",
111+
"identifier": {
112+
"type": "m.id.user",
113+
"user": "i-dont-exist"
114+
},
115+
"password": "superuser"
116+
}`), nil)
117+
if err != nil {
118+
t.Fatalf("unable to make request to /login: %v", err)
119+
}
120+
121+
must.MatchResponse(t, res, match.HTTPResponse{
122+
StatusCode: 403,
123+
})
124+
})
125+
// sytest: POST /login wrong password is rejected
126+
t.Run("POST /login wrong password is rejected", func(t *testing.T) {
127+
t.Parallel()
128+
createDummyUser(t, unauthedClient, "login_wrong_password")
129+
res, err := unauthedClient.Do(t, "POST", []string{"_matrix", "client", "r0", "login"}, json.RawMessage(`{
130+
"type": "m.login.password",
131+
"identifier": {
132+
"type": "m.id.user",
133+
"user": "login_wrong_password"
134+
},
135+
"password": "wrong_password"
136+
}`), nil)
137+
138+
if err != nil {
139+
t.Fatalf("unable to make request to /login: %v", err)
140+
}
141+
142+
must.MatchResponse(t, res, match.HTTPResponse{
143+
StatusCode: 403,
144+
JSON: []match.JSON{
145+
match.JSONKeyEqual("errcode", "M_FORBIDDEN"),
146+
},
147+
})
148+
})
84149
})
85150
}
86151

0 commit comments

Comments
 (0)