From e94c72033243619fab5326d946e41d0ab3d7f85f Mon Sep 17 00:00:00 2001 From: shockrah Date: Wed, 12 May 2021 13:05:52 -0700 Subject: [PATCH] + More comprehensive testing on /neighbor/update The cases that actually matter should be covered now so I'm confident with this endpoint's behavior ! A preliminary db::neighbors::get is done to cover the 404 case Basically if a bad url is used in the request we should 404 the update as there won't be anything relevant to update --- json-api/client-tests/main.py | 1 + json-api/src/meta.rs | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/json-api/client-tests/main.py b/json-api/client-tests/main.py index a3f7470..88ce3da 100644 --- a/json-api/client-tests/main.py +++ b/json-api/client-tests/main.py @@ -141,6 +141,7 @@ if __name__ == '__main__': req(admin,'post', '/neighbor/add', {}, 200, body=to_json(tmp_neighbor)), req(admin, 'get', '/neighbor/list', {}, 200, verbose=True), req(admin, 'put', '/neighbor/update', {'url':str(now)}, 200, body=to_json(updated_neighbor)), + req(admin, 'put', '/neighbor/update', {'url':'fake'}, 404, body=to_json(updated_neighbor)), req(admin, 'get', '/neighbor/list', {}, 200, verbose=True), ]) diff --git a/json-api/src/meta.rs b/json-api/src/meta.rs index cb6bd42..51e3f22 100644 --- a/json-api/src/meta.rs +++ b/json-api/src/meta.rs @@ -104,12 +104,21 @@ pub async fn update_neighbor(p: &Pool, response: &mut Response, params: Ha // Verify query string parameter **and** body before attempting to reach database match (target, json) { (Some(target_url), Ok(neighbor)) => { - // Only return a 500 if something happened on db-lib's end - if let Err(e) = db::neighbors::update(p, target_url, neighbor).await { - *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; - eprintln!("/neighbor/update [DB-LIB] {}", e); + match db::neighbors::get(p, target_url).await { + Ok(row) => if row.is_some() { + if let Err(e) = db::neighbors::update(p, target_url, neighbor).await { + *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; + eprintln!("/neighbor/update [DB-LIB] {}", e); + } + // Nothing to do on success 200 is already set by hyper + } else{ + *response.status_mut() = StatusCode::NOT_FOUND; + }, + Err(e) => { + eprintln!("/neighbor/update {}", e); + *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; + } } - // Nothing to do on success 200 is already set by hyper }, _ => *response.status_mut() = StatusCode::BAD_REQUEST }