Also accept flexible card-order form values, move mock seed under db/mock, and regenerate Swagger. Co-authored-by: Cursor <cursoragent@cursor.com>
72 lines
1.8 KiB
Go
72 lines
1.8 KiB
Go
package activitylog
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"online-back/pkg/logging"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func TestRecordLeavesJSONBodyForHandler(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
repo := &stubRepository{}
|
|
r := gin.New()
|
|
r.Use(Record(repo, logging.GetLogger()))
|
|
r.POST("/api/admin/card-orders", func(c *gin.Context) {
|
|
c.Set("id", "3")
|
|
var dto struct {
|
|
CustomerName string `json:"customerName" binding:"required"`
|
|
Region string `json:"region" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindBodyWithJSON(&dto); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusCreated, dto)
|
|
})
|
|
|
|
body := `{"customerName":"Simple","region":"Aşgabat"}`
|
|
w := httptest.NewRecorder()
|
|
req := httptest.NewRequest(http.MethodPost, "/api/admin/card-orders", bytes.NewBufferString(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
r.ServeHTTP(w, req)
|
|
|
|
if w.Code != http.StatusCreated {
|
|
t.Fatalf("status=%d body=%s", w.Code, w.Body.String())
|
|
}
|
|
|
|
deadline := time.Now().Add(time.Second)
|
|
var created []CreateActivityLogDTO
|
|
for time.Now().Before(deadline) {
|
|
created = repo.createdEntries()
|
|
if len(created) > 0 {
|
|
break
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
if len(created) == 0 {
|
|
t.Fatal("expected activity log to be recorded")
|
|
}
|
|
if created[0].Module != "card-orders" {
|
|
t.Fatalf("module=%q", created[0].Module)
|
|
}
|
|
if created[0].StatusCode != http.StatusCreated {
|
|
t.Fatalf("statusCode=%d", created[0].StatusCode)
|
|
}
|
|
|
|
var logged map[string]interface{}
|
|
if err := json.Unmarshal(created[0].RequestBody, &logged); err != nil {
|
|
t.Fatalf("request body: %v", err)
|
|
}
|
|
if logged["customerName"] != "Simple" {
|
|
t.Fatalf("logged body=%v", logged)
|
|
}
|
|
}
|