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
157 lines
3.9 KiB
Go
157 lines
3.9 KiB
Go
package importer
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"bamort/database"
|
|
"bamort/bmrt/models"
|
|
"bamort/router"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func setupExportTestEnv(t *testing.T) *models.Char {
|
|
t.Helper()
|
|
database.SetupTestDB(true)
|
|
t.Cleanup(database.ResetTestDB)
|
|
|
|
err := models.MigrateStructure()
|
|
require.NoError(t, err)
|
|
|
|
// Create test character
|
|
char := &models.Char{
|
|
BamortBase: models.BamortBase{Name: "Export Test Char"},
|
|
UserID: 4,
|
|
Rasse: "Mensch",
|
|
Typ: "Kr",
|
|
}
|
|
require.NoError(t, database.DB.Create(char).Error)
|
|
return char
|
|
}
|
|
|
|
func setupExportRouter() *gin.Engine {
|
|
r := gin.Default()
|
|
router.SetupGin(r)
|
|
protected := router.BaseRouterGrp(r)
|
|
RegisterRoutes(protected)
|
|
return r
|
|
}
|
|
|
|
func TestExportCharacterVTTHandler(t *testing.T) {
|
|
char := setupExportTestEnv(t)
|
|
|
|
r := setupExportRouter()
|
|
token := getAuthToken()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/importer/export/vtt/"+uintToStr(char.ID), nil)
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
var resp map[string]interface{}
|
|
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &resp))
|
|
assert.NotNil(t, resp)
|
|
}
|
|
|
|
func TestExportCharacterVTTHandlerNotFound(t *testing.T) {
|
|
setupExportTestEnv(t)
|
|
|
|
r := setupExportRouter()
|
|
token := getAuthToken()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/importer/export/vtt/99999", nil)
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
|
}
|
|
|
|
func TestExportCharacterVTTFileHandler(t *testing.T) {
|
|
char := setupExportTestEnv(t)
|
|
|
|
r := setupExportRouter()
|
|
token := getAuthToken()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/importer/export/vtt/"+uintToStr(char.ID)+"/file", nil)
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
contentDisposition := w.Header().Get("Content-Disposition")
|
|
assert.Contains(t, contentDisposition, "attachment")
|
|
}
|
|
|
|
func TestExportCharacterVTTFileHandlerNotFound(t *testing.T) {
|
|
setupExportTestEnv(t)
|
|
|
|
r := setupExportRouter()
|
|
token := getAuthToken()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/importer/export/vtt/99999/file", nil)
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
|
}
|
|
|
|
func TestExportCharacterCSVHandler(t *testing.T) {
|
|
char := setupExportTestEnv(t)
|
|
|
|
r := setupExportRouter()
|
|
token := getAuthToken()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/importer/export/csv/"+uintToStr(char.ID), nil)
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
contentDisposition := w.Header().Get("Content-Disposition")
|
|
assert.Contains(t, contentDisposition, "attachment")
|
|
}
|
|
|
|
func TestExportCharacterCSVHandlerNotFound(t *testing.T) {
|
|
setupExportTestEnv(t)
|
|
|
|
r := setupExportRouter()
|
|
token := getAuthToken()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/importer/export/csv/99999", nil)
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
|
}
|
|
|
|
func TestExportSpellsCSVHandler(t *testing.T) {
|
|
setupExportTestEnv(t)
|
|
|
|
r := setupExportRouter()
|
|
token := getAuthToken()
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/importer/export/spells/csv", nil)
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
w := httptest.NewRecorder()
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
// Should be a file attachment
|
|
contentDisposition := w.Header().Get("Content-Disposition")
|
|
assert.Contains(t, contentDisposition, "attachment")
|
|
}
|
|
|
|
func uintToStr(id uint) string {
|
|
return fmt.Sprintf("%d", id)
|
|
}
|