Files
bamort/backend/maintenance/api_maint_test.go
T
2025-07-24 07:39:43 +02:00

193 lines
5.2 KiB
Go

package maintenance
import (
"bamort/character"
"bamort/database"
"bamort/gsmaster"
"bamort/router"
"bamort/user"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func TestMaintSetupCheck(t *testing.T) {
// Setup proper test database
database.SetupTestDB(true)
// Create a proper HTTP test context
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
// Call the handler
SetupCheck(c)
// Check the response
assert.Equal(t, http.StatusOK, w.Code)
// Parse the JSON response
var response map[string]string
err := json.Unmarshal(w.Body.Bytes(), &response)
assert.NoError(t, err)
assert.Equal(t, "Setup Check OK", response["message"])
}
func TestGetMasterData(t *testing.T) {
database.SetupTestDB(false)
// Initialize a Gin router
r := gin.Default()
router.SetupGin(r)
// Routes
protected := router.BaseRouterGrp(r)
character.RegisterRoutes(protected)
gsmaster.RegisterRoutes(protected)
protected.GET("/test", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "Test OK"})
})
u := user.User{}
u.FirstId(1)
// Create a test HTTP request
req, _ := http.NewRequest("GET", "/api/maintenance", nil)
req.Header.Set("Content-Type", "application/json")
token := user.GenerateToken(&u)
req.Header.Set("Authorization", "Bearer "+token)
// Create a response recorder to capture the handler's response
respRecorder := httptest.NewRecorder()
// Perform the test request
r.ServeHTTP(respRecorder, req)
// Assert the response status code
assert.Equal(t, http.StatusOK, respRecorder.Code)
// Assert the response body
type dtaStruct struct {
Skills []gsmaster.Skill `json:"skills"`
Weaponskills []gsmaster.WeaponSkill `json:"weaponskills"`
Spell []gsmaster.Spell `json:"spells"`
Equipment []gsmaster.Equipment `json:"equipment"`
Weapons []gsmaster.Weapon `json:"weapons"`
}
var dta dtaStruct
err := json.Unmarshal(respRecorder.Body.Bytes(), &dta)
assert.NoError(t, err)
}
func TestGetMDSkillCategories(t *testing.T) {
database.SetupTestDB(false)
//gsmaster.MigrateStructure()
ski := gsmaster.Skill{}
categories, err := ski.GetSkillCategories()
assert.NoError(t, err)
assert.LessOrEqual(t, 1, len(categories))
assert.Equal(t, "Wissen", categories[0])
}
func TestGetMDSkills(t *testing.T) {
database.SetupTestDB(false)
// Initialize a Gin router
r := gin.Default()
router.SetupGin(r)
// Routes
protected := router.BaseRouterGrp(r)
// Character routes
character.RegisterRoutes(protected)
gsmaster.RegisterRoutes(protected)
protected.GET("/test", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "Test OK"})
})
u := user.User{}
u.FirstId(1)
// Create a test HTTP request
req, _ := http.NewRequest("GET", "/api/characters/20", nil)
//req.Header.Set("Content-Type", "application/json")
req.Header.Set("Content-Type", "application/json")
//req.Header.Set("Authorization", "Bearer ${token}")
req.Header.Set("Authorization", "Bearer dc7a780.1:bba7f4daabda117f2a2c14263")
// Create a response recorder to capture the handler's response
respRecorder := httptest.NewRecorder()
// Perform the test request
r.ServeHTTP(respRecorder, req)
// Assert the response status code
assert.Equal(t, http.StatusOK, respRecorder.Code)
// Assert the response body
var listOfCharacter *character.Char
err := json.Unmarshal(respRecorder.Body.Bytes(), &listOfCharacter)
assert.NoError(t, err)
assert.Equal(t, "Harsk Hammerhuter, Zen", listOfCharacter.Name)
assert.Equal(t, "Zwerg", listOfCharacter.Rasse)
assert.Equal(t, 20, int(listOfCharacter.ID)) // Check the simulated ID
assert.Equal(t, "Krieger", listOfCharacter.Typ)
assert.Equal(t, 3, listOfCharacter.Grad)
//assert.Equal(t, "test", listOfCharacter.Owner)
//assert.Equal(t, false, listOfCharacter.Public)
}
/*
func TestUpdateMDSkill(t *testing.T) {
database.SetupTestDB(false)
// Initialize a Gin router
r := gin.Default()
router.SetupGin(r)
// Routes
protected := router.BaseRouterGrp(r)
character.RegisterRoutes(protected)
gsmaster.RegisterRoutes(protected)
protected.GET("/test", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "Test OK"})
})
u := user.User{}
u.FirstId(1)
// Define the test case input
sk := gsmaster.Skill{}
sk.Name = "Geländekunde"
sk.ID = 64
jsonData, err := json.Marshal(sk)
if err != nil {
t.Fatalf("Failed to marshal skill: %v", err)
}
// Create a test HTTP request
req, _ := http.NewRequest("PUT", "/api/maintenance/skills/64", bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
token := user.GenerateToken(&u)
req.Header.Set("Authorization", "Bearer "+token)
// Create a response recorder to capture the handler's response
respRecorder := httptest.NewRecorder()
// Perform the test request
r.ServeHTTP(respRecorder, req)
// Assert the response status code
assert.Equal(t, http.StatusCreated, respRecorder.Code)
// Assert the response body
var createdCharacter character.Char
err = json.Unmarshal(respRecorder.Body.Bytes(), &createdCharacter)
assert.NoError(t, err)
assert.Equal(t, "Aragorn", createdCharacter.Name)
assert.Equal(t, "Human", createdCharacter.Rasse)
assert.Equal(t, 1, createdCharacter.ID) // Check the simulated ID
}
*/