Skip to content

Commit 7ef4dfe

Browse files
author
Baptiste Prevot
committed
editoast: return 404 on object update unknown id
1 parent 02e368b commit 7ef4dfe

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

editoast/src/schema/operation/delete.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ 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(self.obj_id.clone(), infra_id).into()),
3030
Err(err) => Err(err.into()),
3131
}
3232
}

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 '{0}', could not be found in the infrastructure '{1}'")]
6262
#[editoast_error(status = 404)]
63-
ObjectNotFound(String),
63+
ObjectNotFound(String, 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

+39-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,20 @@ 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(self.obj_id.clone(), infra_id).into())
36+
}
37+
Err(err) => return Err(err.into()),
38+
};
3139

3240
// Apply and check patch
3341
let railjson_obj = obj.patch_and_check(self)?;
@@ -43,7 +51,7 @@ impl UpdateOperation {
4351
.execute(conn)
4452
{
4553
Ok(1) => Ok(railjson_obj),
46-
Ok(_) => Err(OperationError::ObjectNotFound(self.obj_id.clone()).into()),
54+
Ok(_) => Err(OperationError::ObjectNotFound(self.obj_id.clone(), infra_id).into()),
4755
Err(err) => Err(err.into()),
4856
}
4957
}
@@ -90,7 +98,6 @@ mod tests {
9098
use crate::schema::operation::OperationError;
9199
use crate::schema::{OSRDIdentified, ObjectType};
92100
use actix_web::test as actix_test;
93-
use actix_web::ResponseError;
94101
use diesel::sql_query;
95102
use diesel::sql_types::{Double, Text};
96103
use diesel::RunQueryDsl;
@@ -157,8 +164,8 @@ mod tests {
157164

158165
assert!(res.is_err());
159166
assert_eq!(
160-
res.unwrap_err().status_code(),
161-
OperationError::ModifyId.get_status()
167+
res.unwrap_err().get_type(),
168+
OperationError::ModifyId.get_type()
162169
);
163170
})
164171
.await;
@@ -250,4 +257,30 @@ mod tests {
250257
assert_eq!(updated_speed.val, 80.0);
251258
}).await;
252259
}
260+
261+
#[actix_test]
262+
async fn wrong_id_update_track() {
263+
test_infra_transaction(|conn, infra| {
264+
let update_track = UpdateOperation {
265+
obj_id: "non_existent_id".to_string(),
266+
obj_type: ObjectType::TrackSection,
267+
railjson_patch: from_str(
268+
r#"[
269+
{ "op": "replace", "path": "/length", "value": 80.0 }
270+
]"#,
271+
)
272+
.unwrap(),
273+
};
274+
275+
let res = update_track.apply(infra.id.unwrap(), conn);
276+
277+
assert!(res.is_err());
278+
assert_eq!(
279+
res.unwrap_err().get_type(),
280+
OperationError::ObjectNotFound("non_existent_id".to_string(), infra.id.unwrap())
281+
.get_type()
282+
);
283+
})
284+
.await;
285+
}
253286
}

0 commit comments

Comments
 (0)