@ -5,8 +5,10 @@
defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
use Pleroma.Web.ConnCase
alias Pleroma.Activity
alias Pleroma.User
alias Pleroma.UserInviteToken
alias Pleroma.Web.CommonAPI
import Pleroma.Factory
describe " /api/pleroma/admin/users " do
@ -949,4 +951,329 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
}
end
end
describe " GET /api/pleroma/admin/reports/:id " do
setup %{ conn : conn } do
admin = insert ( :user , info : %{ is_admin : true } )
%{ conn : assign ( conn , :user , admin ) }
end
test " returns report by its id " , %{ conn : conn } do
[ reporter , target_user ] = insert_pair ( :user )
activity = insert ( :note_activity , user : target_user )
{ :ok , %{ id : report_id } } =
CommonAPI . report ( reporter , %{
" account_id " = > target_user . id ,
" comment " = > " I feel offended " ,
" status_ids " = > [ activity . id ]
} )
response =
conn
|> get ( " /api/pleroma/admin/reports/ #{ report_id } " )
|> json_response ( :ok )
assert response [ " id " ] == report_id
end
test " returns 404 when report id is invalid " , %{ conn : conn } do
conn = get ( conn , " /api/pleroma/admin/reports/test " )
assert json_response ( conn , :not_found ) == " Not found "
end
end
describe " PUT /api/pleroma/admin/reports/:id " do
setup %{ conn : conn } do
admin = insert ( :user , info : %{ is_admin : true } )
[ reporter , target_user ] = insert_pair ( :user )
activity = insert ( :note_activity , user : target_user )
{ :ok , %{ id : report_id } } =
CommonAPI . report ( reporter , %{
" account_id " = > target_user . id ,
" comment " = > " I feel offended " ,
" status_ids " = > [ activity . id ]
} )
%{ conn : assign ( conn , :user , admin ) , id : report_id }
end
test " mark report as resolved " , %{ conn : conn , id : id } do
response =
conn
|> put ( " /api/pleroma/admin/reports/ #{ id } " , %{ " state " = > " resolved " } )
|> json_response ( :ok )
assert response [ " state " ] == " resolved "
end
test " closes report " , %{ conn : conn , id : id } do
response =
conn
|> put ( " /api/pleroma/admin/reports/ #{ id } " , %{ " state " = > " closed " } )
|> json_response ( :ok )
assert response [ " state " ] == " closed "
end
test " returns 400 when state is unknown " , %{ conn : conn , id : id } do
conn =
conn
|> put ( " /api/pleroma/admin/reports/ #{ id } " , %{ " state " = > " test " } )
assert json_response ( conn , :bad_request ) == " Unsupported state "
end
test " returns 404 when report is not exist " , %{ conn : conn } do
conn =
conn
|> put ( " /api/pleroma/admin/reports/test " , %{ " state " = > " closed " } )
assert json_response ( conn , :not_found ) == " Not found "
end
end
describe " GET /api/pleroma/admin/reports " do
setup %{ conn : conn } do
admin = insert ( :user , info : %{ is_admin : true } )
%{ conn : assign ( conn , :user , admin ) }
end
test " returns empty response when no reports created " , %{ conn : conn } do
response =
conn
|> get ( " /api/pleroma/admin/reports " )
|> json_response ( :ok )
assert Enum . empty? ( response [ " reports " ] )
end
test " returns reports " , %{ conn : conn } do
[ reporter , target_user ] = insert_pair ( :user )
activity = insert ( :note_activity , user : target_user )
{ :ok , %{ id : report_id } } =
CommonAPI . report ( reporter , %{
" account_id " = > target_user . id ,
" comment " = > " I feel offended " ,
" status_ids " = > [ activity . id ]
} )
response =
conn
|> get ( " /api/pleroma/admin/reports " )
|> json_response ( :ok )
[ report ] = response [ " reports " ]
assert length ( response [ " reports " ] ) == 1
assert report [ " id " ] == report_id
end
test " returns reports with specified state " , %{ conn : conn } do
[ reporter , target_user ] = insert_pair ( :user )
activity = insert ( :note_activity , user : target_user )
{ :ok , %{ id : first_report_id } } =
CommonAPI . report ( reporter , %{
" account_id " = > target_user . id ,
" comment " = > " I feel offended " ,
" status_ids " = > [ activity . id ]
} )
{ :ok , %{ id : second_report_id } } =
CommonAPI . report ( reporter , %{
" account_id " = > target_user . id ,
" comment " = > " I don't like this user "
} )
CommonAPI . update_report_state ( second_report_id , " closed " )
response =
conn
|> get ( " /api/pleroma/admin/reports " , %{
" state " = > " open "
} )
|> json_response ( :ok )
[ open_report ] = response [ " reports " ]
assert length ( response [ " reports " ] ) == 1
assert open_report [ " id " ] == first_report_id
response =
conn
|> get ( " /api/pleroma/admin/reports " , %{
" state " = > " closed "
} )
|> json_response ( :ok )
[ closed_report ] = response [ " reports " ]
assert length ( response [ " reports " ] ) == 1
assert closed_report [ " id " ] == second_report_id
response =
conn
|> get ( " /api/pleroma/admin/reports " , %{
" state " = > " resolved "
} )
|> json_response ( :ok )
assert Enum . empty? ( response [ " reports " ] )
end
test " returns 403 when requested by a non-admin " do
user = insert ( :user )
conn =
build_conn ( )
|> assign ( :user , user )
|> get ( " /api/pleroma/admin/reports " )
assert json_response ( conn , :forbidden ) == %{ " error " = > " User is not admin. " }
end
test " returns 403 when requested by anonymous " do
conn =
build_conn ( )
|> get ( " /api/pleroma/admin/reports " )
assert json_response ( conn , :forbidden ) == %{ " error " = > " Invalid credentials. " }
end
end
describe " POST /api/pleroma/admin/reports/:id/respond " do
setup %{ conn : conn } do
admin = insert ( :user , info : %{ is_admin : true } )
%{ conn : assign ( conn , :user , admin ) }
end
test " returns created dm " , %{ conn : conn } do
[ reporter , target_user ] = insert_pair ( :user )
activity = insert ( :note_activity , user : target_user )
{ :ok , %{ id : report_id } } =
CommonAPI . report ( reporter , %{
" account_id " = > target_user . id ,
" comment " = > " I feel offended " ,
" status_ids " = > [ activity . id ]
} )
response =
conn
|> post ( " /api/pleroma/admin/reports/ #{ report_id } /respond " , %{
" status " = > " I will check it out "
} )
|> json_response ( :ok )
recipients = Enum . map ( response [ " mentions " ] , & &1 [ " username " ] )
assert conn . assigns [ :user ] . nickname in recipients
assert reporter . nickname in recipients
assert response [ " content " ] == " I will check it out "
assert response [ " visibility " ] == " direct "
end
test " returns 400 when status is missing " , %{ conn : conn } do
conn = post ( conn , " /api/pleroma/admin/reports/test/respond " )
assert json_response ( conn , :bad_request ) == " Invalid parameters "
end
test " returns 404 when report id is invalid " , %{ conn : conn } do
conn =
post ( conn , " /api/pleroma/admin/reports/test/respond " , %{
" status " = > " foo "
} )
assert json_response ( conn , :not_found ) == " Not found "
end
end
describe " PUT /api/pleroma/admin/statuses/:id " do
setup %{ conn : conn } do
admin = insert ( :user , info : %{ is_admin : true } )
activity = insert ( :note_activity )
%{ conn : assign ( conn , :user , admin ) , id : activity . id }
end
test " toggle sensitive flag " , %{ conn : conn , id : id } do
response =
conn
|> put ( " /api/pleroma/admin/statuses/ #{ id } " , %{ " sensitive " = > " true " } )
|> json_response ( :ok )
assert response [ " sensitive " ]
response =
conn
|> put ( " /api/pleroma/admin/statuses/ #{ id } " , %{ " sensitive " = > " false " } )
|> json_response ( :ok )
refute response [ " sensitive " ]
end
test " change visibility flag " , %{ conn : conn , id : id } do
response =
conn
|> put ( " /api/pleroma/admin/statuses/ #{ id } " , %{ " visibility " = > " public " } )
|> json_response ( :ok )
assert response [ " visibility " ] == " public "
response =
conn
|> put ( " /api/pleroma/admin/statuses/ #{ id } " , %{ " visibility " = > " private " } )
|> json_response ( :ok )
assert response [ " visibility " ] == " private "
response =
conn
|> put ( " /api/pleroma/admin/statuses/ #{ id } " , %{ " visibility " = > " unlisted " } )
|> json_response ( :ok )
assert response [ " visibility " ] == " unlisted "
end
test " returns 400 when visibility is unknown " , %{ conn : conn , id : id } do
conn =
conn
|> put ( " /api/pleroma/admin/statuses/ #{ id } " , %{ " visibility " = > " test " } )
assert json_response ( conn , :bad_request ) == " Unsupported visibility "
end
end
describe " DELETE /api/pleroma/admin/statuses/:id " do
setup %{ conn : conn } do
admin = insert ( :user , info : %{ is_admin : true } )
activity = insert ( :note_activity )
%{ conn : assign ( conn , :user , admin ) , id : activity . id }
end
test " deletes status " , %{ conn : conn , id : id } do
conn
|> delete ( " /api/pleroma/admin/statuses/ #{ id } " )
|> json_response ( :ok )
refute Activity . get_by_id ( id )
end
test " returns error when status is not exist " , %{ conn : conn } do
conn =
conn
|> delete ( " /api/pleroma/admin/statuses/test " )
assert json_response ( conn , :bad_request ) == " Could not delete "
end
end
end