2021-04-19 19:42:19 +02:00
/ *
GoToSocial
Copyright ( C ) 2021 GoToSocial Authors admin @ gotosocial . org
This program is free software : you can redistribute it and / or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU Affero General Public License for more details .
You should have received a copy of the GNU Affero General Public License
along with this program . If not , see < http : //www.gnu.org/licenses/>.
* /
2021-05-08 14:25:55 +02:00
package status_test
2021-04-19 19:42:19 +02:00
import (
2021-08-25 15:34:33 +02:00
"context"
2021-04-19 19:42:19 +02:00
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
2021-05-08 14:25:55 +02:00
"github.com/superseriousbusiness/gotosocial/internal/api/client/status"
"github.com/superseriousbusiness/gotosocial/internal/api/model"
2021-05-21 15:48:26 +02:00
"github.com/superseriousbusiness/gotosocial/internal/db"
2021-05-08 14:25:55 +02:00
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
2021-04-19 19:42:19 +02:00
"github.com/superseriousbusiness/gotosocial/internal/oauth"
"github.com/superseriousbusiness/gotosocial/testrig"
)
type StatusCreateTestSuite struct {
2021-05-08 14:25:55 +02:00
StatusStandardTestSuite
2021-04-19 19:42:19 +02:00
}
2021-07-29 13:18:22 +02:00
var statusWithLinksAndTags = ` # test alright , should be able to post # links with fragments in them now , let ' s see ... ... . .
https : //docs.gotosocial.org/en/latest/user_guide/posts/#links
# gotosocial
( tobi remember to pull the docker image challenge ) `
2021-04-19 19:42:19 +02:00
// Post a new status with some custom visibility settings
func ( suite * StatusCreateTestSuite ) TestPostNewStatus ( ) {
t := suite . testTokens [ "local_account_1" ]
2021-08-25 15:34:33 +02:00
oauthToken := oauth . DBTokenToToken ( t )
2021-04-19 19:42:19 +02:00
// setup
recorder := httptest . NewRecorder ( )
ctx , _ := gin . CreateTestContext ( recorder )
ctx . Set ( oauth . SessionAuthorizedApplication , suite . testApplications [ "application_1" ] )
ctx . Set ( oauth . SessionAuthorizedToken , oauthToken )
ctx . Set ( oauth . SessionAuthorizedUser , suite . testUsers [ "local_account_1" ] )
ctx . Set ( oauth . SessionAuthorizedAccount , suite . testAccounts [ "local_account_1" ] )
ctx . Request = httptest . NewRequest ( http . MethodPost , fmt . Sprintf ( "http://localhost:8080/%s" , status . BasePath ) , nil ) // the endpoint we're hitting
2021-12-11 17:50:00 +01:00
ctx . Request . Header . Set ( "accept" , "application/json" )
2021-04-19 19:42:19 +02:00
ctx . Request . Form = url . Values {
2021-08-12 21:03:24 +02:00
"status" : { "this is a brand new status! #helloworld" } ,
"spoiler_text" : { "hello hello" } ,
"sensitive" : { "true" } ,
"visibility" : { string ( model . VisibilityMutualsOnly ) } ,
"likeable" : { "false" } ,
"replyable" : { "false" } ,
"federated" : { "false" } ,
2021-04-19 19:42:19 +02:00
}
suite . statusModule . StatusCreatePOSTHandler ( ctx )
// check response
// 1. we should have OK from our call to the function
suite . EqualValues ( http . StatusOK , recorder . Code )
result := recorder . Result ( )
defer result . Body . Close ( )
b , err := ioutil . ReadAll ( result . Body )
assert . NoError ( suite . T ( ) , err )
2021-05-08 14:25:55 +02:00
statusReply := & model . Status { }
2021-04-19 19:42:19 +02:00
err = json . Unmarshal ( b , statusReply )
assert . NoError ( suite . T ( ) , err )
assert . Equal ( suite . T ( ) , "hello hello" , statusReply . SpoilerText )
2021-07-29 13:18:22 +02:00
assert . Equal ( suite . T ( ) , "<p>this is a brand new status! <a href=\"http://localhost:8080/tags/helloworld\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>helloworld</span></a></p>" , statusReply . Content )
2021-04-19 19:42:19 +02:00
assert . True ( suite . T ( ) , statusReply . Sensitive )
2021-10-04 15:24:19 +02:00
assert . Equal ( suite . T ( ) , model . VisibilityPrivate , statusReply . Visibility ) // even though we set this status to mutuals only, it should serialize to private, because the mastodon api has no idea about mutuals_only
2021-04-19 19:42:19 +02:00
assert . Len ( suite . T ( ) , statusReply . Tags , 1 )
2021-05-08 14:25:55 +02:00
assert . Equal ( suite . T ( ) , model . Tag {
2021-04-19 19:42:19 +02:00
Name : "helloworld" ,
URL : "http://localhost:8080/tags/helloworld" ,
} , statusReply . Tags [ 0 ] )
gtsTag := & gtsmodel . Tag { }
2021-08-25 15:34:33 +02:00
err = suite . db . GetWhere ( context . Background ( ) , [ ] db . Where { { Key : "name" , Value : "helloworld" } } , gtsTag )
2021-04-19 19:42:19 +02:00
assert . NoError ( suite . T ( ) , err )
assert . Equal ( suite . T ( ) , statusReply . Account . ID , gtsTag . FirstSeenFromAccountID )
}
2021-07-29 13:18:22 +02:00
func ( suite * StatusCreateTestSuite ) TestPostAnotherNewStatus ( ) {
t := suite . testTokens [ "local_account_1" ]
2021-08-25 15:34:33 +02:00
oauthToken := oauth . DBTokenToToken ( t )
2021-07-29 13:18:22 +02:00
// setup
recorder := httptest . NewRecorder ( )
ctx , _ := gin . CreateTestContext ( recorder )
ctx . Set ( oauth . SessionAuthorizedApplication , suite . testApplications [ "application_1" ] )
ctx . Set ( oauth . SessionAuthorizedToken , oauthToken )
ctx . Set ( oauth . SessionAuthorizedUser , suite . testUsers [ "local_account_1" ] )
ctx . Set ( oauth . SessionAuthorizedAccount , suite . testAccounts [ "local_account_1" ] )
ctx . Request = httptest . NewRequest ( http . MethodPost , fmt . Sprintf ( "http://localhost:8080/%s" , status . BasePath ) , nil ) // the endpoint we're hitting
2021-12-11 17:50:00 +01:00
ctx . Request . Header . Set ( "accept" , "application/json" )
2021-07-29 13:18:22 +02:00
ctx . Request . Form = url . Values {
"status" : { statusWithLinksAndTags } ,
}
suite . statusModule . StatusCreatePOSTHandler ( ctx )
// check response
// 1. we should have OK from our call to the function
suite . EqualValues ( http . StatusOK , recorder . Code )
result := recorder . Result ( )
defer result . Body . Close ( )
b , err := ioutil . ReadAll ( result . Body )
assert . NoError ( suite . T ( ) , err )
statusReply := & model . Status { }
err = json . Unmarshal ( b , statusReply )
assert . NoError ( suite . T ( ) , err )
2021-08-16 19:17:56 +02:00
assert . Equal ( suite . T ( ) , "<p><a href=\"http://localhost:8080/tags/test\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>test</span></a> alright, should be able to post <a href=\"http://localhost:8080/tags/links\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>links</span></a> with fragments in them now, let's see........<br><br><a href=\"https://docs.gotosocial.org/en/latest/user_guide/posts/#links\" rel=\"noopener nofollow noreferrer\" target=\"_blank\">docs.gotosocial.org/en/latest/user_guide/posts/#links</a><br><br><a href=\"http://localhost:8080/tags/gotosocial\" class=\"mention hashtag\" rel=\"tag nofollow noreferrer noopener\" target=\"_blank\">#<span>gotosocial</span></a><br><br>(tobi remember to pull the docker image challenge)</p>" , statusReply . Content )
2021-07-29 13:18:22 +02:00
}
2021-04-19 19:42:19 +02:00
func ( suite * StatusCreateTestSuite ) TestPostNewStatusWithEmoji ( ) {
t := suite . testTokens [ "local_account_1" ]
2021-08-25 15:34:33 +02:00
oauthToken := oauth . DBTokenToToken ( t )
2021-04-19 19:42:19 +02:00
// setup
recorder := httptest . NewRecorder ( )
ctx , _ := gin . CreateTestContext ( recorder )
ctx . Set ( oauth . SessionAuthorizedApplication , suite . testApplications [ "application_1" ] )
ctx . Set ( oauth . SessionAuthorizedToken , oauthToken )
ctx . Set ( oauth . SessionAuthorizedUser , suite . testUsers [ "local_account_1" ] )
ctx . Set ( oauth . SessionAuthorizedAccount , suite . testAccounts [ "local_account_1" ] )
ctx . Request = httptest . NewRequest ( http . MethodPost , fmt . Sprintf ( "http://localhost:8080/%s" , status . BasePath ) , nil ) // the endpoint we're hitting
2021-12-11 17:50:00 +01:00
ctx . Request . Header . Set ( "accept" , "application/json" )
2021-04-19 19:42:19 +02:00
ctx . Request . Form = url . Values {
"status" : { "here is a rainbow emoji a few times! :rainbow: :rainbow: :rainbow: \n here's an emoji that isn't in the db: :test_emoji: " } ,
}
suite . statusModule . StatusCreatePOSTHandler ( ctx )
suite . EqualValues ( http . StatusOK , recorder . Code )
result := recorder . Result ( )
defer result . Body . Close ( )
b , err := ioutil . ReadAll ( result . Body )
assert . NoError ( suite . T ( ) , err )
2021-05-08 14:25:55 +02:00
statusReply := & model . Status { }
2021-04-19 19:42:19 +02:00
err = json . Unmarshal ( b , statusReply )
assert . NoError ( suite . T ( ) , err )
assert . Equal ( suite . T ( ) , "" , statusReply . SpoilerText )
2021-08-16 19:17:56 +02:00
assert . Equal ( suite . T ( ) , "<p>here is a rainbow emoji a few times! :rainbow: :rainbow: :rainbow:<br>here's an emoji that isn't in the db: :test_emoji:</p>" , statusReply . Content )
2021-04-19 19:42:19 +02:00
assert . Len ( suite . T ( ) , statusReply . Emojis , 1 )
2021-10-04 15:24:19 +02:00
apiEmoji := statusReply . Emojis [ 0 ]
2021-04-19 19:42:19 +02:00
gtsEmoji := testrig . NewTestEmojis ( ) [ "rainbow" ]
2021-10-04 15:24:19 +02:00
assert . Equal ( suite . T ( ) , gtsEmoji . Shortcode , apiEmoji . Shortcode )
assert . Equal ( suite . T ( ) , gtsEmoji . ImageURL , apiEmoji . URL )
assert . Equal ( suite . T ( ) , gtsEmoji . ImageStaticURL , apiEmoji . StaticURL )
2021-04-19 19:42:19 +02:00
}
// Try to reply to a status that doesn't exist
func ( suite * StatusCreateTestSuite ) TestReplyToNonexistentStatus ( ) {
t := suite . testTokens [ "local_account_1" ]
2021-08-25 15:34:33 +02:00
oauthToken := oauth . DBTokenToToken ( t )
2021-04-19 19:42:19 +02:00
// setup
recorder := httptest . NewRecorder ( )
ctx , _ := gin . CreateTestContext ( recorder )
ctx . Set ( oauth . SessionAuthorizedApplication , suite . testApplications [ "application_1" ] )
ctx . Set ( oauth . SessionAuthorizedToken , oauthToken )
ctx . Set ( oauth . SessionAuthorizedUser , suite . testUsers [ "local_account_1" ] )
ctx . Set ( oauth . SessionAuthorizedAccount , suite . testAccounts [ "local_account_1" ] )
ctx . Request = httptest . NewRequest ( http . MethodPost , fmt . Sprintf ( "http://localhost:8080/%s" , status . BasePath ) , nil ) // the endpoint we're hitting
2021-12-11 17:50:00 +01:00
ctx . Request . Header . Set ( "accept" , "application/json" )
2021-04-19 19:42:19 +02:00
ctx . Request . Form = url . Values {
"status" : { "this is a reply to a status that doesn't exist" } ,
"spoiler_text" : { "don't open cuz it won't work" } ,
"in_reply_to_id" : { "3759e7ef-8ee1-4c0c-86f6-8b70b9ad3d50" } ,
}
suite . statusModule . StatusCreatePOSTHandler ( ctx )
// check response
suite . EqualValues ( http . StatusBadRequest , recorder . Code )
result := recorder . Result ( )
defer result . Body . Close ( )
b , err := ioutil . ReadAll ( result . Body )
assert . NoError ( suite . T ( ) , err )
2021-05-08 14:25:55 +02:00
assert . Equal ( suite . T ( ) , ` { "error":"bad request"} ` , string ( b ) )
2021-04-19 19:42:19 +02:00
}
// Post a reply to the status of a local user that allows replies.
func ( suite * StatusCreateTestSuite ) TestReplyToLocalStatus ( ) {
t := suite . testTokens [ "local_account_1" ]
2021-08-25 15:34:33 +02:00
oauthToken := oauth . DBTokenToToken ( t )
2021-04-19 19:42:19 +02:00
// setup
recorder := httptest . NewRecorder ( )
ctx , _ := gin . CreateTestContext ( recorder )
ctx . Set ( oauth . SessionAuthorizedApplication , suite . testApplications [ "application_1" ] )
ctx . Set ( oauth . SessionAuthorizedToken , oauthToken )
ctx . Set ( oauth . SessionAuthorizedUser , suite . testUsers [ "local_account_1" ] )
ctx . Set ( oauth . SessionAuthorizedAccount , suite . testAccounts [ "local_account_1" ] )
ctx . Request = httptest . NewRequest ( http . MethodPost , fmt . Sprintf ( "http://localhost:8080/%s" , status . BasePath ) , nil ) // the endpoint we're hitting
2021-12-11 17:50:00 +01:00
ctx . Request . Header . Set ( "accept" , "application/json" )
2021-04-19 19:42:19 +02:00
ctx . Request . Form = url . Values {
"status" : { fmt . Sprintf ( "hello @%s this reply should work!" , testrig . NewTestAccounts ( ) [ "local_account_2" ] . Username ) } ,
"in_reply_to_id" : { testrig . NewTestStatuses ( ) [ "local_account_2_status_1" ] . ID } ,
}
suite . statusModule . StatusCreatePOSTHandler ( ctx )
// check response
suite . EqualValues ( http . StatusOK , recorder . Code )
result := recorder . Result ( )
defer result . Body . Close ( )
b , err := ioutil . ReadAll ( result . Body )
assert . NoError ( suite . T ( ) , err )
2021-05-08 14:25:55 +02:00
statusReply := & model . Status { }
2021-04-19 19:42:19 +02:00
err = json . Unmarshal ( b , statusReply )
assert . NoError ( suite . T ( ) , err )
assert . Equal ( suite . T ( ) , "" , statusReply . SpoilerText )
2021-07-29 13:18:22 +02:00
assert . Equal ( suite . T ( ) , fmt . Sprintf ( "<p>hello <span class=\"h-card\"><a href=\"http://localhost:8080/@%s\" class=\"u-url mention\" rel=\"nofollow noreferrer noopener\" target=\"_blank\">@<span>%s</span></a></span> this reply should work!</p>" , testrig . NewTestAccounts ( ) [ "local_account_2" ] . Username , testrig . NewTestAccounts ( ) [ "local_account_2" ] . Username ) , statusReply . Content )
2021-04-19 19:42:19 +02:00
assert . False ( suite . T ( ) , statusReply . Sensitive )
2021-05-08 14:25:55 +02:00
assert . Equal ( suite . T ( ) , model . VisibilityPublic , statusReply . Visibility )
2021-04-19 19:42:19 +02:00
assert . Equal ( suite . T ( ) , testrig . NewTestStatuses ( ) [ "local_account_2_status_1" ] . ID , statusReply . InReplyToID )
assert . Equal ( suite . T ( ) , testrig . NewTestAccounts ( ) [ "local_account_2" ] . ID , statusReply . InReplyToAccountID )
assert . Len ( suite . T ( ) , statusReply . Mentions , 1 )
}
// Take a media file which is currently not associated with a status, and attach it to a new status.
func ( suite * StatusCreateTestSuite ) TestAttachNewMediaSuccess ( ) {
t := suite . testTokens [ "local_account_1" ]
2021-08-25 15:34:33 +02:00
oauthToken := oauth . DBTokenToToken ( t )
2021-04-19 19:42:19 +02:00
2021-07-29 13:18:22 +02:00
attachment := suite . testAttachments [ "local_account_1_unattached_1" ]
2021-04-19 19:42:19 +02:00
// setup
recorder := httptest . NewRecorder ( )
ctx , _ := gin . CreateTestContext ( recorder )
ctx . Set ( oauth . SessionAuthorizedApplication , suite . testApplications [ "application_1" ] )
ctx . Set ( oauth . SessionAuthorizedToken , oauthToken )
ctx . Set ( oauth . SessionAuthorizedUser , suite . testUsers [ "local_account_1" ] )
ctx . Set ( oauth . SessionAuthorizedAccount , suite . testAccounts [ "local_account_1" ] )
ctx . Request = httptest . NewRequest ( http . MethodPost , fmt . Sprintf ( "http://localhost:8080/%s" , status . BasePath ) , nil ) // the endpoint we're hitting
2021-12-11 17:50:00 +01:00
ctx . Request . Header . Set ( "accept" , "application/json" )
2021-04-19 19:42:19 +02:00
ctx . Request . Form = url . Values {
"status" : { "here's an image attachment" } ,
2021-07-29 13:18:22 +02:00
"media_ids" : { attachment . ID } ,
2021-04-19 19:42:19 +02:00
}
suite . statusModule . StatusCreatePOSTHandler ( ctx )
// check response
suite . EqualValues ( http . StatusOK , recorder . Code )
result := recorder . Result ( )
defer result . Body . Close ( )
b , err := ioutil . ReadAll ( result . Body )
assert . NoError ( suite . T ( ) , err )
2021-07-29 13:18:22 +02:00
statusResponse := & model . Status { }
err = json . Unmarshal ( b , statusResponse )
2021-04-19 19:42:19 +02:00
assert . NoError ( suite . T ( ) , err )
2021-07-29 13:18:22 +02:00
assert . Equal ( suite . T ( ) , "" , statusResponse . SpoilerText )
2021-08-16 19:17:56 +02:00
assert . Equal ( suite . T ( ) , "<p>here's an image attachment</p>" , statusResponse . Content )
2021-07-29 13:18:22 +02:00
assert . False ( suite . T ( ) , statusResponse . Sensitive )
assert . Equal ( suite . T ( ) , model . VisibilityPublic , statusResponse . Visibility )
2021-04-19 19:42:19 +02:00
// there should be one media attachment
2021-07-29 13:18:22 +02:00
assert . Len ( suite . T ( ) , statusResponse . MediaAttachments , 1 )
2021-04-19 19:42:19 +02:00
// get the updated media attachment from the database
2021-08-25 15:34:33 +02:00
gtsAttachment , err := suite . db . GetAttachmentByID ( context . Background ( ) , statusResponse . MediaAttachments [ 0 ] . ID )
2021-04-19 19:42:19 +02:00
assert . NoError ( suite . T ( ) , err )
2021-10-04 15:24:19 +02:00
// convert it to a api attachment
gtsAttachmentAsapi , err := suite . tc . AttachmentToAPIAttachment ( context . Background ( ) , gtsAttachment )
2021-04-19 19:42:19 +02:00
assert . NoError ( suite . T ( ) , err )
// compare it with what we have now
2021-10-04 15:24:19 +02:00
assert . EqualValues ( suite . T ( ) , statusResponse . MediaAttachments [ 0 ] , gtsAttachmentAsapi )
2021-04-19 19:42:19 +02:00
// the status id of the attachment should now be set to the id of the status we just created
2021-07-29 13:18:22 +02:00
assert . Equal ( suite . T ( ) , statusResponse . ID , gtsAttachment . StatusID )
2021-04-19 19:42:19 +02:00
}
func TestStatusCreateTestSuite ( t * testing . T ) {
suite . Run ( t , new ( StatusCreateTestSuite ) )
}