Skip to content

Commit c907807

Browse files
author
Baptiste Prevot
committed
editoast: return 404 on object update unknown id
1 parent 2babacd commit c907807

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

editoast/src/schema/operation/delete.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ impl DeleteOperation {
2626
.execute(conn)
2727
{
2828
Ok(1) => Ok(()),
29-
Ok(_) => Err(OperationError::ObjectNotFound(self.obj_id.clone()).into()),
29+
Ok(_) => Err(OperationError::ObjectNotFound {
30+
obj_id: self.obj_id.clone(),
31+
infra_id,
32+
}
33+
.into()),
3034
Err(err) => Err(err.into()),
3135
}
3236
}

editoast/src/schema/operation/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ impl Operation {
5858
#[editoast_error(base_id = "operation")]
5959
enum OperationError {
6060
// To modify
61-
#[error("Object '{0}', could not be found")]
61+
#[error("Object '{obj_id}', could not be found in the infrastructure '{infra_id}'")]
6262
#[editoast_error(status = 404)]
63-
ObjectNotFound(String),
63+
ObjectNotFound { obj_id: String, infra_id: i64 },
6464
#[error("Empty string id is forbidden")]
6565
EmptyId,
6666
#[error("Update operation try to modify object id, which is forbidden")]

editoast/src/schema/operation/update.rs

+50-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::error::Result;
22
use crate::schema::operation::RailjsonObject;
33
use crate::schema::{OSRDIdentified, ObjectType};
4+
use diesel::result::Error as DieselError;
45
use diesel::sql_types::{BigInt, Json, Jsonb, Text};
56
use diesel::{sql_query, PgConnection, QueryableByName, RunQueryDsl};
67
use json_patch::Patch;
@@ -21,13 +22,24 @@ impl UpdateOperation {
2122
pub fn apply(&self, infra_id: i64, conn: &mut PgConnection) -> Result<RailjsonObject> {
2223
// Load object
2324

24-
let mut obj: DataObject = sql_query(format!(
25+
let mut obj: DataObject = match sql_query(format!(
2526
"SELECT data FROM {} WHERE infra_id = $1 AND obj_id = $2",
2627
self.obj_type.get_table()
2728
))
2829
.bind::<BigInt, _>(infra_id)
2930
.bind::<Text, _>(&self.obj_id)
30-
.get_result(conn)?;
31+
.get_result(conn)
32+
{
33+
Ok(obj) => obj,
34+
Err(DieselError::NotFound) => {
35+
return Err(OperationError::ObjectNotFound {
36+
obj_id: self.obj_id.clone(),
37+
infra_id,
38+
}
39+
.into())
40+
}
41+
Err(err) => return Err(err.into()),
42+
};
3143

3244
// Apply and check patch
3345
let railjson_obj = obj.patch_and_check(self)?;
@@ -43,7 +55,11 @@ impl UpdateOperation {
4355
.execute(conn)
4456
{
4557
Ok(1) => Ok(railjson_obj),
46-
Ok(_) => Err(OperationError::ObjectNotFound(self.obj_id.clone()).into()),
58+
Ok(_) => Err(OperationError::ObjectNotFound {
59+
obj_id: self.obj_id.clone(),
60+
infra_id,
61+
}
62+
.into()),
4763
Err(err) => Err(err.into()),
4864
}
4965
}
@@ -90,7 +106,6 @@ mod tests {
90106
use crate::schema::operation::OperationError;
91107
use crate::schema::{OSRDIdentified, ObjectType};
92108
use actix_web::test as actix_test;
93-
use actix_web::ResponseError;
94109
use diesel::sql_query;
95110
use diesel::sql_types::{Double, Text};
96111
use diesel::RunQueryDsl;
@@ -157,8 +172,8 @@ mod tests {
157172

158173
assert!(res.is_err());
159174
assert_eq!(
160-
res.unwrap_err().status_code(),
161-
OperationError::ModifyId.get_status()
175+
res.unwrap_err().get_type(),
176+
OperationError::ModifyId.get_type()
162177
);
163178
})
164179
.await;
@@ -250,4 +265,33 @@ mod tests {
250265
assert_eq!(updated_speed.val, 80.0);
251266
}).await;
252267
}
268+
269+
#[actix_test]
270+
async fn wrong_id_update_track() {
271+
test_infra_transaction(|conn, infra| {
272+
let update_track = UpdateOperation {
273+
obj_id: "non_existent_id".to_string(),
274+
obj_type: ObjectType::TrackSection,
275+
railjson_patch: from_str(
276+
r#"[
277+
{ "op": "replace", "path": "/length", "value": 80.0 }
278+
]"#,
279+
)
280+
.unwrap(),
281+
};
282+
283+
let res = update_track.apply(infra.id.unwrap(), conn);
284+
285+
assert!(res.is_err());
286+
assert_eq!(
287+
res.unwrap_err().get_type(),
288+
OperationError::ObjectNotFound {
289+
obj_id: "non_existent_id".to_string(),
290+
infra_id: infra.id.unwrap()
291+
}
292+
.get_type()
293+
);
294+
})
295+
.await;
296+
}
253297
}

0 commit comments

Comments
 (0)