Skip to content

Commit

Permalink
Merge pull request #85 from near/daniyar/negative-tests
Browse files Browse the repository at this point in the history
feat: add negative integration tests
  • Loading branch information
volovyks authored Apr 20, 2023
2 parents 5055cf3 + b36ecd1 commit 10822e6
Show file tree
Hide file tree
Showing 9 changed files with 476 additions and 115 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

198 changes: 91 additions & 107 deletions integration-tests/tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
mod docker;
mod mpc;

use crate::docker::{LeaderNode, SignNode};
use bollard::Docker;
use docker::{redis::Redis, relayer::Relayer};
use futures::future::BoxFuture;
use mpc_recovery::msg::{
AddKeyRequest, AddKeyResponse, LeaderRequest, LeaderResponse, NewAccountRequest,
NewAccountResponse,
};
use rand::{distributions::Alphanumeric, Rng};
use std::time::Duration;
use threshold_crypto::PublicKeySet;
use workspaces::{network::Sandbox, AccountId, Worker};

mod docker;

const NETWORK: &str = "mpc_recovery_integration_test_network";
#[cfg(target_os = "linux")]
const HOST_MACHINE_FROM_DOCKER: &str = "172.17.0.1";
#[cfg(target_os = "macos")]
const HOST_MACHINE_FROM_DOCKER: &str = "docker.for.mac.localhost";

struct TestContext<'a> {
pub struct TestContext<'a> {
leader_node: &'a LeaderNode,
pk_set: &'a PublicKeySet,
worker: &'a Worker<Sandbox>,
Expand Down Expand Up @@ -117,108 +113,96 @@ where
result
}

#[tokio::test]
async fn test_trio() -> anyhow::Result<()> {
with_nodes(4, 3, 3, |ctx| {
Box::pin(async move {
let payload: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(10)
.map(char::from)
.collect();
let (status_code, response) = ctx
.leader_node
.submit(LeaderRequest {
payload: payload.clone(),
})
.await?;

assert_eq!(status_code, 200);
if let LeaderResponse::Ok { signature } = response {
assert!(ctx.pk_set.public_key().verify(&signature, payload));
} else {
panic!("response was not successful");
}
mod account {
use rand::{distributions::Alphanumeric, Rng};
use workspaces::{network::Sandbox, AccountId, Worker};

pub fn random(worker: &Worker<Sandbox>) -> anyhow::Result<AccountId> {
let account_id_rand: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(10)
.map(char::from)
.collect();
Ok(format!(
"mpc-recovery-{}.{}",
account_id_rand.to_lowercase(),
worker.root_account()?.id()
)
.parse()?)
}

Ok(())
})
})
.await
pub fn malformed() -> String {
let random: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(10)
.map(char::from)
.collect();
format!("malformed-account-{}-!@#$%", random.to_lowercase())
}
}

#[tokio::test]
async fn test_basic_action() -> anyhow::Result<()> {
with_nodes(4, 3, 3, |ctx| {
Box::pin(async move {
// Create new account
// TODO: write a test with real token
// "validToken" should triger test token verifyer and return success
let id_token = "validToken".to_string();
let account_id_rand: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(10)
.map(char::from)
.collect();
let account_id: AccountId = format!(
"mpc-recovery-{}.{}",
account_id_rand.to_lowercase(),
ctx.worker.root_account()?.id()
)
.parse()
.unwrap();

let user_public_key =
near_crypto::SecretKey::from_random(near_crypto::KeyType::ED25519)
.public_key()
.to_string();

let (status_code, new_acc_response) = ctx
.leader_node
.new_account(NewAccountRequest {
near_account_id: account_id.to_string(),
oidc_token: id_token.clone(),
public_key: user_public_key.clone(),
})
.await
.unwrap();
assert_eq!(status_code, 200);
assert!(matches!(new_acc_response, NewAccountResponse::Ok));

tokio::time::sleep(Duration::from_millis(2000)).await;

// Check that account exists and it has the requested public key
let access_keys = ctx.worker.view_access_keys(&account_id).await?;
assert!(access_keys
.iter()
.any(|ak| ak.public_key.to_string() == user_public_key));

let new_user_public_key =
near_crypto::SecretKey::from_random(near_crypto::KeyType::ED25519)
.public_key()
.to_string();

let (status_code2, add_key_response) = ctx
.leader_node
.add_key(AddKeyRequest {
near_account_id: account_id.to_string(),
oidc_token: id_token.clone(),
public_key: new_user_public_key.clone(),
})
.await?;

assert_eq!(status_code2, 200);
assert!(matches!(add_key_response, AddKeyResponse::Ok));

tokio::time::sleep(Duration::from_millis(2000)).await;

// Check that account has the requested public key
let access_keys = ctx.worker.view_access_keys(&account_id).await?;
assert!(access_keys
.iter()
.any(|ak| ak.public_key.to_string() == new_user_public_key));
mod key {
use rand::{distributions::Alphanumeric, Rng};

pub fn random() -> String {
near_crypto::SecretKey::from_random(near_crypto::KeyType::ED25519)
.public_key()
.to_string()
}

pub fn malformed() -> String {
let random: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(10)
.map(char::from)
.collect();
format!("malformed-key-{}-!@#$%", random.to_lowercase())
}
}

mod token {
pub fn valid() -> String {
"validToken".to_string()
}

pub fn invalid() -> String {
"invalidToken".to_string()
}
}

mod check {
use crate::TestContext;
use workspaces::AccountId;

pub async fn access_key_exists<'a>(
ctx: &TestContext<'a>,
account_id: &AccountId,
public_key: &str,
) -> anyhow::Result<()> {
let access_keys = ctx.worker.view_access_keys(account_id).await?;

if access_keys
.iter()
.any(|ak| ak.public_key.to_string() == public_key)
{
Ok(())
})
})
.await
} else {
Err(anyhow::anyhow!(
"could not find access key {public_key} on account {account_id}"
))
}
}

pub async fn no_account<'a>(
ctx: &TestContext<'a>,
account_id: &AccountId,
) -> anyhow::Result<()> {
if ctx.worker.view_account(account_id).await.is_err() {
Ok(())
} else {
Err(anyhow::anyhow!(
"expected account {account_id} to not exist, but it does"
))
}
}
}
2 changes: 2 additions & 0 deletions integration-tests/tests/mpc/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod negative;
mod positive;
Loading

0 comments on commit 10822e6

Please sign in to comment.