Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrylavrenov committed Sep 12, 2024
1 parent 0bd240f commit 4682656
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 143 deletions.
66 changes: 2 additions & 64 deletions crates/robonode-client/src/authenticate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ mod tests {
AuthenticateResponse {
auth_ticket: vec![1, 2, 3].into(),
auth_ticket_signature: vec![4, 5, 6].into(),
scan_result_blob: Some("blob".to_owned()),
}
)
}
Expand Down Expand Up @@ -322,7 +323,7 @@ mod tests {
| AuthenticateError::FaceScanRejected(_)
| AuthenticateError::SignatureInvalid(_)
| AuthenticateError::LogicInternal(_) => ResponseTemplate::new(case.0)
.set_body_json(mkerr_returning_blob(case.1, "scan result blob")),
.set_body_json(mkerr_containing_blob(case.1, "scan result blob")),
_ => ResponseTemplate::new(case.0).set_body_json(mkerr(case.1)),
};

Expand All @@ -343,69 +344,6 @@ mod tests {
}
}

#[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::PersonNotFound,
),
(
StatusCode::FORBIDDEN,
"AUTHENTICATE_FACE_SCAN_REJECTED",
AuthenticateError::FaceScanRejected,
),
(
StatusCode::FORBIDDEN,
"AUTHENTICATE_SIGNATURE_INVALID",
AuthenticateError::SignatureInvalid,
),
(
StatusCode::INTERNAL_SERVER_ERROR,
"LOGIC_INTERNAL_ERROR",
AuthenticateError::LogicInternal,
),
(
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))
.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_unknown() {
let mock_server = MockServer::start().await;
Expand Down
71 changes: 1 addition & 70 deletions crates/robonode-client/src/enroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ mod tests {
EnrollError::FaceScanRejected(_)
| EnrollError::PersonAlreadyEnrolled(_)
| EnrollError::LogicInternal(_) => ResponseTemplate::new(case.0)
.set_body_json(mkerr_returning_blob(case.1, "scan result blob")),
.set_body_json(mkerr_containing_blob(case.1, "scan result blob")),
_ => ResponseTemplate::new(case.0).set_body_json(mkerr(case.1)),
};

Expand All @@ -297,75 +297,6 @@ mod tests {
}
}

#[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::FaceScanRejected,
),
(
StatusCode::CONFLICT,
"ENROLL_PUBLIC_KEY_ALREADY_USED",
EnrollError::PublicKeyAlreadyUsed,
),
(
StatusCode::CONFLICT,
"ENROLL_PERSON_ALREADY_ENROLLED",
EnrollError::PersonAlreadyEnrolled,
),
(
StatusCode::INTERNAL_SERVER_ERROR,
"LOGIC_INTERNAL_ERROR",
EnrollError::LogicInternal,
),
(
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))
.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_unknown() {
let mock_server = MockServer::start().await;
Expand Down
11 changes: 2 additions & 9 deletions crates/robonode-client/src/error_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,13 @@ pub mod tests {
}

#[test]
fn decodes_returning_blob() {
let err = mkerr_returning_blob("MY_ERR_CODE", "scan result blob").to_string();
fn decodes_containing_blob() {
let err = mkerr_containing_blob("MY_ERR_CODE", "scan result blob").to_string();
let ErrorResponse {
error_code,
scan_result_blob,
} = err.try_into().unwrap();
assert_eq!(error_code, "MY_ERR_CODE");
assert_eq!(scan_result_blob, Some("scan result blob".to_owned()));
}

#[test]
fn decodes_containing_blob() {
let err = mkerr_containing_blob("MY_ERR_CODE", "scan result blob").to_string();
let ErrorResponse { error_code } = err.try_into().unwrap();
assert_eq!(error_code, "MY_ERR_CODE");
}
}

0 comments on commit 4682656

Please sign in to comment.