2019-09-26 02:38:45 -04:00
# Pleroma: A lightweight social networking server
2020-03-02 00:08:45 -05:00
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
2019-09-26 02:38:45 -04:00
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do
use Pleroma.Web.ConnCase
alias Pleroma.Activity
alias Pleroma.ActivityExpiration
alias Pleroma.Config
2019-10-08 23:51:14 -04:00
alias Pleroma.Conversation.Participation
2019-09-26 02:38:45 -04:00
alias Pleroma.Object
2019-09-27 02:35:45 -04:00
alias Pleroma.Repo
alias Pleroma.ScheduledActivity
2019-10-14 02:09:32 -04:00
alias Pleroma.Tests.ObanHelpers
2019-09-26 02:38:45 -04:00
alias Pleroma.User
2019-09-27 02:35:45 -04:00
alias Pleroma.Web.ActivityPub.ActivityPub
2019-09-26 02:38:45 -04:00
alias Pleroma.Web.CommonAPI
import Pleroma.Factory
2019-10-14 02:31:08 -04:00
clear_config ( [ :instance , :federating ] )
clear_config ( [ :instance , :allow_relay ] )
2020-02-13 13:55:47 -05:00
clear_config ( [ :rich_media , :enabled ] )
2019-10-14 02:31:08 -04:00
2019-09-26 02:38:45 -04:00
describe " posting statuses " do
2019-12-15 14:32:42 -05:00
setup do : oauth_access ( [ " write:statuses " ] )
2019-09-26 02:38:45 -04:00
2019-10-19 19:23:13 -04:00
test " posting a status does not increment reblog_count when relaying " , %{ conn : conn } do
2019-10-14 02:09:32 -04:00
Pleroma.Config . put ( [ :instance , :federating ] , true )
Pleroma.Config . get ( [ :instance , :allow_relay ] , true )
response =
conn
|> post ( " api/v1/statuses " , %{
" content_type " = > " text/plain " ,
" source " = > " Pleroma FE " ,
" status " = > " Hello world " ,
" visibility " = > " public "
} )
|> json_response ( 200 )
assert response [ " reblogs_count " ] == 0
ObanHelpers . perform_all ( )
response =
conn
|> get ( " api/v1/statuses/ #{ response [ " id " ] } " , %{ } )
|> json_response ( 200 )
assert response [ " reblogs_count " ] == 0
end
2019-09-26 02:38:45 -04:00
test " posting a status " , %{ conn : conn } do
idempotency_key = " Pikachu rocks! "
conn_one =
conn
|> put_req_header ( " idempotency-key " , idempotency_key )
|> post ( " /api/v1/statuses " , %{
" status " = > " cofe " ,
" spoiler_text " = > " 2hu " ,
" sensitive " = > " false "
} )
{ :ok , ttl } = Cachex . ttl ( :idempotency_cache , idempotency_key )
# Six hours
assert ttl > :timer . seconds ( 6 * 60 * 60 - 1 )
assert %{ " content " = > " cofe " , " id " = > id , " spoiler_text " = > " 2hu " , " sensitive " = > false } =
json_response ( conn_one , 200 )
assert Activity . get_by_id ( id )
conn_two =
conn
|> put_req_header ( " idempotency-key " , idempotency_key )
|> post ( " /api/v1/statuses " , %{
" status " = > " cofe " ,
" spoiler_text " = > " 2hu " ,
" sensitive " = > " false "
} )
assert %{ " id " = > second_id } = json_response ( conn_two , 200 )
assert id == second_id
conn_three =
conn
|> post ( " /api/v1/statuses " , %{
" status " = > " cofe " ,
" spoiler_text " = > " 2hu " ,
" sensitive " = > " false "
} )
assert %{ " id " = > third_id } = json_response ( conn_three , 200 )
refute id == third_id
# An activity that will expire:
# 2 hours
expires_in = 120 * 60
conn_four =
conn
|> post ( " api/v1/statuses " , %{
" status " = > " oolong " ,
" expires_in " = > expires_in
} )
assert fourth_response = %{ " id " = > fourth_id } = json_response ( conn_four , 200 )
assert activity = Activity . get_by_id ( fourth_id )
assert expiration = ActivityExpiration . get_by_activity_id ( fourth_id )
estimated_expires_at =
NaiveDateTime . utc_now ( )
|> NaiveDateTime . add ( expires_in )
|> NaiveDateTime . truncate ( :second )
# This assert will fail if the test takes longer than a minute. I sure hope it never does:
assert abs ( NaiveDateTime . diff ( expiration . scheduled_at , estimated_expires_at , :second ) ) < 60
assert fourth_response [ " pleroma " ] [ " expires_at " ] ==
NaiveDateTime . to_iso8601 ( expiration . scheduled_at )
end
2020-02-12 10:43:07 -05:00
test " it fails to create a status if `expires_in` is less or equal than an hour " , %{
conn : conn
} do
# 1 hour
expires_in = 60 * 60
assert %{ " error " = > " Expiry date is too soon " } =
conn
|> post ( " api/v1/statuses " , %{
" status " = > " oolong " ,
" expires_in " = > expires_in
} )
|> json_response ( 422 )
# 30 minutes
expires_in = 30 * 60
assert %{ " error " = > " Expiry date is too soon " } =
conn
|> post ( " api/v1/statuses " , %{
" status " = > " oolong " ,
" expires_in " = > expires_in
} )
|> json_response ( 422 )
end
2019-12-15 14:32:42 -05:00
test " posting an undefined status with an attachment " , %{ user : user , conn : conn } do
2019-09-27 18:56:20 -04:00
file = % Plug.Upload {
content_type : " image/jpg " ,
path : Path . absname ( " test/fixtures/image.jpg " ) ,
filename : " an_image.jpg "
}
{ :ok , upload } = ActivityPub . upload ( file , actor : user . ap_id )
conn =
2019-12-15 14:32:42 -05:00
post ( conn , " /api/v1/statuses " , %{
2019-09-27 19:21:28 -04:00
" media_ids " = > [ to_string ( upload . id ) ]
2019-09-27 18:56:20 -04:00
} )
assert json_response ( conn , 200 )
end
2019-12-15 14:32:42 -05:00
test " replying to a status " , %{ user : user , conn : conn } do
2019-09-26 02:38:45 -04:00
{ :ok , replied_to } = CommonAPI . post ( user , %{ " status " = > " cofe " } )
conn =
conn
|> post ( " /api/v1/statuses " , %{ " status " = > " xD " , " in_reply_to_id " = > replied_to . id } )
assert %{ " content " = > " xD " , " id " = > id } = json_response ( conn , 200 )
activity = Activity . get_by_id ( id )
assert activity . data [ " context " ] == replied_to . data [ " context " ]
assert Activity . get_in_reply_to_activity ( activity ) . id == replied_to . id
end
2019-12-15 14:32:42 -05:00
test " replying to a direct message with visibility other than direct " , %{
user : user ,
conn : conn
} do
2019-09-26 02:38:45 -04:00
{ :ok , replied_to } = CommonAPI . post ( user , %{ " status " = > " suya.. " , " visibility " = > " direct " } )
Enum . each ( [ " public " , " private " , " unlisted " ] , fn visibility ->
conn =
conn
|> post ( " /api/v1/statuses " , %{
" status " = > " @ #{ user . nickname } hey " ,
" in_reply_to_id " = > replied_to . id ,
" visibility " = > visibility
} )
assert json_response ( conn , 422 ) == %{ " error " = > " The message visibility must be direct " }
end )
end
test " posting a status with an invalid in_reply_to_id " , %{ conn : conn } do
2019-12-15 14:32:42 -05:00
conn = post ( conn , " /api/v1/statuses " , %{ " status " = > " xD " , " in_reply_to_id " = > " " } )
2019-09-26 02:38:45 -04:00
assert %{ " content " = > " xD " , " id " = > id } = json_response ( conn , 200 )
assert Activity . get_by_id ( id )
end
test " posting a sensitive status " , %{ conn : conn } do
2019-12-15 14:32:42 -05:00
conn = post ( conn , " /api/v1/statuses " , %{ " status " = > " cofe " , " sensitive " = > true } )
2019-09-26 02:38:45 -04:00
assert %{ " content " = > " cofe " , " id " = > id , " sensitive " = > true } = json_response ( conn , 200 )
assert Activity . get_by_id ( id )
end
test " posting a fake status " , %{ conn : conn } do
real_conn =
2019-12-15 14:32:42 -05:00
post ( conn , " /api/v1/statuses " , %{
2019-09-26 02:38:45 -04:00
" status " = >
" \" Tenshi Eating a Corndog \" is a much discussed concept on /jp/. The significance of it is disputed, so I will focus on one core concept: the symbolism behind it "
} )
real_status = json_response ( real_conn , 200 )
assert real_status
assert Object . get_by_ap_id ( real_status [ " uri " ] )
real_status =
real_status
|> Map . put ( " id " , nil )
|> Map . put ( " url " , nil )
|> Map . put ( " uri " , nil )
|> Map . put ( " created_at " , nil )
|> Kernel . put_in ( [ " pleroma " , " conversation_id " ] , nil )
fake_conn =
2019-12-15 14:32:42 -05:00
post ( conn , " /api/v1/statuses " , %{
2019-09-26 02:38:45 -04:00
" status " = >
" \" Tenshi Eating a Corndog \" is a much discussed concept on /jp/. The significance of it is disputed, so I will focus on one core concept: the symbolism behind it " ,
" preview " = > true
} )
fake_status = json_response ( fake_conn , 200 )
assert fake_status
refute Object . get_by_ap_id ( fake_status [ " uri " ] )
fake_status =
fake_status
|> Map . put ( " id " , nil )
|> Map . put ( " url " , nil )
|> Map . put ( " uri " , nil )
|> Map . put ( " created_at " , nil )
|> Kernel . put_in ( [ " pleroma " , " conversation_id " ] , nil )
assert real_status == fake_status
end
test " posting a status with OGP link preview " , %{ conn : conn } do
Tesla.Mock . mock ( fn env -> apply ( HttpRequestMock , :request , [ env ] ) end )
Config . put ( [ :rich_media , :enabled ] , true )
conn =
2019-12-15 14:32:42 -05:00
post ( conn , " /api/v1/statuses " , %{
2019-09-26 02:38:45 -04:00
" status " = > " https://example.com/ogp "
} )
assert %{ " id " = > id , " card " = > %{ " title " = > " The Rock " } } = json_response ( conn , 200 )
assert Activity . get_by_id ( id )
end
test " posting a direct status " , %{ conn : conn } do
user2 = insert ( :user )
content = " direct cofe @ #{ user2 . nickname } "
2019-12-15 14:32:42 -05:00
conn = post ( conn , " api/v1/statuses " , %{ " status " = > content , " visibility " = > " direct " } )
2019-09-26 02:38:45 -04:00
assert %{ " id " = > id } = response = json_response ( conn , 200 )
assert response [ " visibility " ] == " direct "
assert response [ " pleroma " ] [ " direct_conversation_id " ]
assert activity = Activity . get_by_id ( id )
assert activity . recipients == [ user2 . ap_id , conn . assigns [ :user ] . ap_id ]
assert activity . data [ " to " ] == [ user2 . ap_id ]
assert activity . data [ " cc " ] == [ ]
end
end
2019-09-27 02:35:45 -04:00
describe " posting scheduled statuses " do
2019-12-15 14:32:42 -05:00
setup do : oauth_access ( [ " write:statuses " ] )
2019-09-27 02:35:45 -04:00
test " creates a scheduled activity " , %{ conn : conn } do
scheduled_at = NaiveDateTime . add ( NaiveDateTime . utc_now ( ) , :timer . minutes ( 120 ) , :millisecond )
conn =
2019-12-15 14:32:42 -05:00
post ( conn , " /api/v1/statuses " , %{
2019-09-27 02:35:45 -04:00
" status " = > " scheduled " ,
" scheduled_at " = > scheduled_at
} )
assert %{ " scheduled_at " = > expected_scheduled_at } = json_response ( conn , 200 )
assert expected_scheduled_at == CommonAPI.Utils . to_masto_date ( scheduled_at )
assert [ ] == Repo . all ( Activity )
end
2019-12-15 14:32:42 -05:00
test " creates a scheduled activity with a media attachment " , %{ user : user , conn : conn } do
2019-09-27 02:35:45 -04:00
scheduled_at = NaiveDateTime . add ( NaiveDateTime . utc_now ( ) , :timer . minutes ( 120 ) , :millisecond )
file = % Plug.Upload {
content_type : " image/jpg " ,
path : Path . absname ( " test/fixtures/image.jpg " ) ,
filename : " an_image.jpg "
}
{ :ok , upload } = ActivityPub . upload ( file , actor : user . ap_id )
conn =
2019-12-15 14:32:42 -05:00
post ( conn , " /api/v1/statuses " , %{
2019-09-27 02:35:45 -04:00
" media_ids " = > [ to_string ( upload . id ) ] ,
" status " = > " scheduled " ,
" scheduled_at " = > scheduled_at
} )
assert %{ " media_attachments " = > [ media_attachment ] } = json_response ( conn , 200 )
assert %{ " type " = > " image " } = media_attachment
end
test " skips the scheduling and creates the activity if scheduled_at is earlier than 5 minutes from now " ,
%{ conn : conn } do
scheduled_at =
NaiveDateTime . add ( NaiveDateTime . utc_now ( ) , :timer . minutes ( 5 ) - 1 , :millisecond )
conn =
2019-12-15 14:32:42 -05:00
post ( conn , " /api/v1/statuses " , %{
2019-09-27 02:35:45 -04:00
" status " = > " not scheduled " ,
" scheduled_at " = > scheduled_at
} )
assert %{ " content " = > " not scheduled " } = json_response ( conn , 200 )
assert [ ] == Repo . all ( ScheduledActivity )
end
2019-12-15 14:32:42 -05:00
test " returns error when daily user limit is exceeded " , %{ user : user , conn : conn } do
2019-09-27 02:35:45 -04:00
today =
NaiveDateTime . utc_now ( )
|> NaiveDateTime . add ( :timer . minutes ( 6 ) , :millisecond )
|> NaiveDateTime . to_iso8601 ( )
attrs = %{ params : %{ } , scheduled_at : today }
{ :ok , _ } = ScheduledActivity . create ( user , attrs )
{ :ok , _ } = ScheduledActivity . create ( user , attrs )
2019-12-15 14:32:42 -05:00
conn = post ( conn , " /api/v1/statuses " , %{ " status " = > " scheduled " , " scheduled_at " = > today } )
2019-09-27 02:35:45 -04:00
assert %{ " error " = > " daily limit exceeded " } == json_response ( conn , 422 )
end
2019-12-15 14:32:42 -05:00
test " returns error when total user limit is exceeded " , %{ user : user , conn : conn } do
2019-09-27 02:35:45 -04:00
today =
NaiveDateTime . utc_now ( )
|> NaiveDateTime . add ( :timer . minutes ( 6 ) , :millisecond )
|> NaiveDateTime . to_iso8601 ( )
tomorrow =
NaiveDateTime . utc_now ( )
|> NaiveDateTime . add ( :timer . hours ( 36 ) , :millisecond )
|> NaiveDateTime . to_iso8601 ( )
attrs = %{ params : %{ } , scheduled_at : today }
{ :ok , _ } = ScheduledActivity . create ( user , attrs )
{ :ok , _ } = ScheduledActivity . create ( user , attrs )
{ :ok , _ } = ScheduledActivity . create ( user , %{ params : %{ } , scheduled_at : tomorrow } )
conn =
2019-12-15 14:32:42 -05:00
post ( conn , " /api/v1/statuses " , %{ " status " = > " scheduled " , " scheduled_at " = > tomorrow } )
2019-09-27 02:35:45 -04:00
assert %{ " error " = > " total limit exceeded " } == json_response ( conn , 422 )
end
end
2019-09-26 02:38:45 -04:00
describe " posting polls " do
2019-12-15 14:32:42 -05:00
setup do : oauth_access ( [ " write:statuses " ] )
2019-09-26 02:38:45 -04:00
test " posting a poll " , %{ conn : conn } do
time = NaiveDateTime . utc_now ( )
conn =
2019-12-15 14:32:42 -05:00
post ( conn , " /api/v1/statuses " , %{
2019-09-26 02:38:45 -04:00
" status " = > " Who is the # bestgrill? " ,
" poll " = > %{ " options " = > [ " Rei " , " Asuka " , " Misato " ] , " expires_in " = > 420 }
} )
response = json_response ( conn , 200 )
assert Enum . all? ( response [ " poll " ] [ " options " ] , fn %{ " title " = > title } ->
title in [ " Rei " , " Asuka " , " Misato " ]
end )
assert NaiveDateTime . diff ( NaiveDateTime . from_iso8601! ( response [ " poll " ] [ " expires_at " ] ) , time ) in 420 . . 430
refute response [ " poll " ] [ " expred " ]
2020-02-07 10:57:46 -05:00
question = Object . get_by_id ( response [ " poll " ] [ " id " ] )
# closed contains utc timezone
assert question . data [ " closed " ] =~ " Z "
2019-09-26 02:38:45 -04:00
end
test " option limit is enforced " , %{ conn : conn } do
limit = Config . get ( [ :instance , :poll_limits , :max_options ] )
conn =
2019-12-15 14:32:42 -05:00
post ( conn , " /api/v1/statuses " , %{
2019-09-26 02:38:45 -04:00
" status " = > " desu~ " ,
" poll " = > %{ " options " = > Enum . map ( 0 . . limit , fn _ -> " desu " end ) , " expires_in " = > 1 }
} )
%{ " error " = > error } = json_response ( conn , 422 )
assert error == " Poll can't contain more than #{ limit } options "
end
test " option character limit is enforced " , %{ conn : conn } do
limit = Config . get ( [ :instance , :poll_limits , :max_option_chars ] )
conn =
2019-12-15 14:32:42 -05:00
post ( conn , " /api/v1/statuses " , %{
2019-09-26 02:38:45 -04:00
" status " = > " ... " ,
" poll " = > %{
" options " = > [ Enum . reduce ( 0 . . limit , " " , fn _ , acc -> acc <> " . " end ) ] ,
" expires_in " = > 1
}
} )
%{ " error " = > error } = json_response ( conn , 422 )
assert error == " Poll options cannot be longer than #{ limit } characters each "
end
test " minimal date limit is enforced " , %{ conn : conn } do
limit = Config . get ( [ :instance , :poll_limits , :min_expiration ] )
conn =
2019-12-15 14:32:42 -05:00
post ( conn , " /api/v1/statuses " , %{
2019-09-26 02:38:45 -04:00
" status " = > " imagine arbitrary limits " ,
" poll " = > %{
" options " = > [ " this post was made by pleroma gang " ] ,
" expires_in " = > limit - 1
}
} )
%{ " error " = > error } = json_response ( conn , 422 )
assert error == " Expiration date is too soon "
end
test " maximum date limit is enforced " , %{ conn : conn } do
limit = Config . get ( [ :instance , :poll_limits , :max_expiration ] )
conn =
2019-12-15 14:32:42 -05:00
post ( conn , " /api/v1/statuses " , %{
2019-09-26 02:38:45 -04:00
" status " = > " imagine arbitrary limits " ,
" poll " = > %{
" options " = > [ " this post was made by pleroma gang " ] ,
" expires_in " = > limit + 1
}
} )
%{ " error " = > error } = json_response ( conn , 422 )
assert error == " Expiration date is too far in the future "
end
end
2019-12-15 14:32:42 -05:00
test " get a status " do
%{ conn : conn } = oauth_access ( [ " read:statuses " ] )
2019-09-26 02:38:45 -04:00
activity = insert ( :note_activity )
2019-12-15 14:32:42 -05:00
conn = get ( conn , " /api/v1/statuses/ #{ activity . id } " )
2019-09-26 02:38:45 -04:00
assert %{ " id " = > id } = json_response ( conn , 200 )
assert id == to_string ( activity . id )
end
2019-12-15 14:32:42 -05:00
test " get a direct status " do
%{ user : user , conn : conn } = oauth_access ( [ " read:statuses " ] )
2019-10-08 23:51:14 -04:00
other_user = insert ( :user )
{ :ok , activity } =
CommonAPI . post ( user , %{ " status " = > " @ #{ other_user . nickname } " , " visibility " = > " direct " } )
conn =
conn
|> assign ( :user , user )
|> get ( " /api/v1/statuses/ #{ activity . id } " )
[ participation ] = Participation . for_user ( user )
res = json_response ( conn , 200 )
assert res [ " pleroma " ] [ " direct_conversation_id " ] == participation . id
end
2019-12-15 14:32:42 -05:00
test " get statuses by IDs " do
%{ conn : conn } = oauth_access ( [ " read:statuses " ] )
2019-09-26 02:38:45 -04:00
%{ id : id1 } = insert ( :note_activity )
%{ id : id2 } = insert ( :note_activity )
query_string = " ids[]= #{ id1 } &ids[]= #{ id2 } "
conn = get ( conn , " /api/v1/statuses/? #{ query_string } " )
assert [ %{ " id " = > ^ id1 } , %{ " id " = > ^ id2 } ] = Enum . sort_by ( json_response ( conn , :ok ) , & &1 [ " id " ] )
end
describe " deleting a status " do
2019-12-15 14:32:42 -05:00
test " when you created it " do
%{ user : author , conn : conn } = oauth_access ( [ " write:statuses " ] )
activity = insert ( :note_activity , user : author )
2019-09-26 02:38:45 -04:00
conn =
conn
|> assign ( :user , author )
|> delete ( " /api/v1/statuses/ #{ activity . id } " )
assert %{ } = json_response ( conn , 200 )
refute Activity . get_by_id ( activity . id )
end
2019-12-15 14:32:42 -05:00
test " when you didn't create it " do
%{ conn : conn } = oauth_access ( [ " write:statuses " ] )
2019-09-26 02:38:45 -04:00
activity = insert ( :note_activity )
2019-12-15 14:32:42 -05:00
conn = delete ( conn , " /api/v1/statuses/ #{ activity . id } " )
2019-09-26 02:38:45 -04:00
assert %{ " error " = > _ } = json_response ( conn , 403 )
assert Activity . get_by_id ( activity . id ) == activity
end
test " when you're an admin or moderator " , %{ conn : conn } do
activity1 = insert ( :note_activity )
activity2 = insert ( :note_activity )
2019-10-16 14:59:21 -04:00
admin = insert ( :user , is_admin : true )
moderator = insert ( :user , is_moderator : true )
2019-09-26 02:38:45 -04:00
res_conn =
conn
|> assign ( :user , admin )
2019-12-15 14:32:42 -05:00
|> assign ( :token , insert ( :oauth_token , user : admin , scopes : [ " write:statuses " ] ) )
2019-09-26 02:38:45 -04:00
|> delete ( " /api/v1/statuses/ #{ activity1 . id } " )
assert %{ } = json_response ( res_conn , 200 )
res_conn =
conn
|> assign ( :user , moderator )
2019-12-15 14:32:42 -05:00
|> assign ( :token , insert ( :oauth_token , user : moderator , scopes : [ " write:statuses " ] ) )
2019-09-26 02:38:45 -04:00
|> delete ( " /api/v1/statuses/ #{ activity2 . id } " )
assert %{ } = json_response ( res_conn , 200 )
refute Activity . get_by_id ( activity1 . id )
refute Activity . get_by_id ( activity2 . id )
end
end
describe " reblogging " do
2019-12-15 14:32:42 -05:00
setup do : oauth_access ( [ " write:statuses " ] )
2019-09-26 02:38:45 -04:00
test " reblogs and returns the reblogged status " , %{ conn : conn } do
activity = insert ( :note_activity )
2019-12-15 14:32:42 -05:00
conn = post ( conn , " /api/v1/statuses/ #{ activity . id } /reblog " )
2019-09-26 02:38:45 -04:00
assert %{
" reblog " = > %{ " id " = > id , " reblogged " = > true , " reblogs_count " = > 1 } ,
" reblogged " = > true
} = json_response ( conn , 200 )
assert to_string ( activity . id ) == id
end
2019-10-01 13:08:25 -04:00
test " reblogs privately and returns the reblogged status " , %{ conn : conn } do
activity = insert ( :note_activity )
2019-12-15 14:32:42 -05:00
conn = post ( conn , " /api/v1/statuses/ #{ activity . id } /reblog " , %{ " visibility " = > " private " } )
2019-10-01 13:08:25 -04:00
assert %{
2019-10-01 15:40:35 -04:00
" reblog " = > %{ " id " = > id , " reblogged " = > true , " reblogs_count " = > 1 } ,
2019-10-01 13:08:25 -04:00
" reblogged " = > true ,
" visibility " = > " private "
} = json_response ( conn , 200 )
assert to_string ( activity . id ) == id
end
2019-12-15 14:32:42 -05:00
test " reblogged status for another user " do
2019-09-26 02:38:45 -04:00
activity = insert ( :note_activity )
user1 = insert ( :user )
user2 = insert ( :user )
user3 = insert ( :user )
CommonAPI . favorite ( activity . id , user2 )
{ :ok , _bookmark } = Pleroma.Bookmark . create ( user2 . id , activity . id )
{ :ok , reblog_activity1 , _object } = CommonAPI . repeat ( activity . id , user1 )
{ :ok , _ , _object } = CommonAPI . repeat ( activity . id , user2 )
conn_res =
2019-12-15 14:32:42 -05:00
build_conn ( )
2019-09-26 02:38:45 -04:00
|> assign ( :user , user3 )
2019-12-15 14:32:42 -05:00
|> assign ( :token , insert ( :oauth_token , user : user3 , scopes : [ " read:statuses " ] ) )
2019-09-26 02:38:45 -04:00
|> get ( " /api/v1/statuses/ #{ reblog_activity1 . id } " )
assert %{
" reblog " = > %{ " id " = > id , " reblogged " = > false , " reblogs_count " = > 2 } ,
" reblogged " = > false ,
" favourited " = > false ,
" bookmarked " = > false
} = json_response ( conn_res , 200 )
conn_res =
2019-12-15 14:32:42 -05:00
build_conn ( )
2019-09-26 02:38:45 -04:00
|> assign ( :user , user2 )
2019-12-15 14:32:42 -05:00
|> assign ( :token , insert ( :oauth_token , user : user2 , scopes : [ " read:statuses " ] ) )
2019-09-26 02:38:45 -04:00
|> get ( " /api/v1/statuses/ #{ reblog_activity1 . id } " )
assert %{
" reblog " = > %{ " id " = > id , " reblogged " = > true , " reblogs_count " = > 2 } ,
" reblogged " = > true ,
" favourited " = > true ,
" bookmarked " = > true
} = json_response ( conn_res , 200 )
assert to_string ( activity . id ) == id
end
test " returns 400 error when activity is not exist " , %{ conn : conn } do
2019-12-15 14:32:42 -05:00
conn = post ( conn , " /api/v1/statuses/foo/reblog " )
2019-09-26 02:38:45 -04:00
assert json_response ( conn , 400 ) == %{ " error " = > " Could not repeat " }
end
end
describe " unreblogging " do
2019-12-15 14:32:42 -05:00
setup do : oauth_access ( [ " write:statuses " ] )
test " unreblogs and returns the unreblogged status " , %{ user : user , conn : conn } do
2019-09-26 02:38:45 -04:00
activity = insert ( :note_activity )
{ :ok , _ , _ } = CommonAPI . repeat ( activity . id , user )
2019-12-15 14:32:42 -05:00
conn = post ( conn , " /api/v1/statuses/ #{ activity . id } /unreblog " )
2019-09-26 02:38:45 -04:00
assert %{ " id " = > id , " reblogged " = > false , " reblogs_count " = > 0 } = json_response ( conn , 200 )
assert to_string ( activity . id ) == id
end
test " returns 400 error when activity is not exist " , %{ conn : conn } do
2019-12-15 14:32:42 -05:00
conn = post ( conn , " /api/v1/statuses/foo/unreblog " )
2019-09-26 02:38:45 -04:00
assert json_response ( conn , 400 ) == %{ " error " = > " Could not unrepeat " }
end
end
describe " favoriting " do
2019-12-15 14:32:42 -05:00
setup do : oauth_access ( [ " write:favourites " ] )
2019-09-26 02:38:45 -04:00
test " favs a status and returns it " , %{ conn : conn } do
activity = insert ( :note_activity )
2019-12-15 14:32:42 -05:00
conn = post ( conn , " /api/v1/statuses/ #{ activity . id } /favourite " )
2019-09-26 02:38:45 -04:00
assert %{ " id " = > id , " favourites_count " = > 1 , " favourited " = > true } =
json_response ( conn , 200 )
assert to_string ( activity . id ) == id
end
2020-01-20 08:27:59 -05:00
test " favoriting twice will just return 200 " , %{ conn : conn } do
activity = insert ( :note_activity )
post ( conn , " /api/v1/statuses/ #{ activity . id } /favourite " )
2020-01-20 09:31:14 -05:00
assert post ( conn , " /api/v1/statuses/ #{ activity . id } /favourite " ) |> json_response ( 200 )
2020-01-20 08:27:59 -05:00
end
2019-09-26 02:38:45 -04:00
test " returns 400 error for a wrong id " , %{ conn : conn } do
2019-12-15 14:32:42 -05:00
conn = post ( conn , " /api/v1/statuses/1/favourite " )
2019-09-26 02:38:45 -04:00
assert json_response ( conn , 400 ) == %{ " error " = > " Could not favorite " }
end
end
describe " unfavoriting " do
2019-12-15 14:32:42 -05:00
setup do : oauth_access ( [ " write:favourites " ] )
test " unfavorites a status and returns it " , %{ user : user , conn : conn } do
2019-09-26 02:38:45 -04:00
activity = insert ( :note_activity )
{ :ok , _ , _ } = CommonAPI . favorite ( activity . id , user )
2019-12-15 14:32:42 -05:00
conn = post ( conn , " /api/v1/statuses/ #{ activity . id } /unfavourite " )
2019-09-26 02:38:45 -04:00
assert %{ " id " = > id , " favourites_count " = > 0 , " favourited " = > false } =
json_response ( conn , 200 )
assert to_string ( activity . id ) == id
end
test " returns 400 error for a wrong id " , %{ conn : conn } do
2019-12-15 14:32:42 -05:00
conn = post ( conn , " /api/v1/statuses/1/unfavourite " )
2019-09-26 02:38:45 -04:00
assert json_response ( conn , 400 ) == %{ " error " = > " Could not unfavorite " }
end
end
describe " pinned statuses " do
2019-12-15 14:32:42 -05:00
setup do : oauth_access ( [ " write:accounts " ] )
setup %{ user : user } do
2019-09-26 02:38:45 -04:00
{ :ok , activity } = CommonAPI . post ( user , %{ " status " = > " HI!!! " } )
2019-12-15 14:32:42 -05:00
%{ activity : activity }
2019-09-26 02:38:45 -04:00
end
clear_config ( [ :instance , :max_pinned_statuses ] ) do
Config . put ( [ :instance , :max_pinned_statuses ] , 1 )
end
test " pin status " , %{ conn : conn , user : user , activity : activity } do
id_str = to_string ( activity . id )
assert %{ " id " = > ^ id_str , " pinned " = > true } =
conn
|> post ( " /api/v1/statuses/ #{ activity . id } /pin " )
|> json_response ( 200 )
assert [ %{ " id " = > ^ id_str , " pinned " = > true } ] =
conn
|> get ( " /api/v1/accounts/ #{ user . id } /statuses?pinned=true " )
|> json_response ( 200 )
end
test " /pin: returns 400 error when activity is not public " , %{ conn : conn , user : user } do
{ :ok , dm } = CommonAPI . post ( user , %{ " status " = > " test " , " visibility " = > " direct " } )
2019-12-15 14:32:42 -05:00
conn = post ( conn , " /api/v1/statuses/ #{ dm . id } /pin " )
2019-09-26 02:38:45 -04:00
assert json_response ( conn , 400 ) == %{ " error " = > " Could not pin " }
end
test " unpin status " , %{ conn : conn , user : user , activity : activity } do
{ :ok , _ } = CommonAPI . pin ( activity . id , user )
2019-12-15 14:32:42 -05:00
user = refresh_record ( user )
2019-09-26 02:38:45 -04:00
id_str = to_string ( activity . id )
assert %{ " id " = > ^ id_str , " pinned " = > false } =
conn
|> assign ( :user , user )
|> post ( " /api/v1/statuses/ #{ activity . id } /unpin " )
|> json_response ( 200 )
assert [ ] =
conn
|> get ( " /api/v1/accounts/ #{ user . id } /statuses?pinned=true " )
|> json_response ( 200 )
end
2019-12-15 14:32:42 -05:00
test " /unpin: returns 400 error when activity is not exist " , %{ conn : conn } do
conn = post ( conn , " /api/v1/statuses/1/unpin " )
2019-09-26 02:38:45 -04:00
assert json_response ( conn , 400 ) == %{ " error " = > " Could not unpin " }
end
test " max pinned statuses " , %{ conn : conn , user : user , activity : activity_one } do
{ :ok , activity_two } = CommonAPI . post ( user , %{ " status " = > " HI!!! " } )
id_str_one = to_string ( activity_one . id )
assert %{ " id " = > ^ id_str_one , " pinned " = > true } =
conn
|> post ( " /api/v1/statuses/ #{ id_str_one } /pin " )
|> json_response ( 200 )
user = refresh_record ( user )
assert %{ " error " = > " You have already pinned the maximum number of statuses " } =
conn
|> assign ( :user , user )
|> post ( " /api/v1/statuses/ #{ activity_two . id } /pin " )
|> json_response ( 400 )
end
end
describe " cards " do
setup do
Config . put ( [ :rich_media , :enabled ] , true )
2019-12-15 14:32:42 -05:00
oauth_access ( [ " read:statuses " ] )
2019-09-26 02:38:45 -04:00
end
test " returns rich-media card " , %{ conn : conn , user : user } do
Tesla.Mock . mock ( fn env -> apply ( HttpRequestMock , :request , [ env ] ) end )
{ :ok , activity } = CommonAPI . post ( user , %{ " status " = > " https://example.com/ogp " } )
card_data = %{
" image " = > " http://ia.media-imdb.com/images/rock.jpg " ,
" provider_name " = > " example.com " ,
" provider_url " = > " https://example.com " ,
" title " = > " The Rock " ,
" type " = > " link " ,
" url " = > " https://example.com/ogp " ,
" description " = >
" Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer. " ,
" pleroma " = > %{
" opengraph " = > %{
" image " = > " http://ia.media-imdb.com/images/rock.jpg " ,
" title " = > " The Rock " ,
" type " = > " video.movie " ,
" url " = > " https://example.com/ogp " ,
" description " = >
" Directed by Michael Bay. With Sean Connery, Nicolas Cage, Ed Harris, John Spencer. "
}
}
}
response =
conn
|> get ( " /api/v1/statuses/ #{ activity . id } /card " )
|> json_response ( 200 )
assert response == card_data
# works with private posts
{ :ok , activity } =
CommonAPI . post ( user , %{ " status " = > " https://example.com/ogp " , " visibility " = > " direct " } )
response_two =
conn
|> get ( " /api/v1/statuses/ #{ activity . id } /card " )
|> json_response ( 200 )
assert response_two == card_data
end
test " replaces missing description with an empty string " , %{ conn : conn , user : user } do
Tesla.Mock . mock ( fn env -> apply ( HttpRequestMock , :request , [ env ] ) end )
{ :ok , activity } =
CommonAPI . post ( user , %{ " status " = > " https://example.com/ogp-missing-data " } )
response =
conn
|> get ( " /api/v1/statuses/ #{ activity . id } /card " )
|> json_response ( :ok )
assert response == %{
" type " = > " link " ,
" title " = > " Pleroma " ,
" description " = > " " ,
" image " = > nil ,
" provider_name " = > " example.com " ,
" provider_url " = > " https://example.com " ,
" url " = > " https://example.com/ogp-missing-data " ,
" pleroma " = > %{
" opengraph " = > %{
" title " = > " Pleroma " ,
" type " = > " website " ,
" url " = > " https://example.com/ogp-missing-data "
}
}
}
end
end
test " bookmarks " do
2019-12-15 14:32:42 -05:00
%{ conn : conn } = oauth_access ( [ " write:bookmarks " , " read:bookmarks " ] )
author = insert ( :user )
2019-09-26 02:38:45 -04:00
{ :ok , activity1 } =
2019-12-15 14:32:42 -05:00
CommonAPI . post ( author , %{
2019-09-26 02:38:45 -04:00
" status " = > " heweoo? "
} )
{ :ok , activity2 } =
2019-12-15 14:32:42 -05:00
CommonAPI . post ( author , %{
2019-09-26 02:38:45 -04:00
" status " = > " heweoo! "
} )
2019-12-15 14:32:42 -05:00
response1 = post ( conn , " /api/v1/statuses/ #{ activity1 . id } /bookmark " )
2019-09-26 02:38:45 -04:00
assert json_response ( response1 , 200 ) [ " bookmarked " ] == true
2019-12-15 14:32:42 -05:00
response2 = post ( conn , " /api/v1/statuses/ #{ activity2 . id } /bookmark " )
2019-09-26 02:38:45 -04:00
assert json_response ( response2 , 200 ) [ " bookmarked " ] == true
2019-12-15 14:32:42 -05:00
bookmarks = get ( conn , " /api/v1/bookmarks " )
2019-09-26 02:38:45 -04:00
assert [ json_response ( response2 , 200 ) , json_response ( response1 , 200 ) ] ==
json_response ( bookmarks , 200 )
2019-12-15 14:32:42 -05:00
response1 = post ( conn , " /api/v1/statuses/ #{ activity1 . id } /unbookmark " )
2019-09-26 02:38:45 -04:00
assert json_response ( response1 , 200 ) [ " bookmarked " ] == false
2019-12-15 14:32:42 -05:00
bookmarks = get ( conn , " /api/v1/bookmarks " )
2019-09-26 02:38:45 -04:00
assert [ json_response ( response2 , 200 ) ] == json_response ( bookmarks , 200 )
end
describe " conversation muting " do
2019-12-15 14:32:42 -05:00
setup do : oauth_access ( [ " write:mutes " ] )
2019-09-26 02:38:45 -04:00
setup do
post_user = insert ( :user )
{ :ok , activity } = CommonAPI . post ( post_user , %{ " status " = > " HIE " } )
2019-12-15 14:32:42 -05:00
%{ activity : activity }
2019-09-26 02:38:45 -04:00
end
2019-12-15 14:32:42 -05:00
test " mute conversation " , %{ conn : conn , activity : activity } do
2019-09-26 02:38:45 -04:00
id_str = to_string ( activity . id )
assert %{ " id " = > ^ id_str , " muted " = > true } =
conn
|> post ( " /api/v1/statuses/ #{ activity . id } /mute " )
|> json_response ( 200 )
end
test " cannot mute already muted conversation " , %{ conn : conn , user : user , activity : activity } do
{ :ok , _ } = CommonAPI . add_mute ( user , activity )
2019-12-15 14:32:42 -05:00
conn = post ( conn , " /api/v1/statuses/ #{ activity . id } /mute " )
2019-09-26 02:38:45 -04:00
assert json_response ( conn , 400 ) == %{ " error " = > " conversation is already muted " }
end
test " unmute conversation " , %{ conn : conn , user : user , activity : activity } do
{ :ok , _ } = CommonAPI . add_mute ( user , activity )
id_str = to_string ( activity . id )
assert %{ " id " = > ^ id_str , " muted " = > false } =
conn
2019-12-15 14:32:42 -05:00
# |> assign(:user, user)
2019-09-26 02:38:45 -04:00
|> post ( " /api/v1/statuses/ #{ activity . id } /unmute " )
|> json_response ( 200 )
end
end
test " Repeated posts that are replies incorrectly have in_reply_to_id null " , %{ conn : conn } do
user1 = insert ( :user )
user2 = insert ( :user )
user3 = insert ( :user )
{ :ok , replied_to } = CommonAPI . post ( user1 , %{ " status " = > " cofe " } )
# Reply to status from another user
conn1 =
conn
|> assign ( :user , user2 )
2019-12-15 14:32:42 -05:00
|> assign ( :token , insert ( :oauth_token , user : user2 , scopes : [ " write:statuses " ] ) )
2019-09-26 02:38:45 -04:00
|> post ( " /api/v1/statuses " , %{ " status " = > " xD " , " in_reply_to_id " = > replied_to . id } )
assert %{ " content " = > " xD " , " id " = > id } = json_response ( conn1 , 200 )
activity = Activity . get_by_id_with_object ( id )
assert Object . normalize ( activity ) . data [ " inReplyTo " ] == Object . normalize ( replied_to ) . data [ " id " ]
assert Activity . get_in_reply_to_activity ( activity ) . id == replied_to . id
# Reblog from the third user
conn2 =
conn
|> assign ( :user , user3 )
2019-12-15 14:32:42 -05:00
|> assign ( :token , insert ( :oauth_token , user : user3 , scopes : [ " write:statuses " ] ) )
2019-09-26 02:38:45 -04:00
|> post ( " /api/v1/statuses/ #{ activity . id } /reblog " )
assert %{ " reblog " = > %{ " id " = > id , " reblogged " = > true , " reblogs_count " = > 1 } } =
json_response ( conn2 , 200 )
assert to_string ( activity . id ) == id
# Getting third user status
conn3 =
conn
|> assign ( :user , user3 )
2019-12-15 14:32:42 -05:00
|> assign ( :token , insert ( :oauth_token , user : user3 , scopes : [ " read:statuses " ] ) )
2019-09-26 02:38:45 -04:00
|> get ( " api/v1/timelines/home " )
[ reblogged_activity ] = json_response ( conn3 , 200 )
assert reblogged_activity [ " reblog " ] [ " in_reply_to_id " ] == replied_to . id
replied_to_user = User . get_by_ap_id ( replied_to . data [ " actor " ] )
assert reblogged_activity [ " reblog " ] [ " in_reply_to_account_id " ] == replied_to_user . id
end
describe " GET /api/v1/statuses/:id/favourited_by " do
2019-12-15 14:32:42 -05:00
setup do : oauth_access ( [ " read:accounts " ] )
2019-09-26 02:38:45 -04:00
2019-12-15 14:32:42 -05:00
setup %{ user : user } do
{ :ok , activity } = CommonAPI . post ( user , %{ " status " = > " test " } )
2019-09-26 02:38:45 -04:00
2019-12-15 14:32:42 -05:00
%{ activity : activity }
2019-09-26 02:38:45 -04:00
end
test " returns users who have favorited the status " , %{ conn : conn , activity : activity } do
other_user = insert ( :user )
{ :ok , _ , _ } = CommonAPI . favorite ( activity . id , other_user )
response =
conn
|> get ( " /api/v1/statuses/ #{ activity . id } /favourited_by " )
|> json_response ( :ok )
[ %{ " id " = > id } ] = response
assert id == other_user . id
end
test " returns empty array when status has not been favorited yet " , %{
conn : conn ,
activity : activity
} do
response =
conn
|> get ( " /api/v1/statuses/ #{ activity . id } /favourited_by " )
|> json_response ( :ok )
assert Enum . empty? ( response )
end
test " does not return users who have favorited the status but are blocked " , %{
conn : %{ assigns : %{ user : user } } = conn ,
activity : activity
} do
other_user = insert ( :user )
2019-11-19 15:22:10 -05:00
{ :ok , _user_relationship } = User . block ( user , other_user )
2019-09-26 02:38:45 -04:00
{ :ok , _ , _ } = CommonAPI . favorite ( activity . id , other_user )
response =
conn
|> get ( " /api/v1/statuses/ #{ activity . id } /favourited_by " )
|> json_response ( :ok )
assert Enum . empty? ( response )
end
2019-12-15 14:32:42 -05:00
test " does not fail on an unauthenticated request " , %{ activity : activity } do
2019-09-26 02:38:45 -04:00
other_user = insert ( :user )
{ :ok , _ , _ } = CommonAPI . favorite ( activity . id , other_user )
response =
2019-12-15 14:32:42 -05:00
build_conn ( )
2019-09-26 02:38:45 -04:00
|> get ( " /api/v1/statuses/ #{ activity . id } /favourited_by " )
|> json_response ( :ok )
[ %{ " id " = > id } ] = response
assert id == other_user . id
end
2019-12-15 14:32:42 -05:00
test " requires authentication for private posts " , %{ user : user } do
2019-09-26 02:38:45 -04:00
other_user = insert ( :user )
{ :ok , activity } =
CommonAPI . post ( user , %{
" status " = > " @ #{ other_user . nickname } wanna get some # cofe together? " ,
" visibility " = > " direct "
} )
{ :ok , _ , _ } = CommonAPI . favorite ( activity . id , other_user )
2019-12-15 14:32:42 -05:00
favourited_by_url = " /api/v1/statuses/ #{ activity . id } /favourited_by "
build_conn ( )
|> get ( favourited_by_url )
2019-09-26 02:38:45 -04:00
|> json_response ( 404 )
2019-12-15 14:32:42 -05:00
conn =
2019-09-26 02:38:45 -04:00
build_conn ( )
|> assign ( :user , other_user )
2019-12-15 14:32:42 -05:00
|> assign ( :token , insert ( :oauth_token , user : other_user , scopes : [ " read:accounts " ] ) )
conn
|> assign ( :token , nil )
|> get ( favourited_by_url )
|> json_response ( 404 )
response =
conn
|> get ( favourited_by_url )
2019-09-26 02:38:45 -04:00
|> json_response ( 200 )
[ %{ " id " = > id } ] = response
assert id == other_user . id
end
end
describe " GET /api/v1/statuses/:id/reblogged_by " do
2019-12-15 14:32:42 -05:00
setup do : oauth_access ( [ " read:accounts " ] )
2019-09-26 02:38:45 -04:00
2019-12-15 14:32:42 -05:00
setup %{ user : user } do
{ :ok , activity } = CommonAPI . post ( user , %{ " status " = > " test " } )
2019-09-26 02:38:45 -04:00
2019-12-15 14:32:42 -05:00
%{ activity : activity }
2019-09-26 02:38:45 -04:00
end
test " returns users who have reblogged the status " , %{ conn : conn , activity : activity } do
other_user = insert ( :user )
{ :ok , _ , _ } = CommonAPI . repeat ( activity . id , other_user )
response =
conn
|> get ( " /api/v1/statuses/ #{ activity . id } /reblogged_by " )
|> json_response ( :ok )
[ %{ " id " = > id } ] = response
assert id == other_user . id
end
test " returns empty array when status has not been reblogged yet " , %{
conn : conn ,
activity : activity
} do
response =
conn
|> get ( " /api/v1/statuses/ #{ activity . id } /reblogged_by " )
|> json_response ( :ok )
assert Enum . empty? ( response )
end
test " does not return users who have reblogged the status but are blocked " , %{
conn : %{ assigns : %{ user : user } } = conn ,
activity : activity
} do
other_user = insert ( :user )
2019-11-19 15:22:10 -05:00
{ :ok , _user_relationship } = User . block ( user , other_user )
2019-09-26 02:38:45 -04:00
{ :ok , _ , _ } = CommonAPI . repeat ( activity . id , other_user )
response =
conn
2019-10-01 15:40:35 -04:00
|> get ( " /api/v1/statuses/ #{ activity . id } /reblogged_by " )
|> json_response ( :ok )
assert Enum . empty? ( response )
end
test " does not return users who have reblogged the status privately " , %{
2019-12-15 14:32:42 -05:00
conn : conn ,
2019-10-01 15:40:35 -04:00
activity : activity
} do
other_user = insert ( :user )
{ :ok , _ , _ } = CommonAPI . repeat ( activity . id , other_user , %{ " visibility " = > " private " } )
response =
conn
2019-09-26 02:38:45 -04:00
|> get ( " /api/v1/statuses/ #{ activity . id } /reblogged_by " )
|> json_response ( :ok )
assert Enum . empty? ( response )
end
2019-12-15 14:32:42 -05:00
test " does not fail on an unauthenticated request " , %{ activity : activity } do
2019-09-26 02:38:45 -04:00
other_user = insert ( :user )
{ :ok , _ , _ } = CommonAPI . repeat ( activity . id , other_user )
response =
2019-12-15 14:32:42 -05:00
build_conn ( )
2019-09-26 02:38:45 -04:00
|> get ( " /api/v1/statuses/ #{ activity . id } /reblogged_by " )
|> json_response ( :ok )
[ %{ " id " = > id } ] = response
assert id == other_user . id
end
2019-12-15 14:32:42 -05:00
test " requires authentication for private posts " , %{ user : user } do
2019-09-26 02:38:45 -04:00
other_user = insert ( :user )
{ :ok , activity } =
CommonAPI . post ( user , %{
" status " = > " @ #{ other_user . nickname } wanna get some # cofe together? " ,
" visibility " = > " direct "
} )
2019-12-15 14:32:42 -05:00
build_conn ( )
2019-09-26 02:38:45 -04:00
|> get ( " /api/v1/statuses/ #{ activity . id } /reblogged_by " )
|> json_response ( 404 )
response =
build_conn ( )
|> assign ( :user , other_user )
2019-12-15 14:32:42 -05:00
|> assign ( :token , insert ( :oauth_token , user : other_user , scopes : [ " read:accounts " ] ) )
2019-09-26 02:38:45 -04:00
|> get ( " /api/v1/statuses/ #{ activity . id } /reblogged_by " )
|> json_response ( 200 )
assert [ ] == response
end
end
2019-09-26 03:28:35 -04:00
test " context " do
user = insert ( :user )
{ :ok , %{ id : id1 } } = CommonAPI . post ( user , %{ " status " = > " 1 " } )
{ :ok , %{ id : id2 } } = CommonAPI . post ( user , %{ " status " = > " 2 " , " in_reply_to_status_id " = > id1 } )
{ :ok , %{ id : id3 } } = CommonAPI . post ( user , %{ " status " = > " 3 " , " in_reply_to_status_id " = > id2 } )
{ :ok , %{ id : id4 } } = CommonAPI . post ( user , %{ " status " = > " 4 " , " in_reply_to_status_id " = > id3 } )
{ :ok , %{ id : id5 } } = CommonAPI . post ( user , %{ " status " = > " 5 " , " in_reply_to_status_id " = > id4 } )
response =
build_conn ( )
|> get ( " /api/v1/statuses/ #{ id3 } /context " )
|> json_response ( :ok )
assert %{
" ancestors " = > [ %{ " id " = > ^ id1 } , %{ " id " = > ^ id2 } ] ,
" descendants " = > [ %{ " id " = > ^ id4 } , %{ " id " = > ^ id5 } ]
} = response
end
2019-10-02 07:27:01 -04:00
2019-12-15 14:32:42 -05:00
test " returns the favorites of a user " do
%{ user : user , conn : conn } = oauth_access ( [ " read:favourites " ] )
2019-10-02 07:27:01 -04:00
other_user = insert ( :user )
{ :ok , _ } = CommonAPI . post ( other_user , %{ " status " = > " bla " } )
{ :ok , activity } = CommonAPI . post ( other_user , %{ " status " = > " traps are happy " } )
{ :ok , _ , _ } = CommonAPI . favorite ( activity . id , user )
2019-12-15 14:32:42 -05:00
first_conn = get ( conn , " /api/v1/favourites " )
2019-10-02 07:27:01 -04:00
assert [ status ] = json_response ( first_conn , 200 )
assert status [ " id " ] == to_string ( activity . id )
assert [ { " link " , _link_header } ] =
Enum . filter ( first_conn . resp_headers , fn element -> match? ( { " link " , _ } , element ) end )
# Honours query params
{ :ok , second_activity } =
CommonAPI . post ( other_user , %{
" status " = >
" Trees Are Never Sad Look At Them Every Once In Awhile They're Quite Beautiful. "
} )
{ :ok , _ , _ } = CommonAPI . favorite ( second_activity . id , user )
last_like = status [ " id " ]
2019-12-15 14:32:42 -05:00
second_conn = get ( conn , " /api/v1/favourites?since_id= #{ last_like } " )
2019-10-02 07:27:01 -04:00
assert [ second_status ] = json_response ( second_conn , 200 )
assert second_status [ " id " ] == to_string ( second_activity . id )
2019-12-15 14:32:42 -05:00
third_conn = get ( conn , " /api/v1/favourites?limit=0 " )
2019-10-02 07:27:01 -04:00
assert [ ] = json_response ( third_conn , 200 )
end
2020-02-18 08:09:50 -05:00
test " expires_at is nil for another user " do
%{ conn : conn , user : user } = oauth_access ( [ " read:statuses " ] )
{ :ok , activity } = CommonAPI . post ( user , %{ " status " = > " foobar " , " expires_in " = > 1_000_000 } )
expires_at =
activity . id
|> ActivityExpiration . get_by_activity_id ( )
|> Map . get ( :scheduled_at )
|> NaiveDateTime . to_iso8601 ( )
assert %{ " pleroma " = > %{ " expires_at " = > ^ expires_at } } =
conn |> get ( " /api/v1/statuses/ #{ activity . id } " ) |> json_response ( :ok )
%{ conn : conn } = oauth_access ( [ " read:statuses " ] )
assert %{ " pleroma " = > %{ " expires_at " = > nil } } =
conn |> get ( " /api/v1/statuses/ #{ activity . id } " ) |> json_response ( :ok )
end
2019-09-26 02:38:45 -04:00
end