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)
|
||
|
|
}
|