Skip to content

Commit

Permalink
Tests: Add 100% test coverage for CommentEndpoints (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
jvyden authored Nov 10, 2023
2 parents 785262b + d201c4d commit f48fd58
Show file tree
Hide file tree
Showing 2 changed files with 312 additions and 0 deletions.
158 changes: 158 additions & 0 deletions RefreshTests.GameServer/Tests/Comments/LevelCommentTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
using Refresh.GameServer.Authentication;
using Refresh.GameServer.Types.Comments;
using Refresh.GameServer.Types.Levels;
using Refresh.GameServer.Types.Lists;
using Refresh.GameServer.Types.UserData;
using RefreshTests.GameServer.Extensions;

namespace RefreshTests.GameServer.Tests.Comments;

public class LevelCommentTests : GameServerTest
{
[Test]
public void PostAndDeleteLevelComment()
{
using TestContext context = this.GetServer();
GameUser user = context.CreateUser();
GameLevel level = context.CreateLevel(user);

using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user);

GameComment comment = new()
{
Author = user,
Content = "This is a test comment!",
};

HttpResponseMessage response = client.PostAsync($"/lbp/postComment/user/{level.LevelId}", new StringContent(comment.AsXML())).Result;
Assert.That(response.StatusCode, Is.EqualTo(OK));

response = client.GetAsync($"/lbp/comments/user/{level.LevelId}").Result;
SerializedCommentList userComments = response.Content.ReadAsXML<SerializedCommentList>();
Assert.That(userComments.Items, Has.Count.EqualTo(1));
Assert.That(userComments.Items[0].Content, Is.EqualTo(comment.Content));

response = client.PostAsync($"/lbp/deleteComment/user/{level.LevelId}?commentId={userComments.Items[0].SequentialId}", new ByteArrayContent(Array.Empty<byte>())).Result;
Assert.That(response.StatusCode, Is.EqualTo(OK));

response = client.GetAsync($"/lbp/comments/user/{level.LevelId}").Result;
userComments = response.Content.ReadAsXML<SerializedCommentList>();
Assert.That(userComments.Items, Has.Count.EqualTo(0));
}

[Test]
public void CantPostTooLongLevelComment()
{
using TestContext context = this.GetServer();
GameUser user = context.CreateUser();
GameLevel level = context.CreateLevel(user);

using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user);

GameComment comment = new()
{
Author = user,
Content = new string('S', 5000),
};

HttpResponseMessage response = client.PostAsync($"/lbp/postComment/user/{level.LevelId}", new StringContent(comment.AsXML())).Result;
Assert.That(response.StatusCode, Is.EqualTo(BadRequest));
}

[Test]
public void CantPostCommentToInvalidLevel()
{
using TestContext context = this.GetServer();
GameUser user = context.CreateUser();

using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user);

GameComment comment = new()
{
Author = user,
Content = "This is a test comment",
};

HttpResponseMessage response = client.PostAsync($"/lbp/postComment/user/I_AM_NOT_REAL", new StringContent(comment.AsXML())).Result;
Assert.That(response.StatusCode, Is.EqualTo(NotFound));
}

[Test]
public void CantGetLevelCommentsOfInvalidLevel()
{
using TestContext context = this.GetServer();
GameUser user = context.CreateUser();

using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user);

HttpResponseMessage response = client.GetAsync($"/lbp/comments/user/I_AM_NOT_REAL").Result;
Assert.That(response.StatusCode, Is.EqualTo(NotFound));
}

[Test]
public void CantDeleteInvalidLevelCommentId()
{
using TestContext context = this.GetServer();
GameUser user = context.CreateUser();
GameLevel level = context.CreateLevel(user);

using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user);

HttpResponseMessage response = client.PostAsync($"/lbp/deleteComment/user/{level.LevelId}?commentId=BAD", new ByteArrayContent(Array.Empty<byte>())).Result;
Assert.That(response.StatusCode, Is.EqualTo(BadRequest));
}

[Test]
public void CantDeleteCommentForInvalidLevel()
{
using TestContext context = this.GetServer();
GameUser user = context.CreateUser();

using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user);

HttpResponseMessage response = client.PostAsync($"/lbp/deleteComment/user/I_AM_NOT_REAL?commentId=1", new ByteArrayContent(Array.Empty<byte>())).Result;
Assert.That(response.StatusCode, Is.EqualTo(NotFound));
}

[Test]
public void CantDeleteNonExistentLevelComment()
{
using TestContext context = this.GetServer();
GameUser user = context.CreateUser();
GameLevel level = context.CreateLevel(user);

using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user);

HttpResponseMessage response = client.PostAsync($"/lbp/deleteComment/user/{level.LevelId}?commentId=1", new ByteArrayContent(Array.Empty<byte>())).Result;
Assert.That(response.StatusCode, Is.EqualTo(BadRequest));
}

[Test]
public void CantDeleteAnotherUsersComment()
{
using TestContext context = this.GetServer();
GameUser user1 = context.CreateUser();
GameUser user2 = context.CreateUser();
GameLevel level = context.CreateLevel(user1);

using HttpClient client1 = context.GetAuthenticatedClient(TokenType.Game, user1);
using HttpClient client2 = context.GetAuthenticatedClient(TokenType.Game, user2);

GameComment comment = new()
{
Author = user1,
Content = "This is a test comment!",
};

HttpResponseMessage response = client1.PostAsync($"/lbp/postComment/user/{level.LevelId}", new StringContent(comment.AsXML())).Result;
Assert.That(response.StatusCode, Is.EqualTo(OK));

response = client1.GetAsync($"/lbp/comments/user/{level.LevelId}").Result;
SerializedCommentList userComments = response.Content.ReadAsXML<SerializedCommentList>();
Assert.That(userComments.Items, Has.Count.EqualTo(1));
Assert.That(userComments.Items[0].Content, Is.EqualTo(comment.Content));

response = client2.PostAsync($"/lbp/deleteComment/user/{level.LevelId}?commentId={userComments.Items[0].SequentialId}", new ByteArrayContent(Array.Empty<byte>())).Result;
Assert.That(response.StatusCode, Is.EqualTo(Unauthorized));
}
}
154 changes: 154 additions & 0 deletions RefreshTests.GameServer/Tests/Comments/UserCommentTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
using Refresh.GameServer.Authentication;
using Refresh.GameServer.Types.Comments;
using Refresh.GameServer.Types.Lists;
using Refresh.GameServer.Types.UserData;
using RefreshTests.GameServer.Extensions;

namespace RefreshTests.GameServer.Tests.Comments;

public class UserCommentTests : GameServerTest
{
[Test]
public void PostAndDeleteUserComment()
{
using TestContext context = this.GetServer();
GameUser user1 = context.CreateUser();
GameUser user2 = context.CreateUser();

using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user1);

GameComment comment = new()
{
Author = user1,
Content = "This is a test comment!",
};

HttpResponseMessage response = client.PostAsync($"/lbp/postUserComment/{user2.Username}", new StringContent(comment.AsXML())).Result;
Assert.That(response.StatusCode, Is.EqualTo(OK));

response = client.GetAsync($"/lbp/userComments/{user2.Username}").Result;
SerializedCommentList userComments = response.Content.ReadAsXML<SerializedCommentList>();
Assert.That(userComments.Items, Has.Count.EqualTo(1));
Assert.That(userComments.Items[0].Content, Is.EqualTo(comment.Content));

response = client.PostAsync($"/lbp/deleteUserComment/{user2.Username}?commentId={userComments.Items[0].SequentialId}", new ByteArrayContent(Array.Empty<byte>())).Result;
Assert.That(response.StatusCode, Is.EqualTo(OK));

response = client.GetAsync($"/lbp/userComments/{user2.Username}").Result;
userComments = response.Content.ReadAsXML<SerializedCommentList>();
Assert.That(userComments.Items, Has.Count.EqualTo(0));
}

[Test]
public void CantPostTooLongUserComment()
{
using TestContext context = this.GetServer();
GameUser user1 = context.CreateUser();
GameUser user2 = context.CreateUser();

using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user1);

GameComment comment = new()
{
Author = user1,
Content = new string('S', 5000),
};

HttpResponseMessage response = client.PostAsync($"/lbp/postUserComment/{user2.Username}", new StringContent(comment.AsXML())).Result;
Assert.That(response.StatusCode, Is.EqualTo(BadRequest));
}

[Test]
public void CantUserCommentToInvalidUser()
{
using TestContext context = this.GetServer();
GameUser user = context.CreateUser();

using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user);

GameComment comment = new()
{
Author = user,
Content = "This is a test comment",
};

HttpResponseMessage response = client.PostAsync($"/lbp/postUserComment/I_AM_NOT_REAL", new StringContent(comment.AsXML())).Result;
Assert.That(response.StatusCode, Is.EqualTo(NotFound));
}

[Test]
public void CantGetUserCommentsOfInvalidUser()
{
using TestContext context = this.GetServer();
GameUser user = context.CreateUser();

using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user);

HttpResponseMessage response = client.GetAsync($"/lbp/userComments/I_AM_NOT_REAL").Result;
Assert.That(response.StatusCode, Is.EqualTo(NotFound));
}

[Test]
public void CantDeleteInvalidCommentId()
{
using TestContext context = this.GetServer();
GameUser user = context.CreateUser();

using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user);

HttpResponseMessage response = client.PostAsync($"/lbp/deleteUserComment/I_AM_NOT_REAL?commentId=BAD", new ByteArrayContent(Array.Empty<byte>())).Result;
Assert.That(response.StatusCode, Is.EqualTo(BadRequest));
}

[Test]
public void CantDeleteCommentForInvalidUser()
{
using TestContext context = this.GetServer();
GameUser user = context.CreateUser();

using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user);

HttpResponseMessage response = client.PostAsync($"/lbp/deleteUserComment/I_AM_NOT_REAL?commentId=1", new ByteArrayContent(Array.Empty<byte>())).Result;
Assert.That(response.StatusCode, Is.EqualTo(NotFound));
}

[Test]
public void CantDeleteNonExistantComment()
{
using TestContext context = this.GetServer();
GameUser user = context.CreateUser();

using HttpClient client = context.GetAuthenticatedClient(TokenType.Game, user);

HttpResponseMessage response = client.PostAsync($"/lbp/deleteUserComment/{user.Username}?commentId=1", new ByteArrayContent(Array.Empty<byte>())).Result;
Assert.That(response.StatusCode, Is.EqualTo(BadRequest));
}

[Test]
public void CantDeleteAnotherUsersComment()
{
using TestContext context = this.GetServer();
GameUser user1 = context.CreateUser();
GameUser user2 = context.CreateUser();

using HttpClient client1 = context.GetAuthenticatedClient(TokenType.Game, user1);
using HttpClient client2 = context.GetAuthenticatedClient(TokenType.Game, user2);

GameComment comment = new()
{
Author = user1,
Content = "This is a test comment!",
};

HttpResponseMessage response = client1.PostAsync($"/lbp/postUserComment/{user2.Username}", new StringContent(comment.AsXML())).Result;
Assert.That(response.StatusCode, Is.EqualTo(OK));

response = client1.GetAsync($"/lbp/userComments/{user2.Username}").Result;
SerializedCommentList userComments = response.Content.ReadAsXML<SerializedCommentList>();
Assert.That(userComments.Items, Has.Count.EqualTo(1));
Assert.That(userComments.Items[0].Content, Is.EqualTo(comment.Content));

response = client2.PostAsync($"/lbp/deleteUserComment/{user2.Username}?commentId={userComments.Items[0].SequentialId}", new ByteArrayContent(Array.Empty<byte>())).Result;
Assert.That(response.StatusCode, Is.EqualTo(Unauthorized));
}
}

0 comments on commit f48fd58

Please sign in to comment.