Skip to content
This repository has been archived by the owner on Jun 21, 2024. It is now read-only.

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xvello committed May 6, 2024
1 parent e040018 commit 2e62a76
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
50 changes: 50 additions & 0 deletions hook-worker/src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,53 @@ impl Resolve for PublicIPv4Resolver {
Box::pin(future_result)
}
}

mod tests {
use super::*;
use std::str::FromStr;

#[tokio::test]
async fn it_resolves_google_com() {
let resolver: PublicIPv4Resolver = PublicIPv4Resolver {};
let addrs = resolver
.resolve(Name::from_str("google.com").unwrap())
.await
.expect("lookup has failed");
assert!(addrs.count() > 0, "empty address list")
}

#[tokio::test]
async fn it_denies_ipv6_google_com() {
let resolver: PublicIPv4Resolver = PublicIPv4Resolver {};
match resolver
.resolve(Name::from_str("ipv6.google.com").unwrap())
.await
{
Ok(_) => panic!("should have failed"),
Err(err) => assert!(err.downcast_ref::<NoPublicIPError>().is_some()),
}
}

#[tokio::test]
async fn it_denies_localhost() {
let resolver: PublicIPv4Resolver = PublicIPv4Resolver {};
match resolver.resolve(Name::from_str("localhost").unwrap()).await {
Ok(_) => panic!("should have failed"),
Err(err) => assert!(err.is::<NoPublicIPError>()),
}
}

#[tokio::test]
async fn it_propagates_unknown_domain() {
let resolver: PublicIPv4Resolver = PublicIPv4Resolver {};
match resolver
.resolve(Name::from_str("invalid.domain.unknown").unwrap())
.await
{
Ok(_) => panic!("should have failed"),
Err(err) => assert!(err
.to_string()
.contains("failed to lookup address information")),
}
}
}
5 changes: 3 additions & 2 deletions hook-worker/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl fmt::Display for WebhookRequestError {
None => "No response from the server".to_string(),
};
if is_error_source::<NoPublicIPError>(error) {
writeln!(f, "{}: {}", error ,NoPublicIPError)?;
writeln!(f, "{}: {}", error, NoPublicIPError)?;
} else {
writeln!(f, "{}", error)?;
}
Expand Down Expand Up @@ -140,8 +140,9 @@ pub enum WorkerError {
}

/// Check the error and it's sources (recursively) to return true if an error of the given type is found.
/// TODO: use Error::sources() when stable
pub fn is_error_source<T: Error + 'static>(err: &(dyn std::error::Error + 'static)) -> bool {
if err.downcast_ref::<T>().is_some() {
if err.is::<NoPublicIPError>() {
return true;
}
match err.source() {
Expand Down
4 changes: 2 additions & 2 deletions hook-worker/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,8 @@ mod tests {
webhook_job_parameters.clone(),
webhook_job_metadata,
)
.await
.expect("failed to enqueue job");
.await
.expect("failed to enqueue job");
let worker = WebhookWorker::new(
&worker_id,
&queue,
Expand Down

0 comments on commit 2e62a76

Please sign in to comment.