Skip to content

Commit 82e3ea6

Browse files
committed
Add test coverage
Also update snapshots for token values affected by state changes
1 parent 8e19d98 commit 82e3ea6

File tree

1 file changed

+76
-15
lines changed

1 file changed

+76
-15
lines changed

crates/handlers/src/compat/login.rs

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,12 @@ mod tests {
800800
"###);
801801
}
802802

803-
async fn user_with_password(state: &TestState, username: &str, password: &str) {
803+
async fn user_with_password(
804+
state: &TestState,
805+
username: &str,
806+
password: &str,
807+
locked: bool,
808+
) -> User {
804809
let mut rng = state.rng();
805810
let mut repo = state.repository().await.unwrap();
806811

@@ -826,7 +831,14 @@ mod tests {
826831
.await
827832
.unwrap();
828833

834+
let user = if locked {
835+
repo.user().lock(&state.clock, user).await.unwrap()
836+
} else {
837+
user
838+
};
839+
829840
repo.save().await.unwrap();
841+
return user;
830842
}
831843

832844
/// Test that a user can login with a password using the Matrix
@@ -836,7 +848,7 @@ mod tests {
836848
setup();
837849
let state = TestState::from_pool(pool).await.unwrap();
838850

839-
user_with_password(&state, "alice", "password").await;
851+
let user = user_with_password(&state, "alice", "password", true).await;
840852

841853
// Now let's try to login with the password, without asking for a refresh token.
842854
let request = Request::post("/_matrix/client/v3/login").json(serde_json::json!({
@@ -848,14 +860,30 @@ mod tests {
848860
"password": "password",
849861
}));
850862

863+
// First try to login to a locked account
864+
let response = state.request(request.clone()).await;
865+
response.assert_status(StatusCode::UNAUTHORIZED);
866+
let body: serde_json::Value = response.json();
867+
insta::assert_json_snapshot!(body, @r###"
868+
{
869+
"errcode": "M_USER_LOCKED",
870+
"error": "User account has been locked"
871+
}
872+
"###);
873+
874+
// Now try again after unlocking the account
875+
let mut repo = state.repository().await.unwrap();
876+
let _ = repo.user().unlock(user).await.unwrap();
877+
repo.save().await.unwrap();
878+
851879
let response = state.request(request).await;
852880
response.assert_status(StatusCode::OK);
853881

854882
let body: serde_json::Value = response.json();
855883
insta::assert_json_snapshot!(body, @r###"
856884
{
857-
"access_token": "mct_16tugBE5Ta9LIWoSJaAEHHq2g3fx8S_alcBB4",
858-
"device_id": "ZGpSvYQqlq",
885+
"access_token": "mct_cxG6gZXyvelQWW9XqfNbm5KAQovodf_XvJz43",
886+
"device_id": "42oTpLoieH",
859887
"user_id": "@alice:example.com"
860888
}
861889
"###);
@@ -877,10 +905,10 @@ mod tests {
877905
let body: serde_json::Value = response.json();
878906
insta::assert_json_snapshot!(body, @r###"
879907
{
880-
"access_token": "mct_cxG6gZXyvelQWW9XqfNbm5KAQovodf_XvJz43",
881-
"device_id": "42oTpLoieH",
908+
"access_token": "mct_PGMLvvMXC4Ds1A3lCWc6Hx4l9DGzqG_lVEIV2",
909+
"device_id": "Yp7FM44zJN",
882910
"user_id": "@alice:example.com",
883-
"refresh_token": "mcr_7IvDc44woP66fRQoS9MVcHXO9OeBmR_0jDGr1",
911+
"refresh_token": "mcr_LoYqtrtBUBcWlE4RX6o47chBCGkadB_9gzpc1",
884912
"expires_in_ms": 300000
885913
}
886914
"###);
@@ -898,8 +926,8 @@ mod tests {
898926
let body: serde_json::Value = response.json();
899927
insta::assert_json_snapshot!(body, @r###"
900928
{
901-
"access_token": "mct_PGMLvvMXC4Ds1A3lCWc6Hx4l9DGzqG_lVEIV2",
902-
"device_id": "Yp7FM44zJN",
929+
"access_token": "mct_Xl3bbpfh9yNy9NzuRxyR3b3PLW0rqd_DiXAH2",
930+
"device_id": "6cq7FqNSYo",
903931
"user_id": "@alice:example.com"
904932
}
905933
"###);
@@ -953,7 +981,7 @@ mod tests {
953981
setup();
954982
let state = TestState::from_pool(pool).await.unwrap();
955983

956-
user_with_password(&state, "alice", "password").await;
984+
user_with_password(&state, "alice", "password", false).await;
957985
// Try without a Content-Type header
958986
let mut request = Request::post("/_matrix/client/v3/login").json(serde_json::json!({
959987
"type": "m.login.password",
@@ -985,7 +1013,7 @@ mod tests {
9851013
setup();
9861014
let state = TestState::from_pool(pool).await.unwrap();
9871015

988-
user_with_password(&state, "alice", "password").await;
1016+
let user = user_with_password(&state, "alice", "password", true).await;
9891017

9901018
// Login with a full MXID as identifier
9911019
let request = Request::post("/_matrix/client/v3/login").json(serde_json::json!({
@@ -997,13 +1025,29 @@ mod tests {
9971025
"password": "password",
9981026
}));
9991027

1028+
// First try to login to a locked account
1029+
let response = state.request(request.clone()).await;
1030+
response.assert_status(StatusCode::UNAUTHORIZED);
1031+
let body: serde_json::Value = response.json();
1032+
insta::assert_json_snapshot!(body, @r###"
1033+
{
1034+
"errcode": "M_USER_LOCKED",
1035+
"error": "User account has been locked"
1036+
}
1037+
"###);
1038+
1039+
// Now try again after unlocking the account
1040+
let mut repo = state.repository().await.unwrap();
1041+
let _ = repo.user().unlock(user).await.unwrap();
1042+
repo.save().await.unwrap();
1043+
10001044
let response = state.request(request).await;
10011045
response.assert_status(StatusCode::OK);
10021046
let body: serde_json::Value = response.json();
10031047
insta::assert_json_snapshot!(body, @r###"
10041048
{
1005-
"access_token": "mct_16tugBE5Ta9LIWoSJaAEHHq2g3fx8S_alcBB4",
1006-
"device_id": "ZGpSvYQqlq",
1049+
"access_token": "mct_cxG6gZXyvelQWW9XqfNbm5KAQovodf_XvJz43",
1050+
"device_id": "42oTpLoieH",
10071051
"user_id": "@alice:example.com"
10081052
}
10091053
"###);
@@ -1147,6 +1191,8 @@ mod tests {
11471191
.add(&mut state.rng(), &state.clock, "alice".to_owned())
11481192
.await
11491193
.unwrap();
1194+
// Start with a locked account
1195+
let user = repo.user().lock(&state.clock, user).await.unwrap();
11501196
repo.save().await.unwrap();
11511197

11521198
let mxid = state.homeserver_connection.mxid(&user.username);
@@ -1179,14 +1225,29 @@ mod tests {
11791225
"type": "m.login.token",
11801226
"token": token,
11811227
}));
1228+
let response = state.request(request.clone()).await;
1229+
response.assert_status(StatusCode::UNAUTHORIZED);
1230+
let body: serde_json::Value = response.json();
1231+
insta::assert_json_snapshot!(body, @r###"
1232+
{
1233+
"errcode": "M_USER_LOCKED",
1234+
"error": "User account has been locked"
1235+
}
1236+
"###);
1237+
1238+
// Now try again after unlocking the account
1239+
let mut repo = state.repository().await.unwrap();
1240+
let user = repo.user().unlock(user).await.unwrap();
1241+
repo.save().await.unwrap();
1242+
11821243
let response = state.request(request).await;
11831244
response.assert_status(StatusCode::OK);
11841245

11851246
let body: serde_json::Value = response.json();
11861247
insta::assert_json_snapshot!(body, @r#"
11871248
{
1188-
"access_token": "mct_bnkWh1tPmm1MZOpygPaXwygX8PfxEY_hE6do1",
1189-
"device_id": "O3Ju1MUh3Z",
1249+
"access_token": "mct_bUTa4XIh92RARTPTjqQrCZLAkq2ild_0VsYE6",
1250+
"device_id": "uihy4bk51g",
11901251
"user_id": "@alice:example.com"
11911252
}
11921253
"#);

0 commit comments

Comments
 (0)