diff options
| author | Kamen Mladenov <kamen.d.mladenov@protonmail.com> | 2021-03-14 12:14:59 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-14 12:14:59 +0200 |
| commit | 432fc5890814596d50fb409a6e5dc952d7bb4836 (patch) | |
| tree | 05878ec2aeaa6feda4058e494e586ab0e869a63c /src/Web/DevHive.Web.Tests/CommentController.Tests.cs | |
| parent | 97459890f8c25b1af2a17109055afea53ee0b280 (diff) | |
| parent | 10ac1adbf9fbeaca116d3fd356f85dd64c8c2bf8 (diff) | |
| download | DevHive-432fc5890814596d50fb409a6e5dc952d7bb4836.tar DevHive-432fc5890814596d50fb409a6e5dc952d7bb4836.tar.gz DevHive-432fc5890814596d50fb409a6e5dc952d7bb4836.zip | |
Merge pull request #21 from Team-Kaleidoscope/unit_test_refactoring
Unit test code style refactor
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 7860f3c..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 Mock<ICommentService>(); - this.MapperMock = new Mock<IMapper>(); - this.CommentController = new CommentController(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); } |
