mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 21:42:34 +00:00
write test fixtures for sqlite store
This commit is contained in:
@@ -9,6 +9,8 @@ publish = false
|
||||
|
||||
[dev-dependencies]
|
||||
rustical_store_sqlite = { workspace = true, features = ["test"] }
|
||||
rstest.workspace = true
|
||||
async-std.workspace = true
|
||||
|
||||
[dependencies]
|
||||
axum.workspace = true
|
||||
|
||||
@@ -1,33 +1,38 @@
|
||||
use crate::principal::PrincipalResourceService;
|
||||
use rustical_dav::resource::ResourceService;
|
||||
use rustical_store::auth::{AuthenticationProvider, Principal, PrincipalType};
|
||||
use rustical_store_sqlite::tests::get_test_stores;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::principal::PrincipalResourceService;
|
||||
use rstest::rstest;
|
||||
use rustical_dav::resource::ResourceService;
|
||||
use rustical_store_sqlite::{
|
||||
SqliteStore,
|
||||
calendar_store::SqliteCalendarStore,
|
||||
principal_store::SqlitePrincipalStore,
|
||||
tests::{get_test_calendar_store, get_test_principal_store, get_test_subscription_store},
|
||||
};
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_principal_resource() {
|
||||
let (_, cal_store, sub_store, auth_provider, _) = get_test_stores().await;
|
||||
async fn test_principal_resource(
|
||||
#[from(get_test_calendar_store)]
|
||||
#[future]
|
||||
cal_store: SqliteCalendarStore,
|
||||
#[from(get_test_principal_store)]
|
||||
#[future]
|
||||
auth_provider: SqlitePrincipalStore,
|
||||
#[from(get_test_subscription_store)]
|
||||
#[future]
|
||||
sub_store: SqliteStore,
|
||||
) {
|
||||
let service = PrincipalResourceService {
|
||||
cal_store,
|
||||
sub_store,
|
||||
auth_provider: auth_provider.clone(),
|
||||
cal_store: Arc::new(cal_store.await),
|
||||
sub_store: Arc::new(sub_store.await),
|
||||
auth_provider: Arc::new(auth_provider.await),
|
||||
};
|
||||
|
||||
auth_provider
|
||||
.insert_principal(
|
||||
Principal {
|
||||
id: "user".to_owned(),
|
||||
displayname: None,
|
||||
memberships: vec![],
|
||||
password: None,
|
||||
principal_type: PrincipalType::Individual,
|
||||
},
|
||||
true,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(matches!(
|
||||
service.get_resource(&("anonymous".to_owned(),), true).await,
|
||||
service
|
||||
.get_resource(&("invalid-user".to_owned(),), true)
|
||||
.await,
|
||||
Err(crate::Error::NotFound)
|
||||
));
|
||||
|
||||
@@ -37,3 +42,5 @@ async fn test_principal_resource() {
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_propfind() {}
|
||||
|
||||
@@ -8,7 +8,10 @@ license.workspace = true
|
||||
publish = false
|
||||
|
||||
[features]
|
||||
test = []
|
||||
test = ["dep:rstest"]
|
||||
|
||||
[dev-dependencies]
|
||||
rstest.workspace = true
|
||||
|
||||
[dependencies]
|
||||
tokio.workspace = true
|
||||
@@ -25,3 +28,4 @@ password-hash.workspace = true
|
||||
uuid.workspace = true
|
||||
pbkdf2.workspace = true
|
||||
rustical_ical.workspace = true
|
||||
rstest = { workspace = true, optional = true }
|
||||
|
||||
@@ -2,41 +2,54 @@ use crate::{
|
||||
SqliteStore, addressbook_store::SqliteAddressbookStore, calendar_store::SqliteCalendarStore,
|
||||
principal_store::SqlitePrincipalStore,
|
||||
};
|
||||
use rustical_store::{
|
||||
AddressbookStore, CalendarStore, CollectionOperation, SubscriptionStore,
|
||||
auth::AuthenticationProvider,
|
||||
};
|
||||
use rustical_store::auth::{AuthenticationProvider, Principal, PrincipalType};
|
||||
use sqlx::SqlitePool;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::mpsc::Receiver;
|
||||
use tokio::sync::OnceCell;
|
||||
|
||||
pub async fn get_test_stores() -> (
|
||||
Arc<impl AddressbookStore>,
|
||||
Arc<impl CalendarStore>,
|
||||
Arc<impl SubscriptionStore>,
|
||||
Arc<impl AuthenticationProvider>,
|
||||
Receiver<CollectionOperation>,
|
||||
) {
|
||||
let db = SqlitePool::connect("sqlite::memory:").await.unwrap();
|
||||
sqlx::migrate!("./migrations").run(&db).await.unwrap();
|
||||
// let db = create_db_pool("sqlite::memory:", true).await.unwrap();
|
||||
// Channel to watch for changes (for DAV Push)
|
||||
let (send, recv) = tokio::sync::mpsc::channel(1000);
|
||||
static DB: OnceCell<SqlitePool> = OnceCell::const_new();
|
||||
|
||||
let addressbook_store = Arc::new(SqliteAddressbookStore::new(db.clone(), send.clone()));
|
||||
let cal_store = Arc::new(SqliteCalendarStore::new(db.clone(), send));
|
||||
let subscription_store = Arc::new(SqliteStore::new(db.clone()));
|
||||
let principal_store = Arc::new(SqlitePrincipalStore::new(db.clone()));
|
||||
(
|
||||
addressbook_store,
|
||||
cal_store,
|
||||
subscription_store,
|
||||
principal_store,
|
||||
recv,
|
||||
)
|
||||
async fn get_test_db() -> SqlitePool {
|
||||
DB.get_or_init(async || {
|
||||
let db = SqlitePool::connect("sqlite::memory:").await.unwrap();
|
||||
sqlx::migrate!("./migrations").run(&db).await.unwrap();
|
||||
|
||||
// Populate with test data
|
||||
let principal_store = SqlitePrincipalStore::new(db.clone());
|
||||
principal_store
|
||||
.insert_principal(
|
||||
Principal {
|
||||
id: "user".to_owned(),
|
||||
displayname: None,
|
||||
memberships: vec![],
|
||||
password: None,
|
||||
principal_type: PrincipalType::Individual,
|
||||
},
|
||||
false,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
db
|
||||
})
|
||||
.await
|
||||
.clone()
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_create_store() {
|
||||
get_test_stores().await;
|
||||
#[rstest::fixture]
|
||||
pub async fn get_test_addressbook_store() -> SqliteAddressbookStore {
|
||||
let (send, _recv) = tokio::sync::mpsc::channel(1000);
|
||||
SqliteAddressbookStore::new(get_test_db().await, send)
|
||||
}
|
||||
#[rstest::fixture]
|
||||
pub async fn get_test_calendar_store() -> SqliteCalendarStore {
|
||||
let (send, _recv) = tokio::sync::mpsc::channel(1000);
|
||||
SqliteCalendarStore::new(get_test_db().await, send)
|
||||
}
|
||||
#[rstest::fixture]
|
||||
pub async fn get_test_subscription_store() -> SqliteStore {
|
||||
SqliteStore::new(get_test_db().await)
|
||||
}
|
||||
#[rstest::fixture]
|
||||
pub async fn get_test_principal_store() -> SqlitePrincipalStore {
|
||||
SqlitePrincipalStore::new(get_test_db().await)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user