feat: migrate workbench services
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
package mall
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"git.118im.com/im_team/im_workbench/internal/store"
|
||||
)
|
||||
|
||||
type fakeRepository struct {
|
||||
items []store.ManualProduct
|
||||
detail *store.ManualProduct
|
||||
}
|
||||
|
||||
func (f *fakeRepository) ManualProducts(context.Context, int64, int64, int, int) (*store.ManualProductPage, error) {
|
||||
return &store.ManualProductPage{Items: f.items, Total: int64(len(f.items))}, nil
|
||||
}
|
||||
|
||||
func (f *fakeRepository) ManualProduct(context.Context, int64, int64) (*store.ManualProduct, error) {
|
||||
return f.detail, nil
|
||||
}
|
||||
|
||||
func TestProductsMapsSafeCard(t *testing.T) {
|
||||
service := New(&fakeRepository{items: []store.ManualProduct{{ID: 4, ProductNo: "DEMO-P-03",
|
||||
CategoryID: 2, CategoryCode: "DAILY", CategoryName: "日用", Name: "体验商品", ShopName: "平台",
|
||||
PriceAmount: 12900, Currency: "CNY", CoverURL: "https://example.com/cover.png",
|
||||
DetailHTML: "<h3>标题</h3><script>alert(1)</script><p>内容</p>", ClaimedCount: 2, IsRecommended: true}}})
|
||||
|
||||
result, err := service.Products(context.Background(), 0, 0, 1, 20)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(result.Items) != 1 || result.Items[0].CoverURL == "" || result.Items[0].Summary != "标题 内容" ||
|
||||
len(result.Items[0].AvailableActions) != 1 || result.Items[0].AvailableActions[0] != "contact_support" {
|
||||
t.Fatalf("unexpected product: %#v", result.Items)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProductRejectsUnsafeCoverAndReturnsPlainText(t *testing.T) {
|
||||
service := New(&fakeRepository{detail: &store.ManualProduct{ID: 4, Name: "商品",
|
||||
CoverURL: "javascript:alert(1)", DetailHTML: "<p>安全说明</p>"}})
|
||||
|
||||
result, err := service.Product(context.Background(), 0, 4)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.CoverURL != "" || result.DetailText != "安全说明" {
|
||||
t.Fatalf("unexpected detail: %#v", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProductsValidatesPagination(t *testing.T) {
|
||||
service := New(&fakeRepository{})
|
||||
if _, err := service.Products(context.Background(), 0, 0, -1, 20); !errors.Is(err, ErrPageInvalid) {
|
||||
t.Fatalf("expected page error, got %v", err)
|
||||
}
|
||||
if _, err := service.Products(context.Background(), 0, -1, 1, 20); !errors.Is(err, ErrCategoryIDInvalid) {
|
||||
t.Fatalf("expected category error, got %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user