Files
geo/server/internal/tenant/transport/desktop_client_auth_test.go
T
root a617d39a4a feat(desktop): drop parked review flow, add publish management
SaaS 侧人工审核后才创建 publish job,desktop 不再做二次审核:移除
manual/waiting_user/parked/from_parked 状态机与 LeaseFromParked 查询,
desktop client 只执行发布并新增"发布管理"页(待发布队列 / 历史 / 再次
发送)。同时抽离 @geo/publisher-platforms 共享适配器包、新增 Redis-based
desktop_presence 与 publish_record_support,刷新 admin-web 发布弹窗与
媒体库;plan A / spec 文档同步口径。
2026-04-20 09:52:48 +08:00

96 lines
2.7 KiB
Go

package transport
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/geo-platform/tenant-api/internal/tenant/repository"
)
type stubDesktopClientRepo struct {
client *repository.DesktopClient
}
func (s *stubDesktopClientRepo) Register(ctx context.Context, params repository.RegisterDesktopClientParams) (*repository.DesktopClient, error) {
panic("unexpected call")
}
func (s *stubDesktopClientRepo) GetByID(ctx context.Context, id uuid.UUID, workspaceID int64) (*repository.DesktopClient, error) {
panic("unexpected call")
}
func (s *stubDesktopClientRepo) GetByTokenHash(ctx context.Context, tokenHash []byte) (*repository.DesktopClient, error) {
if s.client == nil {
return nil, context.Canceled
}
return s.client, nil
}
func (s *stubDesktopClientRepo) RotateToken(ctx context.Context, params repository.RotateDesktopClientTokenParams) (*repository.DesktopClient, error) {
panic("unexpected call")
}
func (s *stubDesktopClientRepo) Heartbeat(ctx context.Context, params repository.HeartbeatDesktopClientParams) (*repository.DesktopClient, error) {
panic("unexpected call")
}
func (s *stubDesktopClientRepo) Revoke(ctx context.Context, id uuid.UUID, workspaceID int64) (*repository.DesktopClient, error) {
panic("unexpected call")
}
func (s *stubDesktopClientRepo) ListByWorkspace(ctx context.Context, workspaceID int64) ([]repository.DesktopClient, error) {
panic("unexpected call")
}
func TestDesktopClientMiddleware_RequiresBearerToken(t *testing.T) {
gin.SetMode(gin.TestMode)
router := gin.New()
router.Use(DesktopClientMiddleware(&stubDesktopClientRepo{}))
router.POST("/test", func(c *gin.Context) {
c.Status(http.StatusOK)
})
req := httptest.NewRequest(http.MethodPost, "/test", nil)
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
assert.Equal(t, http.StatusUnauthorized, resp.Code)
}
func TestDesktopClientMiddleware_SetsClientContext(t *testing.T) {
gin.SetMode(gin.TestMode)
expectedID := uuid.New()
router := gin.New()
router.Use(DesktopClientMiddleware(&stubDesktopClientRepo{
client: &repository.DesktopClient{
ID: expectedID,
TenantID: 1,
WorkspaceID: 2,
UserID: 3,
CreatedAt: time.Now(),
},
}))
var gotID uuid.UUID
router.POST("/test", func(c *gin.Context) {
client := MustDesktopClient(c.Request.Context())
gotID = client.ID
c.Status(http.StatusOK)
})
req := httptest.NewRequest(http.MethodPost, "/test", nil)
req.Header.Set("Authorization", "Bearer client-token")
resp := httptest.NewRecorder()
router.ServeHTTP(resp, req)
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, expectedID, gotID)
}