From 8b4b4ccd068d8677fd99ec2e366582102fd4177c Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 1 Jan 2020 14:34:14 +0100 Subject: tests: Verify authorization properly. --- Cargo.toml | 1 + tests/zotapi.rs | 29 ++++++++++++++--------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 77f1fb9..8d1c4c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ serde = "1.0" serde_urlencoded = "0.5.1" [dev-dependencies] +base64 = "0.11.0" clap = "2.33.0" dotenv = "0.13" mockito = "0.19" diff --git a/tests/zotapi.rs b/tests/zotapi.rs index 38bdf8a..4bac884 100644 --- a/tests/zotapi.rs +++ b/tests/zotapi.rs @@ -19,9 +19,14 @@ extern crate mockito; use mockito::{mock, Matcher, Mock}; -fn default_mock(method: &str, url: &str) -> Mock { +fn mock_with_authorization(method: &str, url: &str) -> Mock { mock(method, url) - .match_header("Authorization", Matcher::Regex(r"Basic \w+".into())) + .match_header("Authorization", + Matcher::Exact(format!("Basic {}", base64::encode("testuser:test1234")))) +} + +fn default_mock(method: &str, url: &str) -> Mock { + mock_with_authorization(method, url) .with_status(200) .with_header("content-type", "application/json") .with_body("{}") @@ -64,8 +69,7 @@ fn return_error_if_invalid_auth_provided() { #[test] fn create_new_post() { - let m = mock("POST", "/api/z/1.0/item/update") - .match_header("Authorization", Matcher::Regex(r"Basic \w+".into())) + let m = mock_with_authorization("POST", "/api/z/1.0/item/update") .match_body("body=This+is+a+test") .with_status(200) .with_header("content-type", "application/json") @@ -79,8 +83,7 @@ fn create_new_post() { #[test] fn create_new_post_with_title() { - let m = mock("POST", "/api/z/1.0/item/update") - .match_header("Authorization", Matcher::Regex(r"Basic \w+".into())) + let m = mock_with_authorization("POST", "/api/z/1.0/item/update") .match_body( Matcher::AllOf(vec![ Matcher::UrlEncoded("title".to_string(), "A+title".to_string()), @@ -99,8 +102,7 @@ fn create_new_post_with_title() { #[test] fn upload_item_with_media_file() { - let m = mock("POST", "/api/z/1.0/item/update") - .match_header("authorization", Matcher::Regex(r"Basic \w+=".into())) + let m = mock_with_authorization("POST", "/api/z/1.0/item/update") .match_header("content-type", Matcher::Regex("^multipart/form-data; boundary=.+$".into())) .match_body(Matcher::Regex( "--.+\r\n".to_owned() + @@ -130,9 +132,8 @@ fn upload_item_with_media_file() { #[test] fn upload_item_with_two_files() { - let m = mock("POST", "/api/z/1.0/item/update") - .match_header("authorization", Matcher::Regex(r"Basic \w+=".into())) - .match_header("content-type", Matcher::Regex("^multipart/form-data; boundary=.+$".into())) + let m = mock_with_authorization("POST", "/api/z/1.0/item/update") + .match_header("content-type", Matcher::Regex("multipart/form-data; boundary=.+".into())) .match_body(Matcher::Regex( "--.+\r\n".to_owned() + "Content-Disposition: form-data; name=\"body\"\r\n" + @@ -208,9 +209,8 @@ fn fetch_privacy_groups() { #[test] fn fetch_members_of_group_by_group_id() { - let m = mock("GET", "/api/z/1.0/group_members") + let m = mock_with_authorization("GET", "/api/z/1.0/group_members") .match_query(Matcher::UrlEncoded("group_id".to_string(), "42".to_string())) - .match_header("Authorization", Matcher::Regex(r"Basic \w+".into())) .with_status(200) .with_header("content-type", "application/json") .with_body("{}") @@ -222,9 +222,8 @@ fn fetch_members_of_group_by_group_id() { #[test] fn fetch_members_of_group_by_group_name() { - let m = mock("GET", "/api/z/1.0/group_members") + let m = mock_with_authorization("GET", "/api/z/1.0/group_members") .match_query(Matcher::UrlEncoded("group_name".into(), "Friends+of+pain".into())) - .match_header("Authorization", Matcher::Regex(r"Basic \w+".into())) .with_status(200) .with_header("content-type", "application/json") .with_body("{}") -- cgit v1.2.3