Skip to content

Commit 7d8b394

Browse files
committed
test: [#1231] add tests for AuthenticationService
1 parent 63e773a commit 7d8b394

File tree

1 file changed

+183
-19
lines changed
  • packages/tracker-core/src/authentication

1 file changed

+183
-19
lines changed

packages/tracker-core/src/authentication/service.rs

+183-19
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl AuthenticationService {
3131
///
3232
/// Will return an error if the the authentication key cannot be verified.
3333
pub async fn authenticate(&self, key: &Key) -> Result<(), Error> {
34-
if self.is_private() {
34+
if self.tracker_is_private() {
3535
self.verify_auth_key(key).await
3636
} else {
3737
Ok(())
@@ -40,7 +40,7 @@ impl AuthenticationService {
4040

4141
/// Returns `true` is the tracker is in private mode.
4242
#[must_use]
43-
pub fn is_private(&self) -> bool {
43+
fn tracker_is_private(&self) -> bool {
4444
self.config.private
4545
}
4646

@@ -72,34 +72,198 @@ impl AuthenticationService {
7272
#[cfg(test)]
7373
mod tests {
7474

75-
mod the_tracker_configured_as_private {
75+
mod the_authentication_service {
7676

77-
use std::str::FromStr;
78-
use std::sync::Arc;
77+
mod when_the_tracker_is_public {
7978

80-
use torrust_tracker_test_helpers::configuration;
79+
use std::str::FromStr;
80+
use std::sync::Arc;
8181

82-
use crate::authentication;
83-
use crate::authentication::key::repository::in_memory::InMemoryKeyRepository;
84-
use crate::authentication::service::AuthenticationService;
82+
use torrust_tracker_configuration::Core;
8583

86-
fn instantiate_authentication() -> AuthenticationService {
87-
let config = configuration::ephemeral_private();
84+
use crate::authentication::key::repository::in_memory::InMemoryKeyRepository;
85+
use crate::authentication::service::AuthenticationService;
86+
use crate::authentication::{self};
8887

89-
let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default());
88+
fn instantiate_authentication_for_public_tracker() -> AuthenticationService {
89+
let config = Core {
90+
private: false,
91+
..Default::default()
92+
};
9093

91-
AuthenticationService::new(&config.core, &in_memory_key_repository.clone())
94+
let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default());
95+
96+
AuthenticationService::new(&config, &in_memory_key_repository.clone())
97+
}
98+
99+
#[tokio::test]
100+
async fn it_should_always_authenticate_when_the_tracker_is_public() {
101+
let authentication = instantiate_authentication_for_public_tracker();
102+
103+
let unregistered_key = authentication::Key::from_str("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap();
104+
105+
let result = authentication.authenticate(&unregistered_key).await;
106+
107+
assert!(result.is_ok());
108+
}
92109
}
93110

94-
#[tokio::test]
95-
async fn it_should_not_authenticate_an_unregistered_key() {
96-
let authentication = instantiate_authentication();
111+
mod when_the_tracker_is_private {
112+
113+
use std::str::FromStr;
114+
use std::sync::Arc;
115+
use std::time::Duration;
116+
117+
use torrust_tracker_configuration::v2_0_0::core::PrivateMode;
118+
use torrust_tracker_configuration::Core;
119+
120+
use crate::authentication::key::repository::in_memory::InMemoryKeyRepository;
121+
use crate::authentication::service::AuthenticationService;
122+
use crate::authentication::{self, PeerKey};
123+
124+
fn instantiate_authentication_for_private_tracker() -> AuthenticationService {
125+
let config = Core {
126+
private: true,
127+
..Default::default()
128+
};
129+
130+
let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default());
131+
132+
AuthenticationService::new(&config, &in_memory_key_repository.clone())
133+
}
134+
135+
#[tokio::test]
136+
async fn it_should_authenticate_a_registered_key() {
137+
let config = Core {
138+
private: true,
139+
..Default::default()
140+
};
141+
142+
let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default());
143+
144+
let key = authentication::Key::from_str("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap();
145+
146+
in_memory_key_repository
147+
.insert(&PeerKey {
148+
key: key.clone(),
149+
valid_until: None,
150+
})
151+
.await;
152+
153+
let authentication = AuthenticationService::new(&config, &in_memory_key_repository.clone());
154+
155+
let result = authentication.authenticate(&key).await;
156+
157+
assert!(result.is_ok());
158+
}
159+
160+
#[tokio::test]
161+
async fn it_should_not_authenticate_an_unregistered_key() {
162+
let authentication = instantiate_authentication_for_private_tracker();
163+
164+
let unregistered_key = authentication::Key::from_str("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap();
165+
166+
let result = authentication.authenticate(&unregistered_key).await;
167+
168+
assert!(result.is_err());
169+
}
97170

98-
let unregistered_key = authentication::Key::from_str("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap();
171+
#[tokio::test]
172+
async fn it_should_not_authenticate_a_registered_but_expired_key_by_default() {
173+
let config = Core {
174+
private: true,
175+
..Default::default()
176+
};
99177

100-
let result = authentication.authenticate(&unregistered_key).await;
178+
let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default());
101179

102-
assert!(result.is_err());
180+
let key = authentication::Key::from_str("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap();
181+
182+
// Register the key with an immediate expiration date.
183+
in_memory_key_repository
184+
.insert(&PeerKey {
185+
key: key.clone(),
186+
valid_until: Some(Duration::from_secs(0)),
187+
})
188+
.await;
189+
190+
let authentication = AuthenticationService::new(&config, &in_memory_key_repository.clone());
191+
192+
let result = authentication.authenticate(&key).await;
193+
194+
assert!(result.is_err());
195+
}
196+
197+
#[tokio::test]
198+
async fn it_should_not_authenticate_a_registered_but_expired_key_when_the_tracker_is_explicitly_configured_to_check_keys_expiration() {
199+
let config = Core {
200+
private: true,
201+
private_mode: Some(PrivateMode {
202+
check_keys_expiration: true,
203+
}),
204+
..Default::default()
205+
};
206+
207+
let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default());
208+
209+
let key = authentication::Key::from_str("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap();
210+
211+
// Register the key with an immediate expiration date.
212+
in_memory_key_repository
213+
.insert(&PeerKey {
214+
key: key.clone(),
215+
valid_until: Some(Duration::from_secs(0)),
216+
})
217+
.await;
218+
219+
let authentication = AuthenticationService::new(&config, &in_memory_key_repository.clone());
220+
221+
let result = authentication.authenticate(&key).await;
222+
223+
assert!(result.is_err());
224+
}
225+
226+
mod but_the_key_expiration_check_is_disabled_by_configuration {
227+
use std::str::FromStr;
228+
use std::sync::Arc;
229+
use std::time::Duration;
230+
231+
use torrust_tracker_configuration::v2_0_0::core::PrivateMode;
232+
use torrust_tracker_configuration::Core;
233+
234+
use crate::authentication::key::repository::in_memory::InMemoryKeyRepository;
235+
use crate::authentication::service::AuthenticationService;
236+
use crate::authentication::{self, PeerKey};
237+
238+
#[tokio::test]
239+
async fn it_should_authenticate_an_expired_registered_key() {
240+
let config = Core {
241+
private: true,
242+
private_mode: Some(PrivateMode {
243+
check_keys_expiration: false,
244+
}),
245+
..Default::default()
246+
};
247+
248+
let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default());
249+
250+
let key = authentication::Key::from_str("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap();
251+
252+
// Register the key with an immediate expiration date.
253+
in_memory_key_repository
254+
.insert(&PeerKey {
255+
key: key.clone(),
256+
valid_until: Some(Duration::from_secs(0)),
257+
})
258+
.await;
259+
260+
let authentication = AuthenticationService::new(&config, &in_memory_key_repository.clone());
261+
262+
let result = authentication.authenticate(&key).await;
263+
264+
assert!(result.is_ok());
265+
}
266+
}
103267
}
104268
}
105269
}

0 commit comments

Comments
 (0)