Skip to content

Commit

Permalink
editoast: add configurable authorization to TestAppBuilder
Browse files Browse the repository at this point in the history
Signed-off-by: hamz2a <[email protected]>
  • Loading branch information
hamz2a committed Dec 26, 2024
1 parent 6a105bf commit 1bf9525
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
23 changes: 23 additions & 0 deletions editoast/src/views/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ pub mod tests {
use serde_json::json;

use super::*;
use crate::core::mocking::MockingClient;
use crate::core::CoreClient;
use crate::models::fixtures::create_project;
use crate::models::prelude::*;
use crate::views::test_app::TestAppBuilder;
Expand Down Expand Up @@ -394,6 +396,27 @@ pub mod tests {
assert_eq!(project.name, project_name);
}

#[rstest]
async fn project_post_should_fail_when_authorization_is_enabled() {
let pool = DbConnectionPoolV2::for_tests_no_transaction();
let app = TestAppBuilder::new()
.db_pool(pool)
.core_client(CoreClient::Mocked(MockingClient::default()))
.disable_authorization(false)
.build();

let project_name = "test_project_failed";

let request = app.post("/projects").json(&json!({
"name": project_name,
"description": "",
"objectives": "",
"funders": "",
}));

app.fetch(request).assert_status(StatusCode::FORBIDDEN);
}

#[rstest]
async fn project_list() {
let app = TestAppBuilder::default_app();
Expand Down
11 changes: 9 additions & 2 deletions editoast/src/views/test_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub(crate) struct TestAppBuilder {
db_pool: Option<DbConnectionPoolV2>,
core_client: Option<CoreClient>,
osrdyne_client: Option<OsrdyneClient>,
disable_authorization: bool,
}

impl TestAppBuilder {
Expand All @@ -47,6 +48,7 @@ impl TestAppBuilder {
db_pool: None,
core_client: None,
osrdyne_client: None,
disable_authorization: true,
}
}

Expand All @@ -68,6 +70,11 @@ impl TestAppBuilder {
self
}

pub fn disable_authorization(mut self, disable_authorization: bool) -> Self {
self.disable_authorization = disable_authorization;
self
}

pub fn default_app() -> TestApp {
let pool = DbConnectionPoolV2::for_tests();
let core_client = CoreClient::Mocked(MockingClient::default());
Expand All @@ -83,7 +90,7 @@ impl TestAppBuilder {
port: 0,
address: String::default(),
health_check_timeout: chrono::Duration::milliseconds(500),
disable_authorization: true,
disable_authorization: self.disable_authorization,
map_layers_max_zoom: 18,
postgres_config: PostgresConfig {
database_url: Url::parse("postgres://osrd:password@localhost:5432/osrd").unwrap(),
Expand Down Expand Up @@ -152,7 +159,7 @@ impl TestAppBuilder {
infra_caches,
map_layers: Arc::new(MapLayers::default()),
speed_limit_tag_ids,
disable_authorization: true,
disable_authorization: self.disable_authorization,
health_check_timeout: config.health_check_timeout,
config: Arc::new(config),
};
Expand Down

0 comments on commit 1bf9525

Please sign in to comment.