fix random failure and improve test to filter the query by portal_type