forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthentication.rs
More file actions
105 lines (75 loc) · 3.04 KB
/
authentication.rs
File metadata and controls
105 lines (75 loc) · 3.04 KB
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
use torrust_tracker_test_helpers::configuration;
use tracing::level_filters::LevelFilter;
use crate::common::http::{Query, QueryParam};
use crate::common::logging::{tracing_stderr_init, INIT};
use crate::servers::api::v1::asserts::{assert_token_not_valid, assert_unauthorized};
use crate::servers::api::v1::client::Client;
use crate::servers::api::Started;
#[tokio::test]
async fn should_authenticate_requests_by_using_a_token_query_param() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});
let env = Started::new(&configuration::ephemeral().into()).await;
let token = env.get_connection_info().api_token.unwrap();
let response = Client::new(env.get_connection_info())
.get_request_with_query("stats", Query::params([QueryParam::new("token", &token)].to_vec()))
.await;
assert_eq!(response.status(), 200);
env.stop().await;
}
#[tokio::test]
async fn should_not_authenticate_requests_when_the_token_is_missing() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});
let env = Started::new(&configuration::ephemeral().into()).await;
let response = Client::new(env.get_connection_info())
.get_request_with_query("stats", Query::default())
.await;
assert_unauthorized(response).await;
env.stop().await;
}
#[tokio::test]
async fn should_not_authenticate_requests_when_the_token_is_empty() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});
let env = Started::new(&configuration::ephemeral().into()).await;
let response = Client::new(env.get_connection_info())
.get_request_with_query("stats", Query::params([QueryParam::new("token", "")].to_vec()))
.await;
assert_token_not_valid(response).await;
env.stop().await;
}
#[tokio::test]
async fn should_not_authenticate_requests_when_the_token_is_invalid() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});
let env = Started::new(&configuration::ephemeral().into()).await;
let response = Client::new(env.get_connection_info())
.get_request_with_query("stats", Query::params([QueryParam::new("token", "INVALID TOKEN")].to_vec()))
.await;
assert_token_not_valid(response).await;
env.stop().await;
}
#[tokio::test]
async fn should_allow_the_token_query_param_to_be_at_any_position_in_the_url_query() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});
let env = Started::new(&configuration::ephemeral().into()).await;
let token = env.get_connection_info().api_token.unwrap();
// At the beginning of the query component
let response = Client::new(env.get_connection_info())
.get_request(&format!("torrents?token={token}&limit=1"))
.await;
assert_eq!(response.status(), 200);
// At the end of the query component
let response = Client::new(env.get_connection_info())
.get_request(&format!("torrents?limit=1&token={token}"))
.await;
assert_eq!(response.status(), 200);
env.stop().await;
}