Use a fake database in tests

This commit is contained in:
Antonio Scandurra 2022-02-17 17:04:04 +01:00
parent 0b46e36189
commit 19b4ecd33a
5 changed files with 669 additions and 369 deletions

View file

@ -111,7 +111,7 @@ async fn create_access_token(request: Request) -> tide::Result {
.get_user_by_github_login(request.param("github_login")?)
.await?
.ok_or_else(|| surf::Error::from_str(StatusCode::NotFound, "user not found"))?;
let access_token = auth::create_access_token(request.db(), user.id).await?;
let access_token = auth::create_access_token(request.db().as_ref(), user.id).await?;
#[derive(Deserialize)]
struct QueryParams {

View file

@ -234,7 +234,7 @@ async fn get_auth_callback(mut request: Request) -> tide::Result {
let mut user_id = user.id;
if let Some(impersonated_login) = app_sign_in_params.impersonate {
log::info!("attempting to impersonate user @{}", impersonated_login);
if let Some(user) = request.db().get_users_by_ids([user_id]).await?.first() {
if let Some(user) = request.db().get_users_by_ids(vec![user_id]).await?.first() {
if user.admin {
user_id = request.db().create_user(&impersonated_login, false).await?;
log::info!("impersonating user {}", user_id.0);
@ -244,7 +244,7 @@ async fn get_auth_callback(mut request: Request) -> tide::Result {
}
}
let access_token = create_access_token(request.db(), user_id).await?;
let access_token = create_access_token(request.db().as_ref(), user_id).await?;
let encrypted_access_token = encrypt_access_token(
&access_token,
app_sign_in_params.native_app_public_key.clone(),
@ -267,7 +267,7 @@ async fn post_sign_out(mut request: Request) -> tide::Result {
const MAX_ACCESS_TOKENS_TO_STORE: usize = 8;
pub async fn create_access_token(db: &db::Db, user_id: UserId) -> tide::Result<String> {
pub async fn create_access_token(db: &dyn db::Db, user_id: UserId) -> tide::Result<String> {
let access_token = zed_auth::random_token();
let access_token_hash =
hash_access_token(&access_token).context("failed to hash access token")?;

File diff suppressed because it is too large Load diff

View file

@ -20,7 +20,7 @@ use anyhow::Result;
use async_std::net::TcpListener;
use async_trait::async_trait;
use auth::RequestExt as _;
use db::Db;
use db::{Db, PostgresDb};
use handlebars::{Handlebars, TemplateRenderError};
use parking_lot::RwLock;
use rust_embed::RustEmbed;
@ -49,7 +49,7 @@ pub struct Config {
}
pub struct AppState {
db: Db,
db: Arc<dyn Db>,
handlebars: RwLock<Handlebars<'static>>,
auth_client: auth::Client,
github_client: Arc<github::AppClient>,
@ -59,7 +59,7 @@ pub struct AppState {
impl AppState {
async fn new(config: Config) -> tide::Result<Arc<Self>> {
let db = Db::new(&config.database_url, 5).await?;
let db = PostgresDb::new(&config.database_url, 5).await?;
let github_client =
github::AppClient::new(config.github_app_id, config.github_private_key.clone());
let repo_client = github_client
@ -68,7 +68,7 @@ impl AppState {
.context("failed to initialize github client")?;
let this = Self {
db,
db: Arc::new(db),
handlebars: Default::default(),
auth_client: auth::build_client(&config.github_client_id, &config.github_client_secret),
github_client,
@ -112,7 +112,7 @@ impl AppState {
#[async_trait]
trait RequestExt {
async fn layout_data(&mut self) -> tide::Result<Arc<LayoutData>>;
fn db(&self) -> &Db;
fn db(&self) -> &Arc<dyn Db>;
}
#[async_trait]
@ -126,7 +126,7 @@ impl RequestExt for Request {
Ok(self.ext::<Arc<LayoutData>>().unwrap().clone())
}
fn db(&self) -> &Db {
fn db(&self) -> &Arc<dyn Db> {
&self.state().db
}
}

View file

@ -785,7 +785,12 @@ impl Server {
self: Arc<Server>,
request: TypedEnvelope<proto::GetUsers>,
) -> tide::Result<proto::GetUsersResponse> {
let user_ids = request.payload.user_ids.into_iter().map(UserId::from_proto);
let user_ids = request
.payload
.user_ids
.into_iter()
.map(UserId::from_proto)
.collect();
let users = self
.app_state
.db
@ -1139,18 +1144,14 @@ mod tests {
}
#[gpui::test(iterations = 10)]
async fn test_share_project(
mut cx_a: TestAppContext,
mut cx_b: TestAppContext,
last_iteration: bool,
) {
async fn test_share_project(mut cx_a: TestAppContext, mut cx_b: TestAppContext) {
let (window_b, _) = cx_b.add_window(|_| EmptyView);
let lang_registry = Arc::new(LanguageRegistry::new());
let fs = Arc::new(FakeFs::new(cx_a.background()));
cx_a.foreground().forbid_parking();
// Connect to a server as 2 clients.
let mut server = TestServer::start(cx_a.foreground(), last_iteration).await;
let mut server = TestServer::start(cx_a.foreground(), cx_a.background()).await;
let client_a = server.create_client(&mut cx_a, "user_a").await;
let client_b = server.create_client(&mut cx_b, "user_b").await;
@ -1282,17 +1283,13 @@ mod tests {
}
#[gpui::test(iterations = 10)]
async fn test_unshare_project(
mut cx_a: TestAppContext,
mut cx_b: TestAppContext,
last_iteration: bool,
) {
async fn test_unshare_project(mut cx_a: TestAppContext, mut cx_b: TestAppContext) {
let lang_registry = Arc::new(LanguageRegistry::new());
let fs = Arc::new(FakeFs::new(cx_a.background()));
cx_a.foreground().forbid_parking();
// Connect to a server as 2 clients.
let mut server = TestServer::start(cx_a.foreground(), last_iteration).await;
let mut server = TestServer::start(cx_a.foreground(), cx_a.background()).await;
let client_a = server.create_client(&mut cx_a, "user_a").await;
let client_b = server.create_client(&mut cx_b, "user_b").await;
@ -1387,14 +1384,13 @@ mod tests {
mut cx_a: TestAppContext,
mut cx_b: TestAppContext,
mut cx_c: TestAppContext,
last_iteration: bool,
) {
let lang_registry = Arc::new(LanguageRegistry::new());
let fs = Arc::new(FakeFs::new(cx_a.background()));
cx_a.foreground().forbid_parking();
// Connect to a server as 3 clients.
let mut server = TestServer::start(cx_a.foreground(), last_iteration).await;
let mut server = TestServer::start(cx_a.foreground(), cx_a.background()).await;
let client_a = server.create_client(&mut cx_a, "user_a").await;
let client_b = server.create_client(&mut cx_b, "user_b").await;
let client_c = server.create_client(&mut cx_c, "user_c").await;
@ -1566,17 +1562,13 @@ mod tests {
}
#[gpui::test(iterations = 10)]
async fn test_buffer_conflict_after_save(
mut cx_a: TestAppContext,
mut cx_b: TestAppContext,
last_iteration: bool,
) {
async fn test_buffer_conflict_after_save(mut cx_a: TestAppContext, mut cx_b: TestAppContext) {
cx_a.foreground().forbid_parking();
let lang_registry = Arc::new(LanguageRegistry::new());
let fs = Arc::new(FakeFs::new(cx_a.background()));
// Connect to a server as 2 clients.
let mut server = TestServer::start(cx_a.foreground(), last_iteration).await;
let mut server = TestServer::start(cx_a.foreground(), cx_a.background()).await;
let client_a = server.create_client(&mut cx_a, "user_a").await;
let client_b = server.create_client(&mut cx_b, "user_b").await;
@ -1658,17 +1650,13 @@ mod tests {
}
#[gpui::test(iterations = 10)]
async fn test_buffer_reloading(
mut cx_a: TestAppContext,
mut cx_b: TestAppContext,
last_iteration: bool,
) {
async fn test_buffer_reloading(mut cx_a: TestAppContext, mut cx_b: TestAppContext) {
cx_a.foreground().forbid_parking();
let lang_registry = Arc::new(LanguageRegistry::new());
let fs = Arc::new(FakeFs::new(cx_a.background()));
// Connect to a server as 2 clients.
let mut server = TestServer::start(cx_a.foreground(), last_iteration).await;
let mut server = TestServer::start(cx_a.foreground(), cx_a.background()).await;
let client_a = server.create_client(&mut cx_a, "user_a").await;
let client_b = server.create_client(&mut cx_b, "user_b").await;
@ -1747,14 +1735,13 @@ mod tests {
async fn test_editing_while_guest_opens_buffer(
mut cx_a: TestAppContext,
mut cx_b: TestAppContext,
last_iteration: bool,
) {
cx_a.foreground().forbid_parking();
let lang_registry = Arc::new(LanguageRegistry::new());
let fs = Arc::new(FakeFs::new(cx_a.background()));
// Connect to a server as 2 clients.
let mut server = TestServer::start(cx_a.foreground(), last_iteration).await;
let mut server = TestServer::start(cx_a.foreground(), cx_a.background()).await;
let client_a = server.create_client(&mut cx_a, "user_a").await;
let client_b = server.create_client(&mut cx_b, "user_b").await;
@ -1830,14 +1817,13 @@ mod tests {
async fn test_leaving_worktree_while_opening_buffer(
mut cx_a: TestAppContext,
mut cx_b: TestAppContext,
last_iteration: bool,
) {
cx_a.foreground().forbid_parking();
let lang_registry = Arc::new(LanguageRegistry::new());
let fs = Arc::new(FakeFs::new(cx_a.background()));
// Connect to a server as 2 clients.
let mut server = TestServer::start(cx_a.foreground(), last_iteration).await;
let mut server = TestServer::start(cx_a.foreground(), cx_a.background()).await;
let client_a = server.create_client(&mut cx_a, "user_a").await;
let client_b = server.create_client(&mut cx_b, "user_b").await;
@ -1906,17 +1892,13 @@ mod tests {
}
#[gpui::test(iterations = 10)]
async fn test_peer_disconnection(
mut cx_a: TestAppContext,
mut cx_b: TestAppContext,
last_iteration: bool,
) {
async fn test_peer_disconnection(mut cx_a: TestAppContext, mut cx_b: TestAppContext) {
cx_a.foreground().forbid_parking();
let lang_registry = Arc::new(LanguageRegistry::new());
let fs = Arc::new(FakeFs::new(cx_a.background()));
// Connect to a server as 2 clients.
let mut server = TestServer::start(cx_a.foreground(), last_iteration).await;
let mut server = TestServer::start(cx_a.foreground(), cx_a.background()).await;
let client_a = server.create_client(&mut cx_a, "user_a").await;
let client_b = server.create_client(&mut cx_b, "user_b").await;
@ -1984,7 +1966,6 @@ mod tests {
async fn test_collaborating_with_diagnostics(
mut cx_a: TestAppContext,
mut cx_b: TestAppContext,
last_iteration: bool,
) {
cx_a.foreground().forbid_parking();
let mut lang_registry = Arc::new(LanguageRegistry::new());
@ -2005,7 +1986,7 @@ mod tests {
)));
// Connect to a server as 2 clients.
let mut server = TestServer::start(cx_a.foreground(), last_iteration).await;
let mut server = TestServer::start(cx_a.foreground(), cx_a.background()).await;
let client_a = server.create_client(&mut cx_a, "user_a").await;
let client_b = server.create_client(&mut cx_b, "user_b").await;
@ -2209,7 +2190,6 @@ mod tests {
async fn test_collaborating_with_completion(
mut cx_a: TestAppContext,
mut cx_b: TestAppContext,
last_iteration: bool,
) {
cx_a.foreground().forbid_parking();
let mut lang_registry = Arc::new(LanguageRegistry::new());
@ -2237,7 +2217,7 @@ mod tests {
)));
// Connect to a server as 2 clients.
let mut server = TestServer::start(cx_a.foreground(), last_iteration).await;
let mut server = TestServer::start(cx_a.foreground(), cx_a.background()).await;
let client_a = server.create_client(&mut cx_a, "user_a").await;
let client_b = server.create_client(&mut cx_b, "user_b").await;
@ -2419,11 +2399,7 @@ mod tests {
}
#[gpui::test(iterations = 10)]
async fn test_formatting_buffer(
mut cx_a: TestAppContext,
mut cx_b: TestAppContext,
last_iteration: bool,
) {
async fn test_formatting_buffer(mut cx_a: TestAppContext, mut cx_b: TestAppContext) {
cx_a.foreground().forbid_parking();
let mut lang_registry = Arc::new(LanguageRegistry::new());
let fs = Arc::new(FakeFs::new(cx_a.background()));
@ -2443,7 +2419,7 @@ mod tests {
)));
// Connect to a server as 2 clients.
let mut server = TestServer::start(cx_a.foreground(), last_iteration).await;
let mut server = TestServer::start(cx_a.foreground(), cx_a.background()).await;
let client_a = server.create_client(&mut cx_a, "user_a").await;
let client_b = server.create_client(&mut cx_b, "user_b").await;
@ -2525,11 +2501,7 @@ mod tests {
}
#[gpui::test(iterations = 10)]
async fn test_definition(
mut cx_a: TestAppContext,
mut cx_b: TestAppContext,
last_iteration: bool,
) {
async fn test_definition(mut cx_a: TestAppContext, mut cx_b: TestAppContext) {
cx_a.foreground().forbid_parking();
let mut lang_registry = Arc::new(LanguageRegistry::new());
let fs = Arc::new(FakeFs::new(cx_a.background()));
@ -2564,7 +2536,7 @@ mod tests {
)));
// Connect to a server as 2 clients.
let mut server = TestServer::start(cx_a.foreground(), last_iteration).await;
let mut server = TestServer::start(cx_a.foreground(), cx_a.background()).await;
let client_a = server.create_client(&mut cx_a, "user_a").await;
let client_b = server.create_client(&mut cx_b, "user_b").await;
@ -2682,7 +2654,6 @@ mod tests {
mut cx_a: TestAppContext,
mut cx_b: TestAppContext,
mut rng: StdRng,
last_iteration: bool,
) {
cx_a.foreground().forbid_parking();
let mut lang_registry = Arc::new(LanguageRegistry::new());
@ -2713,7 +2684,7 @@ mod tests {
)));
// Connect to a server as 2 clients.
let mut server = TestServer::start(cx_a.foreground(), last_iteration).await;
let mut server = TestServer::start(cx_a.foreground(), cx_a.background()).await;
let client_a = server.create_client(&mut cx_a, "user_a").await;
let client_b = server.create_client(&mut cx_b, "user_b").await;
@ -2792,7 +2763,6 @@ mod tests {
async fn test_collaborating_with_code_actions(
mut cx_a: TestAppContext,
mut cx_b: TestAppContext,
last_iteration: bool,
) {
cx_a.foreground().forbid_parking();
let mut lang_registry = Arc::new(LanguageRegistry::new());
@ -2815,7 +2785,7 @@ mod tests {
)));
// Connect to a server as 2 clients.
let mut server = TestServer::start(cx_a.foreground(), last_iteration).await;
let mut server = TestServer::start(cx_a.foreground(), cx_a.background()).await;
let client_a = server.create_client(&mut cx_a, "user_a").await;
let client_b = server.create_client(&mut cx_b, "user_b").await;
@ -3032,15 +3002,11 @@ mod tests {
}
#[gpui::test(iterations = 10)]
async fn test_basic_chat(
mut cx_a: TestAppContext,
mut cx_b: TestAppContext,
last_iteration: bool,
) {
async fn test_basic_chat(mut cx_a: TestAppContext, mut cx_b: TestAppContext) {
cx_a.foreground().forbid_parking();
// Connect to a server as 2 clients.
let mut server = TestServer::start(cx_a.foreground(), last_iteration).await;
let mut server = TestServer::start(cx_a.foreground(), cx_a.background()).await;
let client_a = server.create_client(&mut cx_a, "user_a").await;
let client_b = server.create_client(&mut cx_b, "user_b").await;
@ -3176,10 +3142,10 @@ mod tests {
}
#[gpui::test(iterations = 10)]
async fn test_chat_message_validation(mut cx_a: TestAppContext, last_iteration: bool) {
async fn test_chat_message_validation(mut cx_a: TestAppContext) {
cx_a.foreground().forbid_parking();
let mut server = TestServer::start(cx_a.foreground(), last_iteration).await;
let mut server = TestServer::start(cx_a.foreground(), cx_a.background()).await;
let client_a = server.create_client(&mut cx_a, "user_a").await;
let db = &server.app_state.db;
@ -3236,15 +3202,11 @@ mod tests {
}
#[gpui::test(iterations = 10)]
async fn test_chat_reconnection(
mut cx_a: TestAppContext,
mut cx_b: TestAppContext,
last_iteration: bool,
) {
async fn test_chat_reconnection(mut cx_a: TestAppContext, mut cx_b: TestAppContext) {
cx_a.foreground().forbid_parking();
// Connect to a server as 2 clients.
let mut server = TestServer::start(cx_a.foreground(), last_iteration).await;
let mut server = TestServer::start(cx_a.foreground(), cx_a.background()).await;
let client_a = server.create_client(&mut cx_a, "user_a").await;
let client_b = server.create_client(&mut cx_b, "user_b").await;
let mut status_b = client_b.status();
@ -3456,14 +3418,13 @@ mod tests {
mut cx_a: TestAppContext,
mut cx_b: TestAppContext,
mut cx_c: TestAppContext,
last_iteration: bool,
) {
cx_a.foreground().forbid_parking();
let lang_registry = Arc::new(LanguageRegistry::new());
let fs = Arc::new(FakeFs::new(cx_a.background()));
// Connect to a server as 3 clients.
let mut server = TestServer::start(cx_a.foreground(), last_iteration).await;
let mut server = TestServer::start(cx_a.foreground(), cx_a.background()).await;
let client_a = server.create_client(&mut cx_a, "user_a").await;
let client_b = server.create_client(&mut cx_b, "user_b").await;
let client_c = server.create_client(&mut cx_c, "user_c").await;
@ -3595,7 +3556,7 @@ mod tests {
}
#[gpui::test(iterations = 100)]
async fn test_random_collaboration(cx: TestAppContext, rng: StdRng, last_iteration: bool) {
async fn test_random_collaboration(cx: TestAppContext, rng: StdRng) {
cx.foreground().forbid_parking();
let max_peers = env::var("MAX_PEERS")
.map(|i| i.parse().expect("invalid `MAX_PEERS` variable"))
@ -3654,7 +3615,7 @@ mod tests {
.await;
let operations = Rc::new(Cell::new(0));
let mut server = TestServer::start(cx.foreground(), last_iteration).await;
let mut server = TestServer::start(cx.foreground(), cx.background()).await;
let mut clients = Vec::new();
let mut next_entity_id = 100000;
@ -3849,9 +3810,11 @@ mod tests {
}
impl TestServer {
async fn start(foreground: Rc<executor::Foreground>, clean_db_pool_on_drop: bool) -> Self {
let mut test_db = TestDb::new();
test_db.set_clean_pool_on_drop(clean_db_pool_on_drop);
async fn start(
foreground: Rc<executor::Foreground>,
background: Arc<executor::Background>,
) -> Self {
let test_db = TestDb::fake(background);
let app_state = Self::build_app_state(&test_db).await;
let peer = Peer::new();
let notifications = mpsc::unbounded();