mirror of
https://github.com/h44z/wg-portal.git
synced 2026-11-20 20:53:40 +01:00
@@ -114,7 +114,7 @@ type Session interface {
|
||||
|
||||
type Validator interface {
|
||||
// Struct validates the given struct.
|
||||
Struct(s interface{}) error
|
||||
Struct(s any) error
|
||||
}
|
||||
|
||||
// endregion handler-interfaces
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -18,6 +19,24 @@ import (
|
||||
|
||||
// region test-helper
|
||||
|
||||
type syncEventBus struct {
|
||||
bus evbus.MessageBus
|
||||
onSubscribe func(topic string)
|
||||
}
|
||||
|
||||
func (s *syncEventBus) Subscribe(topic string, fn any) error {
|
||||
defer func() {
|
||||
if s.onSubscribe != nil {
|
||||
s.onSubscribe(topic)
|
||||
}
|
||||
}()
|
||||
return s.bus.Subscribe(topic, fn)
|
||||
}
|
||||
|
||||
func (s *syncEventBus) Unsubscribe(topic string, fn any) error {
|
||||
return s.bus.Unsubscribe(topic, fn)
|
||||
}
|
||||
|
||||
type websocketTestPeerService struct {
|
||||
peers map[domain.PeerIdentifier]*domain.Peer
|
||||
}
|
||||
@@ -39,8 +58,17 @@ func newTestWebsocketConnection(
|
||||
) (*websocket.Conn, func()) {
|
||||
t.Helper()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(2) // TopicPeerStatsUpdated and TopicInterfaceStatsUpdated
|
||||
wrappedBus := &syncEventBus{
|
||||
bus: bus,
|
||||
onSubscribe: func(topic string) {
|
||||
wg.Done()
|
||||
},
|
||||
}
|
||||
|
||||
cfg := &config.Config{}
|
||||
endpoint := NewWebsocketEndpoint(cfg, nil, bus, websocketTestPeerService{peers: peers})
|
||||
endpoint := NewWebsocketEndpoint(cfg, nil, wrappedBus, websocketTestPeerService{peers: peers})
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r = r.WithContext(domain.SetUserInfo(r.Context(), userInfo))
|
||||
@@ -55,6 +83,8 @@ func newTestWebsocketConnection(
|
||||
t.Fatalf("failed to dial websocket: %v", err)
|
||||
}
|
||||
|
||||
wg.Wait() // wait for eventbus to be ready
|
||||
|
||||
cleanup := func() {
|
||||
conn.Close()
|
||||
server.Close()
|
||||
|
||||
@@ -3,6 +3,7 @@ package handlers
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"slices"
|
||||
|
||||
"github.com/h44z/wg-portal/internal/app/api/core/request"
|
||||
"github.com/h44z/wg-portal/internal/app/api/core/respond"
|
||||
@@ -137,10 +138,8 @@ func UserHasScopes(session SessionData, scopes ...Scope) bool {
|
||||
}
|
||||
|
||||
// Check if admin scope is required
|
||||
for _, scope := range scopes {
|
||||
if scope == ScopeAdmin {
|
||||
return false
|
||||
}
|
||||
if slices.Contains(scopes, ScopeAdmin) {
|
||||
return false
|
||||
}
|
||||
|
||||
// For all other scopes, a logged-in user is sufficient (for now)
|
||||
|
||||
Reference in New Issue
Block a user