diff options
| author | Arnaud Bailly <arnaud.bailly@iohk.io> | 2024-10-04 08:41:05 +0200 |
|---|---|---|
| committer | Arnaud Bailly <arnaud.bailly@iohk.io> | 2024-10-04 08:41:05 +0200 |
| commit | 5c2288f43acfc0aa2b89ab27402dc3b1cef28b08 (patch) | |
| tree | 4368281060135d2a44f306675f21c5420b260d17 | |
| parent | 2498ba2b3dc57fa7f47fe7eced0dbab1bbfb5336 (diff) | |
| download | lambda-nantes-5c2288f43acfc0aa2b89ab27402dc3b1cef28b08.tar.gz | |
[wip] client can only register once with same URL
| -rw-r--r-- | rust/src/web.rs | 59 |
1 files changed, 54 insertions, 5 deletions
diff --git a/rust/src/web.rs b/rust/src/web.rs index 9c1b2c2..af74a09 100644 --- a/rust/src/web.rs +++ b/rust/src/web.rs @@ -20,6 +20,7 @@ trait IdGenerator { #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] enum RegistrationResult { Success(RegistrationSuccess), + Failure(RegistrationFailure), } #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] @@ -28,6 +29,11 @@ struct RegistrationSuccess { url: String, } +#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] +enum RegistrationFailure { + UrlAlreadyRegistered { url: String }, +} + #[post("/register")] async fn register( id_gen: web::Data<dyn IdGenerator>, @@ -79,11 +85,16 @@ mod app_tests { async fn post_registration_returns_success_with_unique_id() { let id = "0123456789abcdef0123456789abcdef".to_string(); let id_generator: Arc<dyn IdGenerator> = Arc::new(ConstantIdGenerator::new(id.clone())); - env_logger::init(); - let logger = Logger::default(); - let data = web::Data::from(id_generator); - let app = - test::init_service(App::new().wrap(logger).app_data(data).service(register)).await; + // FIXME should only be called once, move to setup + // env_logger::init(); + + let app = test::init_service( + App::new() + .wrap(Logger::default()) + .app_data(web::Data::from(id_generator)) + .service(register), + ) + .await; let url = "http://192.168.1.1".to_string(); let req = test::TestRequest::post() .uri("/register") @@ -102,4 +113,42 @@ mod app_tests { serde_json::from_slice(&bytes.unwrap()).unwrap() ); } + + #[actix_web::test] + async fn post_registration_fails_given_url_is_already_registered() { + let id = "0123456789abcdef0123456789abcdef".to_string(); + let id_generator: Arc<dyn IdGenerator> = Arc::new(ConstantIdGenerator::new(id.clone())); + + let app = test::init_service( + App::new() + .wrap(Logger::default()) + .app_data(web::Data::from(id_generator)) + .service(register), + ) + .await; + let url = "http://192.168.1.1".to_string(); + let _ = test::TestRequest::post() + .uri("/register") + .set_json(Registration { url: url.clone() }) + .insert_header(ContentType::json()) + .to_request(); + + // second request with the same URL + let req = test::TestRequest::post() + .uri("/register") + .set_json(Registration { url: url.clone() }) + .insert_header(ContentType::json()) + .to_request(); + + let resp = test::call_service(&app, req).await; + + assert!(resp.status().is_client_error()); + + let body = resp.into_body(); + let bytes = body::to_bytes(body).await; + assert_eq!( + RegistrationResult::Failure(RegistrationFailure::UrlAlreadyRegistered { url }), + serde_json::from_slice(&bytes.unwrap()).unwrap() + ); + } } |
