Skip to content

Commit

Permalink
chore: add tests for GET /v1/keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Dec 15, 2023
1 parent a8d24a9 commit 2885911
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
3 changes: 3 additions & 0 deletions moksha-mint/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ pub enum MokshaMintError {

#[error("Invalid quote {0}")]
InvalidQuote(String),

#[error("Keyset not found {0}")]
KeysetNotFound(String),
}

impl IntoResponse for MokshaMintError {
Expand Down
69 changes: 67 additions & 2 deletions moksha-mint/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,13 @@ async fn get_keys(State(mint): State<Mint>) -> Result<Json<KeysResponse>, Moksha
)
)]
async fn get_keys_by_id(
Path(_id): Path<String>,
Path(id): Path<String>,
State(mint): State<Mint>,
) -> Result<Json<KeysResponse>, MokshaMintError> {
// FIXME check if id is valid
if id != mint.keyset.keyset_id {
return Err(MokshaMintError::KeysetNotFound(id));
}

Ok(Json(KeysResponse {
keysets: vec![KeyResponse {
id: mint.keyset.keyset_id.clone(),
Expand Down Expand Up @@ -756,4 +759,66 @@ mod tests {
assert_eq!(16, keysets.keysets[0].id.len());
Ok(())
}

// ### v1 api tests

#[tokio::test]
async fn test_get_v1_keys() -> anyhow::Result<()> {
let app = app(create_mock_mint(Default::default()), None, None);
let response = app
.oneshot(Request::builder().uri("/v1/keys").body(Body::empty())?)
.await?;

assert_eq!(response.status(), StatusCode::OK);
let body = response.into_body().collect().await.unwrap().to_bytes();
let keys: KeysResponse = serde_json::from_slice(&body)?;
assert_eq!(1, keys.keysets.len());
assert_eq!(
64,
keys.keysets.get(0).expect("keyset not found").keys.len()
);
println!("{:#?}", keys.keysets.get(0).unwrap().id);
Ok(())
}

#[tokio::test]
async fn test_get_v1_keys_id_invalid() -> anyhow::Result<()> {
let app = app(create_mock_mint(Default::default()), None, None);
let response = app
.oneshot(
Request::builder()
.uri("/v1/keys/unknownkeyset")
.body(Body::empty())?,
)
.await?;

assert_eq!(response.status(), StatusCode::BAD_REQUEST);
Ok(())
}

#[tokio::test]
async fn test_get_v1_keys_id() -> anyhow::Result<()> {
let app = app(create_mock_mint(Default::default()), None, None);
let response = app
.oneshot(
Request::builder()
.uri("/v1/keys/00e777893f6faa27")
.body(Body::empty())?,
)
.await?;

assert_eq!(response.status(), StatusCode::OK);
let body = response.into_body().collect().await.unwrap().to_bytes();
let keys: KeysResponse = serde_json::from_slice(&body)?;
assert_eq!(1, keys.keysets.len());
assert_eq!(
64,
keys.keysets.get(0).expect("keyset not found").keys.len()
);
assert_eq!(
"00e777893f6faa27",
keys.keysets.get(0).expect("keyset not found").id
);
Ok(())
}
}

0 comments on commit 2885911

Please sign in to comment.