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

Fix track edition and change http code on unknwon objet id #4305

Merged
merged 2 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion editoast/src/schema/operation/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ impl DeleteOperation {
.execute(conn)
{
Ok(1) => Ok(()),
Ok(_) => Err(OperationError::ObjectNotFound(self.obj_id.clone()).into()),
Ok(_) => Err(OperationError::ObjectNotFound {
obj_id: self.obj_id.clone(),
infra_id,
}
.into()),
Err(err) => Err(err.into()),
}
}
Expand Down
4 changes: 2 additions & 2 deletions editoast/src/schema/operation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ impl Operation {
#[editoast_error(base_id = "operation")]
enum OperationError {
// To modify
#[error("Object '{0}', could not be found")]
#[error("Object '{obj_id}', could not be found in the infrastructure '{infra_id}'")]
#[editoast_error(status = 404)]
ObjectNotFound(String),
ObjectNotFound { obj_id: String, infra_id: i64 },
#[error("Empty string id is forbidden")]
EmptyId,
#[error("Update operation try to modify object id, which is forbidden")]
Expand Down
56 changes: 50 additions & 6 deletions editoast/src/schema/operation/update.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::error::Result;
use crate::schema::operation::RailjsonObject;
use crate::schema::{OSRDIdentified, ObjectType};
use diesel::result::Error as DieselError;
use diesel::sql_types::{BigInt, Json, Jsonb, Text};
use diesel::{sql_query, PgConnection, QueryableByName, RunQueryDsl};
use json_patch::Patch;
Expand All @@ -21,13 +22,24 @@ impl UpdateOperation {
pub fn apply(&self, infra_id: i64, conn: &mut PgConnection) -> Result<RailjsonObject> {
// Load object

let mut obj: DataObject = sql_query(format!(
let mut obj: DataObject = match sql_query(format!(
"SELECT data FROM {} WHERE infra_id = $1 AND obj_id = $2",
self.obj_type.get_table()
))
.bind::<BigInt, _>(infra_id)
.bind::<Text, _>(&self.obj_id)
.get_result(conn)?;
.get_result(conn)
{
Ok(obj) => obj,
Err(DieselError::NotFound) => {
return Err(OperationError::ObjectNotFound {
obj_id: self.obj_id.clone(),
infra_id,
}
.into())
}
Err(err) => return Err(err.into()),
};

// Apply and check patch
let railjson_obj = obj.patch_and_check(self)?;
Expand All @@ -43,7 +55,11 @@ impl UpdateOperation {
.execute(conn)
{
Ok(1) => Ok(railjson_obj),
Ok(_) => Err(OperationError::ObjectNotFound(self.obj_id.clone()).into()),
Ok(_) => Err(OperationError::ObjectNotFound {
obj_id: self.obj_id.clone(),
infra_id,
}
.into()),
Err(err) => Err(err.into()),
}
}
Expand Down Expand Up @@ -90,7 +106,6 @@ mod tests {
use crate::schema::operation::OperationError;
use crate::schema::{OSRDIdentified, ObjectType};
use actix_web::test as actix_test;
use actix_web::ResponseError;
use diesel::sql_query;
use diesel::sql_types::{Double, Text};
use diesel::RunQueryDsl;
Expand Down Expand Up @@ -157,8 +172,8 @@ mod tests {

assert!(res.is_err());
assert_eq!(
res.unwrap_err().status_code(),
OperationError::ModifyId.get_status()
res.unwrap_err().get_type(),
OperationError::ModifyId.get_type()
);
})
.await;
Expand Down Expand Up @@ -250,4 +265,33 @@ mod tests {
assert_eq!(updated_speed.val, 80.0);
}).await;
}

#[actix_test]
async fn wrong_id_update_track() {
test_infra_transaction(|conn, infra| {
let update_track = UpdateOperation {
obj_id: "non_existent_id".to_string(),
obj_type: ObjectType::TrackSection,
railjson_patch: from_str(
r#"[
{ "op": "replace", "path": "/length", "value": 80.0 }
]"#,
)
.unwrap(),
};

let res = update_track.apply(infra.id.unwrap(), conn);

assert!(res.is_err());
assert_eq!(
res.unwrap_err().get_type(),
OperationError::ObjectNotFound {
obj_id: "non_existent_id".to_string(),
infra_id: infra.id.unwrap()
}
.get_type()
);
})
.await;
}
}
5 changes: 2 additions & 3 deletions front/src/applications/editor/tools/selection/tool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { getMixedEntities } from '../../data/api';
import { selectInZone } from '../../../../utils/mapboxHelper';
import TOOL_TYPES from '../toolTypes';
import { DEFAULT_COMMON_TOOL_STATE } from '../commonToolState';
import { TrackEditionState } from '../trackEdition/types';
import { Tool } from '../editorContextTypes';

const SelectionTool: Tool<SelectionState> = {
Expand Down Expand Up @@ -98,12 +97,12 @@ const SelectionTool: Tool<SelectionState> = {
// be careful with type here
toolType: TOOL_TYPES.TRACK_EDITION,
toolState: {
initialEntity: selectedElement as TrackSectionEntity,
initialTrack: selectedElement as TrackSectionEntity,
track: selectedElement as TrackSectionEntity,
editionState: {
type: 'movePoint',
},
} as Partial<TrackEditionState>,
},
});
break;
case 'Signal':
Expand Down