Skip to content

Commit

Permalink
refactor: Remove unused imports, factor out test_utils from fission-core
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Mar 25, 2024
1 parent 91d7adb commit 7d6318f
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 82 deletions.
4 changes: 4 additions & 0 deletions fission-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,7 @@ idna = "0.5.0"
[dev-dependencies]
test-log = { workspace = true }
testresult = { workspace = true }

[features]
default = []
test_utils = []
63 changes: 5 additions & 58 deletions fission-core/src/caps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,53 +175,23 @@ impl Serialize for FissionAbility {
#[cfg(test)]
mod tests {
use super::{CmdAccountCreate, CmdAccountManage};
use crate::caps::{CmdAccountInfo, FissionAbility};
use crate::{
caps::{CmdAccountInfo, FissionAbility},
test_utils::{delegate, setup_agents, varsig_header},
};
use assert_matches::assert_matches;
use libipld::Ipld;
use rand::rngs::OsRng;
use std::{collections::BTreeMap, convert::Infallible, time::SystemTime};
use testresult::TestResult;
use ucan::{
ability::{arguments::Named, command::ToCommand, parse::ParseAbility},
crypto::{
signature::Envelope,
varsig::{self, header::EdDsaHeader},
},
delegation::{self, store::Store},
did::preset::{Signer, Verifier},
delegation::{self},
invocation::{
self,
agent::{InvokeError, Recipient},
Agent,
},
time::Timestamp,
Invocation,
};

fn varsig_header() -> varsig::header::Preset {
varsig::header::Preset::EdDsa(EdDsaHeader {
codec: varsig::encoding::Preset::DagCbor,
})
}

fn setup_agents<T: ToCommand + Clone + ParseAbility>(
delegation_store: &delegation::store::MemoryStore,
) -> (
Agent<invocation::store::MemoryStore<T>, &'_ delegation::store::MemoryStore, T>,
delegation::Agent<&'_ delegation::store::MemoryStore>,
)
where
Named<Ipld>: From<T>,
{
let sk = ed25519_dalek::SigningKey::generate(&mut OsRng);
let did = Verifier::Key(ucan::did::key::Verifier::EdDsa(sk.verifying_key()));
let signer = Signer::Key(ucan::did::key::Signer::EdDsa(sk));
let inv_store = invocation::store::MemoryStore::<T>::default();
let invocation_agent = Agent::new(did.clone(), signer.clone(), inv_store, delegation_store);
let delegation_agent = delegation::Agent::new(did, signer, delegation_store);
(invocation_agent, delegation_agent)
}

fn simulate_create_account<'a>(
delegations: &'a delegation::store::MemoryStore,
client: &Agent<
Expand Down Expand Up @@ -310,29 +280,6 @@ mod tests {
)
}

fn delegate(
delegations: &delegation::store::MemoryStore,
from: &delegation::Agent<&delegation::store::MemoryStore>,
to: &ucan::did::preset::Verifier,
subject: Option<&ucan::did::preset::Verifier>,
cmd: &str,
) -> TestResult {
let ucan = from.delegate(
to.clone(),
subject.cloned(),
None,
cmd.to_string(),
Vec::new(),
BTreeMap::new(),
Timestamp::five_years_from_now(),
None,
SystemTime::now(),
varsig_header(),
)?;
delegations.insert(ucan.cid()?, ucan)?;
Ok(())
}

#[test_log::test]
fn simulate_account_create_flow() -> TestResult {
// delegations are simulated to be "public".
Expand Down
2 changes: 2 additions & 0 deletions fission-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ pub mod dns;
pub mod ed_did_key;
pub mod revocation;
pub mod serde_value_source;
#[cfg(any(test, feature = "test_utils"))]
pub mod test_utils;
pub mod username;
97 changes: 97 additions & 0 deletions fission-core/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//! Utilities for testing (TODO this should be a temporary module. Let's figure out simpler APIs, perhaps builders, etc. for rs-ucan)
use anyhow::Result;
use libipld::Ipld;
use rand::rngs::OsRng;
use std::{collections::BTreeMap, time::SystemTime};
use ucan::{
ability::{arguments::Named, command::ToCommand, parse::ParseAbility},
crypto::{
signature::Envelope,
varsig::{self, header::EdDsaHeader},
},
delegation::{self, store::Store},
did::preset::{Signer, Verifier},
invocation::{self, Agent},
time::Timestamp,
Delegation,
};

/// The dag-cbor varsig header
pub fn varsig_header() -> varsig::header::Preset {
varsig::header::Preset::EdDsa(EdDsaHeader {
codec: varsig::encoding::Preset::DagCbor,
})
}

/// Setup a simple agent that can delegate and invoke
pub fn setup_agents<T: ToCommand + Clone + ParseAbility>(
delegation_store: &delegation::store::MemoryStore,
) -> (
Agent<invocation::store::MemoryStore<T>, &'_ delegation::store::MemoryStore, T>,
delegation::Agent<&'_ delegation::store::MemoryStore>,
)
where
Named<Ipld>: From<T>,
{
let sk = ed25519_dalek::SigningKey::generate(&mut OsRng);
let did = Verifier::Key(ucan::did::key::Verifier::EdDsa(sk.verifying_key()));
let signer = Signer::Key(ucan::did::key::Signer::EdDsa(sk));
let inv_store = invocation::store::MemoryStore::<T>::default();
let invocation_agent = Agent::new(did.clone(), signer.clone(), inv_store, delegation_store);
let delegation_agent = delegation::Agent::new(did, signer, delegation_store);
(invocation_agent, delegation_agent)
}

/// Setup a simple agent that can only invoke
pub fn setup_invocation_agent<T: ToCommand + Clone + ParseAbility>(
) -> Agent<invocation::store::MemoryStore<T>, delegation::store::MemoryStore, T>
where
Named<Ipld>: From<T>,
{
let sk = ed25519_dalek::SigningKey::generate(&mut OsRng);
let did = Verifier::Key(ucan::did::key::Verifier::EdDsa(sk.verifying_key()));
let signer = Signer::Key(ucan::did::key::Signer::EdDsa(sk));
let inv_store = invocation::store::MemoryStore::<T>::default();
Agent::new(
did.clone(),
signer.clone(),
inv_store,
delegation::store::MemoryStore::default(),
)
}

/// Create a delegation from a delegation agent
pub fn create_delegation(
from: &delegation::Agent<&delegation::store::MemoryStore>,
to: &ucan::did::preset::Verifier,
subject: Option<&ucan::did::preset::Verifier>,
cmd: &str,
) -> Result<Delegation> {
let ucan = from.delegate(
to.clone(),
subject.cloned(),
None,
cmd.to_string(),
Vec::new(),
BTreeMap::new(),
Timestamp::five_years_from_now(),
None,
SystemTime::now(),
varsig_header(),
)?;

Ok(ucan)
}

/// Create and store a delegation from given agent in given delegation store
pub fn delegate(
delegations: &delegation::store::MemoryStore,
from: &delegation::Agent<&delegation::store::MemoryStore>,
to: &ucan::did::preset::Verifier,
subject: Option<&ucan::did::preset::Verifier>,
cmd: &str,
) -> Result<()> {
let ucan = create_delegation(from, to, subject, cmd)?;
delegations.insert(ucan.cid()?, ucan)?;
Ok(())
}
1 change: 1 addition & 0 deletions fission-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ data-encoding = "2.5.0"
assert-json-diff = "2.0"
assert_matches = "1.5.0"
blake3 = "1.4.1"
fission-core = { path = "../fission-core", version = "0.1", features = ["test_utils"] }
test-log = { workspace = true }
testresult = { workspace = true }

Expand Down
20 changes: 3 additions & 17 deletions fission-server/src/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,12 @@

use crate::{
app_state::AppState,
db,
db::Conn,
error::{AppError, AppResult},
models::revocation::find_revoked_subset,
setups::ServerSetup,
};
use anyhow::{bail, Result};
use fission_core::{
capabilities::did::Did,
caps::FissionAbility,
revocation::{canonical_cid, Revocation},
};
use fission_core::{capabilities::did::Did, caps::FissionAbility, revocation::Revocation};
use http::StatusCode;
use libipld::{raw::RawCodec, Ipld};
use serde::de::DeserializeOwned;
use std::collections::BTreeSet;
use libipld::Ipld;
use ucan::{
ability::{arguments::Named, command::ToCommand, parse::ParseAbility},
crypto::signature::Envelope,
Expand Down Expand Up @@ -55,7 +45,7 @@ where
///
/// The UCAN from the `authorization`'s canonical CID needs to match the revocation's
/// CID.
pub fn validate_revocation(&self, revocation: &Revocation) -> AppResult<()> {
pub fn validate_revocation(&self, _revocation: &Revocation) -> AppResult<()> {
// TODO
Ok(())
}
Expand Down Expand Up @@ -110,10 +100,6 @@ where

#[cfg(test)]
mod tests {
use super::*;

use fission_core::ed_did_key::EdDidKey;
use rs_ucan::builder::UcanBuilder;
use testresult::TestResult;

#[test_log::test(tokio::test)]
Expand Down
1 change: 0 additions & 1 deletion fission-server/src/extract/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use fission_core::authority::{
use http::{HeaderValue, StatusCode};
use libipld::Ipld;
use serde_json::json;
use std::str::FromStr;
use ucan::{
ability::{arguments::Named, command::ToCommand, parse::ParseAbility},
Delegation,
Expand Down
8 changes: 2 additions & 6 deletions fission-server/src/routes/capability_indexing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ use crate::{
};
use axum::extract::State;
use diesel_async::{scoped_futures::ScopedFutureExt, AsyncConnection};
use fission_core::{
capabilities::{did::Did, indexing::IndexingAbility},
caps::CmdCapabilityFetch,
common::UcansResponse,
};
use fission_core::{capabilities::did::Did, caps::CmdCapabilityFetch, common::UcansResponse};
use http::StatusCode;

/// Return capabilities for a given DID
Expand Down Expand Up @@ -59,7 +55,7 @@ mod tests {
};
use anyhow::Result;
use assert_matches::assert_matches;
use fission_core::ed_did_key::EdDidKey;
use fission_core::{capabilities::indexing::IndexingAbility, ed_did_key::EdDidKey};
use http::Method;
use rs_ucan::{
builder::UcanBuilder,
Expand Down

0 comments on commit 7d6318f

Please sign in to comment.