1
1
use torrust_tracker_test_helpers:: configuration;
2
+ use uuid:: Uuid ;
2
3
3
4
use crate :: common:: http:: { Query , QueryParam } ;
4
- use crate :: common:: logging:: { self } ;
5
+ use crate :: common:: logging:: { self , logs_contains_a_line_with } ;
5
6
use crate :: servers:: api:: v1:: asserts:: { assert_token_not_valid, assert_unauthorized} ;
6
- use crate :: servers:: api:: v1:: client:: Client ;
7
+ use crate :: servers:: api:: v1:: client:: { headers_with_request_id , Client } ;
7
8
use crate :: servers:: api:: Started ;
8
9
9
10
#[ tokio:: test]
@@ -15,7 +16,7 @@ async fn should_authenticate_requests_by_using_a_token_query_param() {
15
16
let token = env. get_connection_info ( ) . api_token . unwrap ( ) ;
16
17
17
18
let response = Client :: new ( env. get_connection_info ( ) )
18
- . get_request_with_query ( "stats" , Query :: params ( [ QueryParam :: new ( "token" , & token) ] . to_vec ( ) ) )
19
+ . get_request_with_query ( "stats" , Query :: params ( [ QueryParam :: new ( "token" , & token) ] . to_vec ( ) ) , None )
19
20
. await ;
20
21
21
22
assert_eq ! ( response. status( ) , 200 ) ;
@@ -30,7 +31,7 @@ async fn should_not_authenticate_requests_when_the_token_is_missing() {
30
31
let env = Started :: new ( & configuration:: ephemeral ( ) . into ( ) ) . await ;
31
32
32
33
let response = Client :: new ( env. get_connection_info ( ) )
33
- . get_request_with_query ( "stats" , Query :: default ( ) )
34
+ . get_request_with_query ( "stats" , Query :: default ( ) , None )
34
35
. await ;
35
36
36
37
assert_unauthorized ( response) . await ;
@@ -44,13 +45,24 @@ async fn should_not_authenticate_requests_when_the_token_is_empty() {
44
45
45
46
let env = Started :: new ( & configuration:: ephemeral ( ) . into ( ) ) . await ;
46
47
48
+ let request_id = Uuid :: new_v4 ( ) ;
49
+
47
50
let response = Client :: new ( env. get_connection_info ( ) )
48
- . get_request_with_query ( "stats" , Query :: params ( [ QueryParam :: new ( "token" , "" ) ] . to_vec ( ) ) )
51
+ . get_request_with_query (
52
+ "stats" ,
53
+ Query :: params ( [ QueryParam :: new ( "token" , "" ) ] . to_vec ( ) ) ,
54
+ Some ( headers_with_request_id ( request_id) ) ,
55
+ )
49
56
. await ;
50
57
51
58
assert_token_not_valid ( response) . await ;
52
59
53
60
env. stop ( ) . await ;
61
+
62
+ assert ! (
63
+ logs_contains_a_line_with( & [ "ERROR" , "API" , & format!( "{request_id}" ) ] ) ,
64
+ "Expected logs to contain: ERROR ... API ... request_id={request_id}"
65
+ ) ;
54
66
}
55
67
56
68
#[ tokio:: test]
@@ -60,7 +72,11 @@ async fn should_not_authenticate_requests_when_the_token_is_invalid() {
60
72
let env = Started :: new ( & configuration:: ephemeral ( ) . into ( ) ) . await ;
61
73
62
74
let response = Client :: new ( env. get_connection_info ( ) )
63
- . get_request_with_query ( "stats" , Query :: params ( [ QueryParam :: new ( "token" , "INVALID TOKEN" ) ] . to_vec ( ) ) )
75
+ . get_request_with_query (
76
+ "stats" ,
77
+ Query :: params ( [ QueryParam :: new ( "token" , "INVALID TOKEN" ) ] . to_vec ( ) ) ,
78
+ None ,
79
+ )
64
80
. await ;
65
81
66
82
assert_token_not_valid ( response) . await ;
0 commit comments