Skip to content

Commit

Permalink
Return mock_error_response_containing_blob to explicitly check test
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrylavrenov committed Sep 16, 2024
1 parent 77f2c10 commit a240fb4
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 13 deletions.
90 changes: 83 additions & 7 deletions crates/robonode-client/src/authenticate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
90 changes: 84 additions & 6 deletions crates/robonode-client/src/enroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit a240fb4

Please sign in to comment.