2021-04-19 17:42:19 +00:00
|
|
|
/*
|
|
|
|
GoToSocial
|
2023-01-05 11:43:00 +00:00
|
|
|
Copyright (C) 2021-2023 GoToSocial Authors admin@gotosocial.org
|
2021-04-19 17:42:19 +00:00
|
|
|
|
|
|
|
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 12:25:55 +00:00
|
|
|
package fileserver_test
|
2021-04-19 17:42:19 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
2023-01-02 12:10:50 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/api/fileserver"
|
2021-05-08 12:25:55 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2021-04-19 17:42:19 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/media"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/testrig"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ServeFileTestSuite struct {
|
2022-12-21 10:17:43 +00:00
|
|
|
FileserverTestSuite
|
2021-04-19 17:42:19 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 10:17:43 +00:00
|
|
|
// GetFile is just a convenience function to save repetition in this test suite.
|
|
|
|
// It takes the required params to serve a file, calls the handler, and returns
|
|
|
|
// the http status code, the response headers, and the parsed body bytes.
|
|
|
|
func (suite *ServeFileTestSuite) GetFile(
|
|
|
|
accountID string,
|
|
|
|
mediaType media.Type,
|
|
|
|
mediaSize media.Size,
|
|
|
|
filename string,
|
|
|
|
) (code int, headers http.Header, body []byte) {
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
|
|
|
|
ctx, _ := testrig.CreateGinTestContext(recorder, nil)
|
|
|
|
ctx.Request = httptest.NewRequest(http.MethodGet, "http://localhost:8080/whatever", nil)
|
|
|
|
ctx.Request.Header.Set("accept", "*/*")
|
|
|
|
ctx.AddParam(fileserver.AccountIDKey, accountID)
|
|
|
|
ctx.AddParam(fileserver.MediaTypeKey, string(mediaType))
|
|
|
|
ctx.AddParam(fileserver.MediaSizeKey, string(mediaSize))
|
|
|
|
ctx.AddParam(fileserver.FileNameKey, filename)
|
2021-04-19 17:42:19 +00:00
|
|
|
|
2022-12-21 10:17:43 +00:00
|
|
|
suite.fileServer.ServeFile(ctx)
|
|
|
|
code = recorder.Code
|
|
|
|
headers = recorder.Result().Header
|
|
|
|
|
|
|
|
var err error
|
|
|
|
body, err = ioutil.ReadAll(recorder.Body)
|
|
|
|
if err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
2022-04-28 12:23:11 +00:00
|
|
|
|
2022-12-21 10:17:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-04-28 12:23:11 +00:00
|
|
|
|
2022-12-21 10:17:43 +00:00
|
|
|
// UncacheAttachment is a convenience function that uncaches the targetAttachment by
|
|
|
|
// removing its associated files from storage, and updating the database.
|
|
|
|
func (suite *ServeFileTestSuite) UncacheAttachment(targetAttachment *gtsmodel.MediaAttachment) {
|
|
|
|
ctx := context.Background()
|
2021-10-31 14:46:23 +00:00
|
|
|
|
2022-12-21 10:17:43 +00:00
|
|
|
cached := false
|
|
|
|
targetAttachment.Cached = &cached
|
2021-04-19 17:42:19 +00:00
|
|
|
|
2022-12-21 10:17:43 +00:00
|
|
|
if err := suite.db.UpdateByID(ctx, targetAttachment, targetAttachment.ID, "cached"); err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
|
|
|
if err := suite.storage.Delete(ctx, targetAttachment.File.Path); err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
|
|
|
if err := suite.storage.Delete(ctx, targetAttachment.Thumbnail.Path); err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
2021-04-19 17:42:19 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 10:17:43 +00:00
|
|
|
func (suite *ServeFileTestSuite) TestServeOriginalLocalFileOK() {
|
|
|
|
targetAttachment := >smodel.MediaAttachment{}
|
|
|
|
*targetAttachment = *suite.testAttachments["admin_account_status_1_attachment_1"]
|
|
|
|
fileInStorage, err := suite.storage.Get(context.Background(), targetAttachment.File.Path)
|
|
|
|
if err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
2021-04-19 17:42:19 +00:00
|
|
|
}
|
2022-12-21 10:17:43 +00:00
|
|
|
|
|
|
|
code, headers, body := suite.GetFile(
|
|
|
|
targetAttachment.AccountID,
|
|
|
|
media.TypeAttachment,
|
|
|
|
media.SizeOriginal,
|
2023-01-11 11:13:13 +00:00
|
|
|
targetAttachment.ID+".jpg",
|
2022-12-21 10:17:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
suite.Equal(http.StatusOK, code)
|
|
|
|
suite.Equal("image/jpeg", headers.Get("content-type"))
|
|
|
|
suite.Equal(fileInStorage, body)
|
2021-04-19 17:42:19 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 10:17:43 +00:00
|
|
|
func (suite *ServeFileTestSuite) TestServeSmallLocalFileOK() {
|
|
|
|
targetAttachment := >smodel.MediaAttachment{}
|
|
|
|
*targetAttachment = *suite.testAttachments["admin_account_status_1_attachment_1"]
|
|
|
|
fileInStorage, err := suite.storage.Get(context.Background(), targetAttachment.Thumbnail.Path)
|
|
|
|
if err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
code, headers, body := suite.GetFile(
|
|
|
|
targetAttachment.AccountID,
|
|
|
|
media.TypeAttachment,
|
|
|
|
media.SizeSmall,
|
2023-01-11 11:13:13 +00:00
|
|
|
targetAttachment.ID+".jpg",
|
2022-12-21 10:17:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
suite.Equal(http.StatusOK, code)
|
|
|
|
suite.Equal("image/jpeg", headers.Get("content-type"))
|
|
|
|
suite.Equal(fileInStorage, body)
|
2021-04-19 17:42:19 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 10:17:43 +00:00
|
|
|
func (suite *ServeFileTestSuite) TestServeOriginalRemoteFileOK() {
|
|
|
|
targetAttachment := >smodel.MediaAttachment{}
|
|
|
|
*targetAttachment = *suite.testAttachments["remote_account_1_status_1_attachment_1"]
|
|
|
|
fileInStorage, err := suite.storage.Get(context.Background(), targetAttachment.File.Path)
|
|
|
|
if err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
code, headers, body := suite.GetFile(
|
|
|
|
targetAttachment.AccountID,
|
|
|
|
media.TypeAttachment,
|
|
|
|
media.SizeOriginal,
|
2023-01-11 11:13:13 +00:00
|
|
|
targetAttachment.ID+".jpg",
|
2022-12-21 10:17:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
suite.Equal(http.StatusOK, code)
|
|
|
|
suite.Equal("image/jpeg", headers.Get("content-type"))
|
|
|
|
suite.Equal(fileInStorage, body)
|
2021-04-19 17:42:19 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 10:17:43 +00:00
|
|
|
func (suite *ServeFileTestSuite) TestServeSmallRemoteFileOK() {
|
|
|
|
targetAttachment := >smodel.MediaAttachment{}
|
|
|
|
*targetAttachment = *suite.testAttachments["remote_account_1_status_1_attachment_1"]
|
|
|
|
fileInStorage, err := suite.storage.Get(context.Background(), targetAttachment.Thumbnail.Path)
|
|
|
|
if err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
2021-04-19 17:42:19 +00:00
|
|
|
|
2022-12-21 10:17:43 +00:00
|
|
|
code, headers, body := suite.GetFile(
|
|
|
|
targetAttachment.AccountID,
|
|
|
|
media.TypeAttachment,
|
|
|
|
media.SizeSmall,
|
2023-01-11 11:13:13 +00:00
|
|
|
targetAttachment.ID+".jpg",
|
2022-12-21 10:17:43 +00:00
|
|
|
)
|
2021-04-19 17:42:19 +00:00
|
|
|
|
2022-12-21 10:17:43 +00:00
|
|
|
suite.Equal(http.StatusOK, code)
|
|
|
|
suite.Equal("image/jpeg", headers.Get("content-type"))
|
|
|
|
suite.Equal(fileInStorage, body)
|
|
|
|
}
|
2021-04-19 17:42:19 +00:00
|
|
|
|
2022-12-21 10:17:43 +00:00
|
|
|
func (suite *ServeFileTestSuite) TestServeOriginalRemoteFileRecache() {
|
|
|
|
targetAttachment := >smodel.MediaAttachment{}
|
|
|
|
*targetAttachment = *suite.testAttachments["remote_account_1_status_1_attachment_1"]
|
|
|
|
fileInStorage, err := suite.storage.Get(context.Background(), targetAttachment.File.Path)
|
|
|
|
if err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
2021-04-19 17:42:19 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 10:17:43 +00:00
|
|
|
// uncache the attachment so we'll have to refetch it from the 'remote' instance
|
|
|
|
suite.UncacheAttachment(targetAttachment)
|
2021-04-19 17:42:19 +00:00
|
|
|
|
2022-12-21 10:17:43 +00:00
|
|
|
code, headers, body := suite.GetFile(
|
|
|
|
targetAttachment.AccountID,
|
|
|
|
media.TypeAttachment,
|
|
|
|
media.SizeOriginal,
|
2023-01-11 11:13:13 +00:00
|
|
|
targetAttachment.ID+".jpg",
|
2022-12-21 10:17:43 +00:00
|
|
|
)
|
2021-04-19 17:42:19 +00:00
|
|
|
|
2022-12-21 10:17:43 +00:00
|
|
|
suite.Equal(http.StatusOK, code)
|
|
|
|
suite.Equal("image/jpeg", headers.Get("content-type"))
|
|
|
|
suite.Equal(fileInStorage, body)
|
2022-02-21 10:26:26 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 10:17:43 +00:00
|
|
|
func (suite *ServeFileTestSuite) TestServeSmallRemoteFileRecache() {
|
|
|
|
targetAttachment := >smodel.MediaAttachment{}
|
|
|
|
*targetAttachment = *suite.testAttachments["remote_account_1_status_1_attachment_1"]
|
|
|
|
fileInStorage, err := suite.storage.Get(context.Background(), targetAttachment.Thumbnail.Path)
|
|
|
|
if err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
2022-02-21 10:26:26 +00:00
|
|
|
|
2022-12-21 10:17:43 +00:00
|
|
|
// uncache the attachment so we'll have to refetch it from the 'remote' instance
|
|
|
|
suite.UncacheAttachment(targetAttachment)
|
|
|
|
|
|
|
|
code, headers, body := suite.GetFile(
|
|
|
|
targetAttachment.AccountID,
|
|
|
|
media.TypeAttachment,
|
|
|
|
media.SizeSmall,
|
2023-01-11 11:13:13 +00:00
|
|
|
targetAttachment.ID+".jpg",
|
2022-12-21 10:17:43 +00:00
|
|
|
)
|
2022-02-21 10:26:26 +00:00
|
|
|
|
2022-12-21 10:17:43 +00:00
|
|
|
suite.Equal(http.StatusOK, code)
|
|
|
|
suite.Equal("image/jpeg", headers.Get("content-type"))
|
|
|
|
suite.Equal(fileInStorage, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *ServeFileTestSuite) TestServeOriginalRemoteFileRecacheNotFound() {
|
|
|
|
targetAttachment := >smodel.MediaAttachment{}
|
|
|
|
*targetAttachment = *suite.testAttachments["remote_account_1_status_1_attachment_1"]
|
|
|
|
|
|
|
|
// uncache the attachment *and* set the remote URL to something that will return a 404
|
|
|
|
suite.UncacheAttachment(targetAttachment)
|
|
|
|
targetAttachment.RemoteURL = "http://nothing.at.this.url/weeeeeeeee"
|
|
|
|
if err := suite.db.UpdateByID(context.Background(), targetAttachment, targetAttachment.ID, "remote_url"); err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
2022-02-21 10:26:26 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 10:17:43 +00:00
|
|
|
code, _, _ := suite.GetFile(
|
|
|
|
targetAttachment.AccountID,
|
|
|
|
media.TypeAttachment,
|
|
|
|
media.SizeOriginal,
|
2023-01-11 11:13:13 +00:00
|
|
|
targetAttachment.ID+".jpg",
|
2022-12-21 10:17:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
suite.Equal(http.StatusNotFound, code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *ServeFileTestSuite) TestServeSmallRemoteFileRecacheNotFound() {
|
|
|
|
targetAttachment := >smodel.MediaAttachment{}
|
|
|
|
*targetAttachment = *suite.testAttachments["remote_account_1_status_1_attachment_1"]
|
|
|
|
|
|
|
|
// uncache the attachment *and* set the remote URL to something that will return a 404
|
|
|
|
suite.UncacheAttachment(targetAttachment)
|
|
|
|
targetAttachment.RemoteURL = "http://nothing.at.this.url/weeeeeeeee"
|
|
|
|
if err := suite.db.UpdateByID(context.Background(), targetAttachment, targetAttachment.ID, "remote_url"); err != nil {
|
|
|
|
suite.FailNow(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
code, _, _ := suite.GetFile(
|
|
|
|
targetAttachment.AccountID,
|
|
|
|
media.TypeAttachment,
|
|
|
|
media.SizeSmall,
|
2023-01-11 11:13:13 +00:00
|
|
|
targetAttachment.ID+".jpg",
|
2022-12-21 10:17:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
suite.Equal(http.StatusNotFound, code)
|
|
|
|
}
|
2022-02-21 10:26:26 +00:00
|
|
|
|
2022-12-21 10:17:43 +00:00
|
|
|
// Callers trying to get some random-ass file that doesn't exist should just get a 404
|
|
|
|
func (suite *ServeFileTestSuite) TestServeFileNotFound() {
|
|
|
|
code, _, _ := suite.GetFile(
|
|
|
|
"01GMMY4G9B0QEG0PQK5Q5JGJWZ",
|
|
|
|
media.TypeAttachment,
|
|
|
|
media.SizeOriginal,
|
2023-01-11 11:13:13 +00:00
|
|
|
"01GMMY68Y7E5DJ3CA3Y9SS8524.jpg",
|
2022-12-21 10:17:43 +00:00
|
|
|
)
|
2022-02-21 10:26:26 +00:00
|
|
|
|
2022-12-21 10:17:43 +00:00
|
|
|
suite.Equal(http.StatusNotFound, code)
|
2021-04-19 17:42:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestServeFileTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(ServeFileTestSuite))
|
|
|
|
}
|