-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge #759: User context autorization tests
6b055e9 test: duplicated test removed (Mario) b691eca test: authorization tests for admin users (Mario) ae276ed test: registered users authorization tests (Mario) ab2164e test: authorization tests for guest users (Mario) Pull request description: Part of #615 ACKs for top commit: josecelano: ACK 6b055e9 Tree-SHA512: 5a4d9af5c0d434e6c9818ddf060e2f5cec9be6977f607f470a64c8b93f876c275333a3bd7c10e7653f11e63aebe69b31abe79784cc5a117de24de095c7b08d4a
- Loading branch information
Showing
1 changed file
with
194 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -186,7 +186,7 @@ mod banned_user_list { | |
use crate::common::contexts::user::asserts::assert_banned_user_response; | ||
use crate::common::contexts::user::forms::Username; | ||
use crate::e2e::environment::TestEnv; | ||
use crate::e2e::web::api::v1::contexts::user::steps::{new_logged_in_admin, new_logged_in_user, new_registered_user}; | ||
use crate::e2e::web::api::v1::contexts::user::steps::{new_logged_in_admin, new_registered_user}; | ||
|
||
#[tokio::test] | ||
async fn it_should_allow_an_admin_to_ban_a_user() { | ||
|
@@ -203,34 +203,210 @@ mod banned_user_list { | |
|
||
assert_banned_user_response(&response, ®istered_user); | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn it_should_not_allow_a_non_admin_to_ban_a_user() { | ||
let mut env = TestEnv::new(); | ||
env.start(api::Version::V1).await; | ||
mod authorization { | ||
mod for_guest_users { | ||
use torrust_index::web::api; | ||
|
||
let logged_non_admin = new_logged_in_user(&env).await; | ||
use crate::common::client::Client; | ||
use crate::common::contexts::user::fixtures::{random_user_registration_form, DEFAULT_PASSWORD, VALID_PASSWORD}; | ||
use crate::common::contexts::user::forms::{ChangePasswordForm, Username}; | ||
use crate::e2e::environment::TestEnv; | ||
use crate::e2e::web::api::v1::contexts::user::steps::{new_logged_in_user, new_registered_user}; | ||
|
||
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_non_admin.token); | ||
#[tokio::test] | ||
async fn it_should_allow_a_guest_user_to_register() { | ||
let mut env = TestEnv::new(); | ||
env.start(api::Version::V1).await; | ||
|
||
let registered_user = new_registered_user(&env).await; | ||
let client = Client::unauthenticated(&env.server_socket_addr().unwrap()); | ||
|
||
let response = client.ban_user(Username::new(registered_user.username.clone())).await; | ||
let form = random_user_registration_form(); | ||
|
||
assert_eq!(response.status, 403); | ||
let response = client.register_user(form).await; | ||
|
||
assert_eq!(response.status, 200); | ||
} | ||
|
||
#[tokio::test] | ||
async fn it_should_not_allow_guest_users_to_change_passwords() { | ||
let mut env = TestEnv::new(); | ||
env.start(api::Version::V1).await; | ||
|
||
let logged_in_user = new_logged_in_user(&env).await; | ||
|
||
let client = Client::unauthenticated(&env.server_socket_addr().unwrap()); | ||
|
||
let new_password = VALID_PASSWORD.to_string(); | ||
|
||
let response = client | ||
.change_password( | ||
Username::new(logged_in_user.username.clone()), | ||
ChangePasswordForm { | ||
current_password: DEFAULT_PASSWORD.to_string(), | ||
password: new_password.clone(), | ||
confirm_password: new_password.clone(), | ||
}, | ||
) | ||
.await; | ||
|
||
assert_eq!(response.status, 401); | ||
} | ||
#[tokio::test] | ||
async fn it_should_not_allow_a_guest_to_ban_a_user() { | ||
let mut env = TestEnv::new(); | ||
env.start(api::Version::V1).await; | ||
|
||
let client = Client::unauthenticated(&env.server_socket_addr().unwrap()); | ||
|
||
let registered_user = new_registered_user(&env).await; | ||
|
||
let response = client.ban_user(Username::new(registered_user.username.clone())).await; | ||
|
||
assert_eq!(response.status, 401); | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn it_should_not_allow_a_guest_to_ban_a_user() { | ||
let mut env = TestEnv::new(); | ||
env.start(api::Version::V1).await; | ||
mod for_registered_users { | ||
use torrust_index::web::api; | ||
|
||
let client = Client::unauthenticated(&env.server_socket_addr().unwrap()); | ||
use crate::common::client::Client; | ||
use crate::common::contexts::user::fixtures::{DEFAULT_PASSWORD, VALID_PASSWORD}; | ||
use crate::common::contexts::user::forms::{ChangePasswordForm, RegistrationForm, Username}; | ||
use crate::e2e::environment::TestEnv; | ||
use crate::e2e::web::api::v1::contexts::user::steps::{new_logged_in_user, new_registered_user}; | ||
|
||
let registered_user = new_registered_user(&env).await; | ||
#[tokio::test] | ||
async fn it_should_not_allow_a_registered_user_to_register() { | ||
let mut env = TestEnv::new(); | ||
env.start(api::Version::V1).await; | ||
|
||
let response = client.ban_user(Username::new(registered_user.username.clone())).await; | ||
let logged_in_user = new_logged_in_user(&env).await; | ||
|
||
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_user.token); | ||
|
||
let response = client | ||
.register_user(RegistrationForm { | ||
username: logged_in_user.username, | ||
email: Some("[email protected]".to_string()), | ||
password: VALID_PASSWORD.to_string(), | ||
confirm_password: VALID_PASSWORD.to_string(), | ||
}) | ||
.await; | ||
|
||
assert_eq!(response.status, 400); | ||
} | ||
|
||
#[tokio::test] | ||
async fn it_should_allow_registered_users_to_change_their_passwords() { | ||
let mut env = TestEnv::new(); | ||
env.start(api::Version::V1).await; | ||
|
||
let logged_in_user = new_logged_in_user(&env).await; | ||
|
||
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_user.token); | ||
|
||
let new_password = VALID_PASSWORD.to_string(); | ||
|
||
let response = client | ||
.change_password( | ||
Username::new(logged_in_user.username.clone()), | ||
ChangePasswordForm { | ||
current_password: DEFAULT_PASSWORD.to_string(), | ||
password: new_password.clone(), | ||
confirm_password: new_password.clone(), | ||
}, | ||
) | ||
.await; | ||
|
||
assert_eq!(response.status, 200); | ||
} | ||
#[tokio::test] | ||
async fn it_should_not_allow_a_registered_user_to_ban_a_user() { | ||
let mut env = TestEnv::new(); | ||
env.start(api::Version::V1).await; | ||
|
||
let logged_in_user = new_logged_in_user(&env).await; | ||
|
||
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_user.token); | ||
|
||
let registered_user = new_registered_user(&env).await; | ||
|
||
let response = client.ban_user(Username::new(registered_user.username.clone())).await; | ||
|
||
assert_eq!(response.status, 403); | ||
} | ||
} | ||
mod for_admin_users { | ||
use torrust_index::web::api; | ||
|
||
use crate::common::client::Client; | ||
use crate::common::contexts::user::fixtures::{DEFAULT_PASSWORD, VALID_PASSWORD}; | ||
use crate::common::contexts::user::forms::{ChangePasswordForm, RegistrationForm, Username}; | ||
use crate::e2e::environment::TestEnv; | ||
use crate::e2e::web::api::v1::contexts::user::steps::{new_logged_in_admin, new_registered_user}; | ||
|
||
#[tokio::test] | ||
async fn it_should_not_allow_an_admin_user_to_register() { | ||
let mut env = TestEnv::new(); | ||
env.start(api::Version::V1).await; | ||
|
||
let logged_in_admin = new_logged_in_admin(&env).await; | ||
|
||
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_admin.token); | ||
|
||
let response = client | ||
.register_user(RegistrationForm { | ||
username: logged_in_admin.username, | ||
email: Some("[email protected]".to_string()), | ||
password: VALID_PASSWORD.to_string(), | ||
confirm_password: VALID_PASSWORD.to_string(), | ||
}) | ||
.await; | ||
|
||
assert_eq!(response.status, 400); | ||
} | ||
|
||
#[tokio::test] | ||
async fn it_should_allow_admin_users_to_change_their_passwords() { | ||
let mut env = TestEnv::new(); | ||
env.start(api::Version::V1).await; | ||
|
||
let logged_in_admin = new_logged_in_admin(&env).await; | ||
|
||
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_admin.token); | ||
|
||
let new_password = VALID_PASSWORD.to_string(); | ||
|
||
let response = client | ||
.change_password( | ||
Username::new(logged_in_admin.username.clone()), | ||
ChangePasswordForm { | ||
current_password: DEFAULT_PASSWORD.to_string(), | ||
password: new_password.clone(), | ||
confirm_password: new_password.clone(), | ||
}, | ||
) | ||
.await; | ||
|
||
assert_eq!(response.status, 200); | ||
} | ||
|
||
#[tokio::test] | ||
async fn it_should_allow_an_admin_to_ban_a_user() { | ||
let mut env = TestEnv::new(); | ||
env.start(api::Version::V1).await; | ||
|
||
let logged_in_admin = new_logged_in_admin(&env).await; | ||
|
||
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_admin.token); | ||
|
||
let registered_user = new_registered_user(&env).await; | ||
|
||
let response = client.ban_user(Username::new(registered_user.username.clone())).await; | ||
|
||
assert_eq!(response.status, 401); | ||
assert_eq!(response.status, 200); | ||
} | ||
} | ||
} |