Skip to content

Commit

Permalink
v2, tests: server_app_test! Happy new year!
Browse files Browse the repository at this point in the history
  • Loading branch information
cypherkitty committed Jan 1, 2024
1 parent ba6b8eb commit f4ecbf6
Show file tree
Hide file tree
Showing 30 changed files with 288 additions and 129 deletions.
2 changes: 1 addition & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ use crate::secret::shared_secret::{PlainText, SharedSecret, SharedSecretEncrypti

pub mod crypto;
pub mod errors;
pub mod meta_tests;
pub mod node;
pub mod secret;
pub mod meta_tests;

pub type CoreResult<T> = std::result::Result<T, CoreError>;

Expand Down
17 changes: 9 additions & 8 deletions core/src/meta_tests/action/generate_user.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use std::sync::Arc;
use anyhow::Result;
use std::sync::Arc;

use crate::node::{db::{
repo::{generic_db::KvLogEventRepo, credentials_repo::CredentialsRepo},
objects::persistent_object::PersistentObject
}, common::model::{user::UserCredentials, device::DeviceName, vault::VaultName}};

use crate::node::{
common::model::{device::DeviceName, user::UserCredentials, vault::VaultName},
db::{
objects::persistent_object::PersistentObject,
repo::{credentials_repo::CredentialsRepo, generic_db::KvLogEventRepo},
},
};

pub struct GenerateUserTestAction<Repo: KvLogEventRepo> {
creds_repo: Arc<CredentialsRepo<Repo>>
creds_repo: Arc<CredentialsRepo<Repo>>,
}

impl<Repo: KvLogEventRepo> GenerateUserTestAction<Repo> {

pub fn new(p_obj: Arc<PersistentObject<Repo>>) -> Self {
let creds_repo = Arc::new(CredentialsRepo { p_obj });
Self { creds_repo }
Expand Down
14 changes: 13 additions & 1 deletion core/src/meta_tests/action/global_index.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
use std::sync::Arc;

use crate::node::{server::{request::{SyncRequest, GlobalIndexRequest}, server_app::ServerApp}, common::model::device::{DeviceData, DeviceCredentials}, db::{events::{generic_log_event::GenericKvLogEvent, object_id::ObjectId}, objects::persistent_object::PersistentObject, in_mem_db::InMemKvLogEventRepo, descriptors::{global_index::GlobalIndexDescriptor, object_descriptor::ToObjectDescriptor}}};
use crate::node::{
common::model::device::{DeviceCredentials, DeviceData},
db::{
descriptors::{global_index::GlobalIndexDescriptor, object_descriptor::ToObjectDescriptor},
events::{generic_log_event::GenericKvLogEvent, object_id::ObjectId},
in_mem_db::InMemKvLogEventRepo,
objects::persistent_object::PersistentObject,
},
server::{
request::{GlobalIndexRequest, SyncRequest},
server_app::ServerApp,
},
};

pub struct ServerTestNode {
pub device: DeviceCredentials,
Expand Down
2 changes: 1 addition & 1 deletion core/src/meta_tests/action/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod generate_user;
pub mod sign_up_claim_action;
pub mod global_index;
pub mod sign_up_claim_action;
19 changes: 12 additions & 7 deletions core/src/meta_tests/action/sign_up_claim_action.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
use std::sync::Arc;
use anyhow::Result;
use std::sync::Arc;

use crate::node::{db::{repo::generic_db::KvLogEventRepo, objects::persistent_object::PersistentObject, actions::sign_up_claim::SignUpClaim}, common::model::vault::VaultStatus};
use crate::node::{
common::model::vault::VaultStatus,
db::{
actions::sign_up_claim::SignUpClaim, objects::persistent_object::PersistentObject,
repo::generic_db::KvLogEventRepo,
},
};

use super::generate_user::GenerateUserTestAction;


pub struct SignUpClaimTestAction<Repo: KvLogEventRepo> {
p_obj: Arc<PersistentObject<Repo>>,
generate_user_action: Arc<GenerateUserTestAction<Repo>>
generate_user_action: Arc<GenerateUserTestAction<Repo>>,
}

impl<Repo: KvLogEventRepo> SignUpClaimTestAction<Repo> {
pub fn new(p_obj: Arc<PersistentObject<Repo>>) -> Self {
Self {
p_obj: p_obj.clone(),
generate_user_action: Arc::new(GenerateUserTestAction::new(p_obj))
generate_user_action: Arc::new(GenerateUserTestAction::new(p_obj)),
}
}
}
Expand All @@ -24,8 +29,8 @@ impl<Repo: KvLogEventRepo> SignUpClaimTestAction<Repo> {
pub async fn sign_up(&self) -> Result<VaultStatus> {
let _user = self.generate_user_action.generate_user().await?;

let sign_up_claim = SignUpClaim {
p_obj: self.p_obj.clone()
let sign_up_claim = SignUpClaim {
p_obj: self.p_obj.clone(),
};

sign_up_claim.sign_up().await
Expand Down
22 changes: 15 additions & 7 deletions core/src/meta_tests/fixture/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
use crate::node::common::model::device::{DeviceCredentials, DeviceName};


use crate::node::common::model::{
device::{DeviceCredentials, DeviceName},
user::UserCredentials,
vault::VaultName,
};

pub struct ClientDeviceFixture {
pub device: DeviceCredentials,
pub device_creds: DeviceCredentials,
pub user_creds: UserCredentials,
}

impl Default for ClientDeviceFixture {
fn default() -> Self {
let device = DeviceCredentials::generate(DeviceName::from("test_device"));
Self { device }
let user_creds = UserCredentials::generate(DeviceName::from("client_device"), VaultName::from("test_vault"));
let device_creds = user_creds.device_creds.clone();

Self {
device_creds,
user_creds,
}
}
}
}
2 changes: 1 addition & 1 deletion core/src/meta_tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod spec;
pub mod action;
pub mod fixture;
pub mod spec;
20 changes: 11 additions & 9 deletions core/src/meta_tests/spec/global_index_specs.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use std::sync::Arc;

use crate::node::{db::{repo::generic_db::KvLogEventRepo, descriptors::{global_index::GlobalIndexDescriptor, object_descriptor::ToObjectDescriptor}, events::{object_id::ObjectId, global_index::GlobalIndexObject, generic_log_event::ObjIdExtractor}}, common::model::device::DeviceData};
use crate::node::{
common::model::device::DeviceData,
db::{
descriptors::{global_index::GlobalIndexDescriptor, object_descriptor::ToObjectDescriptor},
events::{generic_log_event::ObjIdExtractor, global_index::GlobalIndexObject, object_id::ObjectId},
repo::generic_db::KvLogEventRepo,
},
};
use anyhow::Result;


pub struct GlobalIndexSpec<Repo: KvLogEventRepo> {
pub repo: Arc<Repo>,
pub server_device: DeviceData
pub server_device: DeviceData,
}

impl<Repo: KvLogEventRepo> GlobalIndexSpec<Repo> {
Expand All @@ -15,18 +21,14 @@ impl<Repo: KvLogEventRepo> GlobalIndexSpec<Repo> {

let unit_event = {
let unit_id = ObjectId::unit(gi_obj_desc.clone());
let event = self.repo.find_one(unit_id)
.await?
.unwrap();
let event = self.repo.find_one(unit_id).await?.unwrap();
GlobalIndexObject::try_from(event)?
};
assert_eq!(unit_event.obj_id().get_unit_id().id.id, 0);

let genesis_event = {
let genesis_id = ObjectId::genesis(gi_obj_desc.clone());
let event = self.repo.find_one(genesis_id)
.await?
.unwrap();
let event = self.repo.find_one(genesis_id).await?.unwrap();
GlobalIndexObject::try_from(event.clone())?
};

Expand Down
2 changes: 1 addition & 1 deletion core/src/meta_tests/spec/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod test_spec;
pub mod global_index_specs;
pub mod sign_up_claim_spec;
pub mod test_spec;
25 changes: 18 additions & 7 deletions core/src/meta_tests/spec/sign_up_claim_spec.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
use std::sync::Arc;

use crate::node::{db::{repo::generic_db::KvLogEventRepo, descriptors::{vault::VaultDescriptor, shared_secret::SharedSecretDescriptor, object_descriptor::ToObjectDescriptor}, events::{object_id::{ObjectId, UnitId, Next}, vault_event::{DeviceLogObject, VaultAction}}, objects::persistent_object::PersistentObject}, common::model::user::{UserData, UserMembership, UserDataMember}};
use crate::node::{
common::model::user::UserData,
db::{
descriptors::{
object_descriptor::ToObjectDescriptor, shared_secret::SharedSecretDescriptor, vault::VaultDescriptor,
},
events::{
object_id::{Next, ObjectId, UnitId},
vault_event::{DeviceLogObject, VaultAction},
},
objects::persistent_object::PersistentObject,
repo::generic_db::KvLogEventRepo,
},
};
use anyhow::Result;

use super::test_spec::TestSpec;
use async_trait::async_trait;


pub struct SignUpClaimSpec<Repo: KvLogEventRepo> {
pub p_obj: Arc<PersistentObject<Repo>>,
pub user: UserData
pub user: UserData,
}

#[async_trait(? Send)]
Expand All @@ -23,7 +35,7 @@ impl<Repo: KvLogEventRepo> TestSpec for SignUpClaimSpec<Repo> {

let _unit_event = self.p_obj.repo.find_one(ObjectId::from(unit_id)).await?.unwrap();
let _genesis_event = self.p_obj.repo.find_one(ObjectId::from(genesis_id)).await?.unwrap();

let generic_sign_up_request = self.p_obj.repo.find_one(ObjectId::from(request_id)).await?.unwrap();

let sign_up_request = DeviceLogObject::try_from(generic_sign_up_request)?;
Expand All @@ -33,11 +45,11 @@ impl<Repo: KvLogEventRepo> TestSpec for SignUpClaimSpec<Repo> {
};

let vault_action = vault_action_event.value;
let VaultAction::UpdateMembership { update: UserMembership::Member(UserDataMember(user)), .. } = vault_action else {
let VaultAction::Create(candidate) = vault_action else {
panic!("Invalid action: {:?}", vault_action);
};

assert_eq!(user.device, self.user.device);
assert_eq!(candidate.device, self.user.device);

// check SSLog
let ss_obj_desc = SharedSecretDescriptor::SSDeviceLog(self.user.device.id.clone()).to_obj_desc();
Expand All @@ -47,7 +59,6 @@ impl<Repo: KvLogEventRepo> TestSpec for SignUpClaimSpec<Repo> {
let _ = self.p_obj.repo.find_one(ObjectId::from(ss_unit_id)).await?;
let _ = self.p_obj.repo.find_one(ObjectId::from(ss_genesis_id)).await?;


Ok(())
}
}
2 changes: 1 addition & 1 deletion core/src/meta_tests/spec/test_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ use async_trait::async_trait;
#[async_trait(? Send)]
pub trait TestSpec {
async fn check(&self) -> Result<()>;
}
}
6 changes: 3 additions & 3 deletions core/src/node/app/meta_app/meta_client_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::node::common::data_transfer::MpscDataTransfer;
use crate::node::common::model::ApplicationState;
use crate::node::db::actions::recover::RecoveryAction;
use crate::node::db::actions::sign_up_claim::SignUpClaim;
use crate::node::db::objects::vault::PersistentVault;
use crate::node::db::objects::persistent_vault::PersistentVault;
use crate::node::db::repo::credentials_repo::CredentialsRepo;
use crate::node::db::repo::generic_db::KvLogEventRepo;
use crate::secret::MetaDistributor;
Expand Down Expand Up @@ -46,8 +46,8 @@ where

match request {
GenericAppStateRequest::SignUp => {
let sign_up_claim = SignUpClaim {
p_obj: self.sync_gateway.persistent_object.clone()
let sign_up_claim = SignUpClaim {
p_obj: self.sync_gateway.persistent_object.clone(),
};

sign_up_claim.sign_up().await?;
Expand Down
4 changes: 2 additions & 2 deletions core/src/node/app/sync_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ use crate::node::db::events::kv_log_event::{KvKey, KvLogEvent};
use crate::node::db::events::local::{CredentialsObject, DbTailObject};
use crate::node::db::events::vault_event::VaultMembershipObject;
use crate::node::db::objects::persistent_object::PersistentObject;
use crate::node::db::objects::vault::PersistentVault;
use crate::node::db::objects::persistent_vault::PersistentVault;
use crate::node::db::repo::credentials_repo::CredentialsRepo;
use crate::node::db::repo::generic_db::KvLogEventRepo;
use crate::node::server::data_sync::DataSyncRequest;
use crate::node::server::request::{GlobalIndexRequest, SharedSecretRequest, SyncRequest, VaultRequest};
use crate::node::server::server_app::ServerDataTransfer;
use crate::node::server::server_data_sync::DataSyncRequest;

pub struct SyncGateway<Repo: KvLogEventRepo> {
pub id: String,
Expand Down
11 changes: 7 additions & 4 deletions core/src/node/app/virtual_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use crate::node::common::model::vault::VaultStatus;
use crate::node::db::descriptors::object_descriptor::ToObjectDescriptor;
use crate::node::db::descriptors::vault::VaultDescriptor;
use crate::node::db::events::vault_event::{VaultAction, VaultLogObject};
use crate::node::db::objects::device_log::PersistentDeviceLog;
use crate::node::db::objects::persistent_device_log::PersistentDeviceLog;
use crate::node::db::objects::persistent_object::PersistentObject;
use crate::node::db::objects::shared_secret::PersistentSharedSecret;
use crate::node::db::objects::vault::PersistentVault;
use crate::node::db::objects::persistent_shared_secret::PersistentSharedSecret;
use crate::node::db::objects::persistent_vault::PersistentVault;
use crate::node::db::repo::generic_db::KvLogEventRepo;
use crate::node::server::server_app::ServerDataTransfer;

Expand Down Expand Up @@ -80,14 +80,17 @@ impl<Repo: KvLogEventRepo> VirtualDevice<Repo> {
p_obj: self.persistent_object.clone(),
};

p_device_log.accept_join_cluster_request(candidate).await?;
p_device_log.save_join_cluster_request(candidate).await?;
}
VaultAction::UpdateMembership { .. } => {
//changes made by another device, no need for any actions
}
VaultAction::AddMetaPassword { .. } => {
//changes made by another device, no need for any actions
}
VaultAction::Create(_) => {
// server's responsibities
}
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/node/db/actions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod recover;
pub mod sign_up;
pub mod ss_replication;
pub mod sign_up_claim;
pub mod ss_replication;
26 changes: 26 additions & 0 deletions core/src/node/db/actions/sign_up.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,29 @@ impl SignUpAction {
commit_log
}
}

#[cfg(test)]
mod test {
use anyhow::Result;

use crate::{
meta_tests::fixture::ClientDeviceFixture,
node::{
common::model::device::{DeviceCredentials, DeviceName},
db::actions::sign_up::SignUpAction,
},
};

#[tokio::test]
async fn test() -> Result<()> {
let client_fixture = ClientDeviceFixture::default();
let server_creds = DeviceCredentials::generate(DeviceName::from("server_device"));

let sign_up_action = SignUpAction {};
let events = sign_up_action.accept(client_fixture.user_creds.user(), server_creds.device);

assert_eq!(events.len(), 8);

Ok(())
}
}
Loading

0 comments on commit f4ecbf6

Please sign in to comment.