gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

mediacleanup_test.go (3775B)


      1 // GoToSocial
      2 // Copyright (C) GoToSocial Authors admin@gotosocial.org
      3 // SPDX-License-Identifier: AGPL-3.0-or-later
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 package admin_test
     19 
     20 import (
     21 	"context"
     22 	"net/http"
     23 	"net/http/httptest"
     24 	"testing"
     25 	"time"
     26 
     27 	"github.com/stretchr/testify/suite"
     28 	"github.com/superseriousbusiness/gotosocial/internal/api/client/admin"
     29 	"github.com/superseriousbusiness/gotosocial/testrig"
     30 )
     31 
     32 type MediaCleanupTestSuite struct {
     33 	AdminStandardTestSuite
     34 }
     35 
     36 func (suite *MediaCleanupTestSuite) TestMediaCleanup() {
     37 	testAttachment := suite.testAttachments["remote_account_1_status_1_attachment_1"]
     38 	suite.True(*testAttachment.Cached)
     39 
     40 	// set up the request
     41 	recorder := httptest.NewRecorder()
     42 	ctx := suite.newContext(recorder, http.MethodPost, []byte("{\"remote_cache_days\": 1}"), admin.MediaCleanupPath, "application/json")
     43 
     44 	// call the handler
     45 	suite.adminModule.MediaCleanupPOSTHandler(ctx)
     46 
     47 	// we should have OK because our request was valid
     48 	suite.Equal(http.StatusOK, recorder.Code)
     49 
     50 	// the attachment should be updated in the database
     51 	if !testrig.WaitFor(func() bool {
     52 		if prunedAttachment, _ := suite.db.GetAttachmentByID(context.Background(), testAttachment.ID); prunedAttachment != nil {
     53 			return !*prunedAttachment.Cached
     54 		}
     55 		return false
     56 	}) {
     57 		suite.FailNow("timed out waiting for attachment to be pruned")
     58 	}
     59 }
     60 
     61 func (suite *MediaCleanupTestSuite) TestMediaCleanupNoArg() {
     62 	testAttachment := suite.testAttachments["remote_account_1_status_1_attachment_1"]
     63 	suite.True(*testAttachment.Cached)
     64 	println("TIME: ", testAttachment.CreatedAt.String())
     65 
     66 	// set up the request
     67 	recorder := httptest.NewRecorder()
     68 	ctx := suite.newContext(recorder, http.MethodPost, []byte("{}"), admin.MediaCleanupPath, "application/json")
     69 
     70 	// call the handler
     71 	suite.adminModule.MediaCleanupPOSTHandler(ctx)
     72 
     73 	// we should have OK because our request was valid
     74 	suite.Equal(http.StatusOK, recorder.Code)
     75 
     76 	if !testrig.WaitFor(func() bool {
     77 		if prunedAttachment, _ := suite.db.GetAttachmentByID(context.Background(), testAttachment.ID); prunedAttachment != nil {
     78 			return !*prunedAttachment.Cached
     79 		}
     80 		return false
     81 	}) {
     82 		suite.FailNow("timed out waiting for attachment to be pruned")
     83 	}
     84 }
     85 
     86 func (suite *MediaCleanupTestSuite) TestMediaCleanupNotOldEnough() {
     87 	testAttachment := suite.testAttachments["remote_account_1_status_1_attachment_1"]
     88 	suite.True(*testAttachment.Cached)
     89 
     90 	// set up the request
     91 	recorder := httptest.NewRecorder()
     92 	ctx := suite.newContext(recorder, http.MethodPost, []byte("{\"remote_cache_days\": 10000}"), admin.MediaCleanupPath, "application/json")
     93 
     94 	// call the handler
     95 	suite.adminModule.MediaCleanupPOSTHandler(ctx)
     96 
     97 	// we should have OK because our request was valid
     98 	suite.Equal(http.StatusOK, recorder.Code)
     99 
    100 	// Wait for async task to finish
    101 	time.Sleep(1 * time.Second)
    102 
    103 	// Get media we pruned
    104 	prunedAttachment, err := suite.db.GetAttachmentByID(context.Background(), testAttachment.ID)
    105 	suite.NoError(err)
    106 
    107 	// the media should still be cached
    108 	suite.True(*prunedAttachment.Cached)
    109 }
    110 
    111 func TestMediaCleanupTestSuite(t *testing.T) {
    112 	suite.Run(t, &MediaCleanupTestSuite{})
    113 }