-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tests: Add 100% test coverage for CommentEndpoints (#264)
- Loading branch information
Showing
2 changed files
with
312 additions
and
0 deletions.
There are no files selected for viewing
158 changes: 158 additions & 0 deletions
158
RefreshTests.GameServer/Tests/Comments/LevelCommentTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
154
RefreshTests.GameServer/Tests/Comments/UserCommentTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |