107 lines
2.6 KiB
Go
107 lines
2.6 KiB
Go
package taskcontroller_test
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/controllers/taskcontroller"
|
|
"code.alt-gnome.ru/aides-infra/aides-repo-api/internal/services/taskservice"
|
|
)
|
|
|
|
func TestUpload(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
taskID string
|
|
files []byte
|
|
mockService *TaskServiceMock
|
|
expectedStatus int
|
|
}{
|
|
{
|
|
name: "Valid upload",
|
|
taskID: "1",
|
|
files: []byte("test file content"), // можно заменить на реальный файл, если нужно
|
|
mockService: &TaskServiceMock{
|
|
UploadFunc: func(input *taskservice.TaskUploadInput) error {
|
|
return nil
|
|
},
|
|
},
|
|
expectedStatus: http.StatusOK,
|
|
},
|
|
{
|
|
name: "Invalid taskID",
|
|
taskID: "",
|
|
files: []byte("test file content"),
|
|
mockService: &TaskServiceMock{},
|
|
expectedStatus: http.StatusBadRequest,
|
|
},
|
|
/*
|
|
TODO:
|
|
|
|
{
|
|
name: "File too large",
|
|
taskID: "1",
|
|
files: []byte("test file content"),
|
|
mockService: &TaskServiceMock{
|
|
UploadFunc: func(input *taskservice.TaskUploadInput) error {
|
|
return nil
|
|
},
|
|
},
|
|
expectedStatus: http.StatusBadRequest,
|
|
},
|
|
*/
|
|
{
|
|
name: "Error during file upload",
|
|
taskID: "1",
|
|
files: []byte("test file content"),
|
|
mockService: &TaskServiceMock{
|
|
UploadFunc: func(input *taskservice.TaskUploadInput) error {
|
|
return errors.New("internal upload error")
|
|
},
|
|
},
|
|
expectedStatus: http.StatusInternalServerError,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
controller := taskcontroller.New(tt.mockService)
|
|
|
|
r := chi.NewRouter()
|
|
r.Post("/tasks/{taskID}/upload", controller.Upload)
|
|
|
|
body := &bytes.Buffer{}
|
|
writer := multipart.NewWriter(body)
|
|
part, err := writer.CreateFormFile("files", "file.txt")
|
|
if err != nil {
|
|
t.Fatalf("Failed to create form file: %v", err)
|
|
}
|
|
part.Write(tt.files)
|
|
writer.Close()
|
|
|
|
ts := httptest.NewServer(r)
|
|
defer ts.Close()
|
|
|
|
req, err := http.NewRequest(http.MethodPost, ts.URL+"/tasks/"+tt.taskID+"/upload", body)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create request: %v", err)
|
|
}
|
|
req.Header.Set("Content-Type", writer.FormDataContentType())
|
|
|
|
resp, err := ts.Client().Do(req)
|
|
if err != nil {
|
|
t.Fatalf("Failed to execute request: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if status := resp.StatusCode; status != tt.expectedStatus {
|
|
t.Errorf("handler returned wrong status code: got %v want %v", status, tt.expectedStatus)
|
|
}
|
|
})
|
|
}
|
|
}
|