web3-proxy/web3_proxy/tests/test_users.rs

87 lines
2.3 KiB
Rust
Raw Normal View History

2023-06-29 22:41:21 +03:00
mod common;
use crate::common::TestApp;
2023-06-30 22:53:21 +03:00
use axum::headers::Authorization;
use ethers::{signers::Signer, types::Signature};
use hashbrown::HashMap;
use serde::Deserialize;
use serde_json::Value;
use tracing::{debug, info, trace};
use ulid::Ulid;
use web3_proxy::frontend::users::authentication::PostLogin;
/// TODO: use this type in the frontend
#[derive(Debug, Deserialize)]
struct LoginPostResponse {
pub bearer_token: Ulid,
pub rpc_keys: Value,
/// unknown data gets put here
#[serde(flatten, default = "HashMap::default")]
pub extra: HashMap<String, serde_json::Value>,
}
2023-06-29 22:41:21 +03:00
/// TODO: 191 and the other message formats in another test
#[cfg_attr(not(feature = "tests-needing-docker"), ignore)]
2023-06-29 22:41:21 +03:00
#[test_log::test(tokio::test)]
async fn test_log_in_and_out() {
let x = TestApp::spawn(true).await;
2023-06-29 22:41:21 +03:00
2023-06-30 22:53:21 +03:00
let r = reqwest::Client::new();
2023-06-29 23:12:35 +03:00
let w = x.wallet(0);
2023-06-30 22:53:21 +03:00
let login_get_url = format!("{}user/login/{:?}", x.proxy_provider.url(), w.address());
let login_message = r.get(login_get_url).send().await.unwrap();
2023-06-30 22:53:21 +03:00
let login_message = login_message.text().await.unwrap();
2023-06-30 22:53:21 +03:00
// sign the message and POST it
let signed: Signature = w.sign_message(&login_message).await.unwrap();
trace!(?signed);
2023-06-30 22:53:21 +03:00
let post_login_data = PostLogin {
msg: login_message,
sig: signed.to_string(),
referral_code: None,
};
debug!(?post_login_data);
2023-06-30 22:53:21 +03:00
let login_post_url = format!("{}user/login", x.proxy_provider.url());
let login_response = r
.post(login_post_url)
.json(&post_login_data)
.send()
.await
.unwrap()
.json::<LoginPostResponse>()
.await
.unwrap();
2023-06-30 22:53:21 +03:00
info!(?login_response);
// use the bearer token to log out
let logout_post_url = format!("{}user/logout", x.proxy_provider.url());
let logout_response = r
.post(logout_post_url)
.bearer_auth(login_response.bearer_token)
.send()
.await
.unwrap()
.text()
.await
.unwrap();
info!(?logout_response);
assert_eq!(logout_response, "goodbye");
2023-06-29 22:41:21 +03:00
}
2023-06-30 22:53:21 +03:00
// #[cfg_attr(not(feature = "tests-needing-docker"), ignore)]
#[ignore = "under construction"]
2023-06-29 22:41:21 +03:00
#[test_log::test(tokio::test)]
async fn test_referral_bonus() {
let x = TestApp::spawn(true).await;
2023-06-29 22:41:21 +03:00
todo!();
}