1 extern crate lemmy_server;
5 kind::{CreateType, FollowType},
8 base::{BaseExt, ExtendsExt},
9 object::{Note, ObjectExt},
11 use actix::prelude::*;
12 use actix_web::{test::TestRequest, web, web::Path, HttpRequest};
15 r2d2::{ConnectionManager, Pool},
18 use http_signature_normalization_actix::PrepareVerifyError;
19 use lemmy_api::match_websocket_operation;
21 activity_queue::create_activity_queue,
24 community_inbox::community_inbox,
26 shared_inbox::shared_inbox,
28 user_inbox::user_inbox,
33 community::{Community, CommunityForm},
40 use lemmy_rate_limit::{rate_limiter::RateLimiter, RateLimit};
41 use lemmy_utils::{apub::generate_actor_keypair, settings::Settings};
42 use lemmy_websocket::{chat_server::ChatServer, LemmyContext};
44 use serde::{Deserialize, Serialize};
46 use tokio::sync::Mutex;
49 fn create_context() -> LemmyContext {
50 let settings = Settings::get();
51 let db_url = settings.get_database_url();
52 let manager = ConnectionManager::<PgConnection>::new(&db_url);
53 let pool = Pool::builder()
54 .max_size(settings.database.pool_size)
57 let rate_limiter = RateLimit {
58 rate_limiter: Arc::new(Mutex::new(RateLimiter::default())),
60 let activity_queue = create_activity_queue();
61 let chat_server = ChatServer::startup(
64 |c, i, o, d| Box::pin(match_websocket_operation(c, i, o, d)),
66 activity_queue.clone(),
73 create_activity_queue(),
77 fn create_user(conn: &PgConnection, name: &str) -> User_ {
78 let user_keypair = generate_actor_keypair().unwrap();
79 let new_user = UserForm {
81 preferred_username: None,
82 password_encrypted: "nope".into(),
92 theme: "browser".into(),
93 default_sort_type: SortType::Hot as i16,
94 default_listing_type: ListingType::Subscribed as i16,
95 lang: "browser".into(),
97 send_notifications_to_email: false,
98 actor_id: Some(format!("http://localhost:8536/u/{}", name).to_string()),
101 private_key: Some(user_keypair.private_key),
102 public_key: Some(user_keypair.public_key),
103 last_refreshed_at: None,
106 User_::create(&conn, &new_user).unwrap()
109 fn create_community(conn: &PgConnection, creator_id: i32) -> Community {
110 let new_community = CommunityForm {
111 name: "test_community".into(),
113 title: "test_community".to_owned(),
124 last_refreshed_at: None,
129 Community::create(&conn, &new_community).unwrap()
131 fn create_activity<'a, Activity, Return>(user_id: String) -> web::Json<Return>
133 for<'de> Return: Deserialize<'de> + 'a,
134 Activity: std::default::Default + Serialize,
136 let mut activity = ActorAndObject::<Activity>::new(user_id, Note::new().into_any_base().unwrap());
138 .set_id(Url::parse("http://localhost:8536/create/1").unwrap())
139 .set_many_ccs(vec![Url::parse("http://localhost:8536/c/main").unwrap()]);
140 let activity = serde_json::to_value(&activity).unwrap();
141 let activity: Return = serde_json::from_value(activity).unwrap();
145 fn create_http_request() -> HttpRequest {
146 let time1 = Utc::now().timestamp();
147 let time2 = Utc::now().timestamp();
148 let signature = format!(
149 r#"keyId="my-key-id",algorithm="hs2019",created="{}",expires="{}",headers="(request-target) (created) (expires) date content-type",signature="blah blah blah""#,
153 .uri("http://localhost:8536/")
154 .header("Signature", signature)
160 async fn test_shared_inbox_expired_signature() {
161 let request = create_http_request();
162 let context = create_context();
163 let connection = &context.pool().get().unwrap();
164 let user = create_user(connection, "shared_inbox_rvgfd");
166 create_activity::<CreateType, ActorAndObject<shared_inbox::ValidTypes>>(user.actor_id);
167 let response = shared_inbox(request, activity, web::Data::new(context)).await;
169 format!("{}", response.err().unwrap()),
170 format!("{}", PrepareVerifyError::Expired)
172 User_::delete(connection, user.id).unwrap();
177 async fn test_user_inbox_expired_signature() {
178 let request = create_http_request();
179 let context = create_context();
180 let connection = &context.pool().get().unwrap();
181 let user = create_user(connection, "user_inbox_cgsax");
183 create_activity::<CreateType, ActorAndObject<user_inbox::UserValidTypes>>(user.actor_id);
184 let path = Path::<String> {
185 0: "username".to_string(),
187 let response = user_inbox(request, activity, path, web::Data::new(context)).await;
189 format!("{}", response.err().unwrap()),
190 format!("{}", PrepareVerifyError::Expired)
192 User_::delete(connection, user.id).unwrap();
197 async fn test_community_inbox_expired_signature() {
198 let context = create_context();
199 let connection = &context.pool().get().unwrap();
200 let user = create_user(connection, "community_inbox_hrxa");
201 let community = create_community(connection, user.id);
202 let request = create_http_request();
203 let activity = create_activity::<FollowType, ActorAndObject<community_inbox::CommunityValidTypes>>(
206 let path = Path::<String> { 0: community.name };
207 let response = community_inbox(request, activity, path, web::Data::new(context)).await;
209 format!("{}", response.err().unwrap()),
210 format!("{}", PrepareVerifyError::Expired)
212 User_::delete(connection, user.id).unwrap();
213 Community::delete(connection, community.id).unwrap();