98 lines
2.4 KiB
Go
98 lines
2.4 KiB
Go
package activitylog
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"sync"
|
|
"testing"
|
|
|
|
"online-back/internal/shared"
|
|
"online-back/pkg/logging"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type stubRepository struct {
|
|
mu sync.Mutex
|
|
listFilter ListFilter
|
|
listResult ListActivityLogDTO
|
|
created []CreateActivityLogDTO
|
|
}
|
|
|
|
func (s *stubRepository) List(_ context.Context, filter ListFilter) (ListActivityLogDTO, error) {
|
|
s.listFilter = filter
|
|
return s.listResult, nil
|
|
}
|
|
|
|
func (s *stubRepository) GetByID(_ context.Context, _ int64) (ActivityLogDTO, error) {
|
|
return ActivityLogDTO{}, nil
|
|
}
|
|
|
|
func (s *stubRepository) Create(_ context.Context, dto CreateActivityLogDTO) (ActivityLogDTO, error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
s.created = append(s.created, dto)
|
|
return ActivityLogDTO{Id: 1}, nil
|
|
}
|
|
|
|
func (s *stubRepository) createdEntries() []CreateActivityLogDTO {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
out := make([]CreateActivityLogDTO, len(s.created))
|
|
copy(out, s.created)
|
|
return out
|
|
}
|
|
|
|
func TestParseListFilterIgnoresUndefinedAndCapsLimit(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
w := httptest.NewRecorder()
|
|
c, _ := gin.CreateTestContext(w)
|
|
req := httptest.NewRequest(http.MethodGet, "/activity-logs?user_id=undefined&module=card-orders&limit=1000&page=2", nil)
|
|
c.Request = req
|
|
|
|
filter, ok := parseListFilter(c)
|
|
if !ok {
|
|
t.Fatal("expected filter to parse")
|
|
}
|
|
if filter.UserId != nil {
|
|
t.Fatalf("user_id should be ignored, got %v", *filter.UserId)
|
|
}
|
|
if filter.Module != "card-orders" {
|
|
t.Fatalf("module=%q", filter.Module)
|
|
}
|
|
if filter.Limit != shared.MaxLimit {
|
|
t.Fatalf("limit=%d", filter.Limit)
|
|
}
|
|
if filter.Page != 2 {
|
|
t.Fatalf("page=%d", filter.Page)
|
|
}
|
|
}
|
|
|
|
func TestGetByIDListsModuleWhenPathIsNotNumeric(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
repo := &stubRepository{listResult: ListActivityLogDTO{Count: 0, Logs: []ActivityLogDTO{}}}
|
|
h := &handler{logger: logging.GetLogger(), repository: repo}
|
|
|
|
r := gin.New()
|
|
group := r.Group("/activity-logs")
|
|
group.GET("", h.list)
|
|
group.GET("/:id", h.getByID)
|
|
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodGet, "/activity-logs/card-orders?limit=1000", nil)
|
|
r.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("status=%d body=%s", w.Code, w.Body.String())
|
|
}
|
|
if repo.listFilter.Module != "card-orders" {
|
|
t.Fatalf("module=%q", repo.listFilter.Module)
|
|
}
|
|
if repo.listFilter.Limit != shared.MaxLimit {
|
|
t.Fatalf("limit=%d", repo.listFilter.Limit)
|
|
}
|
|
}
|