+ 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
This commit is contained in:
shockrah 2021-05-12 13:05:52 -07:00
parent 8bff61ab71
commit e94c720332
2 changed files with 15 additions and 5 deletions

View File

@ -141,6 +141,7 @@ if __name__ == '__main__':
req(admin,'post', '/neighbor/add', {}, 200, body=to_json(tmp_neighbor)), req(admin,'post', '/neighbor/add', {}, 200, body=to_json(tmp_neighbor)),
req(admin, 'get', '/neighbor/list', {}, 200, verbose=True), 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':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), req(admin, 'get', '/neighbor/list', {}, 200, verbose=True),
]) ])

View File

@ -104,12 +104,21 @@ pub async fn update_neighbor(p: &Pool, response: &mut Response<Body>, params: Ha
// Verify query string parameter **and** body before attempting to reach database // Verify query string parameter **and** body before attempting to reach database
match (target, json) { match (target, json) {
(Some(target_url), Ok(neighbor)) => { (Some(target_url), Ok(neighbor)) => {
// Only return a 500 if something happened on db-lib's end match db::neighbors::get(p, target_url).await {
if let Err(e) = db::neighbors::update(p, target_url, neighbor).await { Ok(row) => if row.is_some() {
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; if let Err(e) = db::neighbors::update(p, target_url, neighbor).await {
eprintln!("/neighbor/update [DB-LIB] {}", e); *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 _ => *response.status_mut() = StatusCode::BAD_REQUEST
} }