Skip to content

Commit 8397033

Browse files
committed
tests: add tests for DatabaseKeyRepository
1 parent 04ee425 commit 8397033

File tree

1 file changed

+73
-0
lines changed
  • packages/tracker-core/src/authentication/key/repository

1 file changed

+73
-0
lines changed

packages/tracker-core/src/authentication/key/repository/persisted.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,76 @@ impl DatabaseKeyRepository {
4646
Ok(keys)
4747
}
4848
}
49+
50+
#[cfg(test)]
51+
mod tests {
52+
53+
mod the_persisted_keys_repository_should {
54+
55+
use std::time::Duration;
56+
57+
use torrust_tracker_test_helpers::configuration;
58+
59+
use crate::authentication::key::repository::persisted::DatabaseKeyRepository;
60+
use crate::authentication::{Key, PeerKey};
61+
use crate::databases::setup::initialize_database;
62+
63+
#[test]
64+
fn persist_a_new_peer_key() {
65+
let configuration = configuration::ephemeral_public();
66+
67+
let database = initialize_database(&configuration);
68+
69+
let repository = DatabaseKeyRepository::new(&database);
70+
71+
let peer_key = PeerKey {
72+
key: Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap(),
73+
valid_until: Some(Duration::new(9999, 0)),
74+
};
75+
76+
let result = repository.add(&peer_key);
77+
78+
assert!(result.is_ok());
79+
}
80+
81+
#[test]
82+
fn remove_a_persisted_peer_key() {
83+
let configuration = configuration::ephemeral_public();
84+
85+
let database = initialize_database(&configuration);
86+
87+
let repository = DatabaseKeyRepository::new(&database);
88+
89+
let peer_key = PeerKey {
90+
key: Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap(),
91+
valid_until: Some(Duration::new(9999, 0)),
92+
};
93+
94+
let _unused = repository.add(&peer_key);
95+
96+
let result = repository.remove(&peer_key.key);
97+
98+
assert!(result.is_ok());
99+
}
100+
101+
#[test]
102+
fn load_all_persisted_peer_keys() {
103+
let configuration = configuration::ephemeral_public();
104+
105+
let database = initialize_database(&configuration);
106+
107+
let repository = DatabaseKeyRepository::new(&database);
108+
109+
let peer_key = PeerKey {
110+
key: Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap(),
111+
valid_until: Some(Duration::new(9999, 0)),
112+
};
113+
114+
let _unused = repository.add(&peer_key);
115+
116+
let keys = repository.load_keys().unwrap();
117+
118+
assert_eq!(keys, vec!(peer_key));
119+
}
120+
}
121+
}

0 commit comments

Comments
 (0)