Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create utilities for writing unit tests with a Mock Provider (no rpc eth node required) #42

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions xps-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,29 @@ pub async fn run(host: String, port: u16) -> Result<()> {
handle.stopped().await;
Ok(())
}

#[cfg(test)]
mod test {
use std::str::FromStr;

use ethers::{
providers::{MockProvider, Provider},
types::{Address, U64},
};

use crate::types::GatewayContext;

pub async fn create_mock_context() -> (GatewayContext<Provider<MockProvider>>, MockProvider) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really cool. Thanks for leading the way on this!

let (provider, mock) = Provider::mocked();
mock.push(U64::from(2)).unwrap();

let gateway = GatewayContext::new(
Address::from_str("0x0000000000000000000000000000000000000000").unwrap(),
provider,
)
.await
.unwrap();

(gateway, mock)
}
}
40 changes: 40 additions & 0 deletions xps-gateway/src/rpc/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,43 @@ impl XpsServer for XpsMethods {
Ok(self.wallet.address())
}
}

#[cfg(test)]
mod tests {
use ethers::types::{Block, Transaction, U64};
use lib_didethresolver::types::{KeyEncoding, XmtpKeyPurpose};

use super::*;

#[tokio::test]
async fn test_rpc_revoke_installation() {
let (context, mock) = crate::test::create_mock_context().await;

mock.push(U64::from(0)).unwrap(); // transactioncount
mock.push(Block::<Transaction>::default()).unwrap(); // latest block

let methods = XpsMethods::new(&context);

let attr = XmtpAttribute {
encoding: KeyEncoding::Hex,
purpose: XmtpKeyPurpose::Installation,
};
let value = vec![0x01, 0x02, 0x03];
let res = methods
.revoke_installation(
"0x7e575682a8e450e33eb0493f9972821ae333cd7f".to_string(),
attr,
value,
Signature {
r: [0x01; 32].into(),
s: [0x02; 32].into(),
v: 0x01,
},
)
.await;
if let Err(e) = res {
println!("{:?}", e);
println!("{}", e);
}
}
}
Loading