gtsocial-umbx

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

media_test.go (6313B)


      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 dereferencing_test
     19 
     20 import (
     21 	"context"
     22 	"testing"
     23 	"time"
     24 
     25 	"github.com/stretchr/testify/suite"
     26 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
     27 	"github.com/superseriousbusiness/gotosocial/internal/media"
     28 )
     29 
     30 type AttachmentTestSuite struct {
     31 	DereferencerStandardTestSuite
     32 }
     33 
     34 func (suite *AttachmentTestSuite) TestDereferenceAttachmentBlocking() {
     35 	ctx := context.Background()
     36 
     37 	fetchingAccount := suite.testAccounts["local_account_1"]
     38 
     39 	attachmentOwner := "01FENS9F666SEQ6TYQWEEY78GM"
     40 	attachmentStatus := "01FENS9NTTVNEX1YZV7GB63MT8"
     41 	attachmentContentType := "image/jpeg"
     42 	attachmentURL := "https://s3-us-west-2.amazonaws.com/plushcity/media_attachments/files/106/867/380/219/163/828/original/88e8758c5f011439.jpg"
     43 	attachmentDescription := "It's a cute plushie."
     44 	attachmentBlurhash := "LtQ9yKi__4%g%MRjWCt7%hozM_az"
     45 
     46 	media, err := suite.dereferencer.GetRemoteMedia(ctx, fetchingAccount.Username, attachmentOwner, attachmentURL, &media.AdditionalMediaInfo{
     47 		StatusID:    &attachmentStatus,
     48 		RemoteURL:   &attachmentURL,
     49 		Description: &attachmentDescription,
     50 		Blurhash:    &attachmentBlurhash,
     51 	})
     52 	suite.NoError(err)
     53 
     54 	// make a blocking call to load the attachment from the in-process media
     55 	attachment, err := media.LoadAttachment(ctx)
     56 	suite.NoError(err)
     57 
     58 	suite.NotNil(attachment)
     59 
     60 	suite.Equal(attachmentOwner, attachment.AccountID)
     61 	suite.Equal(attachmentStatus, attachment.StatusID)
     62 	suite.Equal(attachmentURL, attachment.RemoteURL)
     63 	suite.NotEmpty(attachment.URL)
     64 	suite.NotEmpty(attachment.Blurhash)
     65 	suite.NotEmpty(attachment.ID)
     66 	suite.NotEmpty(attachment.CreatedAt)
     67 	suite.NotEmpty(attachment.UpdatedAt)
     68 	suite.EqualValues(1.3365462, attachment.FileMeta.Original.Aspect)
     69 	suite.Equal(2071680, attachment.FileMeta.Original.Size)
     70 	suite.Equal(1245, attachment.FileMeta.Original.Height)
     71 	suite.Equal(1664, attachment.FileMeta.Original.Width)
     72 	suite.Equal(attachmentBlurhash, attachment.Blurhash)
     73 	suite.Equal(gtsmodel.ProcessingStatusProcessed, attachment.Processing)
     74 	suite.NotEmpty(attachment.File.Path)
     75 	suite.Equal(attachmentContentType, attachment.File.ContentType)
     76 	suite.Equal(attachmentDescription, attachment.Description)
     77 
     78 	suite.NotEmpty(attachment.Thumbnail.Path)
     79 	suite.NotEmpty(attachment.Type)
     80 
     81 	// attachment should also now be in the database
     82 	dbAttachment, err := suite.db.GetAttachmentByID(context.Background(), attachment.ID)
     83 	suite.NoError(err)
     84 	suite.NotNil(dbAttachment)
     85 
     86 	suite.Equal(attachmentOwner, dbAttachment.AccountID)
     87 	suite.Equal(attachmentStatus, dbAttachment.StatusID)
     88 	suite.Equal(attachmentURL, dbAttachment.RemoteURL)
     89 	suite.NotEmpty(dbAttachment.URL)
     90 	suite.NotEmpty(dbAttachment.Blurhash)
     91 	suite.NotEmpty(dbAttachment.ID)
     92 	suite.NotEmpty(dbAttachment.CreatedAt)
     93 	suite.NotEmpty(dbAttachment.UpdatedAt)
     94 	suite.EqualValues(1.3365462, dbAttachment.FileMeta.Original.Aspect)
     95 	suite.Equal(2071680, dbAttachment.FileMeta.Original.Size)
     96 	suite.Equal(1245, dbAttachment.FileMeta.Original.Height)
     97 	suite.Equal(1664, dbAttachment.FileMeta.Original.Width)
     98 	suite.Equal(attachmentBlurhash, dbAttachment.Blurhash)
     99 	suite.Equal(gtsmodel.ProcessingStatusProcessed, dbAttachment.Processing)
    100 	suite.NotEmpty(dbAttachment.File.Path)
    101 	suite.Equal(attachmentContentType, dbAttachment.File.ContentType)
    102 	suite.Equal(attachmentDescription, dbAttachment.Description)
    103 
    104 	suite.NotEmpty(dbAttachment.Thumbnail.Path)
    105 	suite.NotEmpty(dbAttachment.Type)
    106 }
    107 
    108 func (suite *AttachmentTestSuite) TestDereferenceAttachmentAsync() {
    109 	ctx := context.Background()
    110 
    111 	fetchingAccount := suite.testAccounts["local_account_1"]
    112 
    113 	attachmentOwner := "01FENS9F666SEQ6TYQWEEY78GM"
    114 	attachmentStatus := "01FENS9NTTVNEX1YZV7GB63MT8"
    115 	attachmentContentType := "image/jpeg"
    116 	attachmentURL := "https://s3-us-west-2.amazonaws.com/plushcity/media_attachments/files/106/867/380/219/163/828/original/88e8758c5f011439.jpg"
    117 	attachmentDescription := "It's a cute plushie."
    118 	attachmentBlurhash := "LtQ9yKi__4%g%MRjWCt7%hozM_az"
    119 
    120 	processingMedia, err := suite.dereferencer.GetRemoteMedia(ctx, fetchingAccount.Username, attachmentOwner, attachmentURL, &media.AdditionalMediaInfo{
    121 		StatusID:    &attachmentStatus,
    122 		RemoteURL:   &attachmentURL,
    123 		Description: &attachmentDescription,
    124 		Blurhash:    &attachmentBlurhash,
    125 	})
    126 	suite.NoError(err)
    127 	attachmentID := processingMedia.AttachmentID()
    128 
    129 	time.Sleep(time.Second * 3)
    130 
    131 	// now get the attachment from the database
    132 	attachment, err := suite.db.GetAttachmentByID(ctx, attachmentID)
    133 	suite.NoError(err)
    134 
    135 	suite.NotNil(attachment)
    136 
    137 	suite.Equal(attachmentOwner, attachment.AccountID)
    138 	suite.Equal(attachmentStatus, attachment.StatusID)
    139 	suite.Equal(attachmentURL, attachment.RemoteURL)
    140 	suite.NotEmpty(attachment.URL)
    141 	suite.NotEmpty(attachment.Blurhash)
    142 	suite.NotEmpty(attachment.ID)
    143 	suite.NotEmpty(attachment.CreatedAt)
    144 	suite.NotEmpty(attachment.UpdatedAt)
    145 	suite.EqualValues(1.3365462, attachment.FileMeta.Original.Aspect)
    146 	suite.Equal(2071680, attachment.FileMeta.Original.Size)
    147 	suite.Equal(1245, attachment.FileMeta.Original.Height)
    148 	suite.Equal(1664, attachment.FileMeta.Original.Width)
    149 	suite.Equal(attachmentBlurhash, attachment.Blurhash)
    150 	suite.Equal(gtsmodel.ProcessingStatusProcessed, attachment.Processing)
    151 	suite.NotEmpty(attachment.File.Path)
    152 	suite.Equal(attachmentContentType, attachment.File.ContentType)
    153 	suite.Equal(attachmentDescription, attachment.Description)
    154 
    155 	suite.NotEmpty(attachment.Thumbnail.Path)
    156 	suite.NotEmpty(attachment.Type)
    157 }
    158 
    159 func TestAttachmentTestSuite(t *testing.T) {
    160 	suite.Run(t, new(AttachmentTestSuite))
    161 }