Skip to content

Commit

Permalink
reorganize integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Feb 22, 2024
1 parent ab052d5 commit 986c240
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 140 deletions.
1 change: 1 addition & 0 deletions lib-xps/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ mod integration_util;
mod it {
mod contact_ops;
mod messaging;
mod xps;
}
143 changes: 3 additions & 140 deletions lib-xps/tests/it/contact_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,119 +2,16 @@ use std::str::FromStr;

use anyhow::Error;

use ethers::providers::Middleware;
use ethers::types::{Address, Bytes, TransactionRequest, U256};
use ethers::utils::keccak256;
use ethers::{signers::LocalWallet, signers::Signer};
use crate::integration_util::*;
use ethers::types::{Address, U256};
use ethers::{signers::LocalWallet, signers::Signer};
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 @@ -389,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
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 986c240

Please sign in to comment.