use std::vec;
use serde_json::{Map, Value};
use crate::types::{Services, Software, Usage, Users};
use super::*;
fn deserialize_unwrap_ok(json_str: &str) -> NodeInfo {
let result = deserialize(json_str);
result.unwrap()
}
#[test]
fn test_deserialize_supported_version() {
let result = deserialize_unwrap_ok(
r#"{
"version": "2.1",
"software": { "name": "foo", "version": "1.2.3" },
"protocols": ["diaspora"],
"services": { "inbound": ["imap"], "outbound": ["smtp"] },
"openRegistrations": false,
"usage": {
"users": { "total": 42, "activeMonth": 21, "activeHalfyear": 32 },
"localPosts": 420,
"localComments": 69
},
"metadata": { "foo": "bar", "baz": 42 }
}"#,
);
let mut metadata = Map::with_capacity(2);
metadata.insert("foo".to_string(), Value::String("bar".to_string()));
metadata.insert("baz".to_string(), Value::Number(42.into()));
let expected = NodeInfo {
version: "2.1",
software: Software {
name: "foo",
version: Some("1.2.3"),
repository: None,
homepage: None,
},
protocols: vec!["diaspora"],
services: Services {
inbound: vec!["imap"],
outbound: vec!["smtp"],
},
open_registrations: false,
usage: Usage {
users: Some(Users {
total: Some(42),
active_halfyear: Some(32),
active_month: Some(21),
}),
local_posts: Some(420),
local_comments: Some(69),
},
metadata,
};
assert_eq!(expected, result);
}
#[test]
fn test_deserialize_owned() {
let result = deserialize_owned(
r#"{
"version": "2.1",
"software": { "name": "foo", "version": "1.2.3" },
"protocols": ["diaspora"],
"services": { "inbound": ["imap"], "outbound": ["smtp"] },
"openRegistrations": false,
"usage": {
"users": { "total": 42, "activeMonth": 21, "activeHalfyear": 32 },
"localPosts": 420,
"localComments": 69
},
"metadata": { "foo": "bar", "baz": 42 }
}"#,
)
.unwrap();
let mut metadata = Map::with_capacity(2);
metadata.insert("foo".to_string(), Value::String("bar".to_string()));
metadata.insert("baz".to_string(), Value::Number(42.into()));
let expected = NodeInfoOwned {
version: "2.1".to_string(),
software: Software {
name: "foo".to_string(),
version: Some("1.2.3".to_string()),
repository: None,
homepage: None,
},
protocols: vec!["diaspora".to_string()],
services: Services {
inbound: vec!["imap".to_string()],
outbound: vec!["smtp".to_string()],
},
open_registrations: false,
usage: Usage {
users: Some(Users {
total: Some(42),
active_halfyear: Some(32),
active_month: Some(21),
}),
local_posts: Some(420),
local_comments: Some(69),
},
metadata,
};
assert_eq!(expected, result);
}
#[test]
fn test_deserialize_legacy_version() {
let result = deserialize_unwrap_ok(
r#"{
"version": "1.0",
"software": { "name": "diaspora", "version": "1.2.3" },
"protocols": { "inbound": ["diaspora", "rss"], "outbound": ["diaspora"] }
}"#,
);
let expected = NodeInfo {
version: "1.0",
software: Software {
name: "diaspora",
version: Some("1.2.3"),
repository: None,
homepage: None,
},
protocols: vec!["diaspora", "rss"],
services: Services::default(),
open_registrations: false,
usage: Usage::default(),
metadata: Map::new(),
};
assert_eq!(expected, result);
}
#[test]
fn test_deserialize_unsupported_version() {
let result = deserialize(r#"{ "version": "0.1" }"#);
match result.unwrap_err() {
NodeInfoError::UnsupportedVersion => (),
_ => panic!("wrong error type"),
}
}
#[test]
fn test_diaspora() {
let result = deserialize_unwrap_ok(
r#"{
"version": "2.1",
"software": {
"name": "diaspora",
"version": "1.0.0-dev-p5af17a0f",
"repository": "https://github.com/diaspora/diaspora",
"homepage": "https://diasporafoundation.org/"
},
"protocols": [
"diaspora"
],
"services": {
"inbound": [],
"outbound": []
},
"openRegistrations": false,
"usage": {
"users": {
"total": 316126,
"activeHalfyear": 346,
"activeMonth": 72
},
"localPosts": 2942556,
"localComments": 1387004
},
"metadata": {
"nodeName": "JoinDiaspora*",
"camo": {
"markdown": false,
"opengraph": false,
"remotePods": false
},
"adminAccount": "podmin"
}
}"#,
);
let mut camo = Map::with_capacity(3);
camo.insert("markdown".to_string(), Value::Bool(false));
camo.insert("opengraph".to_string(), Value::Bool(false));
camo.insert("remotePods".to_string(), Value::Bool(false));
let mut metadata = Map::with_capacity(3);
metadata.insert("nodeName".to_string(), Value::String("JoinDiaspora*".to_string()));
metadata.insert("camo".to_string(), Value::Object(camo));
metadata.insert("adminAccount".to_string(), Value::String("podmin".to_string()));
let expected = NodeInfo {
version: "2.1",
software: Software {
name: "diaspora",
version: Some("1.0.0-dev-p5af17a0f"),
repository: Some("https://github.com/diaspora/diaspora"),
homepage: Some("https://diasporafoundation.org/"),
},
protocols: vec!["diaspora"],
services: Services::default(),
open_registrations: false,
usage: Usage {
users: Some(Users {
total: Some(316126),
active_halfyear: Some(346),
active_month: Some(72),
}),
local_posts: Some(2942556),
local_comments: Some(1387004),
},
metadata,
};
assert_eq!(expected, result);
}
#[test]
fn test_mastodon() {
let result = deserialize_unwrap_ok(
r#"{
"version": "2.0",
"software": { "name": "mastodon", "version": "4.1.6" },
"protocols": ["activitypub"],
"services": { "outbound": [], "inbound": [] },
"usage": {
"users": { "total": 11231, "activeMonth": 6060, "activeHalfyear": 7214 },
"localPosts": 4793200
},
"openRegistrations": false,
"metadata": {}
}"#,
);
let expected = NodeInfo {
version: "2.0",
software: Software {
name: "mastodon",
version: Some("4.1.6"),
repository: None,
homepage: None,
},
protocols: vec!["activitypub"],
services: Services {
inbound: Vec::new(),
outbound: Vec::new(),
},
open_registrations: false,
usage: Usage {
users: Some(Users {
total: Some(11231),
active_halfyear: Some(7214),
active_month: Some(6060),
}),
local_posts: Some(4793200),
local_comments: None,
},
metadata: Map::new(),
};
assert_eq!(expected, result);
}
#[test]
fn test_lemmy() {
let result = deserialize_unwrap_ok(
r#"{
"version": "2.0",
"software": {
"name": "lemmy",
"version": "0.18.4"
},
"protocols": [
"activitypub"
],
"usage": {
"users": {
"total": 129817,
"activeHalfyear": 38805,
"activeMonth": 19056
},
"localPosts": 133197,
"localComments": 793699
},
"openRegistrations": true
}"#,
);
let expected = NodeInfo {
version: "2.0",
software: Software {
name: "lemmy",
version: Some("0.18.4"),
repository: None,
homepage: None,
},
protocols: vec!["activitypub"],
services: Services::default(),
open_registrations: true,
usage: Usage {
users: Some(Users {
total: Some(129817),
active_halfyear: Some(38805),
active_month: Some(19056),
}),
local_posts: Some(133197),
local_comments: Some(793699),
},
metadata: Map::new(),
};
assert_eq!(expected, result);
}
#[test]
fn test_pixelfed() {
let result = deserialize_unwrap_ok(
r#"{
"protocols": [
"activitypub"
],
"services": {
"inbound": [],
"outbound": []
},
"software": {
"name": "pixelfed",
"version": "0.11.9"
},
"usage": {
"localPosts": 7125175,
"localComments": 0,
"users": {
"total": 93703,
"activeHalfyear": 28300,
"activeMonth": 9588
}
},
"version": "2.0",
"openRegistrations": true
}"#,
);
let expected = NodeInfo {
version: "2.0",
software: Software {
name: "pixelfed",
version: Some("0.11.9"),
repository: None,
homepage: None,
},
protocols: vec!["activitypub"],
services: Services::default(),
open_registrations: true,
usage: Usage {
users: Some(Users {
total: Some(93703),
active_halfyear: Some(28300),
active_month: Some(9588),
}),
local_posts: Some(7125175),
local_comments: Some(0),
},
metadata: Map::new(),
};
assert_eq!(expected, result);
}
#[test]
fn test_peertube() {
let result = deserialize_unwrap_ok(
r#"{
"version": "2.0",
"software": {
"name": "peertube",
"version": "3.0.0"
},
"protocols": [
"activitypub"
],
"services": {
"inbound": [],
"outbound": [
"atom1.0",
"rss2.0"
]
},
"openRegistrations": true,
"usage": {
"users": {
"total": 18321,
"activeMonth": 633,
"activeHalfyear": 2492
},
"localPosts": 307652,
"localComments": 609
}
}"#,
);
let expected = NodeInfo {
version: "2.0",
software: Software {
name: "peertube",
version: Some("3.0.0"),
repository: None,
homepage: None,
},
protocols: vec!["activitypub"],
services: Services {
inbound: Vec::new(),
outbound: vec!["atom1.0", "rss2.0"],
},
open_registrations: true,
usage: Usage {
users: Some(Users {
total: Some(18321),
active_halfyear: Some(2492),
active_month: Some(633),
}),
local_posts: Some(307652),
local_comments: Some(609),
},
metadata: Map::new(),
};
assert_eq!(expected, result);
}
#[test]
fn test_socialhome() {
let result = deserialize_unwrap_ok(
r#"{
"version": "1.0",
"software": {
"name": "socialhome",
"version": "0.18.0-dev"
},
"protocols": {
"inbound": [
"diaspora"
],
"outbound": [
"diaspora"
]
},
"services": {
"inbound": [],
"outbound": []
},
"openRegistrations": true,
"usage": {
"users": {
"total": 1624,
"activeHalfyear": 133,
"activeMonth": 30
},
"localPosts": 34189,
"localComments": 1508
},
"metadata": {
"nodeName": "Socialhome"
}
}"#,
);
let mut metadata = Map::new();
metadata.insert("nodeName".to_string(), Value::String("Socialhome".to_string()));
let expected = NodeInfo {
version: "1.0",
software: Software {
name: "socialhome",
version: Some("0.18.0-dev"),
repository: None,
homepage: None,
},
protocols: vec!["diaspora"],
services: Services {
inbound: Vec::new(),
outbound: Vec::new(),
},
open_registrations: true,
usage: Usage {
users: Some(Users {
total: Some(1624),
active_halfyear: Some(133),
active_month: Some(30),
}),
local_posts: Some(34189),
local_comments: Some(1508),
},
metadata,
};
assert_eq!(expected, result);
}