From 3de3fc65d131fc84512fcd4b1f25f64a0860e6db Mon Sep 17 00:00:00 2001 From: Vika Date: Sat, 13 Aug 2022 21:54:53 +0300 Subject: tokenauth: migrate tests to Wiremock Wiremock doesn't require external C dependencies. --- kittybox-rs/src/tokenauth.rs | 99 ++++++++++++++++++++------------------------ 1 file changed, 45 insertions(+), 54 deletions(-) (limited to 'kittybox-rs/src') diff --git a/kittybox-rs/src/tokenauth.rs b/kittybox-rs/src/tokenauth.rs index 103f514..244a045 100644 --- a/kittybox-rs/src/tokenauth.rs +++ b/kittybox-rs/src/tokenauth.rs @@ -237,7 +237,8 @@ mod tests { extract::FromRequest, http::{Method, Request}, }; - use httpmock::prelude::*; + use wiremock::{MockServer, Mock, ResponseTemplate}; + use wiremock::matchers::{method, path, header}; #[test] fn user_scopes_are_checkable() { @@ -256,20 +257,17 @@ mod tests { reqwest::Client::new() } - fn request>, T: TryInto + std::fmt::Debug>( + fn request>>( auth: A, - endpoint: T, - ) -> Request<()> - where - >::Error: std::fmt::Debug, - { + endpoint: String, + ) -> Request<()> { let request = Request::builder().method(Method::GET); match auth.into() { Some(auth) => request.header("Authorization", auth), None => request, } - .extension(super::TokenEndpoint(endpoint.try_into().unwrap())) + .extension(super::TokenEndpoint(endpoint.parse().unwrap())) .extension(get_http_client()) .body(()) .unwrap() @@ -277,25 +275,21 @@ mod tests { #[tokio::test] async fn test_require_token_with_token() { - let server = MockServer::start_async().await; - server - .mock_async(|when, then| { - when.path("/token").header("Authorization", "Bearer token"); - - then.status(200) - .header("Content-Type", "application/json") - .json_body( - serde_json::to_value(User::new( - "https://fireburn.ru/", - "https://quill.p3k.io/", - "create update media", - )) - .unwrap(), - ); - }) + let server = MockServer::start().await; + + Mock::given(path("/token")) + .and(header("Authorization", "Bearer token")) + .respond_with(ResponseTemplate::new(200) + .set_body_json(User::new( + "https://fireburn.ru/", + "https://quill.p3k.io/", + "create update media", + )) + ) + .mount(&server) .await; - let request = request("Bearer token", server.url("/token").as_str()); + let request = request("Bearer token", format!("{}/token", &server.uri())); let mut parts = axum::extract::RequestParts::new(request); let user = User::from_request(&mut parts).await.unwrap(); @@ -304,17 +298,16 @@ mod tests { #[tokio::test] async fn test_require_token_fake_token() { - let server = MockServer::start_async().await; - server - .mock_async(|when, then| { - when.path("/refuse_token"); - - then.status(200) - .json_body(serde_json::json!({"active": false})); - }) + let server = MockServer::start().await; + + Mock::given(path("/refuse_token")) + .respond_with(ResponseTemplate::new(200) + .set_body_json(serde_json::json!({"active": false})) + ) + .mount(&server) .await; - let request = request("Bearer token", server.url("/refuse_token").as_str()); + let request = request("Bearer token", format!("{}/refuse_token", &server.uri())); let mut parts = axum::extract::RequestParts::new(request); let err = User::from_request(&mut parts).await.unwrap_err(); @@ -323,41 +316,39 @@ mod tests { #[tokio::test] async fn test_require_token_no_token() { - let server = MockServer::start_async().await; - let mock = server - .mock_async(|when, then| { - when.path("/should_never_be_called"); + let server = MockServer::start().await; - then.status(500); - }) + Mock::given(path("/should_never_be_called")) + .respond_with(ResponseTemplate::new(500)) + .expect(0) + .mount(&server) .await; - let request = request(None, server.url("/should_never_be_called").as_str()); + let request = request(None, format!("{}/should_never_be_called", &server.uri())); let mut parts = axum::extract::RequestParts::new(request); let err = User::from_request(&mut parts).await.unwrap_err(); assert_eq!(err.kind, super::ErrorKind::InvalidHeader); - - mock.assert_hits_async(0).await; } #[tokio::test] async fn test_require_token_400_error_unauthorized() { - let server = MockServer::start_async().await; - server - .mock_async(|when, then| { - when.path("/refuse_token_with_400"); - - then.status(400).json_body(serde_json::json!({ - "error": "unauthorized", - "error_description": "The token provided was malformed" - })); - }) + let server = MockServer::start().await; + + Mock::given(path("/refuse_token_with_400")) + .and(header("Authorization", "Bearer token")) + .respond_with(ResponseTemplate::new(400) + .set_body_json(serde_json::json!({ + "error": "unauthorized", + "error_description": "The token provided was malformed" + })) + ) + .mount(&server) .await; let request = request( "Bearer token", - server.url("/refuse_token_with_400").as_str(), + format!("{}/refuse_token_with_400", &server.uri()), ); let mut parts = axum::extract::RequestParts::new(request); let err = User::from_request(&mut parts).await.unwrap_err(); -- cgit 1.4.1