diff options
| author | Syndamia <kamen.d.mladenov@protonmail.com> | 2021-03-13 15:53:48 +0200 |
|---|---|---|
| committer | Syndamia <kamen.d.mladenov@protonmail.com> | 2021-03-13 15:53:48 +0200 |
| commit | f14ed20bf06cdeee8ce71ee39756360c43c29df1 (patch) | |
| tree | cc299c3a4f450f6e11c29c9bc0b787d99043d395 | |
| parent | 75d57f305f2ed1ecf8b82bd62d4bb8f17c06303b (diff) | |
| download | DevHive-f14ed20bf06cdeee8ce71ee39756360c43c29df1.tar DevHive-f14ed20bf06cdeee8ce71ee39756360c43c29df1.tar.gz DevHive-f14ed20bf06cdeee8ce71ee39756360c43c29df1.zip | |
Updated controller tests to mock jwt service, removed profile picture tests from user controller, as that is in it's own layer
| -rw-r--r-- | src/Web/DevHive.Web.Tests/CommentController.Tests.cs | 19 | ||||
| -rw-r--r-- | src/Web/DevHive.Web.Tests/PostController.Tests.cs | 11 | ||||
| -rw-r--r-- | src/Web/DevHive.Web.Tests/UserController.Tests.cs | 46 |
3 files changed, 35 insertions, 41 deletions
diff --git a/src/Web/DevHive.Web.Tests/CommentController.Tests.cs b/src/Web/DevHive.Web.Tests/CommentController.Tests.cs index 7860f3c..7a15d37 100644 --- a/src/Web/DevHive.Web.Tests/CommentController.Tests.cs +++ b/src/Web/DevHive.Web.Tests/CommentController.Tests.cs @@ -2,6 +2,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,12 +13,13 @@ 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 Mock<IJwtService> JwtServiceMock { get; set; } private CommentController CommentController { get; set; } #region Setup @@ -26,7 +28,8 @@ namespace DevHive.Web.Tests { this.CommentServiceMock = new Mock<ICommentService>(); this.MapperMock = new Mock<IMapper>(); - this.CommentController = new CommentController(this.CommentServiceMock.Object, this.MapperMock.Object); + this.JwtServiceMock = new Mock<IJwtService>(); + this.CommentController = new CommentController(this.CommentServiceMock.Object, this.MapperMock.Object, this.JwtServiceMock.Object); } #endregion @@ -46,6 +49,7 @@ namespace DevHive.Web.Tests 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.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>())).Returns(Task.FromResult(true)); IActionResult result = this.CommentController.AddComment(Guid.NewGuid(), createCommentWebModel, null).Result; @@ -79,6 +83,7 @@ namespace DevHive.Web.Tests 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.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>())).Returns(Task.FromResult(true)); IActionResult result = this.CommentController.AddComment(Guid.NewGuid(), createCommentWebModel, null).Result; @@ -99,6 +104,7 @@ namespace DevHive.Web.Tests Message = MESSAGE }; + 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>())).Returns(Task.FromResult(false)); IActionResult result = this.CommentController.AddComment(Guid.NewGuid(), createCommentWebModel, null).Result; @@ -121,6 +127,7 @@ namespace DevHive.Web.Tests Message = MESSAGE }; + this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())).Returns(true); 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); @@ -150,6 +157,7 @@ namespace DevHive.Web.Tests }; this.CommentServiceMock.Setup(p => p.UpdateComment(It.IsAny<UpdateCommentServiceModel>())).Returns(Task.FromResult(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>())).Returns(Task.FromResult(true)); this.MapperMock.Setup(p => p.Map<UpdateCommentServiceModel>(It.IsAny<UpdateCommentWebModel>())).Returns(updateCommentServiceModel); @@ -178,6 +186,7 @@ namespace DevHive.Web.Tests }; this.CommentServiceMock.Setup(p => p.UpdateComment(It.IsAny<UpdateCommentServiceModel>())).Returns(Task.FromResult(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>())).Returns(Task.FromResult(true)); this.MapperMock.Setup(p => p.Map<UpdateCommentServiceModel>(It.IsAny<UpdateCommentWebModel>())).Returns(updateCommentServiceModel); @@ -198,7 +207,8 @@ 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>())).Returns(Task.FromResult(false)); IActionResult result = this.CommentController.UpdateComment(Guid.Empty, updateCommentWebModel, null).Result; @@ -213,6 +223,7 @@ namespace DevHive.Web.Tests Guid id = Guid.NewGuid(); this.CommentServiceMock.Setup(p => p.DeleteComment(It.IsAny<Guid>())).Returns(Task.FromResult(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>())).Returns(Task.FromResult(true)); IActionResult result = this.CommentController.DeleteComment(id, null).Result; @@ -227,6 +238,7 @@ namespace DevHive.Web.Tests Guid id = Guid.NewGuid(); this.CommentServiceMock.Setup(p => p.DeleteComment(It.IsAny<Guid>())).Returns(Task.FromResult(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>())).Returns(Task.FromResult(true)); IActionResult result = this.CommentController.DeleteComment(id, null).Result; @@ -242,6 +254,7 @@ namespace DevHive.Web.Tests [Test] public void DeleteComment_ReturnsUnauthorizedResult_WhenUserIsNotAuthorized() { + 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>())).Returns(Task.FromResult(false)); IActionResult result = this.CommentController.DeleteComment(Guid.Empty, null).Result; diff --git a/src/Web/DevHive.Web.Tests/PostController.Tests.cs b/src/Web/DevHive.Web.Tests/PostController.Tests.cs index f10f8dd..438eb80 100644 --- a/src/Web/DevHive.Web.Tests/PostController.Tests.cs +++ b/src/Web/DevHive.Web.Tests/PostController.Tests.cs @@ -2,6 +2,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.Post; using DevHive.Web.Controllers; @@ -18,6 +19,7 @@ namespace DevHive.Web.Tests const string MESSAGE = "Gosho Trapov"; private Mock<IPostService> PostServiceMock { get; set; } private Mock<IMapper> MapperMock { get; set; } + private Mock<IJwtService> JwtServiceMock { get; set; } private PostController PostController { get; set; } [SetUp] @@ -25,7 +27,8 @@ namespace DevHive.Web.Tests { this.PostServiceMock = new Mock<IPostService>(); this.MapperMock = new Mock<IMapper>(); - this.PostController = new PostController(this.PostServiceMock.Object, this.MapperMock.Object); + this.JwtServiceMock = new Mock<IJwtService>(); + this.PostController = new PostController(this.PostServiceMock.Object, this.MapperMock.Object, this.JwtServiceMock.Object); } #region Create @@ -44,6 +47,7 @@ namespace DevHive.Web.Tests this.MapperMock.Setup(p => p.Map<CreatePostServiceModel>(It.IsAny<CreatePostWebModel>())).Returns(createPostServiceModel); this.PostServiceMock.Setup(p => p.CreatePost(It.IsAny<CreatePostServiceModel>())).Returns(Task.FromResult(id)); + this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())).Returns(true); this.PostServiceMock.Setup(p => p.ValidateJwtForCreating(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(true)); IActionResult result = this.PostController.Create(Guid.Empty, createPostWebModel, null).Result; @@ -77,6 +81,7 @@ namespace DevHive.Web.Tests this.MapperMock.Setup(p => p.Map<CreatePostServiceModel>(It.IsAny<CreatePostWebModel>())).Returns(createTechnologyServiceModel); this.PostServiceMock.Setup(p => p.CreatePost(It.IsAny<CreatePostServiceModel>())).Returns(Task.FromResult(id)); + this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())).Returns(true); this.PostServiceMock.Setup(p => p.ValidateJwtForCreating(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(true)); IActionResult result = this.PostController.Create(Guid.Empty, createTechnologyWebModel, null).Result; @@ -150,6 +155,7 @@ namespace DevHive.Web.Tests this.PostServiceMock.Setup(p => p.UpdatePost(It.IsAny<UpdatePostServiceModel>())).Returns(Task.FromResult(id)); this.MapperMock.Setup(p => p.Map<UpdatePostServiceModel>(It.IsAny<UpdatePostWebModel>())).Returns(updatePostServiceModel); + this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())).Returns(true); this.PostServiceMock.Setup(p => p.ValidateJwtForPost(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(true)); IActionResult result = this.PostController.Update(id, updatePostWebModel, null).Result; @@ -173,6 +179,7 @@ namespace DevHive.Web.Tests this.PostServiceMock.Setup(p => p.UpdatePost(It.IsAny<UpdatePostServiceModel>())).Returns(Task.FromResult(Guid.Empty)); this.MapperMock.Setup(p => p.Map<UpdatePostServiceModel>(It.IsAny<UpdatePostWebModel>())).Returns(updatePostServiceModel); + this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())).Returns(true); this.PostServiceMock.Setup(p => p.ValidateJwtForPost(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(true)); IActionResult result = this.PostController.Update(id, updatePostWebModel, null).Result; @@ -207,6 +214,7 @@ namespace DevHive.Web.Tests Guid id = Guid.NewGuid(); this.PostServiceMock.Setup(p => p.DeletePost(It.IsAny<Guid>())).Returns(Task.FromResult(true)); + this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())).Returns(true); this.PostServiceMock.Setup(p => p.ValidateJwtForPost(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(true)); IActionResult result = this.PostController.Delete(id, null).Result; @@ -221,6 +229,7 @@ namespace DevHive.Web.Tests Guid id = Guid.NewGuid(); this.PostServiceMock.Setup(p => p.DeletePost(It.IsAny<Guid>())).Returns(Task.FromResult(false)); + this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())).Returns(true); this.PostServiceMock.Setup(p => p.ValidateJwtForPost(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(true)); IActionResult result = this.PostController.Delete(id, null).Result; diff --git a/src/Web/DevHive.Web.Tests/UserController.Tests.cs b/src/Web/DevHive.Web.Tests/UserController.Tests.cs index e12738e..ee08d88 100644 --- a/src/Web/DevHive.Web.Tests/UserController.Tests.cs +++ b/src/Web/DevHive.Web.Tests/UserController.Tests.cs @@ -1,6 +1,7 @@ using System; using System.Threading.Tasks; using AutoMapper; +using DevHive.Common.Jwt.Interfaces; using DevHive.Common.Models.Identity; using DevHive.Services.Interfaces; using DevHive.Services.Models.User; @@ -18,6 +19,7 @@ namespace DevHive.Web.Tests const string USERNAME = "Gosho Trapov"; private Mock<IUserService> UserServiceMock { get; set; } private Mock<IMapper> MapperMock { get; set; } + private Mock<IJwtService> JwtServiceMock { get; set; } private UserController UserController { get; set; } [SetUp] @@ -25,7 +27,8 @@ namespace DevHive.Web.Tests { this.UserServiceMock = new Mock<IUserService>(); this.MapperMock = new Mock<IMapper>(); - this.UserController = new UserController(this.UserServiceMock.Object, this.MapperMock.Object); + this.JwtServiceMock = new Mock<IJwtService>(); + this.UserController = new UserController(this.UserServiceMock.Object, this.MapperMock.Object, this.JwtServiceMock.Object); } #region Create @@ -103,7 +106,7 @@ namespace DevHive.Web.Tests }; this.UserServiceMock.Setup(p => p.GetUserById(It.IsAny<Guid>())).Returns(Task.FromResult(userServiceModel)); - this.UserServiceMock.Setup(p => p.ValidJWT(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(true)); + this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())).Returns(true); this.MapperMock.Setup(p => p.Map<UserWebModel>(It.IsAny<UserServiceModel>())).Returns(userWebModel); IActionResult result = this.UserController.GetById(id, null).Result; @@ -119,7 +122,7 @@ namespace DevHive.Web.Tests [Test] public void GetById_ReturnsUnauthorizedResult_WhenUserIsNotAuthorized() { - this.UserServiceMock.Setup(p => p.ValidJWT(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(false)); + this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())).Returns(false); IActionResult result = this.UserController.GetById(Guid.NewGuid(), null).Result; @@ -171,44 +174,13 @@ namespace DevHive.Web.Tests }; this.UserServiceMock.Setup(p => p.UpdateUser(It.IsAny<UpdateUserServiceModel>())).Returns(Task.FromResult(userServiceModel)); - this.UserServiceMock.Setup(p => p.ValidJWT(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(true)); + this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())).Returns(true); this.MapperMock.Setup(p => p.Map<UpdateUserServiceModel>(It.IsAny<UpdateUserWebModel>())).Returns(updateUserServiceModel); IActionResult result = this.UserController.Update(id, updateUserWebModel, null).Result; Assert.IsInstanceOf<AcceptedResult>(result); } - - [Test] - public void UpdateProfilePicture_ShouldReturnOkObjectResult_WhenProfilePictureIsUpdatedSuccessfully() - { - string profilePictureURL = "goshotrapov"; - UpdateProfilePictureWebModel updateProfilePictureWebModel = new(); - UpdateProfilePictureServiceModel updateProfilePictureServiceModel = new(); - ProfilePictureServiceModel profilePictureServiceModel = new() - { - ProfilePictureURL = profilePictureURL - }; - ProfilePictureWebModel profilePictureWebModel = new() - { - ProfilePictureURL = profilePictureURL - }; - - this.UserServiceMock.Setup(p => p.ValidJWT(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(true)); - this.MapperMock.Setup(p => p.Map<UpdateProfilePictureServiceModel>(It.IsAny<UpdateProfilePictureWebModel>())).Returns(updateProfilePictureServiceModel); - this.UserServiceMock.Setup(p => p.UpdateProfilePicture(It.IsAny<UpdateProfilePictureServiceModel>())).Returns(Task.FromResult(profilePictureServiceModel)); - this.MapperMock.Setup(p => p.Map<ProfilePictureWebModel>(It.IsAny<ProfilePictureServiceModel>())).Returns(profilePictureWebModel); - - - IActionResult result = this.UserController.UpdateProfilePicture(Guid.Empty, updateProfilePictureWebModel, null).Result; - - Assert.IsInstanceOf<AcceptedResult>(result); - - AcceptedResult acceptedResult = result as AcceptedResult; - ProfilePictureWebModel resultModel = acceptedResult.Value as ProfilePictureWebModel; - - Assert.AreEqual(profilePictureURL, resultModel.ProfilePictureURL); - } #endregion #region Delete @@ -217,7 +189,7 @@ namespace DevHive.Web.Tests { Guid id = Guid.NewGuid(); - this.UserServiceMock.Setup(p => p.ValidJWT(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(true)); + this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())).Returns(true); this.UserServiceMock.Setup(p => p.DeleteUser(It.IsAny<Guid>())).Returns(Task.FromResult(true)); IActionResult result = this.UserController.Delete(id, null).Result; @@ -231,7 +203,7 @@ namespace DevHive.Web.Tests string message = "Could not delete User"; Guid id = Guid.NewGuid(); - this.UserServiceMock.Setup(p => p.ValidJWT(It.IsAny<Guid>(), It.IsAny<string>())).Returns(Task.FromResult(true)); + this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())).Returns(true); this.UserServiceMock.Setup(p => p.DeleteUser(It.IsAny<Guid>())).Returns(Task.FromResult(false)); IActionResult result = this.UserController.Delete(id, null).Result; |
