aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2019-06-12 18:38:45 +0200
committerHarald Eilertsen <haraldei@anduin.net>2019-06-12 18:40:36 +0200
commitbd7e83d326f3f96c6483b9668a59f261eedf9991 (patch)
tree0f62c9ddf709fe07f3826b393ebfe61eba7d0459 /tests
parent781cf76d54db703e19684251a64af17d87f127ef (diff)
downloadrust-zotapi-bd7e83d326f3f96c6483b9668a59f261eedf9991.tar.gz
rust-zotapi-bd7e83d326f3f96c6483b9668a59f261eedf9991.tar.bz2
rust-zotapi-bd7e83d326f3f96c6483b9668a59f261eedf9991.zip
Reduce duplication in tests.
Adds a default_mock method for most cases, and a client method that sets up the zotapi client with the proper test url and credentials.
Diffstat (limited to 'tests')
-rw-r--r--tests/zotapi.rs119
1 files changed, 35 insertions, 84 deletions
diff --git a/tests/zotapi.rs b/tests/zotapi.rs
index 1a81dc3..8c9d0b0 100644
--- a/tests/zotapi.rs
+++ b/tests/zotapi.rs
@@ -17,34 +17,34 @@
extern crate zotapi;
extern crate mockito;
-use mockito::{mock, Matcher};
+use mockito::{mock, Matcher, Mock};
-#[test]
-fn get_channel_stream() {
- let m = mock("GET", "/api/z/1.0/channel/stream")
+fn default_mock(method: &str, url: &str) -> Mock {
+ mock(method, url)
.match_header("Authorization", Matcher::Regex(r"Basic \w+".into()))
.with_status(200)
.with_header("content-type", "application/json")
.with_body("{}")
- .create();
+ .create()
+}
+
+fn client() -> zotapi::Client {
+ let listen_url = format!("http://{}", mockito::SERVER_ADDRESS);
+ zotapi::client(&listen_url, "testuser", "test1234")
+}
- let z = zotapi::client(&format!("http://{}", mockito::SERVER_ADDRESS), "testuser", "test1234");
- let data = z.channel_stream();
+#[test]
+fn get_channel_stream() {
+ let m = default_mock("GET", "/api/z/1.0/channel/stream");
+ let data = client().channel_stream();
m.assert();
assert_eq!(data.unwrap(), "{}");
}
#[test]
fn get_network_stream() {
- let m = mock("GET", "/api/z/1.0/network/stream")
- .match_header("Authorization", Matcher::Regex(r"Basic \w+".into()))
- .with_status(200)
- .with_header("content-type", "application/json")
- .with_body("{}")
- .create();
-
- let z = zotapi::client(&format!("http://{}", mockito::SERVER_ADDRESS), "testuser", "test1234");
- let data = z.network_stream();
+ let m = default_mock("GET", "/api/z/1.0/network/stream");
+ let data = client().network_stream();
m.assert();
assert_eq!(data.unwrap(), "{}");
}
@@ -57,8 +57,7 @@ fn return_error_if_invalid_auth_provided() {
.with_body("This api requires login")
.create();
- let z = zotapi::client(&format!("http://{}", mockito::SERVER_ADDRESS), "nouser", "wrongpassword");
- let data = z.channel_stream();
+ let data = client().channel_stream();
m.assert();
assert!(data.is_err());
assert_eq!(format!("{:?}", data), "Err(Unauthorized)");
@@ -74,8 +73,7 @@ fn create_new_post() {
.with_body("{}")
.create();
- let z = zotapi::client(&format!("http://{}", mockito::SERVER_ADDRESS), "testuser", "test1234");
- let _res = z.item().body("This is a test").create();
+ let _res = client().item().body("This is a test").create();
m.assert();
}
@@ -95,8 +93,7 @@ fn create_new_post_with_title() {
.with_body("{}")
.create();
- let z = zotapi::client(&format!("http://{}", mockito::SERVER_ADDRESS), "testuser", "test1234");
- let _res = z.item().title("A title").body("This is a test").create();
+ let _res = client().item().title("A title").body("This is a test").create();
m.assert();
}
@@ -123,8 +120,7 @@ fn upload_item_with_media_file() {
.with_body("{}")
.create();
- let z = zotapi::client(&format!("http://{}", mockito::SERVER_ADDRESS), "testuser", "test1234");
- let _res = z.item()
+ let _res = client().item()
.title("A title")
.body("This is a test")
.file("tests/fixtures/testfile.txt")
@@ -159,8 +155,7 @@ fn upload_item_with_two_files() {
.with_body("{}")
.create();
- let z = zotapi::client(&format!("http://{}", mockito::SERVER_ADDRESS), "testuser", "test1234");
- let _res = z.item()
+ let _res = client().item()
.title("A title")
.body("This is a test")
.file("tests/fixtures/testfile.txt")
@@ -172,85 +167,43 @@ fn upload_item_with_two_files() {
#[test]
fn fetch_xchan_by_address() {
- let m = mock("GET", "/api/z/1.0/xchan?address=test%40test.com")
- .match_header("Authorization", Matcher::Regex(r"Basic \w+".into()))
- .with_status(200)
- .with_header("content-type", "application/json")
- .with_body("{}")
- .create();
-
- let z = zotapi::client(&format!("http://{}", mockito::SERVER_ADDRESS), "testuser", "test1234");
- let _res = z.xchan().by_address("test@test.com").fetch().unwrap();
+ let m = default_mock("GET", "/api/z/1.0/xchan?address=test%40test.com");
+ let _res = client().xchan().by_address("test@test.com").fetch().unwrap();
m.assert();
}
#[test]
fn fetch_xchan_by_hash() {
- let m = mock("GET", "/api/z/1.0/xchan?hash=baffebaff")
- .match_header("Authorization", Matcher::Regex(r"Basic \w+".into()))
- .with_status(200)
- .with_header("content-type", "application/json")
- .with_body("{}")
- .create();
-
- let z = zotapi::client(&format!("http://{}", mockito::SERVER_ADDRESS), "testuser", "test1234");
- let _res = z.xchan().by_hash("baffebaff").fetch().unwrap();
+ let m = default_mock("GET", "/api/z/1.0/xchan?hash=baffebaff");
+ let _res = client().xchan().by_hash("baffebaff").fetch().unwrap();
m.assert();
}
#[test]
fn fetch_xchan_by_guid() {
- let m = mock("GET", "/api/z/1.0/xchan?guid=baffebaff-baff-baff")
- .match_header("Authorization", Matcher::Regex(r"Basic \w+".into()))
- .with_status(200)
- .with_header("content-type", "application/json")
- .with_body("{}")
- .create();
-
- let z = zotapi::client(&format!("http://{}", mockito::SERVER_ADDRESS), "testuser", "test1234");
- let _res = z.xchan().by_guid("baffebaff-baff-baff").fetch().unwrap();
+ let m = default_mock("GET", "/api/z/1.0/xchan?guid=baffebaff-baff-baff");
+ let _res = client().xchan().by_guid("baffebaff-baff-baff").fetch().unwrap();
m.assert();
}
#[test]
fn fetch_connections() {
- let m = mock("GET", "/api/z/1.0/abook")
- .match_header("Authorization", Matcher::Regex(r"Basic \w+".into()))
- .with_status(200)
- .with_header("content-type", "application/json")
- .with_body("{}")
- .create();
-
- let z = zotapi::client(&format!("http://{}", mockito::SERVER_ADDRESS), "testuser", "test1234");
- let _res = z.abook().fetch().unwrap();
+ let m = default_mock("GET", "/api/z/1.0/abook");
+ let _res = client().abook().fetch().unwrap();
m.assert();
}
#[test]
fn fetch_abconfig() {
- let m = mock("GET", "/api/z/1.0/abconfig")
- .match_header("Authorization", Matcher::Regex(r"Basic \w+".into()))
- .with_status(200)
- .with_header("content-type", "application/json")
- .with_body("{}")
- .create();
-
- let z = zotapi::client(&format!("http://{}", mockito::SERVER_ADDRESS), "testuser", "test1234");
- let _res = z.abconfig().fetch().unwrap();
+ let m = default_mock("GET", "/api/z/1.0/abconfig");
+ let _res = client().abconfig().fetch().unwrap();
m.assert();
}
#[test]
fn fetch_privacy_groups() {
- let m = mock("GET", "/api/z/1.0/group")
- .match_header("Authorization", Matcher::Regex(r"Basic \w+".into()))
- .with_status(200)
- .with_header("content-type", "application/json")
- .with_body("{}")
- .create();
-
- let z = zotapi::client(&format!("http://{}", mockito::SERVER_ADDRESS), "testuser", "test1234");
- let _res = z.group().fetch().unwrap();
+ let m = default_mock("GET", "/api/z/1.0/group");
+ let _res = client().group().fetch().unwrap();
m.assert();
}
@@ -264,8 +217,7 @@ fn fetch_members_of_group_by_group_id() {
.with_body("{}")
.create();
- let z = zotapi::client(&format!("http://{}", mockito::SERVER_ADDRESS), "testuser", "test1234");
- let _res = z.group_members().by_group_id(42).fetch().unwrap();
+ let _res = client().group_members().by_group_id(42).fetch().unwrap();
m.assert();
}
@@ -279,7 +231,6 @@ fn fetch_members_of_group_by_group_name() {
.with_body("{}")
.create();
- let z = zotapi::client(&format!("http://{}", mockito::SERVER_ADDRESS), "testuser", "test1234");
- let _res = z.group_members().by_group_name("Friends of pain").fetch().unwrap();
+ let _res = client().group_members().by_group_name("Friends of pain").fetch().unwrap();
m.assert();
}