Skip to content

Commit 6cda319

Browse files
committed
[+test] adapter - DummyAdapter - fix implementation of get_auth
1 parent c6f4b2d commit 6cda319

File tree

2 files changed

+65
-28
lines changed

2 files changed

+65
-28
lines changed

adapter/src/adapter.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use domain::{Asset, BigNum, Channel};
22

33
use crate::sanity::SanityChecker;
44

5+
#[derive(Debug, Eq, PartialEq)]
56
pub enum AdapterError<'a> {
67
Authentication(&'a str),
78
}

adapter/src/dummy.rs

+64-28
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,24 @@
11
use crate::adapter::{Adapter, AdapterError, Config};
22
use crate::sanity::SanityChecker;
33
use hex::encode;
4-
use std::collections::HashSet;
5-
use std::hash::{Hash, Hasher};
4+
use std::collections::HashMap;
65

7-
#[derive(Eq, Debug)]
6+
#[derive(Debug)]
87
pub struct DummyParticipant {
98
pub identity: String,
109
pub token: String,
1110
}
1211

13-
impl PartialEq for DummyParticipant {
14-
fn eq(&self, other: &Self) -> bool {
15-
self.identity == other.identity || self.token == other.token
16-
}
17-
}
18-
19-
impl Hash for DummyParticipant {
20-
fn hash<H: Hasher>(&self, state: &mut H) {
21-
self.identity.hash(state);
22-
self.token.hash(state);
23-
}
24-
}
25-
26-
pub struct DummyAdapter {
12+
pub struct DummyAdapter<'a> {
2713
pub config: Config,
2814
/// Dummy participants which will be used for
2915
/// Creator, Validator Leader, Validator Follower and etc.
30-
pub participants: HashSet<DummyParticipant>,
16+
pub participants: HashMap<&'a str, DummyParticipant>,
3117
}
3218

33-
impl SanityChecker for DummyAdapter {}
19+
impl SanityChecker for DummyAdapter<'_> {}
3420

35-
impl Adapter for DummyAdapter {
21+
impl Adapter for DummyAdapter<'_> {
3622
fn config(&self) -> &Config {
3723
&self.config
3824
}
@@ -42,10 +28,10 @@ impl Adapter for DummyAdapter {
4228
/// ```
4329
/// use adapter::{ConfigBuilder, Adapter};
4430
/// use adapter::dummy::DummyAdapter;
45-
/// use std::collections::HashSet;
31+
/// use std::collections::HashMap;
4632
///
4733
/// let config = ConfigBuilder::new("identity").build();
48-
/// let adapter = DummyAdapter { config, participants: HashSet::new() };
34+
/// let adapter = DummyAdapter { config, participants: HashMap::new() };
4935
///
5036
/// let actual = adapter.sign("abcdefghijklmnopqrstuvwxyz012345");
5137
/// let expected = "Dummy adapter signature for 6162636465666768696a6b6c6d6e6f707172737475767778797a303132333435 by identity";
@@ -64,10 +50,10 @@ impl Adapter for DummyAdapter {
6450
/// ```
6551
/// use adapter::{ConfigBuilder, Adapter};
6652
/// use adapter::dummy::DummyAdapter;
67-
/// use std::collections::HashSet;
53+
/// use std::collections::HashMap;
6854
///
6955
/// let config = ConfigBuilder::new("identity").build();
70-
/// let adapter = DummyAdapter { config, participants: HashSet::new() };
56+
/// let adapter = DummyAdapter { config, participants: HashMap::new() };
7157
///
7258
/// let signature = "Dummy adapter signature for 6162636465666768696a6b6c6d6e6f707172737475767778797a303132333435 by identity";
7359
/// assert!(adapter.verify("identity", "doesn't matter", signature) )
@@ -85,9 +71,9 @@ impl Adapter for DummyAdapter {
8571
match self
8672
.participants
8773
.iter()
88-
.find(|&participant| participant.identity == validator)
74+
.find(|&(_, participant)| participant.identity == validator)
8975
{
90-
Some(participant) => Ok(participant.token.to_string()),
76+
Some((_, participant)) => Ok(participant.token.to_string()),
9177
None => Err(AdapterError::Authentication("Identity not found")),
9278
}
9379
}
@@ -99,11 +85,11 @@ mod test {
9985
use crate::adapter::ConfigBuilder;
10086

10187
#[test]
102-
fn sings_state_root_and_verifies_it() {
88+
fn dummy_adapter_sings_state_root_and_verifies_it() {
10389
let config = ConfigBuilder::new("identity").build();
10490
let adapter = DummyAdapter {
10591
config,
106-
participants: HashSet::new(),
92+
participants: HashMap::new(),
10793
};
10894

10995
let expected_signature = "Dummy adapter signature for 6162636465666768696a6b6c6d6e6f707172737475767778797a303132333435 by identity";
@@ -113,4 +99,54 @@ mod test {
11399

114100
assert!(adapter.verify("identity", "doesn't matter", &actual_signature))
115101
}
102+
103+
#[test]
104+
fn get_auth_with_empty_participators() {
105+
let adapter = DummyAdapter {
106+
config: ConfigBuilder::new("identity").build(),
107+
participants: HashMap::new(),
108+
};
109+
110+
assert_eq!(
111+
AdapterError::Authentication("Identity not found"),
112+
adapter.get_auth("non-existing").unwrap_err()
113+
);
114+
115+
let mut participants = HashMap::new();
116+
participants.insert(
117+
"identity_key",
118+
DummyParticipant {
119+
identity: "identity".to_string(),
120+
token: "token".to_string(),
121+
},
122+
);
123+
let adapter = DummyAdapter {
124+
config: ConfigBuilder::new("identity").build(),
125+
participants,
126+
};
127+
128+
assert_eq!(
129+
AdapterError::Authentication("Identity not found"),
130+
adapter.get_auth("non-existing").unwrap_err()
131+
);
132+
}
133+
134+
#[test]
135+
fn get_auth_with_existing_participator() {
136+
let mut participants = HashMap::new();
137+
participants.insert(
138+
"identity_key",
139+
DummyParticipant {
140+
identity: "identity".to_string(),
141+
token: "token".to_string(),
142+
},
143+
);
144+
145+
let adapter = DummyAdapter {
146+
config: ConfigBuilder::new("identity").build(),
147+
participants,
148+
};
149+
150+
assert_eq!(Ok("token".to_string()), adapter.get_auth("identity"));
151+
}
116152
}

0 commit comments

Comments
 (0)