Skip to content

Commit

Permalink
Reorganize Integration Tests, re-introduce integration test module (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx authored Feb 22, 2024
1 parent e04ae49 commit 2f93b91
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 142 deletions.
7 changes: 7 additions & 0 deletions lib-xps/tests/integration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod integration_util;

mod it {
mod contact_ops;
mod messaging;
mod xps;
}
145 changes: 3 additions & 142 deletions lib-xps/tests/integration_test.rs → lib-xps/tests/it/contact_ops.rs
Original file line number Diff line number Diff line change
@@ -1,122 +1,17 @@
mod integration_util;

use std::str::FromStr;

use anyhow::Error;

use ethers::providers::Middleware;
use ethers::types::{Address, Bytes, TransactionRequest, U256};
use ethers::utils::keccak256;
use crate::integration_util::*;
use ethers::types::{Address, U256};
use ethers::{signers::LocalWallet, signers::Signer};
use integration_util::*;
use jsonrpsee::core::ClientError;
use lib_didethresolver::{
did_registry::RegistrySignerExt,
types::{DidUrl, KeyEncoding, XmtpAttribute, XmtpKeyPurpose, NULL_ADDRESS},
};
use lib_xps::rpc::{XpsClient, DEFAULT_ATTRIBUTE_VALIDITY};
use messaging::ConversationSignerExt;
use xps_types::{Message, Status, Unit};

#[tokio::test]
async fn test_say_hello() -> Result<(), Error> {
with_xps_client(None, None, |client, _, _, _| async move {
let result = client.status().await?;
assert_eq!(result, "OK");
Ok(())
})
.await
}

#[tokio::test]
async fn test_send_message() -> Result<(), Error> {
with_xps_client(None, None, |client, context, _resolver, anvil| async move {
let wallet: LocalWallet = anvil.keys()[3].clone().into();
let me = get_user(&anvil, 3).await;

let conversation_id = keccak256(b"conversation_id");
let payload = Bytes::from_static(b"payload");

let signature = wallet
.sign_xmtp_message(
&context.conversation,
conversation_id,
payload.clone(),
me.address(),
)
.await?;

let message = Message {
conversation_id,
payload,
identity: me.address(),
signature,
};

let pre_nonce = context.conversation.nonce(me.address()).call().await?;
assert!(pre_nonce == U256::zero());

let result = client.send_message(message).await;
assert!(result.is_ok());
assert!(result.unwrap().status == Status::Success);

// post-nonce should be same as pre-nonce + 1
let post_nonce = context.conversation.nonce(me.address()).call().await?;
assert!(post_nonce == pre_nonce + 1);
Ok(())
})
.await
}

#[tokio::test]
async fn test_send_message_fail() -> Result<(), Error> {
with_xps_client(None, None, |client, context, _resolver, anvil| async move {
let wallet: LocalWallet = anvil.keys()[3].clone().into();
let me = get_user(&anvil, 3).await;

let conversation_id = keccak256(b"conversation_id");
let payload = Bytes::from_static(b"payload");

let signature = wallet
.sign_xmtp_message(
&context.conversation,
keccak256(b"unmatched_conversation_id"),
payload.clone(),
me.address(),
)
.await?;

let message = Message {
conversation_id,
payload,
identity: me.address(),
signature,
};

let pre_nonce = context.conversation.nonce(me.address()).call().await?;
assert!(pre_nonce == U256::zero());

let result = client.send_message(message).await;
assert!(result.is_err());
println!("{:?}", result.err());

// post-nonce should be same as pre-nonce
let post_nonce = context.conversation.nonce(me.address()).call().await?;
assert!(post_nonce == pre_nonce);
Ok(())
})
.await
}

#[tokio::test]
async fn test_wallet_address() -> Result<(), Error> {
with_xps_client(None, None, |client, _, _, _| async move {
let result = client.wallet_address().await?;
assert_ne!(result, Address::zero());
Ok(())
})
.await
}
use xps_types::Status;

#[tokio::test]
async fn test_grant_revoke() -> Result<(), Error> {
Expand Down Expand Up @@ -391,40 +286,6 @@ async fn test_revoke_installation() -> Result<(), Error> {
.await
}

#[tokio::test]
async fn test_balance() -> Result<(), Error> {
with_xps_client(
None,
Some(0.into()),
|client, context, _resolver, _anvil| async move {
// by default, we have no balance. verify that.
let mut balance = client.balance().await?;
assert_eq!(balance.balance, U256::from(0));
assert_eq!(balance.unit, Unit::Eth);

// fund the wallet account.
let accounts = context.signer.get_accounts().await?;
let from = accounts[1];
let tx = TransactionRequest::new()
.to(client.wallet_address().await?)
.value(5_000_000_000_000_000_000_000_u128)
.from(from);
context.signer.send_transaction(tx, None).await?.await?;

// check to see if the balance gets updated.
balance = client.balance().await?;
assert_eq!(
balance.balance,
U256::from(5_000_000_000_000_000_000_000_u128)
);
assert_eq!(balance.unit, Unit::Eth);

Ok(())
},
)
.await
}

#[tokio::test]
async fn test_fetch_key_packages() -> Result<(), Error> {
with_xps_client(None, None, |client, context, _, anvil| async move {
Expand Down
89 changes: 89 additions & 0 deletions lib-xps/tests/it/messaging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use anyhow::Error;

use crate::integration_util::*;
use ethers::signers::LocalWallet;
use ethers::types::{Bytes, U256};
use ethers::utils::keccak256;
use lib_xps::rpc::XpsClient;
use messaging::ConversationSignerExt;
use xps_types::{Message, Status};

#[tokio::test]
async fn test_send_message() -> Result<(), Error> {
with_xps_client(None, None, |client, context, _resolver, anvil| async move {
let wallet: LocalWallet = anvil.keys()[3].clone().into();
let me = get_user(&anvil, 3).await;

let conversation_id = keccak256(b"conversation_id");
let payload = Bytes::from_static(b"payload");

let signature = wallet
.sign_xmtp_message(
&context.conversation,
conversation_id,
payload.clone(),
me.address(),
)
.await?;

let message = Message {
conversation_id,
payload,
identity: me.address(),
signature,
};

let pre_nonce = context.conversation.nonce(me.address()).call().await?;
assert!(pre_nonce == U256::zero());

let result = client.send_message(message).await;
assert!(result.is_ok());
assert!(result.unwrap().status == Status::Success);

// post-nonce should be same as pre-nonce + 1
let post_nonce = context.conversation.nonce(me.address()).call().await?;
assert!(post_nonce == pre_nonce + 1);
Ok(())
})
.await
}

#[tokio::test]
async fn test_send_message_fail() -> Result<(), Error> {
with_xps_client(None, None, |client, context, _resolver, anvil| async move {
let wallet: LocalWallet = anvil.keys()[3].clone().into();
let me = get_user(&anvil, 3).await;

let conversation_id = keccak256(b"conversation_id");
let payload = Bytes::from_static(b"payload");

let signature = wallet
.sign_xmtp_message(
&context.conversation,
keccak256(b"unmatched_conversation_id"),
payload.clone(),
me.address(),
)
.await?;

let message = Message {
conversation_id,
payload,
identity: me.address(),
signature,
};

let pre_nonce = context.conversation.nonce(me.address()).call().await?;
assert!(pre_nonce == U256::zero());

let result = client.send_message(message).await;
assert!(result.is_err());
println!("{:?}", result.err());

// post-nonce should be same as pre-nonce
let post_nonce = context.conversation.nonce(me.address()).call().await?;
assert!(post_nonce == pre_nonce);
Ok(())
})
.await
}
52 changes: 52 additions & 0 deletions lib-xps/tests/it/xps.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use anyhow::Error;

use crate::integration_util::*;
use ethers::providers::Middleware;
use ethers::types::{TransactionRequest, U256};

use lib_xps::rpc::XpsClient;
use xps_types::Unit;

#[tokio::test]
async fn test_say_hello() -> Result<(), Error> {
with_xps_client(None, None, |client, _, _, _| async move {
let result = client.status().await?;
assert_eq!(result, "OK");
Ok(())
})
.await
}

#[tokio::test]
async fn test_balance() -> Result<(), Error> {
with_xps_client(
None,
Some(0.into()),
|client, context, _resolver, _anvil| async move {
// by default, we have no balance. verify that.
let mut balance = client.balance().await?;
assert_eq!(balance.balance, U256::from(0));
assert_eq!(balance.unit, Unit::Eth);

// fund the wallet account.
let accounts = context.signer.get_accounts().await?;
let from = accounts[1];
let tx = TransactionRequest::new()
.to(client.wallet_address().await?)
.value(5_000_000_000_000_000_000_000_u128)
.from(from);
context.signer.send_transaction(tx, None).await?.await?;

// check to see if the balance gets updated.
balance = client.balance().await?;
assert_eq!(
balance.balance,
U256::from(5_000_000_000_000_000_000_000_u128)
);
assert_eq!(balance.unit, Unit::Eth);

Ok(())
},
)
.await
}

0 comments on commit 2f93b91

Please sign in to comment.