1eab1b99c1
- Rename all QINIU_* config/code/docs to OSS_* to match actual Alibaba Cloud OSS - Refactor upload module from internal/common/qiniu to internal/common/upload - Add backend proxy upload endpoint (POST /api/admin/marketing/upload) to avoid CORS - Auto-generate compressed thumbnail (800px, JPEG 80%) on admin image upload - Add dev-login endpoint (POST /api/v1/auth/dev-login) for H5 debugging - Add imageutil package for server-side image resizing Made-with: Cursor
104 lines
3.2 KiB
Go
104 lines
3.2 KiB
Go
package handler
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha1"
|
|
"encoding/base64"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"wx_service/config"
|
|
uploadservice "wx_service/internal/common/upload/service"
|
|
)
|
|
|
|
func TestUploadCallbackSuccess(t *testing.T) {
|
|
t.Parallel()
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
cfg := config.OSSConfig{AccessKey: "ak-test", SecretKey: "sk-test"}
|
|
h := NewUploadHandler(uploadservice.NewUploadService(cfg))
|
|
|
|
body := "key=uploads/test.png&hash=abc&fsize=12&mimeType=image%2Fpng"
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/common/upload/oss/callback", strings.NewReader(body))
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
req.Header.Set("Authorization", "QBox "+cfg.AccessKey+":"+signCallback(req.URL.Path+"\n"+body, cfg.SecretKey))
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Request = req
|
|
|
|
h.UploadCallback(c)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status=%d, want=200, body=%s", w.Code, w.Body.String())
|
|
}
|
|
if !strings.Contains(w.Body.String(), `"code":200`) {
|
|
t.Fatalf("unexpected response body: %s", w.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestUploadCallbackInvalidSignature(t *testing.T) {
|
|
t.Parallel()
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
cfg := config.OSSConfig{AccessKey: "ak-test", SecretKey: "sk-test"}
|
|
h := NewUploadHandler(uploadservice.NewUploadService(cfg))
|
|
|
|
body := "key=uploads/test.png&hash=abc"
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/common/upload/oss/callback", strings.NewReader(body))
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
req.Header.Set("Authorization", "QBox ak-test:bad-sign")
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Request = req
|
|
|
|
h.UploadCallback(c)
|
|
|
|
if w.Code != http.StatusUnauthorized {
|
|
t.Fatalf("status=%d, want=401, body=%s", w.Code, w.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestUploadCallbackMissingKey(t *testing.T) {
|
|
t.Parallel()
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
cfg := config.OSSConfig{AccessKey: "ak-test", SecretKey: "sk-test"}
|
|
h := NewUploadHandler(uploadservice.NewUploadService(cfg))
|
|
|
|
body := "hash=abc&fsize=12"
|
|
req := httptest.NewRequest(http.MethodPost, "/api/v1/common/upload/oss/callback", strings.NewReader(body))
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
req.Header.Set("Authorization", "QBox "+cfg.AccessKey+":"+signCallback(req.URL.Path+"\n"+body, cfg.SecretKey))
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
c.Request = req
|
|
|
|
h.UploadCallback(c)
|
|
|
|
if w.Code != http.StatusBadRequest {
|
|
t.Fatalf("status=%d, want=400, body=%s", w.Code, w.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestParseCallbackPayloadJSON(t *testing.T) {
|
|
t.Parallel()
|
|
raw := []byte(`{"key":"uploads/test.png","hash":"abc","fsize":321,"mimeType":"image/png"}`)
|
|
got, err := parseCallbackPayload("application/json", raw)
|
|
if err != nil {
|
|
t.Fatalf("parseCallbackPayload: %v", err)
|
|
}
|
|
if got.Key != "uploads/test.png" || got.Hash != "abc" || got.Fsize != 321 {
|
|
t.Fatalf("unexpected payload: %+v", got)
|
|
}
|
|
}
|
|
|
|
func signCallback(signing, secret string) string {
|
|
mac := hmac.New(sha1.New, []byte(secret))
|
|
_, _ = mac.Write([]byte(signing))
|
|
return base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(mac.Sum(nil))
|
|
}
|