This commit is contained in:
2026-10-11 14:59:51 +07:00
parent e3576db693
commit 5da08f6916
17 changed files with 1235 additions and 258 deletions
+2
View File
@@ -52,7 +52,9 @@ func (s *server) GetDevices(ctx context.Context, req *proto.GetDevicesReq) (*pro
if err != nil {
return nil, deviceErrorStatus(err)
}
wifi, stale := s.wifiService.Get(ctx)
return &proto.GetDevicesRsp{
Wifi: mapWiFi(wifi, stale),
Devices: mapVenueDevices(devices),
ServerTime: timestamppb.New(now),
}, nil
+27
View File
@@ -1,6 +1,7 @@
package app
import (
"evening_detective_server/internal/modules/cudy"
"evening_detective_server/internal/services/device_presence_service"
proto "evening_detective_server/proto"
@@ -51,3 +52,29 @@ func mapVenueDevices(devices []device_presence_service.Device) []*proto.VenueDev
}
return result
}
func mapWiFi(snapshot cudy.Snapshot, stale bool) *proto.VenueWiFi {
result := &proto.VenueWiFi{
Enabled: snapshot.Enabled,
Stale: stale,
HasError: snapshot.HasError,
Clients: make([]*proto.WiFiClient, 0, len(snapshot.Clients)),
}
if !snapshot.CollectedAt.IsZero() {
result.CollectedAt = timestamppb.New(snapshot.CollectedAt)
}
for _, client := range snapshot.Clients {
var signal *int32
if client.Signal != nil {
value := int32(*client.Signal)
signal = &value
}
result.Clients = append(result.Clients, &proto.WiFiClient{
IP: client.IP,
MAC: client.MAC,
Band: client.Band,
Signal: signal,
})
}
return result
}
+35
View File
@@ -2,15 +2,21 @@ package app
import (
"context"
"encoding/json"
"errors"
"os"
"path/filepath"
"strings"
"testing"
"time"
"evening_detective_server/internal/modules/cudy"
"evening_detective_server/internal/modules/processor_jwt"
"evening_detective_server/internal/services/device_presence_service"
"evening_detective_server/internal/services/game_service"
"evening_detective_server/internal/services/screen_service"
"evening_detective_server/internal/services/team_service"
"evening_detective_server/internal/services/wifi_service"
proto "evening_detective_server/proto"
"github.com/google/uuid"
@@ -97,3 +103,32 @@ func TestDeviceErrors(t *testing.T) {
}
}
}
func TestWiFiUsesExistingPresencePermissions(t *testing.T) {
path := filepath.Join(t.TempDir(), "state.json")
signal := -65
snapshot := cudy.Snapshot{Enabled: true, CollectedAt: time.Now().UTC(), Clients: []cudy.Client{{IP: "192.168.10.9", MAC: "AA:BB:CC:DD:EE:01", Band: "5 GHz", Signal: &signal}}}
data, err := json.Marshal(snapshot)
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, data, 0600); err != nil {
t.Fatal(err)
}
s := &server{devicePresenceService: device_presence_service.NewDevicePresenceService(true, nil, nil, nil), wifiService: wifi_service.New(path)}
for _, role := range []string{"organizer", "developer", "admin"} {
ctx := processor_jwt.ContextWithClaims(context.Background(), &processor_jwt.JWTClaims{Roles: []string{role}})
result, err := s.GetDevices(ctx, &proto.GetDevicesReq{})
if err != nil || !result.Wifi.Enabled || result.Wifi.Stale || len(result.Wifi.Clients) != 1 || result.Wifi.Clients[0].GetSignal() != -65 {
t.Fatalf("Wi-Fi for %s: %v, %v", role, result, err)
}
}
if _, err := s.GetDevices(context.Background(), &proto.GetDevicesReq{}); status.Code(err) != codes.PermissionDenied {
t.Fatal(err)
}
s.devicePresenceService = device_presence_service.NewDevicePresenceService(false, nil, nil, nil)
ctx := processor_jwt.ContextWithClaims(context.Background(), &processor_jwt.JWTClaims{Roles: []string{"developer"}})
if _, err := s.GetDevices(ctx, &proto.GetDevicesReq{}); status.Code(err) != codes.FailedPrecondition {
t.Fatal("prod exposes Wi-Fi", err)
}
}
+4
View File
@@ -21,6 +21,7 @@ import (
"evening_detective_server/internal/services/ticket_service"
"evening_detective_server/internal/services/ui_service"
"evening_detective_server/internal/services/users_service"
"evening_detective_server/internal/services/wifi_service"
proto "evening_detective_server/proto"
)
@@ -28,6 +29,7 @@ type server struct {
proto.UnsafeEveningDetectiveServerServer
devicePresenceService *device_presence_service.DevicePresenceService
wifiService *wifi_service.Service
usersService *users_service.UsersService
uiService *ui_service.UIService
taskService *task_service.TaskService
@@ -73,9 +75,11 @@ func NewServer(
gameResultsService *game_results_package_service.GameResultsPackageService,
gameResultsApplyService *game_results_package_service.GameResultsApplyService,
devicePresenceService *device_presence_service.DevicePresenceService,
wifiService *wifi_service.Service,
) proto.EveningDetectiveServerServer {
return &server{
devicePresenceService: devicePresenceService,
wifiService: wifiService,
gameImportService: gameImportService,
gameResultsService: gameResultsService,
gameResultsApplyService: gameResultsApplyService,