Skip to content

Commit

Permalink
Improve tests to explicitly distinguish old and new robonode behaviou…
Browse files Browse the repository at this point in the history
…r at `robonode-client` (#1271)

* Improve naming in tests

* Introduce ResponseIncludesBlob logic in tests

* Some refactoring

* Simplify response template creation

* Unify mkerr test util

* Move ResponseIncludesBlob to test_utils

* Rename status_code, body to http_code, error_code

* Improve response template creation
  • Loading branch information
dmitrylavrenov authored Sep 16, 2024
1 parent d33fdec commit 5f72c45
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 37 deletions.
39 changes: 25 additions & 14 deletions crates/robonode-client/src/authenticate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ mod tests {
use wiremock::{matchers, Mock, MockServer, ResponseTemplate};

use super::*;
use crate::test_utils::{mkerr, mkerr_containing_blob};
use crate::test_utils::{mkerr, ResponseIncludesBlob};

#[test]
fn request_serialization() {
Expand All @@ -115,7 +115,7 @@ mod tests {
}

#[test]
fn response_deserialization() {
fn response_deserialization_before_2023_05() {
let sample_response = serde_json::json!({
"authTicket": [1, 2, 3],
"authTicketSignature": [4, 5, 6],
Expand All @@ -132,7 +132,7 @@ mod tests {
}

#[test]
fn response_deserialization_containing_blob() {
fn response_deserialization() {
let sample_response = serde_json::json!({
"authTicket": [1, 2, 3],
"authTicketSignature": [4, 5, 6],
Expand All @@ -150,7 +150,7 @@ mod tests {
}

#[tokio::test]
async fn mock_success() {
async fn mock_success_before_2023_05() {
let mock_server = MockServer::start().await;

let sample_request = AuthenticateRequest {
Expand Down Expand Up @@ -182,7 +182,7 @@ mod tests {
}

#[tokio::test]
async fn mock_success_containing_blob() {
async fn mock_success() {
let mock_server = MockServer::start().await;

let sample_request = AuthenticateRequest {
Expand Down Expand Up @@ -215,7 +215,7 @@ mod tests {
}

#[tokio::test]
async fn mock_error_response() {
async fn mock_error_response_before_2023_05() {
let cases = [
(
StatusCode::BAD_REQUEST,
Expand Down Expand Up @@ -249,15 +249,15 @@ mod tests {
),
];

for case in cases {
for (http_code, error_code, expected_error) 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(case.1));
let response = ResponseTemplate::new(http_code).set_body_json(mkerr(error_code, None));

Mock::given(matchers::method("POST"))
.and(matchers::path("/authenticate"))
Expand All @@ -272,55 +272,66 @@ mod tests {
};

let actual_error = client.authenticate(sample_request).await.unwrap_err();
assert_matches!(actual_error, Error::Call(err) if err == case.2);
assert_matches!(actual_error, Error::Call(err) if err == expected_error);
}
}

#[tokio::test]
async fn mock_error_response_containing_blob() {
async fn mock_error_response() {
let cases = [
(
StatusCode::BAD_REQUEST,
"AUTHENTICATE_INVALID_LIVENESS_DATA",
ResponseIncludesBlob::No,
AuthenticateError::InvalidLivenessData,
),
(
StatusCode::NOT_FOUND,
"AUTHENTICATE_PERSON_NOT_FOUND",
ResponseIncludesBlob::Yes,
AuthenticateError::PersonNotFound,
),
(
StatusCode::FORBIDDEN,
"AUTHENTICATE_FACE_SCAN_REJECTED",
ResponseIncludesBlob::Yes,
AuthenticateError::FaceScanRejected,
),
(
StatusCode::FORBIDDEN,
"AUTHENTICATE_SIGNATURE_INVALID",
ResponseIncludesBlob::Yes,
AuthenticateError::SignatureInvalid,
),
(
StatusCode::INTERNAL_SERVER_ERROR,
"LOGIC_INTERNAL_ERROR",
ResponseIncludesBlob::Yes,
AuthenticateError::LogicInternal,
),
(
StatusCode::BAD_REQUEST,
"MY_ERR_CODE",
ResponseIncludesBlob::No,
AuthenticateError::UnknownCode("MY_ERR_CODE".to_owned()),
),
];

for case in cases {
for (http_code, error_code, response_includes_blob, expected_error) 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"));
let response_scan_result_blob = match response_includes_blob {
ResponseIncludesBlob::Yes => Some("scan result blob"),
ResponseIncludesBlob::No => None,
};

let response = ResponseTemplate::new(http_code)
.set_body_json(mkerr(error_code, response_scan_result_blob));

Mock::given(matchers::method("POST"))
.and(matchers::path("/authenticate"))
Expand All @@ -335,7 +346,7 @@ mod tests {
};

let actual_error = client.authenticate(sample_request).await.unwrap_err();
assert_matches!(actual_error, Error::Call(err) if err == case.2);
assert_matches!(actual_error, Error::Call(err) if err == expected_error);
}
}

Expand Down
36 changes: 24 additions & 12 deletions crates/robonode-client/src/enroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ mod tests {
use wiremock::{matchers, Mock, MockServer, ResponseTemplate};

use super::*;
use crate::test_utils::{mkerr, mkerr_containing_blob};
use crate::test_utils::{mkerr, ResponseIncludesBlob};

#[test]
fn request_serialization() {
Expand All @@ -110,7 +110,7 @@ mod tests {
}

#[tokio::test]
async fn mock_success() {
async fn mock_success_before_2023_05() {
let mock_server = MockServer::start().await;

let sample_request = EnrollRequest {
Expand All @@ -135,7 +135,7 @@ mod tests {
}

#[tokio::test]
async fn mock_success_containing_blob() {
async fn mock_success() {
let mock_server = MockServer::start().await;

let sample_request = EnrollRequest {
Expand Down Expand Up @@ -163,7 +163,7 @@ mod tests {
}

#[tokio::test]
async fn mock_error_response() {
async fn mock_error_response_before_2023_05() {
let cases = [
(
StatusCode::BAD_REQUEST,
Expand Down Expand Up @@ -202,7 +202,7 @@ mod tests {
),
];

for case in cases {
for (http_code, error_code, expected_error) in cases {
let mock_server = MockServer::start().await;

let sample_request = EnrollRequest {
Expand All @@ -211,7 +211,7 @@ mod tests {
public_key: b"123",
};

let response = ResponseTemplate::new(case.0).set_body_json(mkerr(case.1));
let response = ResponseTemplate::new(http_code).set_body_json(mkerr(error_code, None));

Mock::given(matchers::method("POST"))
.and(matchers::path("/enroll"))
Expand All @@ -226,51 +226,58 @@ mod tests {
};

let actual_error = client.enroll(sample_request).await.unwrap_err();
assert_matches!(actual_error, Error::Call(err) if err == case.2);
assert_matches!(actual_error, Error::Call(err) if err == expected_error);
}
}

#[tokio::test]
async fn mock_error_response_containing_blob() {
async fn mock_error_response() {
let cases = [
(
StatusCode::BAD_REQUEST,
"ENROLL_INVALID_PUBLIC_KEY",
ResponseIncludesBlob::No,
EnrollError::InvalidPublicKey,
),
(
StatusCode::BAD_REQUEST,
"ENROLL_INVALID_LIVENESS_DATA",
ResponseIncludesBlob::No,
EnrollError::InvalidLivenessData,
),
(
StatusCode::FORBIDDEN,
"ENROLL_FACE_SCAN_REJECTED",
ResponseIncludesBlob::Yes,
EnrollError::FaceScanRejected,
),
(
StatusCode::CONFLICT,
"ENROLL_PUBLIC_KEY_ALREADY_USED",
ResponseIncludesBlob::No,
EnrollError::PublicKeyAlreadyUsed,
),
(
StatusCode::CONFLICT,
"ENROLL_PERSON_ALREADY_ENROLLED",
ResponseIncludesBlob::Yes,
EnrollError::PersonAlreadyEnrolled,
),
(
StatusCode::INTERNAL_SERVER_ERROR,
"LOGIC_INTERNAL_ERROR",
ResponseIncludesBlob::Yes,
EnrollError::LogicInternal,
),
(
StatusCode::BAD_REQUEST,
"MY_ERR_CODE",
ResponseIncludesBlob::No,
EnrollError::UnknownCode("MY_ERR_CODE".to_owned()),
),
];

for case in cases {
for (http_code, error_code, response_includes_blob, expected_error) in cases {
let mock_server = MockServer::start().await;

let sample_request = EnrollRequest {
Expand All @@ -279,8 +286,13 @@ mod tests {
public_key: b"123",
};

let response =
ResponseTemplate::new(case.0).set_body_json(mkerr_containing_blob(case.1, "blob"));
let response_scan_result_blob = match response_includes_blob {
ResponseIncludesBlob::Yes => Some("scan result blob"),
ResponseIncludesBlob::No => None,
};

let response = ResponseTemplate::new(http_code)
.set_body_json(mkerr(error_code, response_scan_result_blob));

Mock::given(matchers::method("POST"))
.and(matchers::path("/enroll"))
Expand All @@ -295,7 +307,7 @@ mod tests {
};

let actual_error = client.enroll(sample_request).await.unwrap_err();
assert_matches!(actual_error, Error::Call(err) if err == case.2);
assert_matches!(actual_error, Error::Call(err) if err == expected_error);
}
}

Expand Down
10 changes: 5 additions & 5 deletions crates/robonode-client/src/error_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ impl TryFrom<String> for ErrorResponse {
#[cfg(test)]
pub mod tests {
use super::*;
use crate::test_utils::{mkerr, mkerr_containing_blob};
use crate::test_utils::mkerr;

#[test]
fn decodes() {
let err = mkerr("MY_ERR_CODE").to_string();
fn decodes_before_2023_05() {
let err = mkerr("MY_ERR_CODE", None).to_string();
let ErrorResponse { error_code } = err.try_into().unwrap();
assert_eq!(error_code, "MY_ERR_CODE");
}

#[test]
fn decodes_containing_blob() {
let err = mkerr_containing_blob("MY_ERR_CODE", "scan result blob").to_string();
fn decodes() {
let err = mkerr("MY_ERR_CODE", Some("scan result blob")).to_string();
let ErrorResponse { error_code } = err.try_into().unwrap();
assert_eq!(error_code, "MY_ERR_CODE");
}
Expand Down
18 changes: 12 additions & 6 deletions crates/robonode-client/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
pub fn mkerr(error_code: &str) -> serde_json::Value {
serde_json::json!({ "errorCode": error_code })
pub enum ResponseIncludesBlob {
Yes,
No,
}

pub fn mkerr_containing_blob(error_code: &str, scan_result_blob: &str) -> serde_json::Value {
serde_json::json!({ "errorCode": error_code, "scanResultBlob": scan_result_blob })
pub fn mkerr(error_code: &str, maybe_scan_result_blob: Option<&str>) -> serde_json::Value {
match maybe_scan_result_blob {
None => serde_json::json!({ "errorCode": error_code}),
Some(scan_result_blob) => {
serde_json::json!({ "errorCode": error_code, "scanResultBlob": scan_result_blob })
}
}
}

#[cfg(test)]
Expand All @@ -13,12 +19,12 @@ mod tests {
#[test]
fn evals_properly() {
assert_eq!(
mkerr("MY_ERR_CODE").to_string(),
mkerr("MY_ERR_CODE", None).to_string(),
serde_json::json!({ "errorCode": "MY_ERR_CODE" }).to_string()
);

assert_eq!(
mkerr_containing_blob("MY_ERR_CODE", "scan result blob").to_string(),
mkerr("MY_ERR_CODE", Some("scan result blob")).to_string(),
serde_json::json!({ "errorCode": "MY_ERR_CODE", "scanResultBlob": "scan result blob" })
.to_string()
);
Expand Down

0 comments on commit 5f72c45

Please sign in to comment.