Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 29b3dfe

Browse files
committedNov 4, 2024··
test: authorization tests for admin users
1 parent ae276ed commit 29b3dfe

File tree

1 file changed

+72
-1
lines changed

1 file changed

+72
-1
lines changed
 

‎tests/e2e/web/api/v1/contexts/user/contract.rs

+72-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ mod authorization {
339339
assert_eq!(response.status, 200);
340340
}
341341
#[tokio::test]
342-
async fn it_should_not_allow_a_registered_to_ban_a_user() {
342+
async fn it_should_not_allow_a_registered_user_to_ban_a_user() {
343343
let mut env = TestEnv::new();
344344
env.start(api::Version::V1).await;
345345

@@ -354,4 +354,75 @@ mod authorization {
354354
assert_eq!(response.status, 403);
355355
}
356356
}
357+
mod for_admin_users {
358+
use torrust_index::web::api;
359+
360+
use crate::common::client::Client;
361+
use crate::common::contexts::user::fixtures::{DEFAULT_PASSWORD, VALID_PASSWORD};
362+
use crate::common::contexts::user::forms::{ChangePasswordForm, RegistrationForm, Username};
363+
use crate::e2e::environment::TestEnv;
364+
use crate::e2e::web::api::v1::contexts::user::steps::{new_logged_in_admin, new_registered_user};
365+
366+
#[tokio::test]
367+
async fn it_should_not_allow_an_admin_user_to_register() {
368+
let mut env = TestEnv::new();
369+
env.start(api::Version::V1).await;
370+
371+
let logged_in_admin = new_logged_in_admin(&env).await;
372+
373+
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_admin.token);
374+
375+
let response = client
376+
.register_user(RegistrationForm {
377+
username: logged_in_admin.username,
378+
email: Some("test@email.com".to_string()),
379+
password: VALID_PASSWORD.to_string(),
380+
confirm_password: VALID_PASSWORD.to_string(),
381+
})
382+
.await;
383+
384+
assert_eq!(response.status, 400);
385+
}
386+
387+
#[tokio::test]
388+
async fn it_should_allow_admin_users_to_change_their_passwords() {
389+
let mut env = TestEnv::new();
390+
env.start(api::Version::V1).await;
391+
392+
let logged_in_admin = new_logged_in_admin(&env).await;
393+
394+
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_admin.token);
395+
396+
let new_password = VALID_PASSWORD.to_string();
397+
398+
let response = client
399+
.change_password(
400+
Username::new(logged_in_admin.username.clone()),
401+
ChangePasswordForm {
402+
current_password: DEFAULT_PASSWORD.to_string(),
403+
password: new_password.clone(),
404+
confirm_password: new_password.clone(),
405+
},
406+
)
407+
.await;
408+
409+
assert_eq!(response.status, 200);
410+
}
411+
412+
#[tokio::test]
413+
async fn it_should_allow_an_admin_to_ban_a_user() {
414+
let mut env = TestEnv::new();
415+
env.start(api::Version::V1).await;
416+
417+
let logged_in_admin = new_logged_in_admin(&env).await;
418+
419+
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_admin.token);
420+
421+
let registered_user = new_registered_user(&env).await;
422+
423+
let response = client.ban_user(Username::new(registered_user.username.clone())).await;
424+
425+
assert_eq!(response.status, 200);
426+
}
427+
}
357428
}

0 commit comments

Comments
 (0)
Please sign in to comment.