diff --git a/RefreshTests.GameServer/Tests/Comments/LevelCommentTests.cs b/RefreshTests.GameServer/Tests/Comments/LevelCommentTests.cs new file mode 100644 index 00000000..1ceae68c --- /dev/null +++ b/RefreshTests.GameServer/Tests/Comments/LevelCommentTests.cs @@ -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(); + 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())).Result; + Assert.That(response.StatusCode, Is.EqualTo(OK)); + + response = client.GetAsync($"/lbp/comments/user/{level.LevelId}").Result; + userComments = response.Content.ReadAsXML(); + 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())).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())).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())).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(); + 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())).Result; + Assert.That(response.StatusCode, Is.EqualTo(Unauthorized)); + } +} \ No newline at end of file diff --git a/RefreshTests.GameServer/Tests/Comments/UserCommentTests.cs b/RefreshTests.GameServer/Tests/Comments/UserCommentTests.cs new file mode 100644 index 00000000..b5f124aa --- /dev/null +++ b/RefreshTests.GameServer/Tests/Comments/UserCommentTests.cs @@ -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(); + 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())).Result; + Assert.That(response.StatusCode, Is.EqualTo(OK)); + + response = client.GetAsync($"/lbp/userComments/{user2.Username}").Result; + userComments = response.Content.ReadAsXML(); + 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())).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())).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())).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(); + 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())).Result; + Assert.That(response.StatusCode, Is.EqualTo(Unauthorized)); + } +} \ No newline at end of file