generated from VLADIMIR/template
up
This commit is contained in:
@@ -271,7 +271,7 @@ func (s *server) SetScreenMode(ctx context.Context, req *proto.SetScreenModeReq)
|
||||
if !canManageScreens(ctx) {
|
||||
return nil, status.Error(codes.PermissionDenied, "permission denied")
|
||||
}
|
||||
if req == nil || req.GameID <= 0 || req.Mode == proto.ScreenMode_SCREEN_MODE_UNSPECIFIED {
|
||||
if req == nil || req.GameID <= 0 || !screen_service.ValidMode(int32(req.Mode)) {
|
||||
return nil, status.Error(codes.InvalidArgument, "некорректный режим экрана")
|
||||
}
|
||||
if _, err := s.gameService.GetGame(ctx, int(req.GameID)); err != nil {
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"evening_detective_server/internal/modules/processor_jwt"
|
||||
"evening_detective_server/proto"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
func TestSetScreenModeRejectsRemovedModes(t *testing.T) {
|
||||
ctx := processor_jwt.ContextWithClaims(context.Background(), &processor_jwt.JWTClaims{
|
||||
Roles: []string{"organizer"},
|
||||
})
|
||||
s := &server{}
|
||||
for _, mode := range []int32{3, 5} {
|
||||
_, err := s.SetScreenMode(ctx, &proto.SetScreenModeReq{
|
||||
GameID: 1,
|
||||
Mode: proto.ScreenMode(mode),
|
||||
})
|
||||
if status.Code(err) != codes.InvalidArgument {
|
||||
t.Errorf("SetScreenMode(%d) = %v, want InvalidArgument", mode, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -118,7 +118,7 @@ func normalizePlan(steps []Step, currentStep int) ([]Step, error) {
|
||||
}
|
||||
normalized := make([]Step, 0, len(steps))
|
||||
for _, step := range steps {
|
||||
if step.Mode <= 0 || step.Mode > 8 {
|
||||
if !screen_service.ValidMode(step.Mode) {
|
||||
return nil, ErrInvalidPlan
|
||||
}
|
||||
step.Text = strings.TrimSpace(step.Text)
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package screen_plan_service
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNormalizePlan(t *testing.T) {
|
||||
tests := []struct {
|
||||
@@ -16,6 +19,8 @@ func TestNormalizePlan(t *testing.T) {
|
||||
{Mode: 7, TimerDurationSeconds: 60},
|
||||
},
|
||||
},
|
||||
{name: "rejects removed intro mode", steps: []Step{{Mode: 3}}, wantErr: true},
|
||||
{name: "rejects removed conclusion mode", steps: []Step{{Mode: 5}}, wantErr: true},
|
||||
{
|
||||
name: "rejects empty text",
|
||||
steps: []Step{{Mode: 8}},
|
||||
@@ -36,6 +41,9 @@ func TestNormalizePlan(t *testing.T) {
|
||||
t.Fatalf("normalizePlan() error = %v, want error: %v", err, tt.wantErr)
|
||||
}
|
||||
if tt.wantErr {
|
||||
if !errors.Is(err, ErrInvalidPlan) {
|
||||
t.Fatalf("want ErrInvalidPlan, got %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if steps[0].Text != "Внимание" || steps[1].TimerLabel != "Перерыв" {
|
||||
|
||||
@@ -7,6 +7,9 @@ import (
|
||||
"evening_detective_server/internal/repos/screen_sessions_repo"
|
||||
)
|
||||
|
||||
// ErrInvalidMode indicates an unsupported display mode.
|
||||
var ErrInvalidMode = errors.New("некорректный режим экрана")
|
||||
|
||||
// ErrScreenNotFound indicates that a screen session was not found.
|
||||
var ErrScreenNotFound = errors.New("экран не найден")
|
||||
|
||||
@@ -47,8 +50,21 @@ func (s *ScreenService) ListByGame(ctx context.Context, gameID int) ([]*ScreenSe
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// ValidMode reports whether mode is a supported display mode.
|
||||
func ValidMode(mode int32) bool {
|
||||
switch mode {
|
||||
case 1, 2, 4, 6, 7, 8:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// SetMode updates the display mode for a game.
|
||||
func (s *ScreenService) SetMode(ctx context.Context, gameID int, mode int32) error {
|
||||
if !ValidMode(mode) {
|
||||
return ErrInvalidMode
|
||||
}
|
||||
return s.repo.SetMode(ctx, gameID, mode)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package screen_service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSetModeRejectsUnsupportedModes(t *testing.T) {
|
||||
s := NewScreenService(nil)
|
||||
for _, mode := range []int32{-1, 0, 3, 5, 9} {
|
||||
if err := s.SetMode(context.Background(), 1, mode); !errors.Is(err, ErrInvalidMode) {
|
||||
t.Errorf("SetMode(%d) = %v, want ErrInvalidMode", mode, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user