This commit is contained in:
Tomas Dvorak
2026-01-26 08:13:18 +01:00
parent aa036b6550
commit dfc079288f
505 changed files with 95755 additions and 5712 deletions
@@ -0,0 +1,86 @@
package eshop
import (
"net/http"
"net/http/httptest"
"testing"
"fotbal-club/internal/config"
"fotbal-club/internal/models"
"fotbal-club/internal/services"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
// TestShippingController_UpdatePacketStatuses_UpdatesLabelStatus verifies that
// the background updater uses PacketaService to refresh label statuses.
func TestShippingController_UpdatePacketStatuses_UpdatesLabelStatus(t *testing.T) {
// In-memory SQLite DB for isolated test
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
if err != nil {
t.Fatalf("failed to open in-memory db: %v", err)
}
// Migrate only the necessary e-shop tables
if err := db.AutoMigrate(&models.EshopOrder{}, &models.EshopShippingLabel{}); err != nil {
t.Fatalf("failed to migrate schemas: %v", err)
}
// Seed one label in a non-terminal state
label := models.EshopShippingLabel{
OrderID: 1,
Carrier: "packeta",
PacketaPacketID: "12345",
Status: "created",
}
if err := db.Create(&label).Error; err != nil {
t.Fatalf("failed to seed label: %v", err)
}
// Fake Packeta API that always returns DELIVERED
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/xml")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`
<response>
<status>ok</status>
<result>
<statusCode>DELIVERED</statusCode>
<statusText>DELIVERED</statusText>
</result>
</response>
`))
}))
defer server.Close()
cfg := &config.Config{
PacketaAPIPassword: "test-password",
PacketaEshopName: "TestEshop",
}
// Use the shared PacketaService but point it to our test server
packetaSvc := &services.PacketaService{
ApiPassword: cfg.PacketaAPIPassword,
ApiUrl: server.URL,
EshopName: cfg.PacketaEshopName,
}
ctrl := &ShippingController{
Config: cfg,
PacketaService: packetaSvc,
}
// Run the updater
ctrl.UpdatePacketStatuses(db)
// Reload label and verify status was updated from Packeta response
var updated models.EshopShippingLabel
if err := db.First(&updated, label.ID).Error; err != nil {
t.Fatalf("failed to load updated label: %v", err)
}
if updated.Status != "DELIVERED" {
t.Fatalf("expected status DELIVERED, got %q", updated.Status)
}
}