Skip to content

Commit

Permalink
Add test coverage for grief reports (#266)
Browse files Browse the repository at this point in the history
Not 100% due to unreachable switch cases, disabling those would require
adding ugly #pragma lines, so i didnt bother
  • Loading branch information
jvyden authored Nov 12, 2023
2 parents e5e4a36 + dc17606 commit 2d936d4
Show file tree
Hide file tree
Showing 7 changed files with 364 additions and 13 deletions.
7 changes: 6 additions & 1 deletion Refresh.GameServer/Database/GameDatabaseContext.Reports.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ public partial class GameDatabaseContext
public void AddGriefReport(GameReport report)
{
this.AddSequentialObject(report, () => {});
}
}

public DatabaseList<GameReport> GetGriefReports(int count, int skip)
{
return new DatabaseList<GameReport>(this._realm.All<GameReport>(), skip, count);
}
}
8 changes: 8 additions & 0 deletions Refresh.GameServer/Database/GameDatabaseContext.Users.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@ public void ResetUserPlanets(GameUser user)
user.VitaPlanetsHash = "0";
});
}

public void SetUserGriefReportRedirection(GameUser user, bool value)
{
this._realm.Write(() =>
{
user.RedirectGriefReportsToPhotos = value;
});
}

#if DEBUG
public void ForceUserTokenGame(Token token, TokenGame game)
Expand Down
3 changes: 2 additions & 1 deletion Refresh.GameServer/Endpoints/Game/ModerationEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ public string Filter(RequestContext context, CommandService commandService, stri
commandService.HandleCommand(command, database, user, token);
return "(Command)";
}
catch
catch(Exception ex)
{
context.Logger.LogWarning(BunkumCategory.Commands, $"Error running command {body}. ex {ex}");
//do nothing
}
}
Expand Down
29 changes: 20 additions & 9 deletions Refresh.GameServer/Endpoints/Game/ReportingEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,26 @@ public Response UploadReport(RequestContext context, GameDatabaseContext databas
{
GameLevel? level = database.GetLevelById(body.LevelId);

Size imageSize = token.TokenGame switch {
TokenGame.LittleBigPlanet1 => new Size(640, 360),
TokenGame.LittleBigPlanet2 => new Size(640, 360),
TokenGame.LittleBigPlanet3 => new Size(640, 360),
TokenGame.LittleBigPlanetVita => new Size(512, 290),
TokenGame.LittleBigPlanetPSP => new Size(480, 272),
_ => throw new ArgumentOutOfRangeException(nameof(token), $"Token game {token.TokenGame} is not allowed for grief upload!"),
};

Size imageSize;
switch (token.TokenGame)
{
case TokenGame.LittleBigPlanet1:
case TokenGame.LittleBigPlanet2:
case TokenGame.LittleBigPlanet3:
imageSize = new Size(640, 360);
break;
case TokenGame.LittleBigPlanetVita:
imageSize = new Size(512, 290);
break;
case TokenGame.LittleBigPlanetPSP:
imageSize = new Size(480, 272);
break;
case TokenGame.Website:
default:
context.Logger.LogWarning(BunkumCategory.Game, $"User {user} tried to upload grief report with token type {token.TokenGame}!");
return BadRequest;
}

//If the level is specified but its invalid, return BadRequest
if (body.LevelId != 0 && level == null)
return BadRequest;
Expand Down
4 changes: 2 additions & 2 deletions Refresh.GameServer/Services/CommandService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ public void HandleCommand(CommandInvocation command, GameDatabaseContext databas
}
case "griefphotoson":
{
user.RedirectGriefReportsToPhotos = true;
database.SetUserGriefReportRedirection(user, true);
break;
}
case "griefphotosoff":
{
user.RedirectGriefReportsToPhotos = false;
database.SetUserGriefReportRedirection(user, false);
break;
}
case "play":
Expand Down
16 changes: 16 additions & 0 deletions RefreshTests.GameServer/TestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public HttpClient GetAuthenticatedClient(TokenType type,
{
return this.GetAuthenticatedClient(type, out _, user, tokenExpirySeconds);
}

public HttpClient GetAuthenticatedClient(TokenType type, TokenGame game, TokenPlatform platform,
GameUser? user = null,
int tokenExpirySeconds = GameDatabaseContext.DefaultTokenExpirySeconds)
{
return this.GetAuthenticatedClient(type, game, platform, out _, user, tokenExpirySeconds);
}

public HttpClient GetAuthenticatedClient(TokenType type, out string tokenData,
GameUser? user = null,
Expand All @@ -56,6 +63,15 @@ public HttpClient GetAuthenticatedClient(TokenType type, out string tokenData,
_ => TokenPlatform.Website,
};

return this.GetAuthenticatedClient(type, game, platform, out tokenData, user, tokenExpirySeconds);
}

public HttpClient GetAuthenticatedClient(TokenType type, TokenGame game, TokenPlatform platform, out string tokenData,
GameUser? user = null,
int tokenExpirySeconds = GameDatabaseContext.DefaultTokenExpirySeconds)
{
user ??= this.CreateUser();

Token token = this.Database.GenerateTokenForUser(user, type, game, platform, tokenExpirySeconds);
tokenData = token.TokenData;

Expand Down
Loading

0 comments on commit 2d936d4

Please sign in to comment.