add server api get and update service control endpoints
This commit is contained in:
@@ -1,547 +0,0 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"acc-server-manager/local/controller"
|
||||
"acc-server-manager/local/model"
|
||||
"acc-server-manager/local/service"
|
||||
"acc-server-manager/local/utl/common"
|
||||
"acc-server-manager/tests"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestConfigController_GetConfig_Success(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create mock services
|
||||
mockConfigService := &MockConfigService{}
|
||||
mockApiService := &MockApiService{}
|
||||
|
||||
// Setup expected response
|
||||
expectedConfig := &model.Configuration{
|
||||
UdpPort: model.IntString(9231),
|
||||
TcpPort: model.IntString(9232),
|
||||
MaxConnections: model.IntString(30),
|
||||
LanDiscovery: model.IntString(1),
|
||||
RegisterToLobby: model.IntString(1),
|
||||
ConfigVersion: model.IntString(1),
|
||||
}
|
||||
mockConfigService.getConfigResponse = expectedConfig
|
||||
|
||||
// Create Fiber app with controller
|
||||
app := fiber.New()
|
||||
routeGroups := &common.RouteGroups{
|
||||
Config: app.Group("/config"),
|
||||
}
|
||||
mockAuth := &MockAuthMiddleware{}
|
||||
controller.NewConfigController(mockConfigService, routeGroups, mockApiService, mockAuth)
|
||||
|
||||
// Create test request
|
||||
serverID := uuid.New().String()
|
||||
req := httptest.NewRequest("GET", "/config/configuration.json", nil)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// Mock authentication
|
||||
mockAuth.authenticated = true
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 200, resp.StatusCode)
|
||||
|
||||
// Parse response
|
||||
var response model.Configuration
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
tests.AssertNoError(t, err)
|
||||
err = json.Unmarshal(body, &response)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Verify response
|
||||
tests.AssertEqual(t, expectedConfig.UdpPort, response.UdpPort)
|
||||
tests.AssertEqual(t, expectedConfig.TcpPort, response.TcpPort)
|
||||
tests.AssertEqual(t, expectedConfig.MaxConnections, response.MaxConnections)
|
||||
}
|
||||
|
||||
func TestConfigController_GetConfig_Unauthorized(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create mock services
|
||||
mockConfigService := &MockConfigService{}
|
||||
mockApiService := &MockApiService{}
|
||||
|
||||
// Create Fiber app with controller
|
||||
app := fiber.New()
|
||||
routeGroups := &common.RouteGroups{
|
||||
Config: app.Group("/config"),
|
||||
}
|
||||
mockAuth := &MockAuthMiddleware{}
|
||||
controller.NewConfigController(mockConfigService, routeGroups, mockApiService, mockAuth)
|
||||
|
||||
// Create test request
|
||||
req := httptest.NewRequest("GET", "/config/configuration.json", nil)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// Mock authentication failure
|
||||
mockAuth.authenticated = false
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 401, resp.StatusCode)
|
||||
}
|
||||
|
||||
func TestConfigController_GetConfig_ServiceError(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create mock services
|
||||
mockConfigService := &MockConfigService{}
|
||||
mockApiService := &MockApiService{}
|
||||
|
||||
// Setup service error
|
||||
mockConfigService.shouldFailGet = true
|
||||
|
||||
// Create Fiber app with controller
|
||||
app := fiber.New()
|
||||
routeGroups := &common.RouteGroups{
|
||||
Config: app.Group("/config"),
|
||||
}
|
||||
mockAuth := &MockAuthMiddleware{}
|
||||
controller.NewConfigController(mockConfigService, routeGroups, mockApiService, mockAuth)
|
||||
|
||||
// Create test request
|
||||
req := httptest.NewRequest("GET", "/config/configuration.json", nil)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// Mock authentication
|
||||
mockAuth.authenticated = true
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 500, resp.StatusCode)
|
||||
}
|
||||
|
||||
func TestConfigController_UpdateConfig_Success(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create mock services
|
||||
mockConfigService := &MockConfigService{}
|
||||
mockApiService := &MockApiService{}
|
||||
|
||||
// Setup expected response
|
||||
expectedConfig := &model.Config{
|
||||
ID: uuid.New(),
|
||||
ServerID: uuid.New(),
|
||||
ConfigFile: "configuration.json",
|
||||
OldConfig: `{"udpPort": "9231"}`,
|
||||
NewConfig: `{"udpPort": "9999"}`,
|
||||
}
|
||||
mockConfigService.updateConfigResponse = expectedConfig
|
||||
|
||||
// Create Fiber app with controller
|
||||
app := fiber.New()
|
||||
routeGroups := &common.RouteGroups{
|
||||
Config: app.Group("/config/:id"),
|
||||
}
|
||||
mockAuth := &MockAuthMiddleware{}
|
||||
controller.NewConfigController(mockConfigService, routeGroups, mockApiService, mockAuth)
|
||||
|
||||
// Prepare request body
|
||||
updateData := map[string]interface{}{
|
||||
"udpPort": "9999",
|
||||
"tcpPort": "10000",
|
||||
}
|
||||
bodyBytes, err := json.Marshal(updateData)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Create test request
|
||||
serverID := uuid.New().String()
|
||||
req := httptest.NewRequest("PUT", "/config/"+serverID+"/configuration.json", bytes.NewReader(bodyBytes))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// Mock authentication
|
||||
mockAuth.authenticated = true
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 200, resp.StatusCode)
|
||||
|
||||
// Parse response
|
||||
var response model.Config
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
tests.AssertNoError(t, err)
|
||||
err = json.Unmarshal(body, &response)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Verify response
|
||||
tests.AssertEqual(t, expectedConfig.ConfigFile, response.ConfigFile)
|
||||
tests.AssertEqual(t, expectedConfig.OldConfig, response.OldConfig)
|
||||
tests.AssertEqual(t, expectedConfig.NewConfig, response.NewConfig)
|
||||
|
||||
// Verify service was called with correct data
|
||||
tests.AssertEqual(t, true, mockConfigService.updateConfigCalled)
|
||||
}
|
||||
|
||||
func TestConfigController_UpdateConfig_WithRestart(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create mock services
|
||||
mockConfigService := &MockConfigService{}
|
||||
mockApiService := &MockApiService{}
|
||||
|
||||
// Setup expected response
|
||||
expectedConfig := &model.Config{
|
||||
ID: uuid.New(),
|
||||
ServerID: uuid.New(),
|
||||
ConfigFile: "configuration.json",
|
||||
OldConfig: `{"udpPort": "9231"}`,
|
||||
NewConfig: `{"udpPort": "9999"}`,
|
||||
}
|
||||
mockConfigService.updateConfigResponse = expectedConfig
|
||||
|
||||
// Create Fiber app with controller
|
||||
app := fiber.New()
|
||||
routeGroups := &common.RouteGroups{
|
||||
Config: app.Group("/config/:id"),
|
||||
}
|
||||
mockAuth := &MockAuthMiddleware{}
|
||||
controller.NewConfigController(mockConfigService, routeGroups, mockApiService, mockAuth)
|
||||
|
||||
// Prepare request body
|
||||
updateData := map[string]interface{}{
|
||||
"udpPort": "9999",
|
||||
}
|
||||
bodyBytes, err := json.Marshal(updateData)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Create test request with restart parameter
|
||||
serverID := uuid.New().String()
|
||||
req := httptest.NewRequest("PUT", "/config/"+serverID+"/configuration.json?restart=true", bytes.NewReader(bodyBytes))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// Mock authentication
|
||||
mockAuth.authenticated = true
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 200, resp.StatusCode)
|
||||
|
||||
// Verify both services were called
|
||||
tests.AssertEqual(t, true, mockConfigService.updateConfigCalled)
|
||||
tests.AssertEqual(t, true, mockApiService.restartServerCalled)
|
||||
}
|
||||
|
||||
func TestConfigController_UpdateConfig_InvalidUUID(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create mock services
|
||||
mockConfigService := &MockConfigService{}
|
||||
mockApiService := &MockApiService{}
|
||||
|
||||
// Create Fiber app with controller
|
||||
app := fiber.New()
|
||||
routeGroups := &common.RouteGroups{
|
||||
Config: app.Group("/config/:id"),
|
||||
}
|
||||
mockAuth := &MockAuthMiddleware{}
|
||||
controller.NewConfigController(mockConfigService, routeGroups, mockApiService, mockAuth)
|
||||
|
||||
// Prepare request body
|
||||
updateData := map[string]interface{}{
|
||||
"udpPort": "9999",
|
||||
}
|
||||
bodyBytes, err := json.Marshal(updateData)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Create test request with invalid UUID
|
||||
req := httptest.NewRequest("PUT", "/config/invalid-uuid/configuration.json", bytes.NewReader(bodyBytes))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// Mock authentication
|
||||
mockAuth.authenticated = true
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 400, resp.StatusCode)
|
||||
|
||||
// Verify service was not called
|
||||
tests.AssertEqual(t, false, mockConfigService.updateConfigCalled)
|
||||
}
|
||||
|
||||
func TestConfigController_UpdateConfig_InvalidJSON(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create mock services
|
||||
mockConfigService := &MockConfigService{}
|
||||
mockApiService := &MockApiService{}
|
||||
|
||||
// Create Fiber app with controller
|
||||
app := fiber.New()
|
||||
routeGroups := &common.RouteGroups{
|
||||
Config: app.Group("/config/:id"),
|
||||
}
|
||||
mockAuth := &MockAuthMiddleware{}
|
||||
controller.NewConfigController(mockConfigService, routeGroups, mockApiService, mockAuth)
|
||||
|
||||
// Create test request with invalid JSON
|
||||
serverID := uuid.New().String()
|
||||
req := httptest.NewRequest("PUT", "/config/"+serverID+"/configuration.json", bytes.NewReader([]byte("invalid json")))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// Mock authentication
|
||||
mockAuth.authenticated = true
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 400, resp.StatusCode)
|
||||
|
||||
// Verify service was not called
|
||||
tests.AssertEqual(t, false, mockConfigService.updateConfigCalled)
|
||||
}
|
||||
|
||||
func TestConfigController_UpdateConfig_ServiceError(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create mock services
|
||||
mockConfigService := &MockConfigService{}
|
||||
mockApiService := &MockApiService{}
|
||||
|
||||
// Setup service error
|
||||
mockConfigService.shouldFailUpdate = true
|
||||
|
||||
// Create Fiber app with controller
|
||||
app := fiber.New()
|
||||
routeGroups := &common.RouteGroups{
|
||||
Config: app.Group("/config/:id"),
|
||||
}
|
||||
mockAuth := &MockAuthMiddleware{}
|
||||
controller.NewConfigController(mockConfigService, routeGroups, mockApiService, mockAuth)
|
||||
|
||||
// Prepare request body
|
||||
updateData := map[string]interface{}{
|
||||
"udpPort": "9999",
|
||||
}
|
||||
bodyBytes, err := json.Marshal(updateData)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Create test request
|
||||
serverID := uuid.New().String()
|
||||
req := httptest.NewRequest("PUT", "/config/"+serverID+"/configuration.json", bytes.NewReader(bodyBytes))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// Mock authentication
|
||||
mockAuth.authenticated = true
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 500, resp.StatusCode)
|
||||
|
||||
// Verify service was called
|
||||
tests.AssertEqual(t, true, mockConfigService.updateConfigCalled)
|
||||
}
|
||||
|
||||
func TestConfigController_GetConfigs_Success(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create mock services
|
||||
mockConfigService := &MockConfigService{}
|
||||
mockApiService := &MockApiService{}
|
||||
|
||||
// Setup expected response
|
||||
expectedConfigs := &model.Configurations{
|
||||
Configuration: model.Configuration{
|
||||
UdpPort: model.IntString(9231),
|
||||
TcpPort: model.IntString(9232),
|
||||
},
|
||||
Settings: model.ServerSettings{
|
||||
ServerName: "Test Server",
|
||||
},
|
||||
Event: model.EventConfig{
|
||||
Track: "spa",
|
||||
},
|
||||
}
|
||||
mockConfigService.getConfigsResponse = expectedConfigs
|
||||
|
||||
// Create Fiber app with controller
|
||||
app := fiber.New()
|
||||
routeGroups := &common.RouteGroups{
|
||||
Config: app.Group("/config"),
|
||||
}
|
||||
mockAuth := &MockAuthMiddleware{}
|
||||
controller.NewConfigController(mockConfigService, routeGroups, mockApiService, mockAuth)
|
||||
|
||||
// Create test request
|
||||
req := httptest.NewRequest("GET", "/config/", nil)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// Mock authentication
|
||||
mockAuth.authenticated = true
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 200, resp.StatusCode)
|
||||
|
||||
// Parse response
|
||||
var response model.Configurations
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
tests.AssertNoError(t, err)
|
||||
err = json.Unmarshal(body, &response)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Verify response
|
||||
tests.AssertEqual(t, expectedConfigs.Configuration.UdpPort, response.Configuration.UdpPort)
|
||||
tests.AssertEqual(t, expectedConfigs.Settings.ServerName, response.Settings.ServerName)
|
||||
tests.AssertEqual(t, expectedConfigs.Event.Track, response.Event.Track)
|
||||
}
|
||||
|
||||
// MockConfigService implements the ConfigService interface for testing
|
||||
type MockConfigService struct {
|
||||
getConfigResponse interface{}
|
||||
getConfigsResponse *model.Configurations
|
||||
updateConfigResponse *model.Config
|
||||
shouldFailGet bool
|
||||
shouldFailUpdate bool
|
||||
getConfigCalled bool
|
||||
getConfigsCalled bool
|
||||
updateConfigCalled bool
|
||||
}
|
||||
|
||||
func (m *MockConfigService) GetConfig(c *fiber.Ctx) (interface{}, error) {
|
||||
m.getConfigCalled = true
|
||||
if m.shouldFailGet {
|
||||
return nil, tests.ErrorForTesting("service error")
|
||||
}
|
||||
return m.getConfigResponse, nil
|
||||
}
|
||||
|
||||
func (m *MockConfigService) GetConfigs(c *fiber.Ctx) (*model.Configurations, error) {
|
||||
m.getConfigsCalled = true
|
||||
if m.shouldFailGet {
|
||||
return nil, tests.ErrorForTesting("service error")
|
||||
}
|
||||
return m.getConfigsResponse, nil
|
||||
}
|
||||
|
||||
func (m *MockConfigService) UpdateConfig(c *fiber.Ctx, body *map[string]interface{}) (*model.Config, error) {
|
||||
m.updateConfigCalled = true
|
||||
if m.shouldFailUpdate {
|
||||
return nil, tests.ErrorForTesting("service error")
|
||||
}
|
||||
return m.updateConfigResponse, nil
|
||||
}
|
||||
|
||||
// Additional methods that might be needed by the service interface
|
||||
func (m *MockConfigService) LoadConfigs(server *model.Server) (*model.Configurations, error) {
|
||||
return m.getConfigsResponse, nil
|
||||
}
|
||||
|
||||
func (m *MockConfigService) GetConfiguration(server *model.Server) (*model.Configuration, error) {
|
||||
if config, ok := m.getConfigResponse.(*model.Configuration); ok {
|
||||
return config, nil
|
||||
}
|
||||
return nil, tests.ErrorForTesting("type assertion failed")
|
||||
}
|
||||
|
||||
func (m *MockConfigService) GetEventConfig(server *model.Server) (*model.EventConfig, error) {
|
||||
if config, ok := m.getConfigResponse.(*model.EventConfig); ok {
|
||||
return config, nil
|
||||
}
|
||||
return nil, tests.ErrorForTesting("type assertion failed")
|
||||
}
|
||||
|
||||
func (m *MockConfigService) SaveConfiguration(server *model.Server, config *model.Configuration) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockConfigService) SetServerService(serverService *service.ServerService) {
|
||||
// Mock implementation
|
||||
}
|
||||
|
||||
// MockApiService implements the ApiService interface for testing
|
||||
type MockApiService struct {
|
||||
restartServerCalled bool
|
||||
shouldFailRestart bool
|
||||
}
|
||||
|
||||
func (m *MockApiService) ApiRestartServer(c *fiber.Ctx) (interface{}, error) {
|
||||
m.restartServerCalled = true
|
||||
if m.shouldFailRestart {
|
||||
return nil, tests.ErrorForTesting("restart failed")
|
||||
}
|
||||
return fiber.Map{"message": "server restarted"}, nil
|
||||
}
|
||||
|
||||
// MockAuthMiddleware implements the AuthMiddleware interface for testing
|
||||
type MockAuthMiddleware struct {
|
||||
authenticated bool
|
||||
hasPermission bool
|
||||
}
|
||||
|
||||
func (m *MockAuthMiddleware) Authenticate(c *fiber.Ctx) error {
|
||||
if !m.authenticated {
|
||||
return c.Status(401).JSON(fiber.Map{"error": "Unauthorized"})
|
||||
}
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
func (m *MockAuthMiddleware) HasPermission(permission string) fiber.Handler {
|
||||
return func(c *fiber.Ctx) error {
|
||||
if !m.authenticated {
|
||||
return c.Status(401).JSON(fiber.Map{"error": "Unauthorized"})
|
||||
}
|
||||
if !m.hasPermission {
|
||||
return c.Status(403).JSON(fiber.Map{"error": "Forbidden"})
|
||||
}
|
||||
return c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockAuthMiddleware) AuthRateLimit() fiber.Handler {
|
||||
return func(c *fiber.Ctx) error {
|
||||
return c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockAuthMiddleware) RequireHTTPS() fiber.Handler {
|
||||
return func(c *fiber.Ctx) error {
|
||||
return c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockAuthMiddleware) InvalidateUserPermissions(userID string) {
|
||||
// Mock implementation
|
||||
}
|
||||
|
||||
func (m *MockAuthMiddleware) InvalidateAllUserPermissions() {
|
||||
// Mock implementation
|
||||
}
|
||||
@@ -14,6 +14,11 @@ import (
|
||||
)
|
||||
|
||||
func TestController_JSONParsing_Success(t *testing.T) {
|
||||
// Setup environment and test helper
|
||||
tests.SetTestEnv()
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Test basic JSON parsing functionality
|
||||
app := fiber.New()
|
||||
|
||||
@@ -55,6 +60,11 @@ func TestController_JSONParsing_Success(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestController_JSONParsing_InvalidJSON(t *testing.T) {
|
||||
// Setup environment and test helper
|
||||
tests.SetTestEnv()
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Test handling of invalid JSON
|
||||
app := fiber.New()
|
||||
|
||||
@@ -87,6 +97,11 @@ func TestController_JSONParsing_InvalidJSON(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestController_UUIDValidation_Success(t *testing.T) {
|
||||
// Setup environment and test helper
|
||||
tests.SetTestEnv()
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Test UUID parameter validation
|
||||
app := fiber.New()
|
||||
|
||||
@@ -123,6 +138,11 @@ func TestController_UUIDValidation_Success(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestController_UUIDValidation_InvalidUUID(t *testing.T) {
|
||||
// Setup environment and test helper
|
||||
tests.SetTestEnv()
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Test handling of invalid UUID
|
||||
app := fiber.New()
|
||||
|
||||
@@ -157,6 +177,11 @@ func TestController_UUIDValidation_InvalidUUID(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestController_QueryParameters_Success(t *testing.T) {
|
||||
// Setup environment and test helper
|
||||
tests.SetTestEnv()
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Test query parameter handling
|
||||
app := fiber.New()
|
||||
|
||||
@@ -194,6 +219,11 @@ func TestController_QueryParameters_Success(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestController_HTTPMethods_Success(t *testing.T) {
|
||||
// Setup environment and test helper
|
||||
tests.SetTestEnv()
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Test different HTTP methods
|
||||
app := fiber.New()
|
||||
|
||||
@@ -249,6 +279,11 @@ func TestController_HTTPMethods_Success(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestController_ErrorHandling_StatusCodes(t *testing.T) {
|
||||
// Setup environment and test helper
|
||||
tests.SetTestEnv()
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Test different error status codes
|
||||
app := fiber.New()
|
||||
|
||||
@@ -293,6 +328,11 @@ func TestController_ErrorHandling_StatusCodes(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestController_ConfigurationModel_JSONSerialization(t *testing.T) {
|
||||
// Setup environment and test helper
|
||||
tests.SetTestEnv()
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Test Configuration model JSON serialization
|
||||
app := fiber.New()
|
||||
|
||||
@@ -333,6 +373,11 @@ func TestController_ConfigurationModel_JSONSerialization(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestController_UserModel_JSONSerialization(t *testing.T) {
|
||||
// Setup environment and test helper
|
||||
tests.SetTestEnv()
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Test User model JSON serialization (password should be hidden)
|
||||
app := fiber.New()
|
||||
|
||||
@@ -370,6 +415,11 @@ func TestController_UserModel_JSONSerialization(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestController_MiddlewareChaining_Success(t *testing.T) {
|
||||
// Setup environment and test helper
|
||||
tests.SetTestEnv()
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Test middleware chaining
|
||||
app := fiber.New()
|
||||
|
||||
|
||||
@@ -1,598 +0,0 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"acc-server-manager/local/controller"
|
||||
"acc-server-manager/local/model"
|
||||
"acc-server-manager/local/service"
|
||||
"acc-server-manager/local/utl/common"
|
||||
"acc-server-manager/tests"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestMembershipController_Login_Success(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create mock service
|
||||
mockMembershipService := &MockMembershipService{
|
||||
loginResponse: "mock-jwt-token-12345",
|
||||
}
|
||||
|
||||
// Create Fiber app with controller
|
||||
app := fiber.New()
|
||||
routeGroups := &common.RouteGroups{
|
||||
Auth: app.Group("/auth"),
|
||||
Membership: app.Group("/membership"),
|
||||
}
|
||||
mockAuth := &MockAuthMiddleware{}
|
||||
controller.NewMembershipController(mockMembershipService, mockAuth, routeGroups)
|
||||
|
||||
// Prepare request body
|
||||
loginData := map[string]string{
|
||||
"username": "testuser",
|
||||
"password": "password123",
|
||||
}
|
||||
bodyBytes, err := json.Marshal(loginData)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Create test request
|
||||
req := httptest.NewRequest("POST", "/auth/login", bytes.NewReader(bodyBytes))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 200, resp.StatusCode)
|
||||
|
||||
// Parse response
|
||||
var response map[string]string
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
tests.AssertNoError(t, err)
|
||||
err = json.Unmarshal(body, &response)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Verify response
|
||||
tests.AssertEqual(t, "mock-jwt-token-12345", response["token"])
|
||||
tests.AssertEqual(t, true, mockMembershipService.loginCalled)
|
||||
}
|
||||
|
||||
func TestMembershipController_Login_InvalidCredentials(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create mock service with login failure
|
||||
mockMembershipService := &MockMembershipService{
|
||||
shouldFailLogin: true,
|
||||
}
|
||||
|
||||
// Create Fiber app with controller
|
||||
app := fiber.New()
|
||||
routeGroups := &common.RouteGroups{
|
||||
Auth: app.Group("/auth"),
|
||||
Membership: app.Group("/membership"),
|
||||
}
|
||||
mockAuth := &MockAuthMiddleware{}
|
||||
controller.NewMembershipController(mockMembershipService, mockAuth, routeGroups)
|
||||
|
||||
// Prepare request body
|
||||
loginData := map[string]string{
|
||||
"username": "baduser",
|
||||
"password": "wrongpassword",
|
||||
}
|
||||
bodyBytes, err := json.Marshal(loginData)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Create test request
|
||||
req := httptest.NewRequest("POST", "/auth/login", bytes.NewReader(bodyBytes))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 401, resp.StatusCode)
|
||||
|
||||
// Verify service was called
|
||||
tests.AssertEqual(t, true, mockMembershipService.loginCalled)
|
||||
}
|
||||
|
||||
func TestMembershipController_Login_InvalidJSON(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create mock service
|
||||
mockMembershipService := &MockMembershipService{}
|
||||
|
||||
// Create Fiber app with controller
|
||||
app := fiber.New()
|
||||
routeGroups := &common.RouteGroups{
|
||||
Auth: app.Group("/auth"),
|
||||
Membership: app.Group("/membership"),
|
||||
}
|
||||
mockAuth := &MockAuthMiddleware{}
|
||||
controller.NewMembershipController(mockMembershipService, mockAuth, routeGroups)
|
||||
|
||||
// Create test request with invalid JSON
|
||||
req := httptest.NewRequest("POST", "/auth/login", bytes.NewReader([]byte("invalid json")))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 400, resp.StatusCode)
|
||||
|
||||
// Verify service was not called
|
||||
tests.AssertEqual(t, false, mockMembershipService.loginCalled)
|
||||
}
|
||||
|
||||
func TestMembershipController_CreateUser_Success(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create expected user response
|
||||
expectedUser := &model.User{
|
||||
ID: uuid.New(),
|
||||
Username: "newuser",
|
||||
RoleID: uuid.New(),
|
||||
}
|
||||
|
||||
// Create mock service
|
||||
mockMembershipService := &MockMembershipService{
|
||||
createUserResponse: expectedUser,
|
||||
}
|
||||
|
||||
// Create Fiber app with controller
|
||||
app := fiber.New()
|
||||
routeGroups := &common.RouteGroups{
|
||||
Auth: app.Group("/auth"),
|
||||
Membership: app.Group("/membership"),
|
||||
}
|
||||
mockAuth := &MockAuthMiddleware{authenticated: true}
|
||||
controller.NewMembershipController(mockMembershipService, mockAuth, routeGroups)
|
||||
|
||||
// Prepare request body
|
||||
createUserData := map[string]string{
|
||||
"username": "newuser",
|
||||
"password": "password123",
|
||||
"role": "User",
|
||||
}
|
||||
bodyBytes, err := json.Marshal(createUserData)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Create test request
|
||||
req := httptest.NewRequest("POST", "/membership/", bytes.NewReader(bodyBytes))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 200, resp.StatusCode)
|
||||
|
||||
// Parse response
|
||||
var response model.User
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
tests.AssertNoError(t, err)
|
||||
err = json.Unmarshal(body, &response)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Verify response
|
||||
tests.AssertEqual(t, expectedUser.ID, response.ID)
|
||||
tests.AssertEqual(t, expectedUser.Username, response.Username)
|
||||
tests.AssertEqual(t, true, mockMembershipService.createUserCalled)
|
||||
}
|
||||
|
||||
func TestMembershipController_CreateUser_Unauthorized(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create mock service
|
||||
mockMembershipService := &MockMembershipService{}
|
||||
|
||||
// Create Fiber app with controller
|
||||
app := fiber.New()
|
||||
routeGroups := &common.RouteGroups{
|
||||
Auth: app.Group("/auth"),
|
||||
Membership: app.Group("/membership"),
|
||||
}
|
||||
mockAuth := &MockAuthMiddleware{authenticated: false}
|
||||
controller.NewMembershipController(mockMembershipService, mockAuth, routeGroups)
|
||||
|
||||
// Prepare request body
|
||||
createUserData := map[string]string{
|
||||
"username": "newuser",
|
||||
"password": "password123",
|
||||
"role": "User",
|
||||
}
|
||||
bodyBytes, err := json.Marshal(createUserData)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Create test request
|
||||
req := httptest.NewRequest("POST", "/membership/", bytes.NewReader(bodyBytes))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 401, resp.StatusCode)
|
||||
|
||||
// Verify service was not called
|
||||
tests.AssertEqual(t, false, mockMembershipService.createUserCalled)
|
||||
}
|
||||
|
||||
func TestMembershipController_CreateUser_Forbidden(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create mock service
|
||||
mockMembershipService := &MockMembershipService{}
|
||||
|
||||
// Create Fiber app with controller
|
||||
app := fiber.New()
|
||||
routeGroups := &common.RouteGroups{
|
||||
Auth: app.Group("/auth"),
|
||||
Membership: app.Group("/membership"),
|
||||
}
|
||||
mockAuth := &MockAuthMiddleware{
|
||||
authenticated: true,
|
||||
hasPermission: false, // User doesn't have MembershipCreate permission
|
||||
}
|
||||
controller.NewMembershipController(mockMembershipService, mockAuth, routeGroups)
|
||||
|
||||
// Prepare request body
|
||||
createUserData := map[string]string{
|
||||
"username": "newuser",
|
||||
"password": "password123",
|
||||
"role": "User",
|
||||
}
|
||||
bodyBytes, err := json.Marshal(createUserData)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Create test request
|
||||
req := httptest.NewRequest("POST", "/membership/", bytes.NewReader(bodyBytes))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 403, resp.StatusCode)
|
||||
|
||||
// Verify service was not called
|
||||
tests.AssertEqual(t, false, mockMembershipService.createUserCalled)
|
||||
}
|
||||
|
||||
func TestMembershipController_ListUsers_Success(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create expected users response
|
||||
expectedUsers := []*model.User{
|
||||
{
|
||||
ID: uuid.New(),
|
||||
Username: "user1",
|
||||
RoleID: uuid.New(),
|
||||
},
|
||||
{
|
||||
ID: uuid.New(),
|
||||
Username: "user2",
|
||||
RoleID: uuid.New(),
|
||||
},
|
||||
}
|
||||
|
||||
// Create mock service
|
||||
mockMembershipService := &MockMembershipService{
|
||||
listUsersResponse: expectedUsers,
|
||||
}
|
||||
|
||||
// Create Fiber app with controller
|
||||
app := fiber.New()
|
||||
routeGroups := &common.RouteGroups{
|
||||
Auth: app.Group("/auth"),
|
||||
Membership: app.Group("/membership"),
|
||||
}
|
||||
mockAuth := &MockAuthMiddleware{
|
||||
authenticated: true,
|
||||
hasPermission: true,
|
||||
}
|
||||
controller.NewMembershipController(mockMembershipService, mockAuth, routeGroups)
|
||||
|
||||
// Create test request
|
||||
req := httptest.NewRequest("GET", "/membership/", nil)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 200, resp.StatusCode)
|
||||
|
||||
// Parse response
|
||||
var response []*model.User
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
tests.AssertNoError(t, err)
|
||||
err = json.Unmarshal(body, &response)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Verify response
|
||||
tests.AssertEqual(t, 2, len(response))
|
||||
tests.AssertEqual(t, expectedUsers[0].Username, response[0].Username)
|
||||
tests.AssertEqual(t, expectedUsers[1].Username, response[1].Username)
|
||||
tests.AssertEqual(t, true, mockMembershipService.listUsersCalled)
|
||||
}
|
||||
|
||||
func TestMembershipController_GetUser_Success(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create expected user response
|
||||
userID := uuid.New()
|
||||
expectedUser := &model.User{
|
||||
ID: userID,
|
||||
Username: "testuser",
|
||||
RoleID: uuid.New(),
|
||||
}
|
||||
|
||||
// Create mock service
|
||||
mockMembershipService := &MockMembershipService{
|
||||
getUserResponse: expectedUser,
|
||||
}
|
||||
|
||||
// Create Fiber app with controller
|
||||
app := fiber.New()
|
||||
routeGroups := &common.RouteGroups{
|
||||
Auth: app.Group("/auth"),
|
||||
Membership: app.Group("/membership"),
|
||||
}
|
||||
mockAuth := &MockAuthMiddleware{
|
||||
authenticated: true,
|
||||
hasPermission: true,
|
||||
}
|
||||
controller.NewMembershipController(mockMembershipService, mockAuth, routeGroups)
|
||||
|
||||
// Create test request
|
||||
req := httptest.NewRequest("GET", "/membership/"+userID.String(), nil)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 200, resp.StatusCode)
|
||||
|
||||
// Parse response
|
||||
var response model.User
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
tests.AssertNoError(t, err)
|
||||
err = json.Unmarshal(body, &response)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Verify response
|
||||
tests.AssertEqual(t, expectedUser.ID, response.ID)
|
||||
tests.AssertEqual(t, expectedUser.Username, response.Username)
|
||||
tests.AssertEqual(t, true, mockMembershipService.getUserCalled)
|
||||
}
|
||||
|
||||
func TestMembershipController_GetUser_InvalidUUID(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create mock service
|
||||
mockMembershipService := &MockMembershipService{}
|
||||
|
||||
// Create Fiber app with controller
|
||||
app := fiber.New()
|
||||
routeGroups := &common.RouteGroups{
|
||||
Auth: app.Group("/auth"),
|
||||
Membership: app.Group("/membership"),
|
||||
}
|
||||
mockAuth := &MockAuthMiddleware{
|
||||
authenticated: true,
|
||||
hasPermission: true,
|
||||
}
|
||||
controller.NewMembershipController(mockMembershipService, mockAuth, routeGroups)
|
||||
|
||||
// Create test request with invalid UUID
|
||||
req := httptest.NewRequest("GET", "/membership/invalid-uuid", nil)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 400, resp.StatusCode)
|
||||
|
||||
// Verify service was not called
|
||||
tests.AssertEqual(t, false, mockMembershipService.getUserCalled)
|
||||
}
|
||||
|
||||
func TestMembershipController_DeleteUser_Success(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create mock service
|
||||
mockMembershipService := &MockMembershipService{}
|
||||
|
||||
// Create Fiber app with controller
|
||||
app := fiber.New()
|
||||
routeGroups := &common.RouteGroups{
|
||||
Auth: app.Group("/auth"),
|
||||
Membership: app.Group("/membership"),
|
||||
}
|
||||
mockAuth := &MockAuthMiddleware{
|
||||
authenticated: true,
|
||||
hasPermission: true,
|
||||
}
|
||||
controller.NewMembershipController(mockMembershipService, mockAuth, routeGroups)
|
||||
|
||||
// Create test request
|
||||
userID := uuid.New().String()
|
||||
req := httptest.NewRequest("DELETE", "/membership/"+userID, nil)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 200, resp.StatusCode)
|
||||
|
||||
// Verify service was called
|
||||
tests.AssertEqual(t, true, mockMembershipService.deleteUserCalled)
|
||||
}
|
||||
|
||||
func TestMembershipController_GetMe_Success(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create expected user response
|
||||
expectedUser := &model.User{
|
||||
ID: uuid.New(),
|
||||
Username: "currentuser",
|
||||
RoleID: uuid.New(),
|
||||
}
|
||||
|
||||
// Create mock service
|
||||
mockMembershipService := &MockMembershipService{
|
||||
getUserWithPermissionsResponse: expectedUser,
|
||||
}
|
||||
|
||||
// Create Fiber app with controller
|
||||
app := fiber.New()
|
||||
routeGroups := &common.RouteGroups{
|
||||
Auth: app.Group("/auth"),
|
||||
Membership: app.Group("/membership"),
|
||||
}
|
||||
mockAuth := &MockAuthMiddleware{authenticated: true}
|
||||
controller.NewMembershipController(mockMembershipService, mockAuth, routeGroups)
|
||||
|
||||
// Create test request
|
||||
req := httptest.NewRequest("GET", "/auth/me", nil)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 200, resp.StatusCode)
|
||||
|
||||
// Parse response
|
||||
var response model.User
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
tests.AssertNoError(t, err)
|
||||
err = json.Unmarshal(body, &response)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Verify response
|
||||
tests.AssertEqual(t, expectedUser.ID, response.ID)
|
||||
tests.AssertEqual(t, expectedUser.Username, response.Username)
|
||||
}
|
||||
|
||||
// MockMembershipService implements the MembershipService interface for testing
|
||||
type MockMembershipService struct {
|
||||
loginResponse string
|
||||
createUserResponse *model.User
|
||||
listUsersResponse []*model.User
|
||||
getUserResponse *model.User
|
||||
getUserWithPermissionsResponse *model.User
|
||||
getRolesResponse []*model.Role
|
||||
shouldFailLogin bool
|
||||
shouldFailCreateUser bool
|
||||
shouldFailListUsers bool
|
||||
shouldFailGetUser bool
|
||||
shouldFailGetUserWithPermissions bool
|
||||
shouldFailDeleteUser bool
|
||||
shouldFailUpdateUser bool
|
||||
shouldFailGetRoles bool
|
||||
loginCalled bool
|
||||
createUserCalled bool
|
||||
listUsersCalled bool
|
||||
getUserCalled bool
|
||||
getUserWithPermissionsCalled bool
|
||||
deleteUserCalled bool
|
||||
updateUserCalled bool
|
||||
getRolesCalled bool
|
||||
}
|
||||
|
||||
func (m *MockMembershipService) Login(ctx context.Context, username, password string) (string, error) {
|
||||
m.loginCalled = true
|
||||
if m.shouldFailLogin {
|
||||
return "", tests.ErrorForTesting("invalid credentials")
|
||||
}
|
||||
return m.loginResponse, nil
|
||||
}
|
||||
|
||||
func (m *MockMembershipService) CreateUser(ctx context.Context, username, password, roleName string) (*model.User, error) {
|
||||
m.createUserCalled = true
|
||||
if m.shouldFailCreateUser {
|
||||
return nil, tests.ErrorForTesting("failed to create user")
|
||||
}
|
||||
return m.createUserResponse, nil
|
||||
}
|
||||
|
||||
func (m *MockMembershipService) ListUsers(ctx context.Context) ([]*model.User, error) {
|
||||
m.listUsersCalled = true
|
||||
if m.shouldFailListUsers {
|
||||
return nil, tests.ErrorForTesting("failed to list users")
|
||||
}
|
||||
return m.listUsersResponse, nil
|
||||
}
|
||||
|
||||
func (m *MockMembershipService) GetUser(ctx context.Context, userID uuid.UUID) (*model.User, error) {
|
||||
m.getUserCalled = true
|
||||
if m.shouldFailGetUser {
|
||||
return nil, tests.ErrorForTesting("user not found")
|
||||
}
|
||||
return m.getUserResponse, nil
|
||||
}
|
||||
|
||||
func (m *MockMembershipService) GetUserWithPermissions(ctx context.Context, userID string) (*model.User, error) {
|
||||
m.getUserWithPermissionsCalled = true
|
||||
if m.shouldFailGetUserWithPermissions {
|
||||
return nil, tests.ErrorForTesting("user not found")
|
||||
}
|
||||
return m.getUserWithPermissionsResponse, nil
|
||||
}
|
||||
|
||||
func (m *MockMembershipService) DeleteUser(ctx context.Context, userID uuid.UUID) error {
|
||||
m.deleteUserCalled = true
|
||||
if m.shouldFailDeleteUser {
|
||||
return tests.ErrorForTesting("failed to delete user")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockMembershipService) UpdateUser(ctx context.Context, userID uuid.UUID, updates map[string]interface{}) (*model.User, error) {
|
||||
m.updateUserCalled = true
|
||||
if m.shouldFailUpdateUser {
|
||||
return nil, tests.ErrorForTesting("failed to update user")
|
||||
}
|
||||
return m.getUserResponse, nil
|
||||
}
|
||||
|
||||
func (m *MockMembershipService) GetRoles(ctx context.Context) ([]*model.Role, error) {
|
||||
m.getRolesCalled = true
|
||||
if m.shouldFailGetRoles {
|
||||
return nil, tests.ErrorForTesting("failed to get roles")
|
||||
}
|
||||
return m.getRolesResponse, nil
|
||||
}
|
||||
|
||||
func (m *MockMembershipService) SetCacheInvalidator(invalidator service.CacheInvalidator) {
|
||||
// Mock implementation
|
||||
}
|
||||
|
||||
func (m *MockMembershipService) SetupInitialData(ctx context.Context) error {
|
||||
// Mock implementation - no-op for testing
|
||||
return nil
|
||||
}
|
||||
@@ -1,684 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"acc-server-manager/local/middleware"
|
||||
"acc-server-manager/local/model"
|
||||
"acc-server-manager/local/service"
|
||||
"acc-server-manager/local/utl/cache"
|
||||
"acc-server-manager/local/utl/jwt"
|
||||
"acc-server-manager/tests"
|
||||
"context"
|
||||
"errors"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
jwtLib "github.com/golang-jwt/jwt/v4"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestAuthMiddleware_Authenticate_Success(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create test user
|
||||
user := &model.User{
|
||||
ID: uuid.New(),
|
||||
Username: "testuser",
|
||||
Password: "password123",
|
||||
RoleID: uuid.New(),
|
||||
Role: model.Role{
|
||||
ID: uuid.New(),
|
||||
Name: "User",
|
||||
Permissions: []model.Permission{
|
||||
{Name: "read", Description: "Read permission"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Create mock membership service
|
||||
mockMembershipService := &MockMembershipService{
|
||||
users: map[string]*model.User{
|
||||
user.ID.String(): user,
|
||||
},
|
||||
}
|
||||
|
||||
// Create cache and auth middleware
|
||||
cache := cache.NewInMemoryCache()
|
||||
authMiddleware := middleware.NewAuthMiddleware(mockMembershipService, cache)
|
||||
|
||||
// Generate valid JWT
|
||||
token, err := jwt.GenerateToken(user)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Create Fiber app for testing
|
||||
app := fiber.New()
|
||||
app.Use(authMiddleware.Authenticate)
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
return c.JSON(fiber.Map{"message": "success"})
|
||||
})
|
||||
|
||||
// Create test request with valid token
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 200, resp.StatusCode)
|
||||
}
|
||||
|
||||
func TestAuthMiddleware_Authenticate_MissingToken(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create cache and auth middleware
|
||||
cache := cache.NewInMemoryCache()
|
||||
mockMembershipService := &MockMembershipService{}
|
||||
authMiddleware := middleware.NewAuthMiddleware(mockMembershipService, cache)
|
||||
|
||||
// Create Fiber app for testing
|
||||
app := fiber.New()
|
||||
app.Use(authMiddleware.Authenticate)
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
return c.JSON(fiber.Map{"message": "success"})
|
||||
})
|
||||
|
||||
// Create test request without token
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 401, resp.StatusCode)
|
||||
}
|
||||
|
||||
func TestAuthMiddleware_Authenticate_InvalidToken(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create cache and auth middleware
|
||||
cache := cache.NewInMemoryCache()
|
||||
mockMembershipService := &MockMembershipService{}
|
||||
authMiddleware := middleware.NewAuthMiddleware(mockMembershipService, cache)
|
||||
|
||||
// Create Fiber app for testing
|
||||
app := fiber.New()
|
||||
app.Use(authMiddleware.Authenticate)
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
return c.JSON(fiber.Map{"message": "success"})
|
||||
})
|
||||
|
||||
// Create test request with invalid token
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
req.Header.Set("Authorization", "Bearer invalid-token")
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 401, resp.StatusCode)
|
||||
}
|
||||
|
||||
func TestAuthMiddleware_Authenticate_MalformedHeader(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create cache and auth middleware
|
||||
cache := cache.NewInMemoryCache()
|
||||
mockMembershipService := &MockMembershipService{}
|
||||
authMiddleware := middleware.NewAuthMiddleware(mockMembershipService, cache)
|
||||
|
||||
// Create Fiber app for testing
|
||||
app := fiber.New()
|
||||
app.Use(authMiddleware.Authenticate)
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
return c.JSON(fiber.Map{"message": "success"})
|
||||
})
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
header string
|
||||
}{
|
||||
{"Missing Bearer", "invalid-token"},
|
||||
{"Extra parts", "Bearer token1 token2"},
|
||||
{"Wrong prefix", "Basic token"},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
req.Header.Set("Authorization", tc.header)
|
||||
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 401, resp.StatusCode)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthMiddleware_Authenticate_ExpiredToken(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create test user
|
||||
user := &model.User{
|
||||
ID: uuid.New(),
|
||||
Username: "testuser",
|
||||
Password: "password123",
|
||||
RoleID: uuid.New(),
|
||||
}
|
||||
|
||||
// Create mock membership service
|
||||
mockMembershipService := &MockMembershipService{
|
||||
users: map[string]*model.User{
|
||||
user.ID.String(): user,
|
||||
},
|
||||
}
|
||||
|
||||
// Create cache and auth middleware
|
||||
cache := cache.NewInMemoryCache()
|
||||
authMiddleware := middleware.NewAuthMiddleware(mockMembershipService, cache)
|
||||
|
||||
// Generate token with very short expiration (simulate expired token)
|
||||
claims := &jwt.Claims{
|
||||
UserID: user.ID.String(),
|
||||
RegisteredClaims: jwtLib.RegisteredClaims{
|
||||
ExpiresAt: jwtLib.NewNumericDate(time.Now().Add(-1 * time.Hour)), // Expired
|
||||
},
|
||||
}
|
||||
token := jwtLib.NewWithClaims(jwtLib.SigningMethodHS256, claims)
|
||||
tokenString, err := token.SignedString(jwt.SecretKey)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Create Fiber app for testing
|
||||
app := fiber.New()
|
||||
app.Use(authMiddleware.Authenticate)
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
return c.JSON(fiber.Map{"message": "success"})
|
||||
})
|
||||
|
||||
// Create test request with expired token
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+tokenString)
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 401, resp.StatusCode)
|
||||
}
|
||||
|
||||
func TestAuthMiddleware_HasPermission_Success(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create test user with permissions
|
||||
user := &model.User{
|
||||
ID: uuid.New(),
|
||||
Username: "testuser",
|
||||
Password: "password123",
|
||||
RoleID: uuid.New(),
|
||||
Role: model.Role{
|
||||
ID: uuid.New(),
|
||||
Name: "User",
|
||||
Permissions: []model.Permission{
|
||||
{Name: "read", Description: "Read permission"},
|
||||
{Name: "write", Description: "Write permission"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Create mock membership service
|
||||
mockMembershipService := &MockMembershipService{
|
||||
users: map[string]*model.User{
|
||||
user.ID.String(): user,
|
||||
},
|
||||
}
|
||||
|
||||
// Create cache and auth middleware
|
||||
cache := cache.NewInMemoryCache()
|
||||
authMiddleware := middleware.NewAuthMiddleware(mockMembershipService, cache)
|
||||
|
||||
// Generate valid JWT
|
||||
token, err := jwt.GenerateToken(user)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Create Fiber app for testing
|
||||
app := fiber.New()
|
||||
app.Use(authMiddleware.Authenticate)
|
||||
app.Use(authMiddleware.HasPermission("read"))
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
return c.JSON(fiber.Map{"message": "success"})
|
||||
})
|
||||
|
||||
// Create test request with valid token
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 200, resp.StatusCode)
|
||||
}
|
||||
|
||||
func TestAuthMiddleware_HasPermission_Forbidden(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create test user without required permission
|
||||
user := &model.User{
|
||||
ID: uuid.New(),
|
||||
Username: "testuser",
|
||||
Password: "password123",
|
||||
RoleID: uuid.New(),
|
||||
Role: model.Role{
|
||||
ID: uuid.New(),
|
||||
Name: "User",
|
||||
Permissions: []model.Permission{
|
||||
{Name: "read", Description: "Read permission"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Create mock membership service
|
||||
mockMembershipService := &MockMembershipService{
|
||||
users: map[string]*model.User{
|
||||
user.ID.String(): user,
|
||||
},
|
||||
}
|
||||
|
||||
// Create cache and auth middleware
|
||||
cache := cache.NewInMemoryCache()
|
||||
authMiddleware := middleware.NewAuthMiddleware(mockMembershipService, cache)
|
||||
|
||||
// Generate valid JWT
|
||||
token, err := jwt.GenerateToken(user)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Create Fiber app for testing
|
||||
app := fiber.New()
|
||||
app.Use(authMiddleware.Authenticate)
|
||||
app.Use(authMiddleware.HasPermission("admin")) // User doesn't have admin permission
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
return c.JSON(fiber.Map{"message": "success"})
|
||||
})
|
||||
|
||||
// Create test request with valid token
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 403, resp.StatusCode)
|
||||
}
|
||||
|
||||
func TestAuthMiddleware_HasPermission_SuperAdmin(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create test user with Super Admin role
|
||||
user := &model.User{
|
||||
ID: uuid.New(),
|
||||
Username: "superadmin",
|
||||
Password: "password123",
|
||||
RoleID: uuid.New(),
|
||||
Role: model.Role{
|
||||
ID: uuid.New(),
|
||||
Name: "Super Admin",
|
||||
Permissions: []model.Permission{
|
||||
{Name: "basic", Description: "Basic permission"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Create mock membership service
|
||||
mockMembershipService := &MockMembershipService{
|
||||
users: map[string]*model.User{
|
||||
user.ID.String(): user,
|
||||
},
|
||||
}
|
||||
|
||||
// Create cache and auth middleware
|
||||
cache := cache.NewInMemoryCache()
|
||||
authMiddleware := middleware.NewAuthMiddleware(mockMembershipService, cache)
|
||||
|
||||
// Generate valid JWT
|
||||
token, err := jwt.GenerateToken(user)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Create Fiber app for testing
|
||||
app := fiber.New()
|
||||
app.Use(authMiddleware.Authenticate)
|
||||
app.Use(authMiddleware.HasPermission("any-permission")) // Super Admin has all permissions
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
return c.JSON(fiber.Map{"message": "success"})
|
||||
})
|
||||
|
||||
// Create test request with valid token
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 200, resp.StatusCode)
|
||||
}
|
||||
|
||||
func TestAuthMiddleware_HasPermission_Admin(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create test user with Admin role
|
||||
user := &model.User{
|
||||
ID: uuid.New(),
|
||||
Username: "admin",
|
||||
Password: "password123",
|
||||
RoleID: uuid.New(),
|
||||
Role: model.Role{
|
||||
ID: uuid.New(),
|
||||
Name: "Admin",
|
||||
Permissions: []model.Permission{
|
||||
{Name: "basic", Description: "Basic permission"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Create mock membership service
|
||||
mockMembershipService := &MockMembershipService{
|
||||
users: map[string]*model.User{
|
||||
user.ID.String(): user,
|
||||
},
|
||||
}
|
||||
|
||||
// Create cache and auth middleware
|
||||
cache := cache.NewInMemoryCache()
|
||||
authMiddleware := middleware.NewAuthMiddleware(mockMembershipService, cache)
|
||||
|
||||
// Generate valid JWT
|
||||
token, err := jwt.GenerateToken(user)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Create Fiber app for testing
|
||||
app := fiber.New()
|
||||
app.Use(authMiddleware.Authenticate)
|
||||
app.Use(authMiddleware.HasPermission("any-permission")) // Admin has all permissions
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
return c.JSON(fiber.Map{"message": "success"})
|
||||
})
|
||||
|
||||
// Create test request with valid token
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 200, resp.StatusCode)
|
||||
}
|
||||
|
||||
func TestAuthMiddleware_HasPermission_NoUserInContext(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create cache and auth middleware
|
||||
cache := cache.NewInMemoryCache()
|
||||
mockMembershipService := &MockMembershipService{}
|
||||
authMiddleware := middleware.NewAuthMiddleware(mockMembershipService, cache)
|
||||
|
||||
// Create Fiber app for testing (skip authentication middleware)
|
||||
app := fiber.New()
|
||||
app.Use(authMiddleware.HasPermission("read")) // No user in context
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
return c.JSON(fiber.Map{"message": "success"})
|
||||
})
|
||||
|
||||
// Create test request
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 401, resp.StatusCode)
|
||||
}
|
||||
|
||||
func TestAuthMiddleware_UserCaching(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create test user
|
||||
user := &model.User{
|
||||
ID: uuid.New(),
|
||||
Username: "testuser",
|
||||
Password: "password123",
|
||||
RoleID: uuid.New(),
|
||||
Role: model.Role{
|
||||
ID: uuid.New(),
|
||||
Name: "User",
|
||||
Permissions: []model.Permission{
|
||||
{Name: "read", Description: "Read permission"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Create mock membership service that tracks calls
|
||||
mockMembershipService := &MockMembershipService{
|
||||
users: map[string]*model.User{
|
||||
user.ID.String(): user,
|
||||
},
|
||||
getUserCallCount: 0,
|
||||
}
|
||||
|
||||
// Create cache and auth middleware
|
||||
cache := cache.NewInMemoryCache()
|
||||
authMiddleware := middleware.NewAuthMiddleware(mockMembershipService, cache)
|
||||
|
||||
// Generate valid JWT
|
||||
token, err := jwt.GenerateToken(user)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Create Fiber app for testing
|
||||
app := fiber.New()
|
||||
app.Use(authMiddleware.Authenticate)
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
return c.JSON(fiber.Map{"message": "success"})
|
||||
})
|
||||
|
||||
// Create test request
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
// First request - should call database
|
||||
resp1, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 200, resp1.StatusCode)
|
||||
tests.AssertEqual(t, 1, mockMembershipService.getUserCallCount)
|
||||
|
||||
// Second request - should use cache
|
||||
resp2, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 200, resp2.StatusCode)
|
||||
tests.AssertEqual(t, 1, mockMembershipService.getUserCallCount) // Should still be 1 (cached)
|
||||
}
|
||||
|
||||
func TestAuthMiddleware_CacheInvalidation(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create test user
|
||||
user := &model.User{
|
||||
ID: uuid.New(),
|
||||
Username: "testuser",
|
||||
Password: "password123",
|
||||
RoleID: uuid.New(),
|
||||
Role: model.Role{
|
||||
ID: uuid.New(),
|
||||
Name: "User",
|
||||
Permissions: []model.Permission{
|
||||
{Name: "read", Description: "Read permission"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Create mock membership service
|
||||
mockMembershipService := &MockMembershipService{
|
||||
users: map[string]*model.User{
|
||||
user.ID.String(): user,
|
||||
},
|
||||
getUserCallCount: 0,
|
||||
}
|
||||
|
||||
// Create cache and auth middleware
|
||||
cache := cache.NewInMemoryCache()
|
||||
authMiddleware := middleware.NewAuthMiddleware(mockMembershipService, cache)
|
||||
|
||||
// Generate valid JWT
|
||||
token, err := jwt.GenerateToken(user)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Create Fiber app for testing
|
||||
app := fiber.New()
|
||||
app.Use(authMiddleware.Authenticate)
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
return c.JSON(fiber.Map{"message": "success"})
|
||||
})
|
||||
|
||||
// Create test request
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
// First request - should call database
|
||||
resp1, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 200, resp1.StatusCode)
|
||||
tests.AssertEqual(t, 1, mockMembershipService.getUserCallCount)
|
||||
|
||||
// Invalidate cache
|
||||
authMiddleware.InvalidateUserPermissions(user.ID.String())
|
||||
|
||||
// Second request - should call database again due to cache invalidation
|
||||
resp2, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 200, resp2.StatusCode)
|
||||
tests.AssertEqual(t, 2, mockMembershipService.getUserCallCount) // Should be 2 (cache invalidated)
|
||||
}
|
||||
|
||||
func TestAuthMiddleware_UserNotFound(t *testing.T) {
|
||||
// Setup
|
||||
helper := tests.NewTestHelper(t)
|
||||
defer helper.Cleanup()
|
||||
|
||||
// Create test user for token generation
|
||||
user := &model.User{
|
||||
ID: uuid.New(),
|
||||
Username: "testuser",
|
||||
Password: "password123",
|
||||
RoleID: uuid.New(),
|
||||
}
|
||||
|
||||
// Create mock membership service without the user (user not found scenario)
|
||||
mockMembershipService := &MockMembershipService{
|
||||
users: map[string]*model.User{}, // Empty - user not found
|
||||
}
|
||||
|
||||
// Create cache and auth middleware
|
||||
cache := cache.NewInMemoryCache()
|
||||
authMiddleware := middleware.NewAuthMiddleware(mockMembershipService, cache)
|
||||
|
||||
// Generate valid JWT for non-existent user
|
||||
token, err := jwt.GenerateToken(user)
|
||||
tests.AssertNoError(t, err)
|
||||
|
||||
// Create Fiber app for testing
|
||||
app := fiber.New()
|
||||
app.Use(authMiddleware.Authenticate)
|
||||
app.Get("/test", func(c *fiber.Ctx) error {
|
||||
return c.JSON(fiber.Map{"message": "success"})
|
||||
})
|
||||
|
||||
// Create test request with valid token but non-existent user
|
||||
req := httptest.NewRequest("GET", "/test", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
// Execute request
|
||||
resp, err := app.Test(req)
|
||||
tests.AssertNoError(t, err)
|
||||
tests.AssertEqual(t, 401, resp.StatusCode)
|
||||
}
|
||||
|
||||
// MockMembershipService implements the MembershipService interface for testing
|
||||
type MockMembershipService struct {
|
||||
users map[string]*model.User
|
||||
getUserCallCount int
|
||||
shouldFailGet bool
|
||||
}
|
||||
|
||||
func (m *MockMembershipService) GetUserWithPermissions(ctx context.Context, userID string) (*model.User, error) {
|
||||
m.getUserCallCount++
|
||||
if m.shouldFailGet {
|
||||
return nil, errors.New("database error")
|
||||
}
|
||||
user, exists := m.users[userID]
|
||||
if !exists {
|
||||
return nil, errors.New("user not found")
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (m *MockMembershipService) SetCacheInvalidator(invalidator service.CacheInvalidator) {
|
||||
// Mock implementation
|
||||
}
|
||||
|
||||
func (m *MockMembershipService) Login(ctx context.Context, username, password string) (string, error) {
|
||||
for _, user := range m.users {
|
||||
if user.Username == username {
|
||||
if err := user.VerifyPassword(password); err == nil {
|
||||
return jwt.GenerateToken(user)
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", errors.New("invalid credentials")
|
||||
}
|
||||
|
||||
func (m *MockMembershipService) CreateUser(ctx context.Context, username, password, roleName string) (*model.User, error) {
|
||||
user := &model.User{
|
||||
ID: uuid.New(),
|
||||
Username: username,
|
||||
Password: password,
|
||||
RoleID: uuid.New(),
|
||||
}
|
||||
m.users[user.ID.String()] = user
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (m *MockMembershipService) ListUsers(ctx context.Context) ([]*model.User, error) {
|
||||
users := make([]*model.User, 0, len(m.users))
|
||||
for _, user := range m.users {
|
||||
users = append(users, user)
|
||||
}
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (m *MockMembershipService) GetUser(ctx context.Context, userID uuid.UUID) (*model.User, error) {
|
||||
user, exists := m.users[userID.String()]
|
||||
if !exists {
|
||||
return nil, errors.New("user not found")
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (m *MockMembershipService) SetShouldFailGet(shouldFail bool) {
|
||||
m.shouldFailGet = shouldFail
|
||||
}
|
||||
Reference in New Issue
Block a user