Files
bamort/backend/bmrt/character/image_handler_test.go
T
Bardioc26 042a1d4773 Learncost frontend (#42)
* introduced central package  registry by package init function
* dynamic registration of routes, model, migrations and initializers.
* setting a docker compose project name to prevent shutdown of other containers with the same (composer)name
* ai documentation
* app template
* 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.
* adopting agent instructions for a more consistent coding style
* added desired module layout and debugging information
* Fix All Failing tests All failing tests are fixed now that makes the refactoring more easy since all tests must pass
* restored routes for maintenance
* added common translations
* added new tests for API Endpoint
* Merge branch 'separate_business_logic'
* added lern and skill improvement cost editing
* Set Docker image tag when building to prevent rebuild when nothing has changed
* add and remove PP for Weaponskill fixed
* add and remove PP for same named skills fixed
* add new task
2026-05-01 18:15:31 +02:00

103 lines
2.7 KiB
Go

package character
import (
"bamort/database"
"bamort/bmrt/models"
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func TestUpdateCharacterImage(t *testing.T) {
setupTestEnvironment(t)
database.SetupTestDB()
t.Cleanup(database.ResetTestDB)
router := gin.Default()
protected := router.Group("/api")
protected.Use(func(c *gin.Context) {
c.Set("userID", uint(1))
c.Next()
})
RegisterRoutes(protected)
// Get existing character
var char models.Char
err := char.FirstID("18")
assert.NoError(t, err, "Test character 18 should exist")
// Prepare image data
imageData := "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
requestBody := map[string]string{
"image": imageData,
}
jsonData, _ := json.Marshal(requestBody)
// Update character image
req, _ := http.NewRequest("PUT", "/api/characters/18/image", bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, "Should successfully update image")
// Verify image was saved
var updatedChar models.Char
err = updatedChar.FirstID("18")
assert.NoError(t, err)
assert.Equal(t, imageData, updatedChar.Image, "Image should be updated in database")
}
func TestUpdateCharacterImageInvalidID(t *testing.T) {
setupTestEnvironment(t)
database.SetupTestDB()
t.Cleanup(database.ResetTestDB)
router := gin.Default()
protected := router.Group("/api")
protected.Use(func(c *gin.Context) {
c.Set("userID", uint(1))
c.Next()
})
RegisterRoutes(protected)
requestBody := map[string]string{
"image": "data:image/png;base64,test",
}
jsonData, _ := json.Marshal(requestBody)
req, _ := http.NewRequest("PUT", "/api/characters/99999/image", bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusNotFound, w.Code, "Should return 404 for non-existent character")
}
func TestUpdateCharacterImageInvalidData(t *testing.T) {
setupTestEnvironment(t)
database.SetupTestDB()
t.Cleanup(database.ResetTestDB)
router := gin.Default()
protected := router.Group("/api")
protected.Use(func(c *gin.Context) {
c.Set("userID", uint(1))
c.Next()
})
RegisterRoutes(protected)
req, _ := http.NewRequest("PUT", "/api/characters/18/image", bytes.NewBuffer([]byte("invalid json")))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusBadRequest, w.Code, "Should return 400 for invalid JSON")
}