|
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 torrust_axum_rest_tracker_api_server::environment::Started; |
| 3 | + use torrust_rest_tracker_api_client::common::http::Query; |
| 4 | + use torrust_rest_tracker_api_client::v1::client::{headers_with_auth_token, Client}; |
| 5 | + use torrust_tracker_test_helpers::{configuration, logging}; |
7 | 6 |
|
8 |
| -use crate::server::v1::asserts::{assert_token_not_valid, assert_unauthorized}; |
| 7 | + #[tokio::test] |
| 8 | + async fn it_should_authenticate_requests_when_the_token_is_provided_in_the_authentication_header() { |
| 9 | + logging::setup(); |
9 | 10 |
|
10 |
| -#[tokio::test] |
11 |
| -async fn should_authenticate_requests_by_using_a_token_query_param() { |
12 |
| - logging::setup(); |
| 11 | + let env = Started::new(&configuration::ephemeral().into()).await; |
13 | 12 |
|
14 |
| - let env = Started::new(&configuration::ephemeral().into()).await; |
| 13 | + let token = env.get_connection_info().api_token.unwrap(); |
15 | 14 |
|
16 |
| - let token = env.get_connection_info().api_token.unwrap(); |
| 15 | + let response = Client::new(env.get_connection_info()) |
| 16 | + .unwrap() |
| 17 | + .get_request_with_query("stats", Query::default(), Some(headers_with_auth_token(&token))) |
| 18 | + .await; |
17 | 19 |
|
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; |
| 20 | + assert_eq!(response.status(), 200); |
22 | 21 |
|
23 |
| - assert_eq!(response.status(), 200); |
24 |
| - |
25 |
| - env.stop().await; |
| 22 | + env.stop().await; |
| 23 | + } |
26 | 24 | }
|
| 25 | +mod given_that_the_token_is_only_provided_in_the_query_param { |
27 | 26 |
|
28 |
| -#[tokio::test] |
29 |
| -async fn should_not_authenticate_requests_when_the_token_is_missing() { |
30 |
| - logging::setup(); |
| 27 | + use torrust_axum_rest_tracker_api_server::environment::Started; |
| 28 | + use torrust_rest_tracker_api_client::common::http::{Query, QueryParam}; |
| 29 | + use torrust_rest_tracker_api_client::v1::client::{headers_with_request_id, Client}; |
| 30 | + use torrust_tracker_test_helpers::logging::logs_contains_a_line_with; |
| 31 | + use torrust_tracker_test_helpers::{configuration, logging}; |
| 32 | + use uuid::Uuid; |
31 | 33 |
|
32 |
| - let env = Started::new(&configuration::ephemeral().into()).await; |
| 34 | + use crate::server::v1::asserts::assert_token_not_valid; |
33 | 35 |
|
34 |
| - let request_id = Uuid::new_v4(); |
| 36 | + #[tokio::test] |
| 37 | + async fn it_should_authenticate_requests_when_the_token_is_provided_as_a_query_param() { |
| 38 | + logging::setup(); |
35 | 39 |
|
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; |
| 40 | + let env = Started::new(&configuration::ephemeral().into()).await; |
40 | 41 |
|
41 |
| - assert_unauthorized(response).await; |
| 42 | + let token = env.get_connection_info().api_token.unwrap(); |
42 | 43 |
|
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 |
| - ); |
| 44 | + let response = Client::new(env.get_connection_info()) |
| 45 | + .unwrap() |
| 46 | + .get_request_with_query("stats", Query::params([QueryParam::new("token", &token)].to_vec()), None) |
| 47 | + .await; |
47 | 48 |
|
48 |
| - env.stop().await; |
49 |
| -} |
| 49 | + assert_eq!(response.status(), 200); |
50 | 50 |
|
51 |
| -#[tokio::test] |
52 |
| -async fn should_not_authenticate_requests_when_the_token_is_empty() { |
53 |
| - logging::setup(); |
| 51 | + env.stop().await; |
| 52 | + } |
54 | 53 |
|
55 |
| - let env = Started::new(&configuration::ephemeral().into()).await; |
| 54 | + #[tokio::test] |
| 55 | + async fn it_should_not_authenticate_requests_when_the_token_is_empty() { |
| 56 | + logging::setup(); |
56 | 57 |
|
57 |
| - let request_id = Uuid::new_v4(); |
| 58 | + let env = Started::new(&configuration::ephemeral().into()).await; |
58 | 59 |
|
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; |
| 60 | + let request_id = Uuid::new_v4(); |
67 | 61 |
|
68 |
| - assert_token_not_valid(response).await; |
| 62 | + let response = Client::new(env.get_connection_info()) |
| 63 | + .unwrap() |
| 64 | + .get_request_with_query( |
| 65 | + "stats", |
| 66 | + Query::params([QueryParam::new("token", "")].to_vec()), |
| 67 | + Some(headers_with_request_id(request_id)), |
| 68 | + ) |
| 69 | + .await; |
69 | 70 |
|
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 |
| - ); |
| 71 | + assert_token_not_valid(response).await; |
74 | 72 |
|
75 |
| - env.stop().await; |
76 |
| -} |
| 73 | + assert!( |
| 74 | + logs_contains_a_line_with(&["ERROR", "API", &format!("{request_id}")]), |
| 75 | + "Expected logs to contain: ERROR ... API ... request_id={request_id}" |
| 76 | + ); |
| 77 | + |
| 78 | + env.stop().await; |
| 79 | + } |
| 80 | + |
| 81 | + #[tokio::test] |
| 82 | + async fn it_should_not_authenticate_requests_when_the_token_is_invalid() { |
| 83 | + logging::setup(); |
| 84 | + |
| 85 | + let env = Started::new(&configuration::ephemeral().into()).await; |
| 86 | + |
| 87 | + let request_id = Uuid::new_v4(); |
77 | 88 |
|
78 |
| -#[tokio::test] |
79 |
| -async fn should_not_authenticate_requests_when_the_token_is_invalid() { |
80 |
| - logging::setup(); |
| 89 | + let response = Client::new(env.get_connection_info()) |
| 90 | + .unwrap() |
| 91 | + .get_request_with_query( |
| 92 | + "stats", |
| 93 | + Query::params([QueryParam::new("token", "INVALID TOKEN")].to_vec()), |
| 94 | + Some(headers_with_request_id(request_id)), |
| 95 | + ) |
| 96 | + .await; |
81 | 97 |
|
82 |
| - let env = Started::new(&configuration::ephemeral().into()).await; |
| 98 | + assert_token_not_valid(response).await; |
83 | 99 |
|
84 |
| - let request_id = Uuid::new_v4(); |
| 100 | + assert!( |
| 101 | + logs_contains_a_line_with(&["ERROR", "API", &format!("{request_id}")]), |
| 102 | + "Expected logs to contain: ERROR ... API ... request_id={request_id}" |
| 103 | + ); |
85 | 104 |
|
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; |
| 105 | + env.stop().await; |
| 106 | + } |
94 | 107 |
|
95 |
| - assert_token_not_valid(response).await; |
| 108 | + #[tokio::test] |
| 109 | + async fn it_should_allow_the_token_query_param_to_be_at_any_position_in_the_url_query() { |
| 110 | + logging::setup(); |
96 | 111 |
|
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 |
| - ); |
| 112 | + let env = Started::new(&configuration::ephemeral().into()).await; |
101 | 113 |
|
102 |
| - env.stop().await; |
| 114 | + let token = env.get_connection_info().api_token.unwrap(); |
| 115 | + |
| 116 | + // At the beginning of the query component |
| 117 | + let response = Client::new(env.get_connection_info()) |
| 118 | + .unwrap() |
| 119 | + .get_request(&format!("torrents?token={token}&limit=1")) |
| 120 | + .await; |
| 121 | + |
| 122 | + assert_eq!(response.status(), 200); |
| 123 | + |
| 124 | + // At the end of the query component |
| 125 | + let response = Client::new(env.get_connection_info()) |
| 126 | + .unwrap() |
| 127 | + .get_request(&format!("torrents?limit=1&token={token}")) |
| 128 | + .await; |
| 129 | + |
| 130 | + assert_eq!(response.status(), 200); |
| 131 | + |
| 132 | + env.stop().await; |
| 133 | + } |
103 | 134 | }
|
104 | 135 |
|
105 |
| -#[tokio::test] |
106 |
| -async fn should_allow_the_token_query_param_to_be_at_any_position_in_the_url_query() { |
107 |
| - logging::setup(); |
| 136 | +mod given_that_not_token_is_provided { |
| 137 | + |
| 138 | + use torrust_axum_rest_tracker_api_server::environment::Started; |
| 139 | + use torrust_rest_tracker_api_client::common::http::Query; |
| 140 | + use torrust_rest_tracker_api_client::v1::client::{headers_with_request_id, Client}; |
| 141 | + use torrust_tracker_test_helpers::logging::logs_contains_a_line_with; |
| 142 | + use torrust_tracker_test_helpers::{configuration, logging}; |
| 143 | + use uuid::Uuid; |
| 144 | + |
| 145 | + use crate::server::v1::asserts::assert_unauthorized; |
108 | 146 |
|
109 |
| - let env = Started::new(&configuration::ephemeral().into()).await; |
| 147 | + #[tokio::test] |
| 148 | + async fn it_should_not_authenticate_requests_when_the_token_is_missing() { |
| 149 | + logging::setup(); |
110 | 150 |
|
111 |
| - let token = env.get_connection_info().api_token.unwrap(); |
| 151 | + let env = Started::new(&configuration::ephemeral().into()).await; |
112 | 152 |
|
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; |
| 153 | + let request_id = Uuid::new_v4(); |
118 | 154 |
|
119 |
| - assert_eq!(response.status(), 200); |
| 155 | + let response = Client::new(env.get_connection_info()) |
| 156 | + .unwrap() |
| 157 | + .get_request_with_query("stats", Query::default(), Some(headers_with_request_id(request_id))) |
| 158 | + .await; |
120 | 159 |
|
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; |
| 160 | + assert_unauthorized(response).await; |
126 | 161 |
|
127 |
| - assert_eq!(response.status(), 200); |
| 162 | + assert!( |
| 163 | + logs_contains_a_line_with(&["ERROR", "API", &format!("{request_id}")]), |
| 164 | + "Expected logs to contain: ERROR ... API ... request_id={request_id}" |
| 165 | + ); |
128 | 166 |
|
129 |
| - env.stop().await; |
| 167 | + env.stop().await; |
| 168 | + } |
130 | 169 | }
|
0 commit comments