From a240fb477601465dd36d3823ae7afac37d301616 Mon Sep 17 00:00:00 2001 From: Dmitry Lavrenov Date: Mon, 16 Sep 2024 18:16:08 +0300 Subject: [PATCH] Return mock_error_response_containing_blob to explicitly check test --- crates/robonode-client/src/authenticate.rs | 90 ++++++++++++++++++++-- crates/robonode-client/src/enroll.rs | 90 ++++++++++++++++++++-- 2 files changed, 167 insertions(+), 13 deletions(-) diff --git a/crates/robonode-client/src/authenticate.rs b/crates/robonode-client/src/authenticate.rs index adc90804c..b38648d3a 100644 --- a/crates/robonode-client/src/authenticate.rs +++ b/crates/robonode-client/src/authenticate.rs @@ -318,15 +318,91 @@ mod tests { liveness_data_signature: b"123", }; - let response = match case.2 { - AuthenticateError::PersonNotFound(_) - | AuthenticateError::FaceScanRejected(_) - | AuthenticateError::SignatureInvalid(_) - | AuthenticateError::LogicInternal(_) => ResponseTemplate::new(case.0) - .set_body_json(mkerr_containing_blob(case.1, "scan result blob")), - _ => ResponseTemplate::new(case.0).set_body_json(mkerr(case.1)), + let response = ResponseTemplate::new(case.0).set_body_json(mkerr(case.1)); + + Mock::given(matchers::method("POST")) + .and(matchers::path("/authenticate")) + .and(matchers::body_json(&sample_request)) + .respond_with(response) + .mount(&mock_server) + .await; + + let client = Client { + base_url: mock_server.uri(), + reqwest: reqwest::Client::new(), }; + let actual_error = client.authenticate(sample_request).await.unwrap_err(); + assert_matches!(actual_error, Error::Call(err) if err == case.2); + } + } + + #[tokio::test] + async fn mock_error_response_containing_blob() { + let cases = [ + ( + StatusCode::BAD_REQUEST, + "AUTHENTICATE_INVALID_LIVENESS_DATA", + AuthenticateError::InvalidLivenessData, + ), + ( + StatusCode::NOT_FOUND, + "AUTHENTICATE_PERSON_NOT_FOUND", + AuthenticateError::PersonNotFoundNoBlob, + ), + ( + StatusCode::NOT_FOUND, + "AUTHENTICATE_PERSON_NOT_FOUND", + AuthenticateError::PersonNotFound("scan result blob".to_owned()), + ), + ( + StatusCode::FORBIDDEN, + "AUTHENTICATE_FACE_SCAN_REJECTED", + AuthenticateError::FaceScanRejectedNoBlob, + ), + ( + StatusCode::FORBIDDEN, + "AUTHENTICATE_FACE_SCAN_REJECTED", + AuthenticateError::FaceScanRejected("scan result blob".to_owned()), + ), + ( + StatusCode::FORBIDDEN, + "AUTHENTICATE_SIGNATURE_INVALID", + AuthenticateError::SignatureInvalidNoBlob, + ), + ( + StatusCode::FORBIDDEN, + "AUTHENTICATE_SIGNATURE_INVALID", + AuthenticateError::SignatureInvalid("scan result blob".to_owned()), + ), + ( + StatusCode::INTERNAL_SERVER_ERROR, + "LOGIC_INTERNAL_ERROR", + AuthenticateError::LogicInternalNoBlob, + ), + ( + StatusCode::INTERNAL_SERVER_ERROR, + "LOGIC_INTERNAL_ERROR", + AuthenticateError::LogicInternal("scan result blob".to_owned()), + ), + ( + StatusCode::BAD_REQUEST, + "MY_ERR_CODE", + AuthenticateError::UnknownCode("MY_ERR_CODE".to_owned()), + ), + ]; + + for case in cases { + let mock_server = MockServer::start().await; + + let sample_request = AuthenticateRequest { + liveness_data: b"dummy liveness data", + liveness_data_signature: b"123", + }; + + let response = + ResponseTemplate::new(case.0).set_body_json(mkerr_containing_blob(case.1, "blob")); + Mock::given(matchers::method("POST")) .and(matchers::path("/authenticate")) .and(matchers::body_json(&sample_request)) diff --git a/crates/robonode-client/src/enroll.rs b/crates/robonode-client/src/enroll.rs index 4257bc022..3a2d8b959 100644 --- a/crates/robonode-client/src/enroll.rs +++ b/crates/robonode-client/src/enroll.rs @@ -273,14 +273,92 @@ mod tests { public_key: b"123", }; - let response = match case.2 { - EnrollError::FaceScanRejected(_) - | EnrollError::PersonAlreadyEnrolled(_) - | EnrollError::LogicInternal(_) => ResponseTemplate::new(case.0) - .set_body_json(mkerr_containing_blob(case.1, "scan result blob")), - _ => ResponseTemplate::new(case.0).set_body_json(mkerr(case.1)), + let response = ResponseTemplate::new(case.0).set_body_json(mkerr(case.1)); + + Mock::given(matchers::method("POST")) + .and(matchers::path("/enroll")) + .and(matchers::body_json(&sample_request)) + .respond_with(response) + .mount(&mock_server) + .await; + + let client = Client { + base_url: mock_server.uri(), + reqwest: reqwest::Client::new(), + }; + + let actual_error = client.enroll(sample_request).await.unwrap_err(); + assert_matches!(actual_error, Error::Call(err) if err == case.2); + } + } + + #[tokio::test] + async fn mock_error_response_containing_blob() { + let cases = [ + ( + StatusCode::BAD_REQUEST, + "ENROLL_INVALID_PUBLIC_KEY", + EnrollError::InvalidPublicKey, + ), + ( + StatusCode::BAD_REQUEST, + "ENROLL_INVALID_LIVENESS_DATA", + EnrollError::InvalidLivenessData, + ), + ( + StatusCode::FORBIDDEN, + "ENROLL_FACE_SCAN_REJECTED", + EnrollError::FaceScanRejectedNoBlob, + ), + ( + StatusCode::FORBIDDEN, + "ENROLL_FACE_SCAN_REJECTED", + EnrollError::FaceScanRejected("scan result blob".to_owned()), + ), + ( + StatusCode::CONFLICT, + "ENROLL_PUBLIC_KEY_ALREADY_USED", + EnrollError::PublicKeyAlreadyUsed, + ), + ( + StatusCode::CONFLICT, + "ENROLL_PERSON_ALREADY_ENROLLED", + EnrollError::PersonAlreadyEnrolledNoBlob, + ), + ( + StatusCode::CONFLICT, + "ENROLL_PERSON_ALREADY_ENROLLED", + EnrollError::PersonAlreadyEnrolled("scan result blob".to_owned()), + ), + ( + StatusCode::INTERNAL_SERVER_ERROR, + "LOGIC_INTERNAL_ERROR", + EnrollError::LogicInternalNoBlob, + ), + ( + StatusCode::INTERNAL_SERVER_ERROR, + "LOGIC_INTERNAL_ERROR", + EnrollError::LogicInternal("scan result blob".to_owned()), + ), + ( + StatusCode::BAD_REQUEST, + "MY_ERR_CODE", + EnrollError::UnknownCode("MY_ERR_CODE".to_owned()), + ), + ]; + + for case in cases { + let mock_server = MockServer::start().await; + + let sample_request = EnrollRequest { + liveness_data: b"dummy liveness data", + liveness_data_signature: b"signature", + public_key: b"123", }; + let response = + ResponseTemplate::new(case.0).set_body_json(mkerr_containing_blob(case.1, "blob")); + Mock::given(matchers::method("POST")) .and(matchers::path("/enroll")) .and(matchers::body_json(&sample_request))