ch prefix
This commit is contained in:
4
backend/internal/admin/auth/logs/all.log
Normal file
4
backend/internal/admin/auth/logs/all.log
Normal file
@@ -0,0 +1,4 @@
|
||||
time="2026-08-05T14:40:26+05:00" level=info msg="read application configuration" func="bankline/internal/config.GetConfig.func1()" file="config.go:40"
|
||||
time="2026-08-05T14:40:26+05:00" level=info func="bankline/internal/config.GetConfig.func1()" file="config.go:44"
|
||||
time="2026-08-05T14:40:26+05:00" level=fatal msg="open ./config.yaml: no such file or directory" func="bankline/internal/config.GetConfig.func1()" file="config.go:45"
|
||||
time="2026-08-05T14:41:20+05:00" level=info msg="read application configuration" func="bankline/internal/config.GetConfig.func1()" file="config.go:40"
|
||||
111
backend/internal/admin/auth/upload_test.go
Normal file
111
backend/internal/admin/auth/upload_test.go
Normal file
@@ -0,0 +1,111 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"bankline/internal/config"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestFileUploadPublicImages(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// package dir -> backend root (for config.yaml)
|
||||
backendRoot := filepath.Clean(filepath.Join(wd, "..", "..", ".."))
|
||||
if err := os.Chdir(backendRoot); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = os.Chdir(wd) })
|
||||
|
||||
tmpPublic := t.TempDir()
|
||||
cfg := config.GetConfig()
|
||||
prev := cfg.PublicFilePath
|
||||
cfg.PublicFilePath = tmpPublic
|
||||
t.Cleanup(func() { cfg.PublicFilePath = prev })
|
||||
|
||||
h := &handler{}
|
||||
|
||||
src := filepath.Join(backendRoot, "public", "images", "1698258358562790757.jpg")
|
||||
img, err := os.ReadFile(src)
|
||||
if err != nil {
|
||||
img = []byte{
|
||||
0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01,
|
||||
0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0xff, 0xd9,
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("ok under 10MB", func(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = multipartRequest(t, "ok.jpg", img)
|
||||
|
||||
h.fileUpload(c)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status=%d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
var resp struct {
|
||||
Data string `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if resp.Data == "" || filepath.Ext(resp.Data) != ".jpg" {
|
||||
t.Fatalf("unexpected data path: %q", resp.Data)
|
||||
}
|
||||
|
||||
local := filepath.Join(tmpPublic, "images", filepath.Base(resp.Data))
|
||||
if _, err := os.Stat(local); err != nil {
|
||||
t.Fatalf("file not saved at %s: %v", local, err)
|
||||
}
|
||||
t.Logf("saved path in response: %s", resp.Data)
|
||||
})
|
||||
|
||||
t.Run("reject over 10MB", func(t *testing.T) {
|
||||
big := bytes.Repeat([]byte("a"), 10<<20+1)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = multipartRequest(t, "big.jpg", big)
|
||||
|
||||
h.fileUpload(c)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("expected 400, got %d body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
t.Logf("rejected body: %s", w.Body.String())
|
||||
})
|
||||
}
|
||||
|
||||
func multipartRequest(t *testing.T, filename string, content []byte) *http.Request {
|
||||
t.Helper()
|
||||
var body bytes.Buffer
|
||||
w := multipart.NewWriter(&body)
|
||||
part, err := w.CreateFormFile("file", filename)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := io.Copy(part, bytes.NewReader(content)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := w.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/admin/file", &body)
|
||||
req.Header.Set("Content-Type", w.FormDataContentType())
|
||||
return req
|
||||
}
|
||||
Reference in New Issue
Block a user