@@ -1391,6 +1391,44 @@ func TestRest_TelegramNotification(t *testing.T) {
13911391 assert .Empty (t , mockDestination .Get ()[3 ].Telegrams )
13921392}
13931393
1394+ func TestRest_UserUnauthorised200 (t * testing.T ) {
1395+ ts , _ , teardown := startupT (t )
1396+ defer teardown ()
1397+
1398+ client := & http.Client {Timeout : 1 * time .Second }
1399+ defer client .CloseIdleConnections ()
1400+ req , err := http .NewRequest ("GET" , ts .URL + "/api/v1/user?site=remark42" , http .NoBody )
1401+ require .NoError (t , err )
1402+ resp , err := client .Do (req )
1403+ require .NoError (t , err )
1404+ require .Equal (t , http .StatusUnauthorized , resp .StatusCode )
1405+ body , err := io .ReadAll (resp .Body )
1406+ assert .NoError (t , resp .Body .Close ())
1407+ assert .NoError (t , err )
1408+ assert .Equal (t , "Unauthorized\n " , string (body ))
1409+
1410+ req , err = http .NewRequest ("GET" , ts .URL + "/api/v1/user?site=remark42&unauthorised200=true" , http .NoBody )
1411+ require .NoError (t , err )
1412+ resp , err = client .Do (req )
1413+ require .NoError (t , err )
1414+ require .Equal (t , http .StatusOK , resp .StatusCode , "should fail but with status code 200 due to the unauthorised200 param set" )
1415+ body , err = io .ReadAll (resp .Body )
1416+ assert .NoError (t , resp .Body .Close ())
1417+ assert .NoError (t , err )
1418+ assert .Equal (t , `{"error":"can't extract user info from the token: user can't be parsed"}` + "\n " , string (body ))
1419+
1420+ req , err = http .NewRequest ("GET" , ts .URL + "/api/v1/user?site=wrong_site&unauthorised200=true" , http .NoBody )
1421+ require .NoError (t , err )
1422+ req .Header .Add ("X-JWT" , devToken )
1423+ resp , err = client .Do (req )
1424+ require .NoError (t , err )
1425+ require .Equal (t , http .StatusForbidden , resp .StatusCode , "should fail due to site mismatch" )
1426+ body , err = io .ReadAll (resp .Body )
1427+ assert .NoError (t , resp .Body .Close ())
1428+ assert .NoError (t , err )
1429+ assert .Contains (t , string (body ), "Access denied\n " )
1430+ }
1431+
13941432func TestRest_UserAllData (t * testing.T ) {
13951433 ts , srv , teardown := startupT (t )
13961434 defer teardown ()
0 commit comments