diff options
| author | transtrike <transtrike@gmail.com> | 2021-03-15 09:27:12 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2021-03-15 09:27:12 +0200 |
| commit | 0161be09312fde634865f110504884119a617d5c (patch) | |
| tree | 0fa68366edcb024c054f370ecf90f5b66282aae5 /src/Web/DevHive.Web.Tests/CommentController.Tests.cs | |
| parent | e3b5757b5a5db2f7874b0924cdd4a22b1a9e1ee2 (diff) | |
| parent | ac82c773a5ec43c6a59d3d0b7665b67ac9e6bdde (diff) | |
| download | DevHive-0161be09312fde634865f110504884119a617d5c.tar DevHive-0161be09312fde634865f110504884119a617d5c.tar.gz DevHive-0161be09312fde634865f110504884119a617d5c.zip | |
Fixed to new() where possible and readable
Diffstat (limited to 'src/Web/DevHive.Web.Tests/CommentController.Tests.cs')
| -rw-r--r-- | src/Web/DevHive.Web.Tests/CommentController.Tests.cs | 158 |
1 files changed, 115 insertions, 43 deletions
diff --git a/src/Web/DevHive.Web.Tests/CommentController.Tests.cs b/src/Web/DevHive.Web.Tests/CommentController.Tests.cs index d5ba5a3..830677e 100644 --- a/src/Web/DevHive.Web.Tests/CommentController.Tests.cs +++ b/src/Web/DevHive.Web.Tests/CommentController.Tests.cs @@ -1,7 +1,7 @@ using System; using System.Linq; -using System.Threading.Tasks; using AutoMapper; +using DevHive.Common.Jwt.Interfaces; using DevHive.Services.Interfaces; using DevHive.Services.Models.Comment; using DevHive.Web.Controllers; @@ -12,21 +12,23 @@ using NUnit.Framework; namespace DevHive.Web.Tests { - [TestFixture] + [TestFixture] public class CommentControllerTests { const string MESSAGE = "Gosho Trapov"; - private Mock<ICommentService> CommentServiceMock { get; set; } - private Mock<IMapper> MapperMock { get; set; } - private CommentController CommentController { get; set; } + private Mock<ICommentService> _commentServiceMock; + private Mock<IMapper> _mapperMock; + private Mock<IJwtService> _jwtServiceMock; + private CommentController _commentController; #region Setup [SetUp] public void SetUp() { - this.CommentServiceMock = new(); - this.MapperMock = new(); - this.CommentController = new(this.CommentServiceMock.Object, this.MapperMock.Object); + this._commentServiceMock = new Mock<ICommentService>(); + this._mapperMock = new Mock<IMapper>(); + this._jwtServiceMock = new Mock<IJwtService>(); + this._commentController = new CommentController(this._commentServiceMock.Object, this._mapperMock.Object, this._jwtServiceMock.Object); } #endregion @@ -44,11 +46,20 @@ namespace DevHive.Web.Tests Message = MESSAGE }; - this.MapperMock.Setup(p => p.Map<CreateCommentServiceModel>(It.IsAny<CreateCommentWebModel>())).Returns(createCommentServiceModel); - this.CommentServiceMock.Setup(p => p.AddComment(It.IsAny<CreateCommentServiceModel>())).Returns(Task.FromResult(id)); - this.CommentServiceMock.Setup(p => p.ValidateJwtForCreating(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(true)); - - IActionResult result = this.CommentController.AddComment(Guid.NewGuid(), createCommentWebModel, null).Result; + this._mapperMock + .Setup(p => p.Map<CreateCommentServiceModel>(It.IsAny<CreateCommentWebModel>())) + .Returns(createCommentServiceModel); + this._commentServiceMock + .Setup(p => p.AddComment(It.IsAny<CreateCommentServiceModel>())) + .ReturnsAsync(id); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())) + .Returns(true); + this._commentServiceMock + .Setup(p => p.ValidateJwtForCreating(It.IsAny<Guid>(), It.IsAny<string>())) + .ReturnsAsync(true); + + IActionResult result = this._commentController.AddComment(Guid.NewGuid(), createCommentWebModel, null).Result; Assert.IsInstanceOf<OkObjectResult>(result); @@ -77,11 +88,20 @@ namespace DevHive.Web.Tests string errorMessage = $"Could not create comment!"; - this.MapperMock.Setup(p => p.Map<CreateCommentServiceModel>(It.IsAny<CreateCommentWebModel>())).Returns(createCommentServiceModel); - this.CommentServiceMock.Setup(p => p.AddComment(It.IsAny<CreateCommentServiceModel>())).Returns(Task.FromResult(Guid.Empty)); - this.CommentServiceMock.Setup(p => p.ValidateJwtForCreating(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(true)); + this._mapperMock + .Setup(p => p.Map<CreateCommentServiceModel>(It.IsAny<CreateCommentWebModel>())) + .Returns(createCommentServiceModel); + this._commentServiceMock + .Setup(p => p.AddComment(It.IsAny<CreateCommentServiceModel>())) + .ReturnsAsync(Guid.Empty); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())) + .Returns(true); + this._commentServiceMock + .Setup(p => p.ValidateJwtForCreating(It.IsAny<Guid>(), It.IsAny<string>())) + .ReturnsAsync(true); - IActionResult result = this.CommentController.AddComment(Guid.NewGuid(), createCommentWebModel, null).Result; + IActionResult result = this._commentController.AddComment(Guid.NewGuid(), createCommentWebModel, null).Result; Assert.IsInstanceOf<BadRequestObjectResult>(result); @@ -99,9 +119,14 @@ namespace DevHive.Web.Tests Message = MESSAGE }; - this.CommentServiceMock.Setup(p => p.ValidateJwtForCreating(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(false)); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())) + .Returns(true); + this._commentServiceMock + .Setup(p => p.ValidateJwtForCreating(It.IsAny<Guid>(), It.IsAny<string>())) + .ReturnsAsync(false); - IActionResult result = this.CommentController.AddComment(Guid.NewGuid(), createCommentWebModel, null).Result; + IActionResult result = this._commentController.AddComment(Guid.NewGuid(), createCommentWebModel, null).Result; Assert.IsInstanceOf<UnauthorizedResult>(result); } @@ -121,10 +146,17 @@ namespace DevHive.Web.Tests Message = MESSAGE }; - this.CommentServiceMock.Setup(p => p.GetCommentById(It.IsAny<Guid>())).Returns(Task.FromResult(readCommentServiceModel)); - this.MapperMock.Setup(p => p.Map<ReadCommentWebModel>(It.IsAny<ReadCommentServiceModel>())).Returns(readCommentWebModel); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())) + .Returns(true); + this._commentServiceMock + .Setup(p => p.GetCommentById(It.IsAny<Guid>())) + .ReturnsAsync(readCommentServiceModel); + this._mapperMock + .Setup(p => p.Map<ReadCommentWebModel>(It.IsAny<ReadCommentServiceModel>())) + .Returns(readCommentWebModel); - IActionResult result = this.CommentController.GetCommentById(id).Result; + IActionResult result = this._commentController.GetCommentById(id).Result; Assert.IsInstanceOf<OkObjectResult>(result); @@ -149,11 +181,20 @@ namespace DevHive.Web.Tests NewMessage = MESSAGE }; - this.CommentServiceMock.Setup(p => p.UpdateComment(It.IsAny<UpdateCommentServiceModel>())).Returns(Task.FromResult(id)); - this.CommentServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(true)); - this.MapperMock.Setup(p => p.Map<UpdateCommentServiceModel>(It.IsAny<UpdateCommentWebModel>())).Returns(updateCommentServiceModel); - - IActionResult result = this.CommentController.UpdateComment(Guid.Empty, updateCommentWebModel, null).Result; + this._commentServiceMock + .Setup(p => p.UpdateComment(It.IsAny<UpdateCommentServiceModel>())) + .ReturnsAsync(id); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())) + .Returns(true); + this._commentServiceMock + .Setup(p => p.ValidateJwtForComment(It.IsAny<Guid>(), It.IsAny<string>())) + .ReturnsAsync(true); + this._mapperMock + .Setup(p => p.Map<UpdateCommentServiceModel>(It.IsAny<UpdateCommentWebModel>())) + .Returns(updateCommentServiceModel); + + IActionResult result = this._commentController.UpdateComment(Guid.Empty, updateCommentWebModel, null).Result; Assert.IsInstanceOf<OkObjectResult>(result); @@ -177,11 +218,20 @@ namespace DevHive.Web.Tests NewMessage = MESSAGE }; - this.CommentServiceMock.Setup(p => p.UpdateComment(It.IsAny<UpdateCommentServiceModel>())).Returns(Task.FromResult(Guid.Empty)); - this.CommentServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(true)); - this.MapperMock.Setup(p => p.Map<UpdateCommentServiceModel>(It.IsAny<UpdateCommentWebModel>())).Returns(updateCommentServiceModel); - - IActionResult result = this.CommentController.UpdateComment(Guid.Empty, updateCommentWebModel, null).Result; + this._commentServiceMock + .Setup(p => p.UpdateComment(It.IsAny<UpdateCommentServiceModel>())) + .ReturnsAsync(Guid.Empty); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())) + .Returns(true); + this._commentServiceMock + .Setup(p => p.ValidateJwtForComment(It.IsAny<Guid>(), It.IsAny<string>())) + .ReturnsAsync(true); + this._mapperMock + .Setup(p => p.Map<UpdateCommentServiceModel>(It.IsAny<UpdateCommentWebModel>())) + .Returns(updateCommentServiceModel); + + IActionResult result = this._commentController.UpdateComment(Guid.Empty, updateCommentWebModel, null).Result; Assert.IsInstanceOf<BadRequestObjectResult>(result); BadRequestObjectResult badRequestObjectResult = result as BadRequestObjectResult; @@ -198,9 +248,12 @@ namespace DevHive.Web.Tests NewMessage = MESSAGE }; - this.CommentServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(false)); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())) + .Returns(false); + // this.CommentServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny<Guid>(), It.IsAny<string>())).ReturnsAsync(false)); - IActionResult result = this.CommentController.UpdateComment(Guid.Empty, updateCommentWebModel, null).Result; + IActionResult result = this._commentController.UpdateComment(Guid.Empty, updateCommentWebModel, null).Result; Assert.IsInstanceOf<UnauthorizedResult>(result); } @@ -212,10 +265,17 @@ namespace DevHive.Web.Tests { Guid id = Guid.NewGuid(); - this.CommentServiceMock.Setup(p => p.DeleteComment(It.IsAny<Guid>())).Returns(Task.FromResult(true)); - this.CommentServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(true)); + this._commentServiceMock + .Setup(p => p.DeleteComment(It.IsAny<Guid>())) + .ReturnsAsync(true); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())) + .Returns(true); + this._commentServiceMock + .Setup(p => p.ValidateJwtForComment(It.IsAny<Guid>(), It.IsAny<string>())) + .ReturnsAsync(true); - IActionResult result = this.CommentController.DeleteComment(id, null).Result; + IActionResult result = this._commentController.DeleteComment(id, null).Result; Assert.IsInstanceOf<OkResult>(result); } @@ -226,10 +286,17 @@ namespace DevHive.Web.Tests string message = "Could not delete Comment"; Guid id = Guid.NewGuid(); - this.CommentServiceMock.Setup(p => p.DeleteComment(It.IsAny<Guid>())).Returns(Task.FromResult(false)); - this.CommentServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(true)); + this._commentServiceMock + .Setup(p => p.DeleteComment(It.IsAny<Guid>())) + .ReturnsAsync(false); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())) + .Returns(true); + this._commentServiceMock + .Setup(p => p.ValidateJwtForComment(It.IsAny<Guid>(), It.IsAny<string>())) + .ReturnsAsync(true); - IActionResult result = this.CommentController.DeleteComment(id, null).Result; + IActionResult result = this._commentController.DeleteComment(id, null).Result; Assert.IsInstanceOf<BadRequestObjectResult>(result); @@ -242,9 +309,14 @@ namespace DevHive.Web.Tests [Test] public void DeleteComment_ReturnsUnauthorizedResult_WhenUserIsNotAuthorized() { - this.CommentServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(false)); - - IActionResult result = this.CommentController.DeleteComment(Guid.Empty, null).Result; + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())) + .Returns(true); + this._commentServiceMock + .Setup(p => p.ValidateJwtForComment(It.IsAny<Guid>(), It.IsAny<string>())) + .ReturnsAsync(false); + + IActionResult result = this._commentController.DeleteComment(Guid.Empty, null).Result; Assert.IsInstanceOf<UnauthorizedResult>(result); } |
