Skip to content

Commit

Permalink
Add servers tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmacdonald committed Jun 21, 2024
1 parent a8eda88 commit d284ce4
Show file tree
Hide file tree
Showing 18 changed files with 267 additions and 184 deletions.
2 changes: 1 addition & 1 deletion frontend/src/api/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export const apiSaveServer = async (server_id: number, opts: SaveServerOpts) =>
};

export const apiGetServersAdmin = async (abortController?: AbortController) => {
const resp = await apiCall<Server[]>(`/api/servers_admin`, 'POST', undefined, abortController);
const resp = await apiCall<Server[]>(`/api/servers_admin`, 'GET', undefined, abortController);
return resp.map(transformTimeStampedDates).map((s) => {
s.token_created_on = parseDateTime(s.token_created_on as unknown as string);
return s;
Expand Down
2 changes: 1 addition & 1 deletion internal/auth/auth_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (u *auth) AuthServerMiddleWare() gin.HandlerFunc {
}

var server domain.Server
if errServer := u.servers.GetServerByPassword(ctx, reqAuthHeader, &server, false, false); errServer != nil {
if errServer := u.servers.GetByPassword(ctx, reqAuthHeader, &server, false, false); errServer != nil {
slog.Error("Failed to load server during auth", log.ErrAttr(errServer), slog.String("token", reqAuthHeader), slog.String("IP", ctx.ClientIP()))
ctx.AbortWithStatus(http.StatusUnauthorized)

Expand Down
2 changes: 1 addition & 1 deletion internal/demo/demo_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (d demoUsecase) GetDemos(ctx context.Context) ([]domain.DemoFile, error) {
}

func (d demoUsecase) CreateFromAsset(ctx context.Context, asset domain.Asset, serverID int) (*domain.DemoFile, error) {
_, errGetServer := d.servers.GetServer(ctx, serverID)
_, errGetServer := d.servers.Server(ctx, serverID)
if errGetServer != nil {
return nil, domain.ErrGetServer
}
Expand Down
6 changes: 3 additions & 3 deletions internal/discord/discord_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func (h discordService) makeOnSay() func(context.Context, *discordgo.Session, *d
msg := opts[domain.OptMessage].StringValue()

var server domain.Server
if err := h.servers.GetServerByName(ctx, serverName, &server, false, false); err != nil {
if err := h.servers.GetByName(ctx, serverName, &server, false, false); err != nil {
return nil, domain.ErrUnknownServer
}

Expand All @@ -386,7 +386,7 @@ func (h discordService) makeOnCSay() func(_ context.Context, _ *discordgo.Sessio
msg := opts[domain.OptMessage].StringValue()

var server domain.Server
if err := h.servers.GetServerByName(ctx, serverName, &server, false, false); err != nil {
if err := h.servers.GetByName(ctx, serverName, &server, false, false); err != nil {
return nil, domain.ErrUnknownServer
}

Expand Down Expand Up @@ -665,7 +665,7 @@ func (h discordService) makeOnFind() func(context.Context, *discordgo.Session, *
var found []domain.FoundPlayer

for _, player := range players {
server, errServer := h.servers.GetServer(ctx, player.ServerID)
server, errServer := h.servers.Server(ctx, player.ServerID)
if errServer != nil {
return nil, errors.Join(errServer, domain.ErrGetServer)
}
Expand Down
39 changes: 33 additions & 6 deletions internal/domain/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,40 @@ import (
"github.com/leighmacdonald/steamid/v4/steamid"
)

type RequestServerUpdate struct {
ServerID int `json:"server_id"`
ServerName string `json:"server_name"`
ServerNameShort string `json:"server_name_short"`
Host string `json:"host"`
Port int `json:"port"`
ReservedSlots int `json:"reserved_slots"`
Password string `json:"password"`
RCON string `json:"rcon"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
CC string `json:"cc"`
DefaultMap string `json:"default_map"`
Region string `json:"region"`
IsEnabled bool `json:"is_enabled"`
EnableStats bool `json:"enable_stats"`
LogSecret int `json:"log_secret"`
}

type ServerInfoSafe struct {
ServerNameLong string `json:"server_name_long"`
ServerName string `json:"server_name"`
ServerID int `json:"server_id"`
Colour string `json:"colour"`
}

type ServersUsecase interface {
GetServer(ctx context.Context, serverID int) (Server, error)
GetServerPermissions(ctx context.Context) ([]ServerPermission, error)
GetServers(ctx context.Context, filter ServerQueryFilter) ([]Server, int64, error)
GetServerByName(ctx context.Context, serverName string, server *Server, disabledOk bool, deletedOk bool) error
GetServerByPassword(ctx context.Context, serverPassword string, server *Server, disabledOk bool, deletedOk bool) error
SaveServer(ctx context.Context, server *Server) error
Server(ctx context.Context, serverID int) (Server, error)
ServerPermissions(ctx context.Context) ([]ServerPermission, error)
Servers(ctx context.Context, filter ServerQueryFilter) ([]Server, int64, error)
GetByName(ctx context.Context, serverName string, server *Server, disabledOk bool, deletedOk bool) error
GetByPassword(ctx context.Context, serverPassword string, server *Server, disabledOk bool, deletedOk bool) error
Save(ctx context.Context, req RequestServerUpdate) (Server, error)
Delete(ctx context.Context, serverID int) error
}

type ServersRepository interface {
Expand Down
4 changes: 4 additions & 0 deletions internal/httphelper/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ func HandleErrs(ctx *gin.Context, err error) {
HandleErrPermissionDenied(ctx)
case errors.Is(err, domain.ErrNoResult):
HandleErrNotFound(ctx)
case errors.Is(err, domain.ErrParamKeyMissing):
HandleErrBadRequest(ctx)
case errors.Is(err, domain.ErrInvalidParameter):
HandleErrBadRequest(ctx)
case errors.Is(err, domain.ErrBadRequest):
HandleErrBadRequest(ctx)
case errors.Is(err, domain.ErrDuplicate):
Expand Down
2 changes: 1 addition & 1 deletion internal/match/match_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (r *matchRepository) onMatchComplete(ctx context.Context, matchContext *act
matchContext.match.Title = server.Name
}

fullServer, err := r.servers.GetServer(ctx, server.ServerID)
fullServer, err := r.servers.Server(ctx, server.ServerID)
if err != nil {
return errors.Join(err, domain.ErrLoadServer)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/match/match_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (h matchHandler) onAPIPostMatchStart() gin.HandlerFunc {
return
}

server, errServer := h.servers.GetServer(ctx, serverID)
server, errServer := h.servers.Server(ctx, serverID)
if errServer != nil {
httphelper.ResponseErr(ctx, http.StatusInternalServerError, domain.ErrUnknownServerID)
slog.Error("Failed to get server", log.ErrAttr(errServer))
Expand Down
2 changes: 1 addition & 1 deletion internal/match/match_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (m matchUsecase) EndMatch(ctx context.Context, serverID int) (uuid.UUID, er
return matchID, domain.ErrLoadMatch
}

server, errServer := m.servers.GetServer(ctx, serverID)
server, errServer := m.servers.Server(ctx, serverID)
if errServer != nil {
return matchID, errors.Join(errServer, domain.ErrUnknownServer)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/network/scp.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (f SCPExecer) Start(ctx context.Context) {
}

func (f SCPExecer) update(ctx context.Context) error {
servers, _, errServers := f.serversUsecase.GetServers(ctx, domain.ServerQueryFilter{})
servers, _, errServers := f.serversUsecase.Servers(ctx, domain.ServerQueryFilter{})
if errServers != nil {
return errServers
}
Expand Down
3 changes: 0 additions & 3 deletions internal/servers/servers_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,10 @@ func (r *serversRepository) GetServerByPassword(ctx context.Context, serverPassw

// SaveServer updates or creates the server data in the database.
func (r *serversRepository) SaveServer(ctx context.Context, server *domain.Server) error {
server.UpdatedOn = time.Now()
if server.ServerID > 0 {
return r.updateServer(ctx, server)
}

server.CreatedOn = time.Now()

return r.insertServer(ctx, server)
}

Expand Down
Loading

0 comments on commit d284ce4

Please sign in to comment.