042a1d4773
* 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
198 lines
5.0 KiB
Go
198 lines
5.0 KiB
Go
package character
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"testing"
|
|
|
|
"bamort/database"
|
|
"bamort/bmrt/models"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func setupSharesTestEnv(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 createTestCharForShares(t *testing.T) *models.Char {
|
|
t.Helper()
|
|
char := &models.Char{
|
|
BamortBase: models.BamortBase{Name: "Share Test Char"},
|
|
UserID: 4,
|
|
Rasse: "Mensch",
|
|
Typ: "Kr",
|
|
}
|
|
require.NoError(t, database.DB.Create(char).Error)
|
|
return char
|
|
}
|
|
|
|
func TestGetCharacterSharesEmpty(t *testing.T) {
|
|
setupSharesTestEnv(t)
|
|
|
|
char := createTestCharForShares(t)
|
|
|
|
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)
|
|
|
|
GetCharacterShares(c)
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
body := w.Body.String()
|
|
assert.True(t, body == "null" || body == "[]", "Empty shares should return null or []")
|
|
}
|
|
|
|
func TestGetCharacterSharesNotOwner(t *testing.T) {
|
|
setupSharesTestEnv(t)
|
|
|
|
char := createTestCharForShares(t)
|
|
|
|
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.MethodGet, "/", nil)
|
|
|
|
GetCharacterShares(c)
|
|
|
|
assert.Equal(t, http.StatusForbidden, w.Code)
|
|
}
|
|
|
|
func TestGetCharacterSharesNotFound(t *testing.T) {
|
|
setupSharesTestEnv(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)
|
|
|
|
GetCharacterShares(c)
|
|
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
|
}
|
|
|
|
func TestUpdateCharacterShares(t *testing.T) {
|
|
setupSharesTestEnv(t)
|
|
|
|
char := createTestCharForShares(t)
|
|
|
|
// Share with user 1 (different from owner user 4)
|
|
reqBody := map[string]interface{}{
|
|
"user_ids": []uint{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.MethodPut, "/", bytes.NewBuffer(body))
|
|
c.Request.Header.Set("Content-Type", "application/json")
|
|
|
|
UpdateCharacterShares(c)
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
assert.Contains(t, w.Body.String(), "updated successfully")
|
|
|
|
// Verify the share was created
|
|
var count int64
|
|
database.DB.Model(&models.CharShare{}).Where("character_id = ? AND user_id = ?", char.ID, 1).Count(&count)
|
|
assert.Equal(t, int64(1), count)
|
|
}
|
|
|
|
func TestUpdateCharacterSharesClearAll(t *testing.T) {
|
|
setupSharesTestEnv(t)
|
|
|
|
char := createTestCharForShares(t)
|
|
|
|
// First add a share
|
|
share := &models.CharShare{
|
|
CharacterID: char.ID,
|
|
UserID: 1,
|
|
Permission: "read",
|
|
}
|
|
require.NoError(t, database.DB.Create(share).Error)
|
|
|
|
// Now clear all shares
|
|
reqBody := map[string]interface{}{
|
|
"user_ids": []uint{},
|
|
}
|
|
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.MethodPut, "/", bytes.NewBuffer(body))
|
|
c.Request.Header.Set("Content-Type", "application/json")
|
|
|
|
UpdateCharacterShares(c)
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
|
|
// Verify all shares removed
|
|
var count int64
|
|
database.DB.Model(&models.CharShare{}).Where("character_id = ?", char.ID).Count(&count)
|
|
assert.Equal(t, int64(0), count)
|
|
}
|
|
|
|
func TestUpdateCharacterSharesNotOwner(t *testing.T) {
|
|
setupSharesTestEnv(t)
|
|
|
|
char := createTestCharForShares(t)
|
|
|
|
reqBody := map[string]interface{}{"user_ids": []uint{1}}
|
|
body, _ := json.Marshal(reqBody)
|
|
|
|
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")
|
|
|
|
UpdateCharacterShares(c)
|
|
|
|
assert.Equal(t, http.StatusForbidden, w.Code)
|
|
}
|
|
|
|
func TestUpdateCharacterSharesInvalidBody(t *testing.T) {
|
|
setupSharesTestEnv(t)
|
|
|
|
char := createTestCharForShares(t)
|
|
|
|
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.NewBufferString("invalid json"))
|
|
c.Request.Header.Set("Content-Type", "application/json")
|
|
|
|
UpdateCharacterShares(c)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
|
}
|