-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathpositive.rs
112 lines (95 loc) · 3.85 KB
/
positive.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use crate::{account, check, key, token, with_nodes};
use ed25519_dalek::Verifier;
use hyper::StatusCode;
use mpc_recovery::{
msg::{AddKeyRequest, AddKeyResponse, NewAccountRequest, NewAccountResponse},
transaction::{sign, to_dalek_combined_public_key},
};
use rand::{distributions::Alphanumeric, Rng};
use std::time::Duration;
#[tokio::test]
async fn test_trio() -> anyhow::Result<()> {
with_nodes(4, |ctx| {
Box::pin(async move {
let payload: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(10)
.map(char::from)
.collect();
// TODO integrate this better with testing
let client = reqwest::Client::new();
let signer_urls: Vec<_> = ctx
.signer_nodes
.iter()
.map(|s| s.local_address.clone())
.collect();
let signature = sign(&client, &signer_urls, payload.clone().into()).await?;
let combined_pub = to_dalek_combined_public_key(ctx.pk_set).unwrap();
combined_pub.verify(payload.as_bytes(), &signature)?;
Ok(())
})
})
.await
}
// TODO: write a test with real token
#[tokio::test]
async fn test_basic_action() -> anyhow::Result<()> {
with_nodes(4, |ctx| {
Box::pin(async move {
let account_id = account::random(ctx.worker)?;
let user_public_key = key::random();
// Create account
let (status_code, new_acc_response) = ctx
.leader_node
.new_account(NewAccountRequest {
near_account_id: account_id.to_string(),
oidc_token: token::valid(),
public_key: user_public_key.clone(),
})
.await?;
assert_eq!(status_code, StatusCode::OK);
assert!(matches!(new_acc_response, NewAccountResponse::Ok {
user_public_key: user_pk,
user_recovery_public_key: _,
near_account_id: acc_id,
} if user_pk == user_public_key && acc_id == account_id.to_string()
));
tokio::time::sleep(Duration::from_millis(2000)).await;
check::access_key_exists(&ctx, &account_id, &user_public_key).await?;
// Add key
let new_user_public_key = key::random();
let (status_code, add_key_response) = ctx
.leader_node
.add_key(AddKeyRequest {
near_account_id: Some(account_id.to_string()),
oidc_token: token::valid(),
public_key: new_user_public_key.clone(),
})
.await?;
assert_eq!(status_code, StatusCode::OK);
assert!(matches!(
add_key_response,
AddKeyResponse::Ok {
user_public_key: new_pk,
near_account_id: acc_id,
} if new_pk == new_user_public_key && acc_id == account_id.to_string()
));
tokio::time::sleep(Duration::from_millis(2000)).await;
check::access_key_exists(&ctx, &account_id, &new_user_public_key).await?;
// Adding the same key should now fail
let (status_code, _add_key_response) = ctx
.leader_node
.add_key(AddKeyRequest {
near_account_id: Some(account_id.to_string()),
oidc_token: token::valid(),
public_key: new_user_public_key.clone(),
})
.await?;
assert_eq!(status_code, StatusCode::OK);
tokio::time::sleep(Duration::from_millis(2000)).await;
check::access_key_exists(&ctx, &account_id, &new_user_public_key).await?;
Ok(())
})
})
.await
}