aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs
diff options
context:
space:
mode:
authorSyndamia <kamen.d.mladenov@protonmail.com>2021-01-22 10:00:42 +0200
committerSyndamia <kamen.d.mladenov@protonmail.com>2021-01-22 10:00:42 +0200
commitc8e5dac43004ca5f38b8224d69d76c8613c5eb43 (patch)
treec4f1b146a541a33561b65138537be701a7fb15e1 /src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs
parente039bfac22ed3ad3c9a5face8044e13e6dd76756 (diff)
parent092f8fb6a50e41a7a9ecdd0929f271cde4cd05c9 (diff)
downloadDevHive-c8e5dac43004ca5f38b8224d69d76c8613c5eb43.tar
DevHive-c8e5dac43004ca5f38b8224d69d76c8613c5eb43.tar.gz
DevHive-c8e5dac43004ca5f38b8224d69d76c8613c5eb43.zip
Merge branch 'refactor_user_updating' of github.com:Team-Kaleidoscope/DevHive into dev
Diffstat (limited to 'src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs')
-rw-r--r--src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs231
1 files changed, 227 insertions, 4 deletions
diff --git a/src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs b/src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs
index 18ed1b9..ace1cae 100644
--- a/src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs
+++ b/src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs
@@ -1,12 +1,235 @@
-using System;
-using System.Collections.Generic;
+using System;
using System.Linq;
-using System.Text;
using System.Threading.Tasks;
+using AutoMapper;
+using DevHive.Services.Interfaces;
+using DevHive.Services.Models.Post.Comment;
+using DevHive.Web.Controllers;
+using DevHive.Web.Models.Post.Comment;
+using Microsoft.AspNetCore.Mvc;
+using Moq;
+using NUnit.Framework;
namespace DevHive.Web.Tests
{
- class PostController
+ [TestFixture]
+ public class PostControllerTests
{
+ const string MESSAGE = "Gosho Trapov";
+ private Mock<IPostService> PostServiceMock { get; set; }
+ private Mock<IMapper> MapperMock { get; set; }
+ private PostController PostController { get; set; }
+
+ [SetUp]
+ public void SetUp()
+ {
+ this.PostServiceMock = new Mock<IPostService>();
+ this.MapperMock = new Mock<IMapper>();
+ this.PostController = new PostController(this.PostServiceMock.Object, this.MapperMock.Object);
+ }
+
+ #region Comment
+ #region Create
+ [Test]
+ public void AddComment_ReturnsOkObjectResult_WhenCommentIsSuccessfullyCreated()
+ {
+ CommentWebModel commentWebModel = new CommentWebModel
+ {
+ Message = MESSAGE
+ };
+ CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel
+ {
+ Message = MESSAGE
+ };
+ Guid id = Guid.NewGuid();
+
+ this.MapperMock.Setup(p => p.Map<CreateCommentServiceModel>(It.IsAny<CommentWebModel>())).Returns(createCommentServiceModel);
+ this.PostServiceMock.Setup(p => p.AddComment(It.IsAny<CreateCommentServiceModel>())).Returns(Task.FromResult(id));
+
+ IActionResult result = this.PostController.AddComment(commentWebModel).Result;
+
+ Assert.IsInstanceOf<OkObjectResult>(result);
+
+ var splitted = (result as OkObjectResult).Value
+ .ToString()
+ .Split('{', '}', '=', ' ')
+ .Where(x => !string.IsNullOrEmpty(x))
+ .ToArray();
+
+ Guid resultId = Guid.Parse(splitted[1]);
+
+ Assert.AreEqual(id, resultId);
+ }
+
+ [Test]
+ public void AddComment_ReturnsBadRequestObjectResult_WhenCommentIsNotCreatedSuccessfully()
+ {
+ CommentWebModel commentWebModel = new CommentWebModel
+ {
+ Message = MESSAGE
+ };
+ CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel
+ {
+ Message = MESSAGE
+ };
+ Guid id = Guid.Empty;
+ string errorMessage = $"Could not create comment";
+
+ this.MapperMock.Setup(p => p.Map<CreateCommentServiceModel>(It.IsAny<CommentWebModel>())).Returns(createCommentServiceModel);
+ this.PostServiceMock.Setup(p => p.AddComment(It.IsAny<CreateCommentServiceModel>())).Returns(Task.FromResult(id));
+
+ IActionResult result = this.PostController.AddComment(commentWebModel).Result;
+
+ Assert.IsInstanceOf<BadRequestObjectResult>(result);
+
+ BadRequestObjectResult badRequsetObjectResult = result as BadRequestObjectResult;
+ string resultMessage = badRequsetObjectResult.Value.ToString();
+
+ Assert.AreEqual(errorMessage, resultMessage);
+ }
+ #endregion
+
+ #region Read
+ [Test]
+ public void GetCommentById_ReturnsTheComment_WhenItExists()
+ {
+ Guid id = Guid.NewGuid();
+
+ CommentServiceModel commentServiceModel = new CommentServiceModel
+ {
+ Message = MESSAGE
+ };
+ CommentWebModel commentWebModel = new CommentWebModel
+ {
+ Message = MESSAGE
+ };
+
+ this.PostServiceMock.Setup(p => p.GetCommentById(It.IsAny<Guid>())).Returns(Task.FromResult(commentServiceModel));
+ this.MapperMock.Setup(p => p.Map<CommentWebModel>(It.IsAny<CommentServiceModel>())).Returns(commentWebModel);
+
+ IActionResult result = this.PostController.GetCommentById(id).Result;
+
+ Assert.IsInstanceOf<OkObjectResult>(result);
+
+ OkObjectResult okObjectResult = result as OkObjectResult;
+ CommentWebModel resultModel = okObjectResult.Value as Models.Post.Comment.CommentWebModel;
+
+ Assert.AreEqual(MESSAGE, resultModel.Message);
+ }
+ #endregion
+
+ #region Update
+ [Test]
+ public void UpdateComment_ShouldReturnOkResult_WhenCommentIsUpdatedSuccessfully()
+ {
+ Guid id = Guid.NewGuid();
+ CommentWebModel commentWebModel = new CommentWebModel
+ {
+ Message = MESSAGE
+ };
+ UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel
+ {
+ Message = MESSAGE
+ };
+
+ this.PostServiceMock.Setup(p => p.UpdateComment(It.IsAny<UpdateCommentServiceModel>())).Returns(Task.FromResult(true));
+ this.MapperMock.Setup(p => p.Map<UpdateCommentServiceModel>(It.IsAny<CommentWebModel>())).Returns(updateCommentServiceModel);
+ this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(true));
+
+ IActionResult result = this.PostController.UpdateComment(id, commentWebModel, null).Result;
+
+ Assert.IsInstanceOf<OkResult>(result);
+ }
+
+ [Test]
+ public void UpdateComment_ShouldReturnBadObjectResult_WhenCommentIsNotUpdatedSuccessfully()
+ {
+ Guid id = Guid.NewGuid();
+ string message = "Could not update Comment";
+ CommentWebModel commentWebModel = new CommentWebModel
+ {
+ Message = MESSAGE
+ };
+ UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel
+ {
+ Message = MESSAGE
+ };
+
+ this.PostServiceMock.Setup(p => p.UpdateComment(It.IsAny<UpdateCommentServiceModel>())).Returns(Task.FromResult(false));
+ this.MapperMock.Setup(p => p.Map<UpdateCommentServiceModel>(It.IsAny<CommentWebModel>())).Returns(updateCommentServiceModel);
+ this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(true));
+
+ IActionResult result = this.PostController.UpdateComment(id, commentWebModel, null).Result;
+ Assert.IsInstanceOf<BadRequestObjectResult>(result);
+
+ BadRequestObjectResult badRequestObjectResult = result as BadRequestObjectResult;
+ string resultModel = badRequestObjectResult.Value.ToString();
+
+ Assert.AreEqual(message, resultModel);
+ }
+
+ [Test]
+ public void UpdateComment_ShouldReturnUnauthorizedResult_WhenJwtIsNotValid()
+ {
+ Guid id = Guid.NewGuid();
+
+ CommentWebModel commentWebModel = new CommentWebModel
+ {
+ Message = MESSAGE
+ };
+
+ this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(false));
+
+ IActionResult result = this.PostController.UpdateComment(id, commentWebModel, null).Result;
+ Assert.IsInstanceOf<UnauthorizedResult>(result);
+ }
+ #endregion
+
+ #region Delete
+ [Test]
+ public void DeleteComment_ReturnsOkResult_WhenLanguageIsDeletedSuccessfully()
+ {
+ Guid id = Guid.NewGuid();
+
+ this.PostServiceMock.Setup(p => p.DeleteComment(It.IsAny<Guid>())).Returns(Task.FromResult(true));
+ this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(true));
+
+ IActionResult result = this.PostController.DeleteComment(id, null).Result;
+
+ Assert.IsInstanceOf<OkResult>(result);
+ }
+
+ [Test]
+ public void DeletComment_ReturnsBadRequestObjectResult_WhenLanguageIsNotDeletedSuccessfully()
+ {
+ string message = "Could not delete Comment";
+ Guid id = Guid.NewGuid();
+
+ this.PostServiceMock.Setup(p => p.DeleteComment(It.IsAny<Guid>())).Returns(Task.FromResult(false));
+ this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(true));
+
+ IActionResult result = this.PostController.DeleteComment(id, null).Result;
+
+ Assert.IsInstanceOf<BadRequestObjectResult>(result);
+
+ BadRequestObjectResult badRequestObjectResult = result as BadRequestObjectResult;
+ string resultModel = badRequestObjectResult.Value.ToString();
+
+ Assert.AreEqual(message, resultModel);
+ }
+
+ [Test]
+ public void DeletComment_ReturnsUnauthorizedResult_WhenJwtIsNotValid()
+ {
+ Guid id = Guid.NewGuid();
+
+ this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(false));
+
+ IActionResult result = this.PostController.DeleteComment(id, null).Result;
+
+ Assert.IsInstanceOf<UnauthorizedResult>(result);
+ }
+ #endregion
+ #endregion
}
}