Enhance integration tests with file upload functionality and support for multipart requests

This commit is contained in:
2025-07-10 22:02:03 +02:00
parent ae4e9c8db2
commit 5a6895ecdc
3 changed files with 77 additions and 3 deletions

View File

@@ -239,5 +239,36 @@ func testFileHandlers(t *testing.T, dbConfig DatabaseConfig) {
})
}
})
t.Run("upload file", func(t *testing.T) {
t.Run("successful upload", func(t *testing.T) {
fileName := "uploaded-test.txt"
fileContent := "This is an uploaded file"
rr := h.makeUploadRequest(t, baseURL+"/_op/upload/uploads", fileName, fileContent, h.RegularTestUser)
require.Equal(t, http.StatusOK, rr.Code)
// Verify response structure
var response struct {
FilePath string `json:"filePath"`
Size int64 `json:"size"`
UpdatedAt string `json:"updatedAt"`
}
err := json.NewDecoder(rr.Body).Decode(&response)
require.NoError(t, err)
assert.Equal(t, "uploads/"+fileName, response.FilePath)
assert.Equal(t, int64(len(fileContent)), response.Size)
// Verify file was saved
rr = h.makeRequest(t, http.MethodGet, baseURL+"/uploads/"+fileName, nil, h.RegularTestUser)
require.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, fileContent, rr.Body.String())
})
t.Run("upload without file", func(t *testing.T) {
rr := h.makeUploadRequest(t, baseURL+"/_op/upload/test", "", "", h.RegularTestUser)
assert.Equal(t, http.StatusBadRequest, rr.Code)
})
})
})
}

View File

@@ -6,6 +6,7 @@ import (
"bytes"
"encoding/json"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
@@ -303,11 +304,19 @@ func (h *testHarness) makeRequest(t *testing.T, method, path string, body any, t
return h.executeRequest(req)
}
// makeRequestRawWithHeaders adds support for custom headers with raw body
func (h *testHarness) makeRequestRaw(t *testing.T, method, path string, body io.Reader, testUser *testUser) *httptest.ResponseRecorder {
// makeRequestRaw adds support for raw body requests with optional headers
func (h *testHarness) makeRequestRaw(t *testing.T, method, path string, body io.Reader, testUser *testUser, headers ...map[string]string) *httptest.ResponseRecorder {
t.Helper()
req := h.newRequestRaw(t, method, path, body)
// Set custom headers if provided
if len(headers) > 0 {
for key, value := range headers[0] {
req.Header.Set(key, value)
}
}
h.addAuthCookies(t, req, testUser)
needsCSRF := method != http.MethodGet && method != http.MethodHead && method != http.MethodOptions
@@ -318,3 +327,37 @@ func (h *testHarness) makeRequestRaw(t *testing.T, method, path string, body io.
return h.executeRequest(req)
}
// makeUploadRequest creates a multipart form request for file uploads
// If fileName is empty, creates an empty multipart form without a file
func (h *testHarness) makeUploadRequest(t *testing.T, path, fileName, fileContent string, testUser *testUser) *httptest.ResponseRecorder {
t.Helper()
// Create multipart form
var buf bytes.Buffer
writer := multipart.NewWriter(&buf)
// Only add file part if fileName is not empty
if fileName != "" {
part, err := writer.CreateFormFile("file", fileName)
if err != nil {
t.Fatalf("Failed to create form file: %v", err)
}
_, err = part.Write([]byte(fileContent))
if err != nil {
t.Fatalf("Failed to write file content: %v", err)
}
}
err := writer.Close()
if err != nil {
t.Fatalf("Failed to close multipart writer: %v", err)
}
headers := map[string]string{
"Content-Type": writer.FormDataContentType(),
}
return h.makeRequestRaw(t, http.MethodPost, path, &buf, testUser, headers)
}