Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

615 - Tests for authorization logic #718

Closed
wants to merge 11 commits into from
6 changes: 6 additions & 0 deletions tests/common/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ impl Client {
pub async fn ban_user(&self, username: Username) -> TextResponse {
self.http_client.delete(&format!("/user/ban/{}", &username.value)).await
}

// Context: proxy

/* pub async fn get_image_by_url(&self, url: &str) -> TextResponse {
self.http_client.get(&format!("/proxy/image/{url}"), Query::empty()).await
} */
}

/// Generic HTTP Client
Expand Down
13 changes: 13 additions & 0 deletions tests/e2e/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ impl TestEnv {
}
}

#[allow(dead_code)]
/// Some test requires a real tracker running in `public` mode.
pub fn provides_a_public_tracker(&self) -> bool {
if !self.is_shared() {
return false;
};

match self.server_settings() {
Some(settings) => !settings.tracker.private,
None => false,
}
}

/// Returns the server starting settings if the servers was already started.
/// We do not know the settings until we start the server.
pub fn server_settings(&self) -> Option<Settings> {
Expand Down
106 changes: 106 additions & 0 deletions tests/e2e/web/api/v1/contexts/about/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,109 @@ async fn it_should_load_the_license_page_at_the_api_entrypoint() {
assert_text_ok(&response);
assert_response_title(&response, "Licensing");
}

mod for_guest_users {
use torrust_index::web::api;

use crate::common::asserts::assert_text_ok;
use crate::common::client::Client;
use crate::e2e::environment::TestEnv;

#[tokio::test]
async fn it_should_allow_guest_users_to_see_the_about_page() {
let mut env = TestEnv::new();

env.start(api::Version::V1).await;

let client = Client::unauthenticated(&env.server_socket_addr().unwrap());

let response = client.about().await;
assert_text_ok(&response);
}

#[tokio::test]
async fn it_should_allow_guest_users_to_see_the_license_page() {
let mut env = TestEnv::new();

env.start(api::Version::V1).await;

let client = Client::unauthenticated(&env.server_socket_addr().unwrap());

let response = client.license().await;
assert_text_ok(&response);
}
}

mod for_authenticated_users {
use torrust_index::web::api;

use crate::common::asserts::assert_text_ok;
use crate::common::client::Client;
use crate::e2e::environment::TestEnv;
use crate::e2e::web::api::v1::contexts::user::steps::new_logged_in_user;

#[tokio::test]
async fn it_should_allow_authenticated_users_to_see_the_about_page() {
let mut env = TestEnv::new();

env.start(api::Version::V1).await;

let authenticated_user = new_logged_in_user(&env).await;

let client = Client::authenticated(&env.server_socket_addr().unwrap(), &authenticated_user.token);

let response = client.about().await;
assert_text_ok(&response);
}

#[tokio::test]
async fn it_should_allow_authenticated_users_to_see_the_license_page() {
let mut env = TestEnv::new();

env.start(api::Version::V1).await;

let authenticated_user = new_logged_in_user(&env).await;

let client = Client::authenticated(&env.server_socket_addr().unwrap(), &authenticated_user.token);

let response = client.license().await;
assert_text_ok(&response);
}
}

mod for_admin_users {
use torrust_index::web::api;

use crate::common::asserts::assert_text_ok;
use crate::common::client::Client;
use crate::e2e::environment::TestEnv;
use crate::e2e::web::api::v1::contexts::user::steps::new_logged_in_admin;

#[tokio::test]
async fn it_should_allow_admin_users_to_see_the_about_page() {
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.about().await;
assert_text_ok(&response);
}

#[tokio::test]
async fn it_should_allow_admin_users_to_see_the_license_page() {
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.license().await;
assert_text_ok(&response);
}
}
Loading
Loading