From fb527cbfb94e2723113d67b83ed8f24e32422e56 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Thu, 21 Jan 2021 22:03:50 +0200 Subject: Adding Language layer unit test(Note: Data is to be refactored no data layer tests are added) --- src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs (limited to 'src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs') diff --git a/src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs b/src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs new file mode 100644 index 0000000..18ed1b9 --- /dev/null +++ b/src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DevHive.Web.Tests +{ + class PostController + { + } +} -- cgit v1.2.3 From 092f8fb6a50e41a7a9ecdd0929f271cde4cd05c9 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Thu, 21 Jan 2021 23:52:44 +0200 Subject: Adding Comment layer unit test(Note: Data is to be refactored no data layer tests are added) --- .../LanguageService.Tests.cs | 4 +- .../DevHive.Services.Tests/PostService.Tests.cs | 189 +++++++++++++++++ .../DevHive.Web.Tests/LanguageController.Tests.cs | 4 +- .../DevHive.Web.Tests/PostController.Tests.cs | 231 ++++++++++++++++++++- .../TechnologyController.Tests.cs | 4 +- src/DevHive.Web/Controllers/PostController.cs | 2 +- 6 files changed, 423 insertions(+), 11 deletions(-) create mode 100644 src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs (limited to 'src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs') diff --git a/src/DevHive.Tests/DevHive.Services.Tests/LanguageService.Tests.cs b/src/DevHive.Tests/DevHive.Services.Tests/LanguageService.Tests.cs index 67f1284..fd4a828 100644 --- a/src/DevHive.Tests/DevHive.Services.Tests/LanguageService.Tests.cs +++ b/src/DevHive.Tests/DevHive.Services.Tests/LanguageService.Tests.cs @@ -104,7 +104,7 @@ namespace DevHive.Services.Tests { Guid id = new Guid(); string name = "Gosho Trapov"; - Language language = new Language() + Language language = new Language { Name = name }; @@ -161,7 +161,7 @@ namespace DevHive.Services.Tests } [Test] - public void UpdateLanguage_ReturnsIfUpdateIsSuccessfull_WhenLanguageExistsy() + public void UpdateLanguage_ThrowsArgumentException_WhenLanguageDoesNotExist() { string exceptionMessage = "Language does not exist!"; UpdateLanguageServiceModel updateTechnologyServiceModel = new UpdateLanguageServiceModel diff --git a/src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs b/src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs new file mode 100644 index 0000000..bf6817b --- /dev/null +++ b/src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs @@ -0,0 +1,189 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Interfaces.Repositories; +using DevHive.Data.Models; +using DevHive.Services.Models.Post.Comment; +using DevHive.Services.Services; +using Moq; +using NUnit.Framework; + +namespace DevHive.Services.Tests +{ + [TestFixture] + public class PostServiceTests + { + private const string MESSAGE = "Gosho Trapov"; + private Mock PostRepositoryMock { get; set; } + private Mock UserRepositoryMock { get; set; } + private Mock MapperMock { get; set; } + private PostService PostService { get; set; } + + [SetUp] + public void Setup() + { + this.PostRepositoryMock = new Mock(); + this.MapperMock = new Mock(); + this.UserRepositoryMock = new Mock(); + this.PostService = new PostService(this.PostRepositoryMock.Object, this.UserRepositoryMock.Object, this.MapperMock.Object); + } + + #region Comment + #region Create + [Test] + public async Task AddComment_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully() + { + Guid id = Guid.NewGuid(); + CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel + { + Message = MESSAGE + }; + Comment comment = new Comment + { + Message = MESSAGE, + Id = id + }; + + this.PostRepositoryMock.Setup(p => p.AddCommentAsync(It.IsAny())).Returns(Task.FromResult(true)); + this.PostRepositoryMock.Setup(p => p.GetCommentByIssuerAndTimeCreatedAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(comment)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(comment); + + Guid result = await this.PostService.AddComment(createCommentServiceModel); + + Assert.AreEqual(id, result); + } + + [Test] + public async Task CreateLanguage_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully() + { + CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel + { + Message = MESSAGE + }; + Comment comment = new Comment + { + Message = MESSAGE, + }; + + this.PostRepositoryMock.Setup(p => p.AddCommentAsync(It.IsAny())).Returns(Task.FromResult(false)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(comment); + + Guid result = await this.PostService.AddComment(createCommentServiceModel); + + Assert.IsTrue(result == Guid.Empty); + } + #endregion + + #region Read + [Test] + public async Task GetCommentById_ReturnsTheComment_WhenItExists() + { + Guid id = new Guid(); + Comment comment = new Comment + { + Message = MESSAGE + }; + CommentServiceModel commentServiceModel = new CommentServiceModel + { + Message = MESSAGE + }; + + this.PostRepositoryMock.Setup(p => p.GetCommentByIdAsync(It.IsAny())).Returns(Task.FromResult(comment)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(commentServiceModel); + + CommentServiceModel result = await this.PostService.GetCommentById(id); + + Assert.AreEqual(MESSAGE, result.Message); + } + + [Test] + public void GetLanguageById_ThrowsException_WhenLanguageDoesNotExist() + { + string exceptionMessage = "The comment does not exist"; + Guid id = new Guid(); + this.PostRepositoryMock.Setup(p => p.GetCommentByIdAsync(It.IsAny())).Returns(Task.FromResult(null)); + + Exception ex = Assert.ThrowsAsync(() => this.PostService.GetCommentById(id)); + + Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); + } + #endregion + + #region Update + [Test] + [TestCase(true)] + [TestCase(false)] + public async Task UpdateComment_ReturnsIfUpdateIsSuccessfull_WhenCommentExistsy(bool shouldPass) + { + Comment comment = new Comment + { + Message = MESSAGE + }; + UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel + { + Message = MESSAGE + }; + + this.PostRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(true)); + this.PostRepositoryMock.Setup(p => p.EditCommentAsync(It.IsAny())).Returns(Task.FromResult(shouldPass)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(comment); + + bool result = await this.PostService.UpdateComment(updateCommentServiceModel); + + Assert.AreEqual(shouldPass, result); + } + + [Test] + public void UpdateLanguage_ThrowsArgumentException_WhenCommentDoesNotExist() + { + string exceptionMessage = "Comment does not exist!"; + UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel + { + }; + + this.PostRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(false)); + + Exception ex = Assert.ThrowsAsync(() => this.PostService.UpdateComment(updateCommentServiceModel)); + + Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); + } + #endregion + + #region Delete + [Test] + [TestCase(true)] + [TestCase(false)] + public async Task DeleteComment_ShouldReturnIfDeletionIsSuccessfull_WhenCommentExists(bool shouldPass) + { + Guid id = new Guid(); + Comment comment = new Comment(); + + this.PostRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(true)); + this.PostRepositoryMock.Setup(p => p.GetCommentByIdAsync(It.IsAny())).Returns(Task.FromResult(comment)); + this.PostRepositoryMock.Setup(p => p.DeleteCommentAsync(It.IsAny())).Returns(Task.FromResult(shouldPass)); + + bool result = await this.PostService.DeleteComment(id); + + Assert.AreEqual(shouldPass, result); + } + + [Test] + public void DeleteLanguage_ThrowsException_WhenLanguageDoesNotExist() + { + string exceptionMessage = "Comment does not exist!"; + Guid id = new Guid(); + + this.PostRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(false)); + + Exception ex = Assert.ThrowsAsync(() => this.PostService.DeleteComment(id)); + + Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); + } + #endregion + + #region ValidateJwtForComment + //TO DO: Implement + #endregion + #endregion + } +} diff --git a/src/DevHive.Tests/DevHive.Web.Tests/LanguageController.Tests.cs b/src/DevHive.Tests/DevHive.Web.Tests/LanguageController.Tests.cs index 913d9df..7c8d64e 100644 --- a/src/DevHive.Tests/DevHive.Web.Tests/LanguageController.Tests.cs +++ b/src/DevHive.Tests/DevHive.Web.Tests/LanguageController.Tests.cs @@ -140,7 +140,7 @@ namespace DevHive.Web.Tests } [Test] - public void Update_ShouldReturnOkResult_WhenLanguageIsNotUpdatedSuccessfully() + public void Update_ShouldReturnBadObjectResult_WhenLanguageIsNotUpdatedSuccessfully() { Guid id = Guid.NewGuid(); string message = "Could not update Language"; @@ -168,7 +168,7 @@ namespace DevHive.Web.Tests #region Delete [Test] - public void Delete_ReturnsOkResult_When_LanguageIsDeletedSuccessfully() + public void Delete_ReturnsOkResult_WhenLanguageIsDeletedSuccessfully() { Guid id = Guid.NewGuid(); 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 PostServiceMock { get; set; } + private Mock MapperMock { get; set; } + private PostController PostController { get; set; } + + [SetUp] + public void SetUp() + { + this.PostServiceMock = new Mock(); + this.MapperMock = new Mock(); + 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(It.IsAny())).Returns(createCommentServiceModel); + this.PostServiceMock.Setup(p => p.AddComment(It.IsAny())).Returns(Task.FromResult(id)); + + IActionResult result = this.PostController.AddComment(commentWebModel).Result; + + Assert.IsInstanceOf(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(It.IsAny())).Returns(createCommentServiceModel); + this.PostServiceMock.Setup(p => p.AddComment(It.IsAny())).Returns(Task.FromResult(id)); + + IActionResult result = this.PostController.AddComment(commentWebModel).Result; + + Assert.IsInstanceOf(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())).Returns(Task.FromResult(commentServiceModel)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(commentWebModel); + + IActionResult result = this.PostController.GetCommentById(id).Result; + + Assert.IsInstanceOf(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())).Returns(Task.FromResult(true)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(updateCommentServiceModel); + this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); + + IActionResult result = this.PostController.UpdateComment(id, commentWebModel, null).Result; + + Assert.IsInstanceOf(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())).Returns(Task.FromResult(false)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(updateCommentServiceModel); + this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); + + IActionResult result = this.PostController.UpdateComment(id, commentWebModel, null).Result; + Assert.IsInstanceOf(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(), It.IsAny())).Returns(Task.FromResult(false)); + + IActionResult result = this.PostController.UpdateComment(id, commentWebModel, null).Result; + Assert.IsInstanceOf(result); + } + #endregion + + #region Delete + [Test] + public void DeleteComment_ReturnsOkResult_WhenLanguageIsDeletedSuccessfully() + { + Guid id = Guid.NewGuid(); + + this.PostServiceMock.Setup(p => p.DeleteComment(It.IsAny())).Returns(Task.FromResult(true)); + this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); + + IActionResult result = this.PostController.DeleteComment(id, null).Result; + + Assert.IsInstanceOf(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())).Returns(Task.FromResult(false)); + this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); + + IActionResult result = this.PostController.DeleteComment(id, null).Result; + + Assert.IsInstanceOf(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(), It.IsAny())).Returns(Task.FromResult(false)); + + IActionResult result = this.PostController.DeleteComment(id, null).Result; + + Assert.IsInstanceOf(result); + } + #endregion + #endregion } } diff --git a/src/DevHive.Tests/DevHive.Web.Tests/TechnologyController.Tests.cs b/src/DevHive.Tests/DevHive.Web.Tests/TechnologyController.Tests.cs index 40434d6..72118b3 100644 --- a/src/DevHive.Tests/DevHive.Web.Tests/TechnologyController.Tests.cs +++ b/src/DevHive.Tests/DevHive.Web.Tests/TechnologyController.Tests.cs @@ -141,7 +141,7 @@ namespace DevHive.Web.Tests } [Test] - public void Update_ShouldReturnOkResult_WhenTechnologyIsNotUpdatedSuccessfully() + public void Update_ShouldReturnBadObjectResult_WhenTechnologyIsNotUpdatedSuccessfully() { Guid id = Guid.NewGuid(); string message = "Could not update Technology"; @@ -169,7 +169,7 @@ namespace DevHive.Web.Tests #region Delete [Test] - public void Delete_ReturnsOkResult_When_TechnologyIsDeletedSuccessfully() + public void Delete_ReturnsOkResult_WhenTechnologyIsDeletedSuccessfully() { Guid id = Guid.NewGuid(); diff --git a/src/DevHive.Web/Controllers/PostController.cs b/src/DevHive.Web/Controllers/PostController.cs index 2a08605..50923d2 100644 --- a/src/DevHive.Web/Controllers/PostController.cs +++ b/src/DevHive.Web/Controllers/PostController.cs @@ -48,7 +48,7 @@ namespace DevHive.Web.Controllers Guid id = await this._postService.AddComment(createCommentServiceModel); return id == Guid.Empty ? - new BadRequestObjectResult("Could not create language") : + new BadRequestObjectResult("Could not create comment") : new OkObjectResult(new { Id = id }); } -- cgit v1.2.3 From b5316d00cbaee97a2c4a74005de87cd1bc26ec28 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Tue, 26 Jan 2021 15:58:53 +0200 Subject: Removing tests for deleted User Repository methods --- .../DevHive.Data.Tests/UserRepositoryTests.cs | 129 +---------- .../DevHive.Services.Tests/PostService.Tests.cs | 237 --------------------- .../DevHive.Web.Tests/PostController.Tests.cs | 235 -------------------- 3 files changed, 1 insertion(+), 600 deletions(-) delete mode 100644 src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs delete mode 100644 src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs (limited to 'src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs') diff --git a/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs b/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs index 832cccd..7c3ba87 100644 --- a/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs +++ b/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs @@ -110,38 +110,6 @@ namespace DevHive.Data.Tests } #endregion - #region GetUserLanguages - [Test] - public async Task GetUserLanguages_ReturnsAllSavedUserLanguages_WhenTheyExist() - { - //Arrange - User dummyUser = CreateDummyUser(); - await this._userRepository.AddAsync(dummyUser); - HashSet dummyUserLanguages = dummyUser.Languages; - - //Act - HashSet languages = this._userRepository.GetUserLanguages(dummyUser); - - //Assert - Assert.AreEqual(dummyUserLanguages, languages, "Method doesn't query languages properly"); - } - - [Test] - public async Task GetUserLanguages_EmptyList_WhenNoLanguagesExist() - { - //Arrange - User dummyUser = CreateDummyUser(); - dummyUser.Languages.RemoveWhere(x => x.Name == "csharp"); - await this._userRepository.AddAsync(dummyUser); - - //Act - HashSet languages = this._userRepository.GetUserLanguages(dummyUser); - - //Assert - Assert.IsEmpty(languages, "Method doesn't query languages properly"); - } - #endregion - #region DoesUserExistAsync [Test] public async Task DoesUserExistAsync_ReturnsTrue_WhenUserExists() @@ -270,107 +238,12 @@ namespace DevHive.Data.Tests this._context.Users.Add(dummyUser); await this._context.SaveChangesAsync(); - bool result = this._userRepository.DoesUserHaveThisUsername(dummyUser.Id, username); + bool result = this._userRepository.DoesUserHaveThisUsername(dummyUser.Id, username); Assert.IsFalse(result, "DoesUserNameExistAsync does not return false when user doesnt have the given name"); } #endregion - #region DoesUserHaveFriends - [Test] - public async Task DoesUserHaveFriends_ReturnsTrue_WhenUserHasFriends() - { - User dummyUser = this.CreateDummyUser(); - User anotherDummyUser = this.CreateAnotherDummyUser(); - HashSet friends = new HashSet(); - friends.Add(anotherDummyUser); - dummyUser.Friends = friends; - - this._context.Users.Add(dummyUser); - this._context.Users.Add(anotherDummyUser); - await this._context.SaveChangesAsync(); - - bool result = this._userRepository.DoesUserHaveFriends(dummyUser); - - Assert.IsTrue(result, "DoesUserHaveFriends does not return true when user has friends"); - } - - [Test] - public async Task DoesUserHaveFriends_ReturnsFalse_WhenUserDoesNotHaveTheGivenFriend() - { - User dummyUser = this.CreateDummyUser(); - - this._context.Users.Add(dummyUser); - await this._context.SaveChangesAsync(); - - bool result = this._userRepository.DoesUserHaveFriends(dummyUser); - - Assert.IsFalse(result, "DoesUserHaveFriends does not return false when user des not have friends"); - } - #endregion - - #region DoesUserHaveThisLanguage - [Test] - public async Task DoesUserHaveThisLanguage_ReturnsTrue_WhenUserHasTheGivenLanguage() - { - User dummyUser = this.CreateDummyUser(); - - this._context.Users.Add(dummyUser); - await this._context.SaveChangesAsync(); - - Language language = dummyUser.Languages.FirstOrDefault(); - - bool result = this._userRepository.DoesUserHaveThisLanguage(dummyUser, language); - - Assert.IsTrue(result, "DoesUserHaveThisLanguage does not return true when user has the given language"); - } - - [Test] - public async Task DoesUserHaveThisLanguage_ReturnsFalse_WhenUserDoesNotHaveTheGivenLanguage() - { - User dummyUser = this.CreateDummyUser(); - this._context.Users.Add(dummyUser); - await this._context.SaveChangesAsync(); - - Language language = new Language(); - - bool result = this._userRepository.DoesUserHaveThisLanguage(dummyUser, language); - - Assert.IsFalse(result, "DoesUserHaveThisLanguage does not return false when user does not have the given language"); - } - #endregion - - #region DoesUserHaveThisTechnology - [Test] - public async Task DoesUserHaveThisTechnology_ReturnsTrue_WhenUserHasTheGivenTechnology() - { - User dummyUser = this.CreateDummyUser(); - - this._context.Users.Add(dummyUser); - await this._context.SaveChangesAsync(); - - Technology technology = dummyUser.Technologies.FirstOrDefault(); - - bool result = this._userRepository.DoesUserHaveThisTechnology(dummyUser, technology); - - Assert.IsTrue(result, "DoesUserHaveThisLanguage does not return true when user has the given technology"); - } - - [Test] - public async Task DoesUserHaveThisTechnology_ReturnsFalse_WhenUserDoesNotHaveTheGivenTechnology() - { - User dummyUser = this.CreateDummyUser(); - this._context.Users.Add(dummyUser); - await this._context.SaveChangesAsync(); - - Technology technology = new Technology(); - - bool result = this._userRepository.DoesUserHaveThisTechnology(dummyUser, technology); - - Assert.IsFalse(result, "DoesUserHaveThisLanguage does not return false when user does not have the given technology"); - } - #endregion - #region HelperMethods private User CreateDummyUser() { diff --git a/src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs b/src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs deleted file mode 100644 index 9864c5b..0000000 --- a/src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs +++ /dev/null @@ -1,237 +0,0 @@ -using System; -using System.Threading.Tasks; -using AutoMapper; -using DevHive.Data.Interfaces.Repositories; -using DevHive.Data.Models; -using DevHive.Services.Models.Post.Comment; -using DevHive.Services.Models.Post.Post; -using DevHive.Services.Services; -using Moq; -using NUnit.Framework; - -namespace DevHive.Services.Tests -{ - [TestFixture] - public class PostServiceTests - { - private const string MESSAGE = "Gosho Trapov"; - private Mock PostRepositoryMock { get; set; } - private Mock UserRepositoryMock { get; set; } - private Mock MapperMock { get; set; } - private PostService PostService { get; set; } - - [SetUp] - public void Setup() - { - this.PostRepositoryMock = new Mock(); - this.MapperMock = new Mock(); - this.UserRepositoryMock = new Mock(); - this.PostService = new PostService(this.PostRepositoryMock.Object, this.UserRepositoryMock.Object, this.MapperMock.Object); - } - - #region Comment - #region Create - [Test] - public async Task AddComment_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully() - { - Guid id = Guid.NewGuid(); - CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel - { - Message = MESSAGE - }; - Comment comment = new Comment - { - Message = MESSAGE, - Id = id - }; - - this.PostRepositoryMock.Setup(p => p.AddCommentAsync(It.IsAny())).Returns(Task.FromResult(true)); - this.PostRepositoryMock.Setup(p => p.GetCommentByIssuerAndTimeCreatedAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(comment)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(comment); - - Guid result = await this.PostService.AddComment(createCommentServiceModel); - - Assert.AreEqual(id, result); - } - - [Test] - public async Task CreateLanguage_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully() - { - CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel - { - Message = MESSAGE - }; - Comment comment = new Comment - { - Message = MESSAGE, - }; - - this.PostRepositoryMock.Setup(p => p.AddCommentAsync(It.IsAny())).Returns(Task.FromResult(false)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(comment); - - Guid result = await this.PostService.AddComment(createCommentServiceModel); - - Assert.IsTrue(result == Guid.Empty); - } - #endregion - - #region Read - [Test] - public async Task GetCommentById_ReturnsTheComment_WhenItExists() - { - Guid id = new Guid(); - Comment comment = new Comment - { - Message = MESSAGE - }; - CommentServiceModel commentServiceModel = new CommentServiceModel - { - Message = MESSAGE - }; - - this.PostRepositoryMock.Setup(p => p.GetCommentByIdAsync(It.IsAny())).Returns(Task.FromResult(comment)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(commentServiceModel); - - CommentServiceModel result = await this.PostService.GetCommentById(id); - - Assert.AreEqual(MESSAGE, result.Message); - } - - [Test] - public void GetLanguageById_ThrowsException_WhenLanguageDoesNotExist() - { - string exceptionMessage = "The comment does not exist"; - Guid id = new Guid(); - this.PostRepositoryMock.Setup(p => p.GetCommentByIdAsync(It.IsAny())).Returns(Task.FromResult(null)); - - Exception ex = Assert.ThrowsAsync(() => this.PostService.GetCommentById(id)); - - Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); - } - #endregion - - #region Update - [Test] - [TestCase(true)] - [TestCase(false)] - public async Task UpdateComment_ReturnsIfUpdateIsSuccessfull_WhenCommentExistsy(bool shouldPass) - { - Comment comment = new Comment - { - Message = MESSAGE - }; - UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel - { - Message = MESSAGE - }; - - this.PostRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(true)); - this.PostRepositoryMock.Setup(p => p.EditCommentAsync(It.IsAny())).Returns(Task.FromResult(shouldPass)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(comment); - - bool result = await this.PostService.UpdateComment(updateCommentServiceModel); - - Assert.AreEqual(shouldPass, result); - } - - [Test] - public void UpdateLanguage_ThrowsArgumentException_WhenCommentDoesNotExist() - { - string exceptionMessage = "Comment does not exist!"; - UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel - { - }; - - this.PostRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(false)); - - Exception ex = Assert.ThrowsAsync(() => this.PostService.UpdateComment(updateCommentServiceModel)); - - Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); - } - #endregion - - #region Delete - [Test] - [TestCase(true)] - [TestCase(false)] - public async Task DeleteComment_ShouldReturnIfDeletionIsSuccessfull_WhenCommentExists(bool shouldPass) - { - Guid id = new Guid(); - Comment comment = new Comment(); - - this.PostRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(true)); - this.PostRepositoryMock.Setup(p => p.GetCommentByIdAsync(It.IsAny())).Returns(Task.FromResult(comment)); - this.PostRepositoryMock.Setup(p => p.DeleteCommentAsync(It.IsAny())).Returns(Task.FromResult(shouldPass)); - - bool result = await this.PostService.DeleteComment(id); - - Assert.AreEqual(shouldPass, result); - } - - [Test] - public void DeleteLanguage_ThrowsException_WhenLanguageDoesNotExist() - { - string exceptionMessage = "Comment does not exist!"; - Guid id = new Guid(); - - this.PostRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny())).Returns(Task.FromResult(false)); - - Exception ex = Assert.ThrowsAsync(() => this.PostService.DeleteComment(id)); - - Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); - } - #endregion - - #region ValidateJwtForComment - //TO DO: Implement - #endregion - #endregion - - #region Posts - #region Create - [Test] - public async Task CreatePost_ReturnsIdOfThePost_WhenItIsSuccessfullyCreated() - { - Guid id = Guid.NewGuid(); - CreatePostServiceModel createPostServiceModel = new CreatePostServiceModel - { - }; - Post post = new Post - { - Message = MESSAGE, - Id = id - }; - - this.PostRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(true)); - this.PostRepositoryMock.Setup(p => p.GetPostByIssuerAndTimeCreatedAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(post)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(post); - - Guid result = await this.PostService.CreatePost(createPostServiceModel); - - Assert.AreEqual(id, result, "CreatePost does not return the correct id"); - } - - [Test] - public async Task CreatePost_ReturnsEmptyGuid_WhenItIsNotSuccessfullyCreated() - { - CreatePostServiceModel createPostServiceModel = new CreatePostServiceModel - { - }; - Post post = new Post - { - Message = MESSAGE, - }; - - this.PostRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(false)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(post); - - Guid result = await this.PostService.CreatePost(createPostServiceModel); - - Assert.AreEqual(Guid.Empty, result, "CreatePost does not return empty id"); - } - #endregion - - - #endregion - } -} diff --git a/src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs b/src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs deleted file mode 100644 index ace1cae..0000000 --- a/src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs +++ /dev/null @@ -1,235 +0,0 @@ -using System; -using System.Linq; -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 -{ - [TestFixture] - public class PostControllerTests - { - const string MESSAGE = "Gosho Trapov"; - private Mock PostServiceMock { get; set; } - private Mock MapperMock { get; set; } - private PostController PostController { get; set; } - - [SetUp] - public void SetUp() - { - this.PostServiceMock = new Mock(); - this.MapperMock = new Mock(); - 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(It.IsAny())).Returns(createCommentServiceModel); - this.PostServiceMock.Setup(p => p.AddComment(It.IsAny())).Returns(Task.FromResult(id)); - - IActionResult result = this.PostController.AddComment(commentWebModel).Result; - - Assert.IsInstanceOf(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(It.IsAny())).Returns(createCommentServiceModel); - this.PostServiceMock.Setup(p => p.AddComment(It.IsAny())).Returns(Task.FromResult(id)); - - IActionResult result = this.PostController.AddComment(commentWebModel).Result; - - Assert.IsInstanceOf(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())).Returns(Task.FromResult(commentServiceModel)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(commentWebModel); - - IActionResult result = this.PostController.GetCommentById(id).Result; - - Assert.IsInstanceOf(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())).Returns(Task.FromResult(true)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(updateCommentServiceModel); - this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); - - IActionResult result = this.PostController.UpdateComment(id, commentWebModel, null).Result; - - Assert.IsInstanceOf(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())).Returns(Task.FromResult(false)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(updateCommentServiceModel); - this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); - - IActionResult result = this.PostController.UpdateComment(id, commentWebModel, null).Result; - Assert.IsInstanceOf(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(), It.IsAny())).Returns(Task.FromResult(false)); - - IActionResult result = this.PostController.UpdateComment(id, commentWebModel, null).Result; - Assert.IsInstanceOf(result); - } - #endregion - - #region Delete - [Test] - public void DeleteComment_ReturnsOkResult_WhenLanguageIsDeletedSuccessfully() - { - Guid id = Guid.NewGuid(); - - this.PostServiceMock.Setup(p => p.DeleteComment(It.IsAny())).Returns(Task.FromResult(true)); - this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); - - IActionResult result = this.PostController.DeleteComment(id, null).Result; - - Assert.IsInstanceOf(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())).Returns(Task.FromResult(false)); - this.PostServiceMock.Setup(p => p.ValidateJwtForComment(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); - - IActionResult result = this.PostController.DeleteComment(id, null).Result; - - Assert.IsInstanceOf(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(), It.IsAny())).Returns(Task.FromResult(false)); - - IActionResult result = this.PostController.DeleteComment(id, null).Result; - - Assert.IsInstanceOf(result); - } - #endregion - #endregion - } -} -- cgit v1.2.3 From a41baf58415454d537bef56e8bec80d608ecae32 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 4 Feb 2021 19:59:02 +0200 Subject: Added PostController tests --- .../DevHive.Web.Tests/PostController.Tests.cs | 248 +++++++++++++++++++++ 1 file changed, 248 insertions(+) create mode 100644 src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs (limited to 'src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs') diff --git a/src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs b/src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs new file mode 100644 index 0000000..3a4e45e --- /dev/null +++ b/src/DevHive.Tests/DevHive.Web.Tests/PostController.Tests.cs @@ -0,0 +1,248 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.Post; +using DevHive.Web.Controllers; +using DevHive.Web.Models.Post; +using Microsoft.AspNetCore.Mvc; +using Moq; +using NUnit.Framework; + +namespace DevHive.Web.Tests +{ + [TestFixture] + public class PostControllerTests + { + const string MESSAGE = "Gosho Trapov"; + private Mock PostServiceMock { get; set; } + private Mock MapperMock { get; set; } + private PostController PostController { get; set; } + + [SetUp] + public void SetUp() + { + this.PostServiceMock = new Mock(); + this.MapperMock = new Mock(); + this.PostController = new PostController(this.PostServiceMock.Object, this.MapperMock.Object); + } + + #region Create + [Test] + public void CreatePost_ReturnsOkObjectResult_WhenPostIsSuccessfullyCreated() + { + CreatePostWebModel createPostWebModel = new CreatePostWebModel + { + Message = MESSAGE + }; + CreatePostServiceModel createPostServiceModel = new CreatePostServiceModel + { + Message = MESSAGE + }; + Guid id = Guid.NewGuid(); + + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(createPostServiceModel); + this.PostServiceMock.Setup(p => p.CreatePost(It.IsAny())).Returns(Task.FromResult(id)); + this.PostServiceMock.Setup(p => p.ValidateJwtForCreating(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); + + IActionResult result = this.PostController.Create(Guid.Empty, createPostWebModel, null).Result; + + Assert.IsInstanceOf(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 CreatePost_ReturnsBadRequestObjectResult_WhenPostIsNotCreatedSuccessfully() + { + CreatePostWebModel createTechnologyWebModel = new CreatePostWebModel + { + Message = MESSAGE + }; + CreatePostServiceModel createTechnologyServiceModel = new CreatePostServiceModel + { + Message = MESSAGE + }; + Guid id = Guid.Empty; + string errorMessage = $"Could not create post!"; + + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(createTechnologyServiceModel); + this.PostServiceMock.Setup(p => p.CreatePost(It.IsAny())).Returns(Task.FromResult(id)); + this.PostServiceMock.Setup(p => p.ValidateJwtForCreating(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); + + IActionResult result = this.PostController.Create(Guid.Empty, createTechnologyWebModel, null).Result; + + Assert.IsInstanceOf(result); + + BadRequestObjectResult badRequsetObjectResult = result as BadRequestObjectResult; + string resultMessage = badRequsetObjectResult.Value.ToString(); + + Assert.AreEqual(errorMessage, resultMessage); + } + + [Test] + public void CreatePost_ReturnsUnauthorizedResult_WhenUserIsNotAuthorized() + { + Guid id = Guid.NewGuid(); + CreatePostWebModel createPostWebModel = new CreatePostWebModel + { + Message = MESSAGE + }; + + this.PostServiceMock.Setup(p => p.ValidateJwtForCreating(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); + + IActionResult result = this.PostController.Create(Guid.NewGuid(), createPostWebModel, null).Result; + + Assert.IsInstanceOf(result); + } + #endregion + + #region Read + [Test] + public void GetById_ReturnsThePost_WhenItExists() + { + Guid id = Guid.NewGuid(); + + ReadPostServiceModel readPostServiceModel = new ReadPostServiceModel + { + Message = MESSAGE + }; + ReadPostWebModel readPostWebModel = new ReadPostWebModel + { + Message = MESSAGE + }; + + this.PostServiceMock.Setup(p => p.GetPostById(It.IsAny())).Returns(Task.FromResult(readPostServiceModel)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(readPostWebModel); + + IActionResult result = this.PostController.GetById(id).Result; + + Assert.IsInstanceOf(result); + + OkObjectResult okObjectResult = result as OkObjectResult; + ReadPostWebModel resultModel = okObjectResult.Value as Models.Post.ReadPostWebModel; + + Assert.AreEqual(MESSAGE, resultModel.Message); + } + #endregion + + #region Update + [Test] + public void Update_ShouldReturnOkResult_WhenPostIsUpdatedSuccessfully() + { + Guid id = Guid.NewGuid(); + UpdatePostWebModel updatePostWebModel = new UpdatePostWebModel + { + NewMessage = MESSAGE + }; + UpdatePostServiceModel updatePostServiceModel = new UpdatePostServiceModel + { + NewMessage = MESSAGE + }; + + this.PostServiceMock.Setup(p => p.UpdatePost(It.IsAny())).Returns(Task.FromResult(id)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(updatePostServiceModel); + this.PostServiceMock.Setup(p => p.ValidateJwtForPost(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); + + IActionResult result = this.PostController.Update(id, updatePostWebModel, null).Result; + + Assert.IsInstanceOf(result); + } + + [Test] + public void Update_ShouldReturnBadObjectResult_WhenPostIsNotUpdatedSuccessfully() + { + Guid id = Guid.NewGuid(); + string message = "Could not update post!"; + UpdatePostWebModel updatePostWebModel = new UpdatePostWebModel + { + NewMessage = MESSAGE + }; + UpdatePostServiceModel updatePostServiceModel = new UpdatePostServiceModel + { + NewMessage = MESSAGE + }; + + this.PostServiceMock.Setup(p => p.UpdatePost(It.IsAny())).Returns(Task.FromResult(Guid.Empty)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(updatePostServiceModel); + this.PostServiceMock.Setup(p => p.ValidateJwtForPost(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); + + IActionResult result = this.PostController.Update(id, updatePostWebModel, null).Result; + Assert.IsInstanceOf(result); + + BadRequestObjectResult badRequestObjectResult = result as BadRequestObjectResult; + string resultModel = badRequestObjectResult.Value.ToString(); + + Assert.AreEqual(message, resultModel); + } + + [Test] + public void Update_ShouldReturnUnauthorizedResult_WhenUserIsNotAuthorized() + { + UpdatePostWebModel updatePostWebModel = new UpdatePostWebModel + { + NewMessage = MESSAGE + }; + + this.PostServiceMock.Setup(p => p.ValidateJwtForPost(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); + + IActionResult result = this.PostController.Update(Guid.Empty, updatePostWebModel, null).Result; + + Assert.IsInstanceOf(result); + } + #endregion + + #region Delete + [Test] + public void Delete_ReturnsOkResult_WhenPostIsDeletedSuccessfully() + { + Guid id = Guid.NewGuid(); + + this.PostServiceMock.Setup(p => p.DeletePost(It.IsAny())).Returns(Task.FromResult(true)); + this.PostServiceMock.Setup(p => p.ValidateJwtForPost(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); + + IActionResult result = this.PostController.Delete(id, null).Result; + + Assert.IsInstanceOf(result); + } + + [Test] + public void Delete_ReturnsBadRequestObjectResult_WhenPostIsNotDeletedSuccessfully() + { + string message = "Could not delete Post"; + Guid id = Guid.NewGuid(); + + this.PostServiceMock.Setup(p => p.DeletePost(It.IsAny())).Returns(Task.FromResult(false)); + this.PostServiceMock.Setup(p => p.ValidateJwtForPost(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); + + IActionResult result = this.PostController.Delete(id, null).Result; + + Assert.IsInstanceOf(result); + + BadRequestObjectResult badRequestObjectResult = result as BadRequestObjectResult; + string resultModel = badRequestObjectResult.Value.ToString(); + + Assert.AreEqual(message, resultModel); + } + + [Test] + public void DeletePost_ReturnsUnauthorizedResult_WhenUserIsNotAuthorized() + { + this.PostServiceMock.Setup(p => p.ValidateJwtForPost(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); + + IActionResult result = this.PostController.Delete(Guid.Empty, null).Result; + + Assert.IsInstanceOf(result); + } + #endregion + } +} -- cgit v1.2.3