about summary refs log tree commit diff
path: root/kittybox-rs/src/tokenauth.rs
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2022-08-13 21:54:53 +0300
committerVika <vika@fireburn.ru>2022-08-13 21:54:53 +0300
commit3de3fc65d131fc84512fcd4b1f25f64a0860e6db (patch)
treec945a7c2e6f8f2a2884f082f1af8c63f9ba9dd92 /kittybox-rs/src/tokenauth.rs
parentfde3e415b3412984d33281b28710e0c2b0c9b137 (diff)
downloadkittybox-3de3fc65d131fc84512fcd4b1f25f64a0860e6db.tar.zst
tokenauth: migrate tests to Wiremock
Wiremock doesn't require external C dependencies.
Diffstat (limited to 'kittybox-rs/src/tokenauth.rs')
-rw-r--r--kittybox-rs/src/tokenauth.rs99
1 files changed, 45 insertions, 54 deletions
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<A: Into<Option<&'static str>>, T: TryInto<url::Url> + std::fmt::Debug>(
+    fn request<A: Into<Option<&'static str>>>(
         auth: A,
-        endpoint: T,
-    ) -> Request<()>
-    where
-        <T as std::convert::TryInto<url::Url>>::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();