aides-repo-api/tests/integration/taskservice_upload_test.go

129 lines
3.1 KiB
Go
Raw Normal View History

2024-12-26 13:57:05 +00:00
package integration
import (
"bytes"
"io"
"log"
"mime/multipart"
"os"
"path"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/config"
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/models"
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/taskservice"
)
func createMultipartFileHeader(filePath string) *multipart.FileHeader {
// open the file
file, err := os.Open(filePath)
if err != nil {
log.Fatal(err)
return nil
}
defer file.Close()
// create a buffer to hold the file in memory
var buff bytes.Buffer
buffWriter := io.Writer(&buff)
// create a new form and create a new file field
formWriter := multipart.NewWriter(buffWriter)
formPart, err := formWriter.CreateFormFile("file", filepath.Base(file.Name()))
if err != nil {
2024-12-26 14:03:01 +00:00
log.Println(err)
2024-12-26 13:57:05 +00:00
return nil
}
// copy the content of the file to the form's file field
if _, err := io.Copy(formPart, file); err != nil {
2024-12-26 14:03:01 +00:00
log.Println(err)
2024-12-26 13:57:05 +00:00
return nil
}
// close the form writer after the copying process is finished
// I don't use defer in here to avoid unexpected EOF error
formWriter.Close()
// transform the bytes buffer into a form reader
buffReader := bytes.NewReader(buff.Bytes())
formReader := multipart.NewReader(buffReader, formWriter.Boundary())
// read the form components with max stored memory of 1MB
multipartForm, err := formReader.ReadForm(1 << 20)
if err != nil {
2024-12-26 14:03:01 +00:00
log.Println(err)
2024-12-26 13:57:05 +00:00
return nil
}
// return the multipart file header
files, exists := multipartForm.File["file"]
if !exists || len(files) == 0 {
2024-12-26 14:03:01 +00:00
log.Println("multipart file not exists")
2024-12-26 13:57:05 +00:00
return nil
}
return files[0]
}
func prepareDb(t *testing.T) *gorm.DB {
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
assert.NoError(t, err)
err = db.AutoMigrate(&models.Task{}, &models.GitRepoAltRepoTask{}, &models.RPMFile{})
assert.NoError(t, err)
return db
}
func TestTaskService_Upload(t *testing.T) {
db := prepareDb(t)
task := &models.Task{
ALTRepoID: 1,
RepoID: 1,
Status: models.StatusPending,
}
task.ID = uint(1)
db.Create(task)
config := &config.Config{
UploadDir: "./test_uploads",
}
os.MkdirAll(config.GetUploadDir(), os.ModePerm)
os.MkdirAll(path.Join(config.GetUploadDir(), "repo/Sisyphus"), os.ModePerm)
service := taskservice.New(db, config)
fileName := "example-foo-1.0.0-alt1.x86_64.rpm"
filePath := path.Join("./test_rpms/", fileName)
fileHeader := createMultipartFileHeader(filePath)
input := &taskservice.TaskUploadInput{
TaskID: "1",
Files: []*multipart.FileHeader{fileHeader},
}
// Вызываем метод Upload
err := service.Upload(input)
assert.NoError(t, err)
// Проверяем, что задача обновлена
var updatedTask models.Task
db.First(&updatedTask, 1)
assert.Equal(t, models.StatusCompleted, updatedTask.Status)
// Проверяем, что файл добавлен в базу
var rpmFiles []models.RPMFile
db.Find(&rpmFiles)
assert.Len(t, rpmFiles, 1)
assert.Equal(t, fileName, rpmFiles[0].Filename)
}