From 904a9f4a630da5463d38775159dc19af9dfef77b Mon Sep 17 00:00:00 2001 From: Maxim Slipenko Date: Thu, 26 Dec 2024 16:57:05 +0300 Subject: [PATCH] add TaskService_Upload test --- .gitignore | 4 +- tests/integration/taskservice_upload_test.go | 134 ++++++++++++++++++ .../example-foo-1.0.0-alt1.x86_64.rpm | Bin 0 -> 1128 bytes 3 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 tests/integration/taskservice_upload_test.go create mode 100644 tests/integration/test_rpms/example-foo-1.0.0-alt1.x86_64.rpm diff --git a/.gitignore b/.gitignore index a450265..ebb0d04 100644 --- a/.gitignore +++ b/.gitignore @@ -131,4 +131,6 @@ fabric.properties # Игнорируем папку uploads uploads -app.db \ No newline at end of file +app.db + +tests/integration/test_uploads \ No newline at end of file diff --git a/tests/integration/taskservice_upload_test.go b/tests/integration/taskservice_upload_test.go new file mode 100644 index 0000000..b2c8bc4 --- /dev/null +++ b/tests/integration/taskservice_upload_test.go @@ -0,0 +1,134 @@ +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 { + log.Fatal(err) + return nil + } + + // copy the content of the file to the form's file field + if _, err := io.Copy(formPart, file); err != nil { + log.Fatal(err) + 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 { + log.Fatal(err) + return nil + } + + // return the multipart file header + files, exists := multipartForm.File["file"] + if !exists || len(files) == 0 { + log.Fatal("multipart file not exists") + 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) +} + +// createTestRPMFile создает тестовый RPM файл с заглушкой +func createTestRPMFile(filePath string) error { + content := []byte("dummy rpm content") + return os.WriteFile(filePath, content, 0644) +} diff --git a/tests/integration/test_rpms/example-foo-1.0.0-alt1.x86_64.rpm b/tests/integration/test_rpms/example-foo-1.0.0-alt1.x86_64.rpm new file mode 100644 index 0000000000000000000000000000000000000000..dbe4f2ec1fd3e5d87d72d2fcf86eebaad86f763f GIT binary patch literal 1128 zcma)+&ui2`6vrp-RxOBDtcX9r96Y+)Onx;HsTQ@Uv?8>H3PPPsGHck)CTzB_sE6JZ z1aJNkQhJac^dj`)$(vUXdazJ`wBkiCweh{(cL+Uo;PK5T@6F6Nvu}25W&2Zw!O)_f zNU~N`Q)yb`b)#<7M61g&weO*^p}n;)RyQ?zR|UNfF#&N1Vj5xuHa-q|5CR9(RyIMg z_B!z|;+x2VtAM+&XfEqSgAGIX%f{tOB3v2x0~gc z#>U8UU8czx7-Oxtz0_kp&s}uw^7oYPT|Mu}dM8WD>AnFto6_x+S)8)Qqc~$e3!}ia z+|c)Z;YC(x8KG1Wx0T~69$H)~H-Kvs2;XrWKlHdiE`$`G5r(F(Y}3N!@ptX_dr|CS ze~p}f4zGsW&sb4ie?7c>_sG!to7?W&xmPDIR#(34uI*S;{lAYhI2(Ghvh(=e=e7Bl R7vRPeMTOnj7#_#Q{0|fX$npRH literal 0 HcmV?d00001