mirror of https://github.com/synctv-org/synctv
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
151 lines
4.6 KiB
Rust
151 lines
4.6 KiB
Rust
#![allow(clippy::unwrap_used)]
|
|
|
|
mod support;
|
|
|
|
use synctv_api::ApiRuntimeSettings as Config;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use chrono::Utc;
|
|
use synctv_core::{
|
|
cache::{KeyBuilder, UsernameCache},
|
|
models::{
|
|
room_settings::RequireApproval, RoomSettings, SignupMethod, User, UserId, UserRole,
|
|
UserStatus,
|
|
},
|
|
repository::UserRepository,
|
|
service::{
|
|
BruteForceProtection, InMemoryTokenBlacklistStore, JwtService, RoomService, UserService,
|
|
},
|
|
};
|
|
use synctv_realtime::sync::{ConnectionLimits, ConnectionManager};
|
|
|
|
fn make_user(username: &str) -> User {
|
|
let now = Utc::now();
|
|
User {
|
|
id: UserId::new(),
|
|
username: username.to_string(),
|
|
role: UserRole::User,
|
|
avatar_file_reference_id: None,
|
|
status: UserStatus::Active,
|
|
is_banned: false,
|
|
banned_at: None,
|
|
banned_by: None,
|
|
banned_reason: None,
|
|
signup_method: SignupMethod::Email,
|
|
created_at: now,
|
|
updated_at: now,
|
|
version: 0,
|
|
deleted_at: None,
|
|
}
|
|
}
|
|
|
|
fn make_user_service(pool: &sqlx::PgPool) -> UserService {
|
|
let jwt_service = JwtService::new("Test_Secret_Key_For_JWT_Tokens_32Bytes!!").unwrap();
|
|
let username_cache = UsernameCache::local_only("test:username:".to_string(), 100, 60);
|
|
let token_blacklist = Arc::new(InMemoryTokenBlacklistStore::new(1000, 3600, 86400));
|
|
UserService::new_for_tests(
|
|
pool,
|
|
jwt_service,
|
|
username_cache,
|
|
token_blacklist,
|
|
KeyBuilder::new("test"),
|
|
BruteForceProtection::in_memory("test:user".to_string()),
|
|
)
|
|
}
|
|
|
|
fn make_client_api(
|
|
user_service: Arc<UserService>,
|
|
room_service: Arc<RoomService>,
|
|
) -> synctv_api::ClientApiImpl {
|
|
let connection_manager = Arc::new(ConnectionManager::new(ConnectionLimits::default()));
|
|
|
|
synctv_api::ClientApiImpl::new_with_runtime(
|
|
synctv_api::ClientApiOptions {
|
|
read_pool: None,
|
|
user_service,
|
|
room_service,
|
|
connection_service: connection_manager,
|
|
runtime_settings: Arc::new(Config::default()),
|
|
publish_key_service: None,
|
|
jwt_service: JwtService::new("Test_Secret_Key_For_JWT_Tokens_32Bytes!!").unwrap(),
|
|
live_streaming_infrastructure: None,
|
|
runtime_settings_store: None,
|
|
public_id_codec: Arc::new(synctv_api::PublicIdCodec::plain()),
|
|
chat_service: None,
|
|
provider_stores: Arc::new(synctv_core::provider::ProviderStoreRegistry::local_only(
|
|
"test:provider:",
|
|
)),
|
|
email_api: None,
|
|
passkey_service: None,
|
|
},
|
|
support::client_api_runtime(),
|
|
)
|
|
}
|
|
|
|
#[tokio::test]
|
|
#[ignore = "Requires Docker"]
|
|
async fn test_join_room_response_exposes_pending_membership_contract() {
|
|
let (_postgres, pool) = synctv_core_testing::create_test_pool().await;
|
|
let user_repo = UserRepository::new(pool.clone());
|
|
|
|
let user_service = Arc::new(make_user_service(&pool));
|
|
let room_service = Arc::new(
|
|
RoomService::new_for_tests(pool.clone(), (*user_service).clone())
|
|
.expect("room service should build"),
|
|
);
|
|
let client_api = make_client_api(user_service, room_service.clone());
|
|
|
|
let owner = user_repo
|
|
.create(&make_user("approval_contract_owner"))
|
|
.await
|
|
.unwrap();
|
|
let joiner = user_repo
|
|
.create(&make_user("approval_contract_joiner"))
|
|
.await
|
|
.unwrap();
|
|
|
|
let settings = RoomSettings {
|
|
require_approval: RequireApproval(true),
|
|
..Default::default()
|
|
};
|
|
|
|
let room = room_service
|
|
.create_room(
|
|
"Approval Contract Room".to_string(),
|
|
String::new(),
|
|
owner.id,
|
|
None,
|
|
Some(settings),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
let public_id_codec = synctv_api::PublicIdCodec::plain();
|
|
let room_id = public_id_codec.encode_room_id(room.id).unwrap();
|
|
let response = client_api
|
|
.join_room_with_control(
|
|
&joiner.id,
|
|
&room_id,
|
|
synctv_proto::client::JoinRoomRequest {
|
|
room_id: room_id.clone(),
|
|
password: String::new(),
|
|
remark_name: String::new(),
|
|
display_tag: String::new(),
|
|
},
|
|
None,
|
|
None,
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
assert!(
|
|
response.requires_approval,
|
|
"join response must explicitly tell the client that approval is required"
|
|
);
|
|
assert!(
|
|
response.members.is_empty(),
|
|
"pending join should not leak the room member list before approval"
|
|
);
|
|
}
|