@@ -2,6 +2,22 @@ use super::*;
22
33const TEST_DIR_BASE : & str = "tmp/authentication/" ;
44
5+ async fn check_forbidden ( res : reqwest:: Response ) {
6+ assert_eq ! ( res. status( ) , reqwest:: StatusCode :: FORBIDDEN ) ;
7+ let body: APIErrorResponse = res. json ( ) . await . unwrap ( ) ;
8+ assert_eq ! ( body. code, 403 ) ;
9+ assert_eq ! ( body. error, "You don't have access to this resource" ) ;
10+ assert_eq ! ( body. name, "Forbidden" ) ;
11+ }
12+
13+ async fn check_unauthorized ( res : reqwest:: Response ) {
14+ assert_eq ! ( res. status( ) , reqwest:: StatusCode :: UNAUTHORIZED ) ;
15+ let body: APIErrorResponse = res. json ( ) . await . unwrap ( ) ;
16+ assert_eq ! ( body. code, 401 ) ;
17+ assert_eq ! ( body. error, "Missing or invalid credentials" ) ;
18+ assert_eq ! ( body. name, "Unauthorized" ) ;
19+ }
20+
521fn create_token (
622 root : & KeyPair ,
723 user_role : Option < & str > ,
@@ -105,7 +121,7 @@ async fn authentication() {
105121 . send ( )
106122 . await
107123 . unwrap ( ) ;
108- assert_eq ! ( res. status ( ) , reqwest :: StatusCode :: FORBIDDEN ) ;
124+ check_forbidden ( res) . await ;
109125 while Utc :: now ( ) < ten_seconds_later {
110126 tokio:: time:: sleep ( std:: time:: Duration :: from_secs ( 1 ) ) . await ;
111127 }
@@ -115,7 +131,7 @@ async fn authentication() {
115131 . send ( )
116132 . await
117133 . unwrap ( ) ;
118- assert_eq ! ( res. status ( ) , reqwest :: StatusCode :: UNAUTHORIZED ) ;
134+ check_unauthorized ( res) . await ;
119135
120136 // user with no role cannot do any operation
121137 let user_token = create_token ( & root_keypair, None , vec ! [ "/nodeinfo" ] , None ) ;
@@ -125,7 +141,7 @@ async fn authentication() {
125141 . send ( )
126142 . await
127143 . unwrap ( ) ;
128- assert_eq ! ( res. status ( ) , reqwest :: StatusCode :: UNAUTHORIZED ) ;
144+ check_unauthorized ( res) . await ;
129145
130146 // user with unknown role cannot do any operation
131147 let user_token = create_token ( & root_keypair, Some ( "unknown" ) , vec ! [ "/nodeinfo" ] , None ) ;
@@ -135,7 +151,7 @@ async fn authentication() {
135151 . send ( )
136152 . await
137153 . unwrap ( ) ;
138- assert_eq ! ( res. status ( ) , reqwest :: StatusCode :: UNAUTHORIZED ) ;
154+ check_unauthorized ( res) . await ;
139155
140156 // user with read-only role can only call read-only APIs
141157 let user_token = create_token ( & root_keypair, Some ( "read-only" ) , vec ! [ ] , None ) ;
@@ -156,7 +172,7 @@ async fn authentication() {
156172 . send ( )
157173 . await
158174 . unwrap ( ) ;
159- assert_eq ! ( res. status ( ) , reqwest :: StatusCode :: FORBIDDEN ) ;
175+ check_forbidden ( res) . await ;
160176
161177 // user cannot call any API after token revocation
162178 let user_token = create_token ( & root_keypair, Some ( "custom" ) , vec ! [ "/nodeinfo" ] , None ) ;
@@ -192,15 +208,15 @@ async fn authentication() {
192208 . send ( )
193209 . await
194210 . unwrap ( ) ;
195- assert_eq ! ( res. status ( ) , reqwest :: StatusCode :: UNAUTHORIZED ) ;
211+ check_unauthorized ( res) . await ;
196212
197213 // with no token no API can be called
198214 let res = reqwest:: Client :: new ( )
199215 . get ( format ! ( "http://{node_address}/nodeinfo" ) )
200216 . send ( )
201217 . await
202218 . unwrap ( ) ;
203- assert_eq ! ( res. status ( ) , reqwest :: StatusCode :: UNAUTHORIZED ) ;
219+ check_unauthorized ( res) . await ;
204220
205221 // with an invalid token no API can be called
206222 let res = reqwest:: Client :: new ( )
@@ -209,5 +225,5 @@ async fn authentication() {
209225 . send ( )
210226 . await
211227 . unwrap ( ) ;
212- assert_eq ! ( res. status ( ) , reqwest :: StatusCode :: UNAUTHORIZED ) ;
228+ check_unauthorized ( res) . await ;
213229}
0 commit comments