100 lines
2.9 KiB
Go
100 lines
2.9 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 (s *stubDesktopClientRepo) ListByUser(ctx context.Context, tenantID, workspaceID, userID 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)
|
|
}
|