|
1 |
| -use torrust_axum_rest_tracker_api_server::environment::Started; |
2 |
| -use torrust_rest_tracker_api_client::common::http::{Query, QueryParam}; |
3 |
| -use torrust_rest_tracker_api_client::v1::client::{headers_with_request_id, Client}; |
4 |
| -use torrust_tracker_test_helpers::logging::logs_contains_a_line_with; |
5 |
| -use torrust_tracker_test_helpers::{configuration, logging}; |
6 |
| -use uuid::Uuid; |
| 1 | +mod given_that_the_token_is_only_provided_in_the_authentication_header { |
| 2 | + use hyper::header; |
| 3 | + use torrust_axum_rest_tracker_api_server::environment::Started; |
| 4 | + use torrust_rest_tracker_api_client::common::http::Query; |
| 5 | + use torrust_rest_tracker_api_client::v1::client::{ |
| 6 | + headers_with_auth_token, headers_with_request_id, Client, AUTH_BEARER_TOKEN_HEADER_PREFIX, |
| 7 | + }; |
| 8 | + use torrust_tracker_test_helpers::logging::logs_contains_a_line_with; |
| 9 | + use torrust_tracker_test_helpers::{configuration, logging}; |
| 10 | + use uuid::Uuid; |
7 | 11 |
|
8 |
| -use crate::server::v1::asserts::{assert_token_not_valid, assert_unauthorized}; |
| 12 | + use crate::server::v1::asserts::assert_token_not_valid; |
9 | 13 |
|
10 |
| -#[tokio::test] |
11 |
| -async fn should_authenticate_requests_by_using_a_token_query_param() { |
12 |
| - logging::setup(); |
| 14 | + #[tokio::test] |
| 15 | + async fn it_should_authenticate_requests_when_the_token_is_provided_in_the_authentication_header() { |
| 16 | + logging::setup(); |
13 | 17 |
|
14 |
| - let env = Started::new(&configuration::ephemeral().into()).await; |
| 18 | + let env = Started::new(&configuration::ephemeral().into()).await; |
15 | 19 |
|
16 |
| - let token = env.get_connection_info().api_token.unwrap(); |
| 20 | + let token = env.get_connection_info().api_token.unwrap(); |
17 | 21 |
|
18 |
| - let response = Client::new(env.get_connection_info()) |
19 |
| - .unwrap() |
20 |
| - .get_request_with_query("stats", Query::params([QueryParam::new("token", &token)].to_vec()), None) |
21 |
| - .await; |
| 22 | + let response = Client::new(env.get_connection_info()) |
| 23 | + .unwrap() |
| 24 | + .get_request_with_query("stats", Query::default(), Some(headers_with_auth_token(&token))) |
| 25 | + .await; |
22 | 26 |
|
23 |
| - assert_eq!(response.status(), 200); |
| 27 | + assert_eq!(response.status(), 200); |
24 | 28 |
|
25 |
| - env.stop().await; |
26 |
| -} |
| 29 | + env.stop().await; |
| 30 | + } |
| 31 | + |
| 32 | + #[tokio::test] |
| 33 | + async fn it_should_not_authenticate_requests_when_the_token_is_empty() { |
| 34 | + logging::setup(); |
| 35 | + |
| 36 | + let env = Started::new(&configuration::ephemeral().into()).await; |
| 37 | + |
| 38 | + let request_id = Uuid::new_v4(); |
| 39 | + |
| 40 | + let mut headers = headers_with_request_id(request_id); |
| 41 | + |
| 42 | + // Send the header with an empty token |
| 43 | + headers.insert( |
| 44 | + header::AUTHORIZATION, |
| 45 | + format!("{AUTH_BEARER_TOKEN_HEADER_PREFIX} ") |
| 46 | + .parse() |
| 47 | + .expect("the auth token is not a valid header value"), |
| 48 | + ); |
| 49 | + |
| 50 | + let response = Client::new(env.get_connection_info()) |
| 51 | + .unwrap() |
| 52 | + .get_request_with_query("stats", Query::default(), Some(headers)) |
| 53 | + .await; |
| 54 | + |
| 55 | + assert_token_not_valid(response).await; |
| 56 | + |
| 57 | + assert!( |
| 58 | + logs_contains_a_line_with(&["ERROR", "API", &format!("{request_id}")]), |
| 59 | + "Expected logs to contain: ERROR ... API ... request_id={request_id}" |
| 60 | + ); |
| 61 | + |
| 62 | + env.stop().await; |
| 63 | + } |
27 | 64 |
|
28 |
| -#[tokio::test] |
29 |
| -async fn should_not_authenticate_requests_when_the_token_is_missing() { |
30 |
| - logging::setup(); |
| 65 | + #[tokio::test] |
| 66 | + async fn it_should_not_authenticate_requests_when_the_token_is_invalid() { |
| 67 | + logging::setup(); |
31 | 68 |
|
32 |
| - let env = Started::new(&configuration::ephemeral().into()).await; |
| 69 | + let env = Started::new(&configuration::ephemeral().into()).await; |
33 | 70 |
|
34 |
| - let request_id = Uuid::new_v4(); |
| 71 | + let request_id = Uuid::new_v4(); |
35 | 72 |
|
36 |
| - let response = Client::new(env.get_connection_info()) |
37 |
| - .unwrap() |
38 |
| - .get_request_with_query("stats", Query::default(), Some(headers_with_request_id(request_id))) |
39 |
| - .await; |
| 73 | + let mut headers = headers_with_request_id(request_id); |
40 | 74 |
|
41 |
| - assert_unauthorized(response).await; |
| 75 | + // Send the header with an empty token |
| 76 | + headers.insert( |
| 77 | + header::AUTHORIZATION, |
| 78 | + "Bearer INVALID TOKEN" |
| 79 | + .parse() |
| 80 | + .expect("the auth token is not a valid header value"), |
| 81 | + ); |
42 | 82 |
|
43 |
| - assert!( |
44 |
| - logs_contains_a_line_with(&["ERROR", "API", &format!("{request_id}")]), |
45 |
| - "Expected logs to contain: ERROR ... API ... request_id={request_id}" |
46 |
| - ); |
| 83 | + let response = Client::new(env.get_connection_info()) |
| 84 | + .unwrap() |
| 85 | + .get_request_with_query("stats", Query::default(), Some(headers)) |
| 86 | + .await; |
47 | 87 |
|
48 |
| - env.stop().await; |
| 88 | + assert_token_not_valid(response).await; |
| 89 | + |
| 90 | + assert!( |
| 91 | + logs_contains_a_line_with(&["ERROR", "API", &format!("{request_id}")]), |
| 92 | + "Expected logs to contain: ERROR ... API ... request_id={request_id}" |
| 93 | + ); |
| 94 | + |
| 95 | + env.stop().await; |
| 96 | + } |
49 | 97 | }
|
| 98 | +mod given_that_the_token_is_only_provided_in_the_query_param { |
| 99 | + |
| 100 | + use torrust_axum_rest_tracker_api_server::environment::Started; |
| 101 | + use torrust_rest_tracker_api_client::common::http::{Query, QueryParam}; |
| 102 | + use torrust_rest_tracker_api_client::v1::client::{headers_with_request_id, Client}; |
| 103 | + use torrust_tracker_test_helpers::logging::logs_contains_a_line_with; |
| 104 | + use torrust_tracker_test_helpers::{configuration, logging}; |
| 105 | + use uuid::Uuid; |
| 106 | + |
| 107 | + use crate::server::v1::asserts::assert_token_not_valid; |
| 108 | + |
| 109 | + #[tokio::test] |
| 110 | + async fn it_should_authenticate_requests_when_the_token_is_provided_as_a_query_param() { |
| 111 | + logging::setup(); |
| 112 | + |
| 113 | + let env = Started::new(&configuration::ephemeral().into()).await; |
| 114 | + |
| 115 | + let token = env.get_connection_info().api_token.unwrap(); |
| 116 | + |
| 117 | + let response = Client::new(env.get_connection_info()) |
| 118 | + .unwrap() |
| 119 | + .get_request_with_query("stats", Query::params([QueryParam::new("token", &token)].to_vec()), None) |
| 120 | + .await; |
| 121 | + |
| 122 | + assert_eq!(response.status(), 200); |
50 | 123 |
|
51 |
| -#[tokio::test] |
52 |
| -async fn should_not_authenticate_requests_when_the_token_is_empty() { |
53 |
| - logging::setup(); |
| 124 | + env.stop().await; |
| 125 | + } |
54 | 126 |
|
55 |
| - let env = Started::new(&configuration::ephemeral().into()).await; |
| 127 | + #[tokio::test] |
| 128 | + async fn it_should_not_authenticate_requests_when_the_token_is_empty() { |
| 129 | + logging::setup(); |
56 | 130 |
|
57 |
| - let request_id = Uuid::new_v4(); |
| 131 | + let env = Started::new(&configuration::ephemeral().into()).await; |
58 | 132 |
|
59 |
| - let response = Client::new(env.get_connection_info()) |
60 |
| - .unwrap() |
61 |
| - .get_request_with_query( |
62 |
| - "stats", |
63 |
| - Query::params([QueryParam::new("token", "")].to_vec()), |
64 |
| - Some(headers_with_request_id(request_id)), |
65 |
| - ) |
66 |
| - .await; |
| 133 | + let request_id = Uuid::new_v4(); |
67 | 134 |
|
68 |
| - assert_token_not_valid(response).await; |
| 135 | + let response = Client::new(env.get_connection_info()) |
| 136 | + .unwrap() |
| 137 | + .get_request_with_query( |
| 138 | + "stats", |
| 139 | + Query::params([QueryParam::new("token", "")].to_vec()), |
| 140 | + Some(headers_with_request_id(request_id)), |
| 141 | + ) |
| 142 | + .await; |
69 | 143 |
|
70 |
| - assert!( |
71 |
| - logs_contains_a_line_with(&["ERROR", "API", &format!("{request_id}")]), |
72 |
| - "Expected logs to contain: ERROR ... API ... request_id={request_id}" |
73 |
| - ); |
| 144 | + assert_token_not_valid(response).await; |
74 | 145 |
|
75 |
| - env.stop().await; |
| 146 | + assert!( |
| 147 | + logs_contains_a_line_with(&["ERROR", "API", &format!("{request_id}")]), |
| 148 | + "Expected logs to contain: ERROR ... API ... request_id={request_id}" |
| 149 | + ); |
| 150 | + |
| 151 | + env.stop().await; |
| 152 | + } |
| 153 | + |
| 154 | + #[tokio::test] |
| 155 | + async fn it_should_not_authenticate_requests_when_the_token_is_invalid() { |
| 156 | + logging::setup(); |
| 157 | + |
| 158 | + let env = Started::new(&configuration::ephemeral().into()).await; |
| 159 | + |
| 160 | + let request_id = Uuid::new_v4(); |
| 161 | + |
| 162 | + let response = Client::new(env.get_connection_info()) |
| 163 | + .unwrap() |
| 164 | + .get_request_with_query( |
| 165 | + "stats", |
| 166 | + Query::params([QueryParam::new("token", "INVALID TOKEN")].to_vec()), |
| 167 | + Some(headers_with_request_id(request_id)), |
| 168 | + ) |
| 169 | + .await; |
| 170 | + |
| 171 | + assert_token_not_valid(response).await; |
| 172 | + |
| 173 | + assert!( |
| 174 | + logs_contains_a_line_with(&["ERROR", "API", &format!("{request_id}")]), |
| 175 | + "Expected logs to contain: ERROR ... API ... request_id={request_id}" |
| 176 | + ); |
| 177 | + |
| 178 | + env.stop().await; |
| 179 | + } |
| 180 | + |
| 181 | + #[tokio::test] |
| 182 | + async fn it_should_allow_the_token_query_param_to_be_at_any_position_in_the_url_query() { |
| 183 | + logging::setup(); |
| 184 | + |
| 185 | + let env = Started::new(&configuration::ephemeral().into()).await; |
| 186 | + |
| 187 | + let token = env.get_connection_info().api_token.unwrap(); |
| 188 | + |
| 189 | + // At the beginning of the query component |
| 190 | + let response = Client::new(env.get_connection_info()) |
| 191 | + .unwrap() |
| 192 | + .get_request(&format!("torrents?token={token}&limit=1")) |
| 193 | + .await; |
| 194 | + |
| 195 | + assert_eq!(response.status(), 200); |
| 196 | + |
| 197 | + // At the end of the query component |
| 198 | + let response = Client::new(env.get_connection_info()) |
| 199 | + .unwrap() |
| 200 | + .get_request(&format!("torrents?limit=1&token={token}")) |
| 201 | + .await; |
| 202 | + |
| 203 | + assert_eq!(response.status(), 200); |
| 204 | + |
| 205 | + env.stop().await; |
| 206 | + } |
76 | 207 | }
|
77 | 208 |
|
78 |
| -#[tokio::test] |
79 |
| -async fn should_not_authenticate_requests_when_the_token_is_invalid() { |
80 |
| - logging::setup(); |
| 209 | +mod given_that_not_token_is_provided { |
| 210 | + |
| 211 | + use torrust_axum_rest_tracker_api_server::environment::Started; |
| 212 | + use torrust_rest_tracker_api_client::common::http::Query; |
| 213 | + use torrust_rest_tracker_api_client::v1::client::{headers_with_request_id, Client}; |
| 214 | + use torrust_tracker_test_helpers::logging::logs_contains_a_line_with; |
| 215 | + use torrust_tracker_test_helpers::{configuration, logging}; |
| 216 | + use uuid::Uuid; |
| 217 | + |
| 218 | + use crate::server::v1::asserts::assert_unauthorized; |
| 219 | + |
| 220 | + #[tokio::test] |
| 221 | + async fn it_should_not_authenticate_requests_when_the_token_is_missing() { |
| 222 | + logging::setup(); |
81 | 223 |
|
82 |
| - let env = Started::new(&configuration::ephemeral().into()).await; |
| 224 | + let env = Started::new(&configuration::ephemeral().into()).await; |
83 | 225 |
|
84 |
| - let request_id = Uuid::new_v4(); |
| 226 | + let request_id = Uuid::new_v4(); |
85 | 227 |
|
86 |
| - let response = Client::new(env.get_connection_info()) |
87 |
| - .unwrap() |
88 |
| - .get_request_with_query( |
89 |
| - "stats", |
90 |
| - Query::params([QueryParam::new("token", "INVALID TOKEN")].to_vec()), |
91 |
| - Some(headers_with_request_id(request_id)), |
92 |
| - ) |
93 |
| - .await; |
| 228 | + let response = Client::new(env.get_connection_info()) |
| 229 | + .unwrap() |
| 230 | + .get_request_with_query("stats", Query::default(), Some(headers_with_request_id(request_id))) |
| 231 | + .await; |
94 | 232 |
|
95 |
| - assert_token_not_valid(response).await; |
| 233 | + assert_unauthorized(response).await; |
96 | 234 |
|
97 |
| - assert!( |
98 |
| - logs_contains_a_line_with(&["ERROR", "API", &format!("{request_id}")]), |
99 |
| - "Expected logs to contain: ERROR ... API ... request_id={request_id}" |
100 |
| - ); |
| 235 | + assert!( |
| 236 | + logs_contains_a_line_with(&["ERROR", "API", &format!("{request_id}")]), |
| 237 | + "Expected logs to contain: ERROR ... API ... request_id={request_id}" |
| 238 | + ); |
101 | 239 |
|
102 |
| - env.stop().await; |
| 240 | + env.stop().await; |
| 241 | + } |
103 | 242 | }
|
104 | 243 |
|
105 |
| -#[tokio::test] |
106 |
| -async fn should_allow_the_token_query_param_to_be_at_any_position_in_the_url_query() { |
107 |
| - logging::setup(); |
| 244 | +mod given_that_token_is_provided_via_get_param_and_authentication_header { |
| 245 | + use torrust_axum_rest_tracker_api_server::environment::Started; |
| 246 | + use torrust_rest_tracker_api_client::common::http::{Query, QueryParam}; |
| 247 | + use torrust_rest_tracker_api_client::v1::client::{headers_with_auth_token, Client, TOKEN_PARAM_NAME}; |
| 248 | + use torrust_tracker_test_helpers::{configuration, logging}; |
108 | 249 |
|
109 |
| - let env = Started::new(&configuration::ephemeral().into()).await; |
| 250 | + #[tokio::test] |
| 251 | + async fn it_should_authenticate_requests_using_the_token_provided_in_the_authentication_header() { |
| 252 | + logging::setup(); |
110 | 253 |
|
111 |
| - let token = env.get_connection_info().api_token.unwrap(); |
| 254 | + let env = Started::new(&configuration::ephemeral().into()).await; |
112 | 255 |
|
113 |
| - // At the beginning of the query component |
114 |
| - let response = Client::new(env.get_connection_info()) |
115 |
| - .unwrap() |
116 |
| - .get_request(&format!("torrents?token={token}&limit=1")) |
117 |
| - .await; |
| 256 | + let authorized_token = env.get_connection_info().api_token.unwrap(); |
118 | 257 |
|
119 |
| - assert_eq!(response.status(), 200); |
| 258 | + let non_authorized_token = "NonAuthorizedToken"; |
120 | 259 |
|
121 |
| - // At the end of the query component |
122 |
| - let response = Client::new(env.get_connection_info()) |
123 |
| - .unwrap() |
124 |
| - .get_request(&format!("torrents?limit=1&token={token}")) |
125 |
| - .await; |
| 260 | + let response = Client::new(env.get_connection_info()) |
| 261 | + .unwrap() |
| 262 | + .get_request_with_query( |
| 263 | + "stats", |
| 264 | + Query::params([QueryParam::new(TOKEN_PARAM_NAME, non_authorized_token)].to_vec()), |
| 265 | + Some(headers_with_auth_token(&authorized_token)), |
| 266 | + ) |
| 267 | + .await; |
126 | 268 |
|
127 |
| - assert_eq!(response.status(), 200); |
| 269 | + // The token provided in the query param should be ignored and the token |
| 270 | + // in the authentication header should be used. |
| 271 | + assert_eq!(response.status(), 200); |
128 | 272 |
|
129 |
| - env.stop().await; |
| 273 | + env.stop().await; |
| 274 | + } |
130 | 275 | }
|
0 commit comments