Add webfinger test

This commit is contained in:
silverpill 2023-01-12 21:38:36 +00:00
parent 6d4a6806f4
commit 48de6218eb
4 changed files with 49 additions and 23 deletions

View file

@ -340,7 +340,6 @@ pub fn get_instance_actor(
#[cfg(test)]
mod tests {
use url::Url;
use crate::models::profiles::types::DbActorProfile;
use crate::utils::crypto_rsa::{
generate_weak_rsa_key,
@ -403,9 +402,8 @@ mod tests {
#[test]
fn test_instance_actor() {
let instance_url = Url::parse("https://example.com/").unwrap();
let instance_rsa_key = generate_weak_rsa_key().unwrap();
let instance = Instance::new(instance_url, instance_rsa_key);
let instance_url = "https://example.com/";
let instance = Instance::for_test(instance_url);
let actor = get_instance_actor(&instance).unwrap();
assert_eq!(actor.id, "https://example.com/actor");
assert_eq!(actor.object_type, "Service");

View file

@ -171,17 +171,6 @@ pub struct Instance {
}
impl Instance {
#[cfg(test)]
pub fn new(url: Url, actor_key: RsaPrivateKey) -> Self {
Self {
_url: url,
_version: "0.0.0".to_string(),
actor_key,
proxy_url: None,
is_private: true,
}
}
pub fn url(&self) -> String {
self._url.origin().ascii_serialization()
}
@ -207,6 +196,20 @@ impl Instance {
}
}
#[cfg(test)]
impl Instance {
pub fn for_test(url: &str) -> Self {
use crate::utils::crypto_rsa::generate_weak_rsa_key;
Self {
_url: Url::parse(url).unwrap(),
_version: "0.0.0".to_string(),
actor_key: generate_weak_rsa_key().unwrap(),
proxy_url: None,
is_private: true,
}
}
}
extern "C" {
fn geteuid() -> u32;
}

View file

@ -152,7 +152,7 @@ async fn main() -> std::io::Result<()> {
.service(status_api_scope())
.service(subscription_api_scope())
.service(timeline_api_scope())
.service(webfinger::get_descriptor)
.service(webfinger::webfinger_view)
.service(activitypub::actor_scope())
.service(activitypub::instance_actor_scope())
.service(activitypub::object_view)

View file

@ -26,12 +26,12 @@ fn parse_acct_uri(uri: &str) -> Result<ActorAddress, ValidationError> {
Ok(actor_address)
}
async fn get_user_info(
async fn get_jrd(
db_client: &impl GenericClient,
instance: Instance,
query_params: WebfingerQueryParams,
resource: &str,
) -> Result<JsonResourceDescriptor, HttpError> {
let actor_address = parse_acct_uri(&query_params.resource)?;
let actor_address = parse_acct_uri(resource)?;
if actor_address.hostname != instance.hostname() {
// Wrong instance
return Err(HttpError::NotFoundError("user"));
@ -50,23 +50,23 @@ async fn get_user_info(
href: Some(actor_url),
};
let jrd = JsonResourceDescriptor {
subject: query_params.resource,
subject: resource.to_string(),
links: vec![link],
};
Ok(jrd)
}
#[get("/.well-known/webfinger")]
pub async fn get_descriptor(
pub async fn webfinger_view(
config: web::Data<Config>,
db_pool: web::Data<DbPool>,
query_params: web::Query<WebfingerQueryParams>,
) -> Result<HttpResponse, HttpError> {
let db_client = &**get_database_client(&db_pool).await?;
let jrd = get_user_info(
let jrd = get_jrd(
db_client,
config.instance(),
query_params.into_inner(),
&query_params.resource,
).await?;
let response = HttpResponse::Ok()
.content_type(JRD_CONTENT_TYPE)
@ -76,6 +76,12 @@ pub async fn get_descriptor(
#[cfg(test)]
mod tests {
use serial_test::serial;
use crate::database::test_utils::create_test_database;
use crate::models::users::{
queries::create_user,
types::UserCreateData,
};
use super::*;
#[test]
@ -85,4 +91,23 @@ mod tests {
assert_eq!(actor_address.username, "user_1");
assert_eq!(actor_address.hostname, "example.com");
}
#[tokio::test]
#[serial]
async fn test_get_jrd() {
let db_client = &mut create_test_database().await;
let instance = Instance::for_test("https://example.com");
let user_data = UserCreateData {
username: "test".to_string(),
..Default::default()
};
create_user(db_client, user_data).await.unwrap();
let resource = "acct:test@example.com";
let jrd = get_jrd(db_client, instance, resource).await.unwrap();
assert_eq!(jrd.subject, resource);
assert_eq!(
jrd.links[0].href.as_ref().unwrap(),
"https://example.com/users/test",
);
}
}