Browse Source

add tests for AsyncResolver; use cargo clippy for further guidance.

pull/11/head
Mina Galić 11 months ago
parent
commit
bd0bff44bf
Signed by: igalic <me+github@igalic.co> GPG Key ID: ACFEFF7F6A123A86
2 changed files with 99 additions and 15 deletions
  1. +10
    -12
      src/lib.rs
  2. +89
    -3
      src/tests.rs

+ 10
- 12
src/lib.rs View File

@@ -5,13 +5,13 @@
use reqwest::{header::ACCEPT, Client};
use serde::{Deserialize, Serialize};

mod sync_trait;
pub use crate::sync_trait::*;
mod resolver;
pub use crate::resolver::*;

#[cfg(feature = "async")]
mod async_trait;
mod async_resolver;
#[cfg(feature = "async")]
pub use crate::async_trait::*;
pub use crate::async_resolver::*;

#[cfg(test)]
mod tests;
@@ -93,7 +93,7 @@ impl Into<String> for Prefix {
match self {
Prefix::Acct => "acct".into(),
Prefix::Group => "group".into(),
Prefix::Custom(x) => x.clone(),
Prefix::Custom(x) => x,
}
}
}
@@ -115,7 +115,7 @@ pub fn url_for(
let scheme = if with_https { "https" } else { "http" };

let prefix: String = prefix.into();
acct.split("@")
acct.split('@')
.nth(1)
.ok_or(WebfingerError::ParseError)
.map(|instance| {
@@ -158,13 +158,11 @@ pub async fn resolve(
if first.contains('@') {
// This : was a port number, not a prefix
resolve_with_prefix(Prefix::Acct, acct, with_https).await
} else if let Some(other) = parsed.next() {
resolve_with_prefix(Prefix::from(first), other, with_https).await
} else {
if let Some(other) = parsed.next() {
resolve_with_prefix(Prefix::from(first), other, with_https).await
} else {
// fallback to acct:
resolve_with_prefix(Prefix::Acct, first, with_https).await
}
// fallback to acct:
resolve_with_prefix(Prefix::Acct, first, with_https).await
}
}



+ 89
- 3
src/tests.rs View File

@@ -1,5 +1,4 @@
use super::*;
use serde_json;
use tokio::runtime::Runtime;

#[test]
@@ -162,7 +161,43 @@ impl Resolver<&'static str> for MyResolver {
acct: String,
resource_repo: &'static str,
) -> Result<Webfinger, ResolverError> {
if acct == resource_repo.to_string() && prefix == Prefix::Acct {
if acct == resource_repo && prefix == Prefix::Acct {
Ok(Webfinger {
subject: acct.clone(),
aliases: vec![acct.clone()],
links: vec![Link {
rel: "http://webfinger.net/rel/profile-page".to_string(),
mime_type: None,
href: Some(format!("https://instance.tld/@{}/", acct)),
template: None,
}],
})
} else {
Err(ResolverError::NotFound)
}
}
}

#[cfg(feature = "async")]
pub struct MyAsyncResolver;

// Only one user, represented by a String
#[cfg(feature = "async")]
#[async_trait::async_trait]
impl AsyncResolver for MyAsyncResolver {
type Repo = &'static str;

async fn instance_domain<'a>(&self) -> &'a str {
"instance.tld"
}

async fn find(
&self,
prefix: Prefix,
acct: String,
resource_repo: &'static str,
) -> Result<Webfinger, ResolverError> {
if acct == resource_repo && prefix == Prefix::Acct {
Ok(Webfinger {
subject: acct.clone(),
aliases: vec![acct.clone()],
@@ -182,7 +217,9 @@ impl Resolver<&'static str> for MyResolver {
#[test]
fn test_my_resolver() {
let resolver = MyResolver;
assert!(resolver.endpoint("acct:admin@instance.tld", "admin").is_ok());
assert!(resolver
.endpoint("acct:admin@instance.tld", "admin")
.is_ok());
assert_eq!(
resolver.endpoint("acct:test@instance.tld", "admin"),
Err(ResolverError::NotFound)
@@ -208,3 +245,52 @@ fn test_my_resolver() {
Err(ResolverError::NotFound)
);
}

#[test]
#[cfg(feature = "async")]
fn test_my_async_resolver() {
let resolver = MyAsyncResolver;
let mut r = Runtime::new().unwrap();
r.block_on(async {
assert!(resolver
.endpoint("acct:admin@instance.tld", "admin")
.await
.is_ok());
});
r.block_on(async {
assert_eq!(
resolver.endpoint("acct:test@instance.tld", "admin").await,
Err(ResolverError::NotFound)
);
});
r.block_on(async {
assert_eq!(
resolver.endpoint("acct:admin@oops.ie", "admin").await,
Err(ResolverError::WrongDomain)
);
});
r.block_on(async {
assert_eq!(
resolver.endpoint("admin@instance.tld", "admin").await,
Err(ResolverError::InvalidResource)
);
});
r.block_on(async {
assert_eq!(
resolver.endpoint("admin", "admin").await,
Err(ResolverError::InvalidResource)
);
});
r.block_on(async {
assert_eq!(
resolver.endpoint("acct:admin", "admin").await,
Err(ResolverError::InvalidResource)
);
});
r.block_on(async {
assert_eq!(
resolver.endpoint("group:admin@instance.tld", "admin").await,
Err(ResolverError::NotFound)
);
});
}

Loading…
Cancel
Save