Files
bamort/backend/character/practice_points_additional_test.go
T
Frank 2cac1cc41d Create tests for ALL API entpoints in ALL packages
Based on current data. Ensure that all API endpoints used in frontend are tested. These tests are crucial for the next refactoring tasks.
2026-04-11 23:37:40 +02:00

257 lines
6.6 KiB
Go

package character
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"bamort/database"
"bamort/models"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func setupPPTestEnv(t *testing.T) {
t.Helper()
original := os.Getenv("ENVIRONMENT")
os.Setenv("ENVIRONMENT", "test")
t.Cleanup(func() {
if original != "" {
os.Setenv("ENVIRONMENT", original)
} else {
os.Unsetenv("ENVIRONMENT")
}
})
database.SetupTestDB(true, true)
t.Cleanup(database.ResetTestDB)
require.NoError(t, models.MigrateStructure())
gin.SetMode(gin.TestMode)
}
func createCharWithSkillPP(t *testing.T, skillName string, ppAmount int) *models.Char {
t.Helper()
char := &models.Char{
BamortBase: models.BamortBase{Name: "PP Test Char"},
UserID: 4,
Rasse: "Mensch",
Typ: "Kr",
}
require.NoError(t, database.DB.Create(char).Error)
skill := &models.SkFertigkeit{
BamortCharTrait: models.BamortCharTrait{
BamortBase: models.BamortBase{Name: skillName},
CharacterID: char.ID,
UserID: 4,
},
Fertigkeitswert: 5,
Pp: ppAmount,
Improvable: true,
}
require.NoError(t, database.DB.Create(skill).Error)
return char
}
func TestGetPracticePoints(t *testing.T) {
setupPPTestEnv(t)
char := createCharWithSkillPP(t, "Athletik", 3)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Set("userID", uint(4))
c.Params = gin.Params{{Key: "id", Value: fmt.Sprintf("%d", char.ID)}}
c.Request = httptest.NewRequest(http.MethodGet, "/", nil)
GetPracticePoints(c)
assert.Equal(t, http.StatusOK, w.Code)
var resp []PracticePointResponse
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &resp))
require.Len(t, resp, 1)
assert.Equal(t, "Athletik", resp[0].SkillName)
assert.Equal(t, 3, resp[0].Amount)
}
func TestGetPracticePointsEmpty(t *testing.T) {
setupPPTestEnv(t)
// A character with zero PP skills
char := createCharWithSkillPP(t, "Athletik", 0)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Set("userID", uint(4))
c.Params = gin.Params{{Key: "id", Value: fmt.Sprintf("%d", char.ID)}}
c.Request = httptest.NewRequest(http.MethodGet, "/", nil)
GetPracticePoints(c)
assert.Equal(t, http.StatusOK, w.Code)
// Should return null or empty
body := w.Body.String()
assert.True(t, body == "null" || body == "[]", "Empty PP list should return null or empty array, got: "+body)
}
func TestGetPracticePointsCharNotFound(t *testing.T) {
setupPPTestEnv(t)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Set("userID", uint(4))
c.Params = gin.Params{{Key: "id", Value: "99999"}}
c.Request = httptest.NewRequest(http.MethodGet, "/", nil)
GetPracticePoints(c)
assert.Equal(t, http.StatusNotFound, w.Code)
}
func TestUpdatePracticePoints(t *testing.T) {
setupPPTestEnv(t)
char := createCharWithSkillPP(t, "Athletik", 1)
updates := []PracticePointResponse{
{SkillName: "Athletik", Amount: 5},
}
body, _ := json.Marshal(updates)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Set("userID", uint(4))
c.Params = gin.Params{{Key: "id", Value: fmt.Sprintf("%d", char.ID)}}
c.Request = httptest.NewRequest(http.MethodPut, "/", bytes.NewBuffer(body))
c.Request.Header.Set("Content-Type", "application/json")
UpdatePracticePoints(c)
assert.Equal(t, http.StatusOK, w.Code)
}
func TestUpdatePracticePointsNotOwner(t *testing.T) {
setupPPTestEnv(t)
char := createCharWithSkillPP(t, "Athletik", 1)
updates := []PracticePointResponse{{SkillName: "Athletik", Amount: 5}}
body, _ := json.Marshal(updates)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Set("userID", uint(99))
c.Params = gin.Params{{Key: "id", Value: fmt.Sprintf("%d", char.ID)}}
c.Request = httptest.NewRequest(http.MethodPut, "/", bytes.NewBuffer(body))
c.Request.Header.Set("Content-Type", "application/json")
UpdatePracticePoints(c)
assert.Equal(t, http.StatusForbidden, w.Code)
}
func TestAddPracticePoint(t *testing.T) {
setupPPTestEnv(t)
char := createCharWithSkillPP(t, "Athletik", 0)
reqBody := map[string]interface{}{
"skill_name": "Athletik",
"amount": 1,
}
body, _ := json.Marshal(reqBody)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Set("userID", uint(4))
c.Params = gin.Params{{Key: "id", Value: fmt.Sprintf("%d", char.ID)}}
c.Request = httptest.NewRequest(http.MethodPost, "/", bytes.NewBuffer(body))
c.Request.Header.Set("Content-Type", "application/json")
AddPracticePoint(c)
assert.Equal(t, http.StatusOK, w.Code)
var resp PracticePointActionResponse
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &resp))
assert.True(t, resp.Success)
assert.Equal(t, "Athletik", resp.TargetSkill)
}
func TestAddPracticePointSkillNotFound(t *testing.T) {
setupPPTestEnv(t)
char := createCharWithSkillPP(t, "Athletik", 0)
reqBody := map[string]interface{}{
"skill_name": "Zauberei",
"amount": 1,
}
body, _ := json.Marshal(reqBody)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Set("userID", uint(4))
c.Params = gin.Params{{Key: "id", Value: fmt.Sprintf("%d", char.ID)}}
c.Request = httptest.NewRequest(http.MethodPost, "/", bytes.NewBuffer(body))
c.Request.Header.Set("Content-Type", "application/json")
AddPracticePoint(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}
func TestUsePracticePoint(t *testing.T) {
setupPPTestEnv(t)
char := createCharWithSkillPP(t, "Athletik", 3)
reqBody := map[string]interface{}{
"skill_name": "Athletik",
"amount": 1,
}
body, _ := json.Marshal(reqBody)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Set("userID", uint(4))
c.Params = gin.Params{{Key: "id", Value: fmt.Sprintf("%d", char.ID)}}
c.Request = httptest.NewRequest(http.MethodPost, "/", bytes.NewBuffer(body))
c.Request.Header.Set("Content-Type", "application/json")
UsePracticePoint(c)
assert.Equal(t, http.StatusOK, w.Code)
var resp PracticePointActionResponse
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &resp))
assert.True(t, resp.Success)
}
func TestUsePracticePointInsufficientPP(t *testing.T) {
setupPPTestEnv(t)
char := createCharWithSkillPP(t, "Athletik", 0)
reqBody := map[string]interface{}{
"skill_name": "Athletik",
"amount": 5,
}
body, _ := json.Marshal(reqBody)
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Set("userID", uint(4))
c.Params = gin.Params{{Key: "id", Value: fmt.Sprintf("%d", char.ID)}}
c.Request = httptest.NewRequest(http.MethodPost, "/", bytes.NewBuffer(body))
c.Request.Header.Set("Content-Type", "application/json")
UsePracticePoint(c)
assert.Equal(t, http.StatusBadRequest, w.Code)
}