diff options
Diffstat (limited to 'src/DevHive.Tests/DevHive.Services.Tests')
5 files changed, 449 insertions, 411 deletions
diff --git a/src/DevHive.Tests/DevHive.Services.Tests/CommentService.Tests.cs b/src/DevHive.Tests/DevHive.Services.Tests/CommentService.Tests.cs new file mode 100644 index 0000000..ac022ea --- /dev/null +++ b/src/DevHive.Tests/DevHive.Services.Tests/CommentService.Tests.cs @@ -0,0 +1,262 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Interfaces.Repositories; +using DevHive.Data.Models; +using DevHive.Services.Models.Comment; +using DevHive.Services.Services; +using Moq; +using NUnit.Framework; + +namespace DevHive.Services.Tests +{ + [TestFixture] + public class CommentServiceTests + { + private const string MESSAGE = "Gosho Trapov"; + private Mock<IUserRepository> UserRepositoryMock { get; set; } + private Mock<IPostRepository> PostRepositoryMock { get; set; } + private Mock<ICommentRepository> CommentRepositoryMock { get; set; } + private Mock<IMapper> MapperMock { get; set; } + private CommentService CommentService { get; set; } + + #region Setup + [SetUp] + public void Setup() + { + this.UserRepositoryMock = new Mock<IUserRepository>(); + this.PostRepositoryMock = new Mock<IPostRepository>(); + this.CommentRepositoryMock = new Mock<ICommentRepository>(); + this.MapperMock = new Mock<IMapper>(); + this.CommentService = new CommentService(this.UserRepositoryMock.Object, this.PostRepositoryMock.Object, this.CommentRepositoryMock.Object, this.MapperMock.Object); + } + #endregion + + #region AddComment + [Test] + public async Task AddComment_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully() + { + Guid id = Guid.NewGuid(); + User creator = new User { Id = Guid.NewGuid() }; + CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel + { + Message = MESSAGE + }; + Comment comment = new Comment + { + Message = MESSAGE, + Id = id, + }; + + this.CommentRepositoryMock.Setup(p => p.AddAsync(It.IsAny<Comment>())).Returns(Task.FromResult(true)); + this.CommentRepositoryMock.Setup(p => p.GetCommentByIssuerAndTimeCreatedAsync(It.IsAny<Guid>(), It.IsAny<DateTime>())).Returns(Task.FromResult(comment)); + this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny<Guid>())).Returns(Task.FromResult(true)); + this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(creator)); + this.MapperMock.Setup(p => p.Map<Comment>(It.IsAny<CreateCommentServiceModel>())).Returns(comment); + + Guid result = await this.CommentService.AddComment(createCommentServiceModel); + + Assert.AreEqual(id, result); + } + + [Test] + public async Task AddComment_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully() + { + CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel + { + Message = MESSAGE + }; + Comment comment = new Comment + { + Message = MESSAGE, + }; + + this.CommentRepositoryMock.Setup(p => p.AddAsync(It.IsAny<Comment>())).Returns(Task.FromResult(false)); + this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny<Guid>())).Returns(Task.FromResult(true)); + this.MapperMock.Setup(p => p.Map<Comment>(It.IsAny<CreateCommentServiceModel>())).Returns(comment); + + Guid result = await this.CommentService.AddComment(createCommentServiceModel); + + Assert.IsTrue(result == Guid.Empty); + } + + [Test] + public void AddComment_ThrowsException_WhenPostDoesNotExist() + { + const string EXCEPTION_MESSAGE = "Post does not exist!"; + + CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel + { + Message = MESSAGE + }; + + Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.CommentService.AddComment(createCommentServiceModel), "AddComment does not throw excpeion when the post does not exist"); + + Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorecct exception message"); + } + #endregion + + #region GetCommentById + [Test] + public async Task GetCommentById_ReturnsTheComment_WhenItExists() + { + Guid creatorId = new Guid(); + User creator = new User { Id = creatorId }; + Comment comment = new Comment + { + Message = MESSAGE, + Creator = creator + }; + ReadCommentServiceModel commentServiceModel = new ReadCommentServiceModel + { + Message = MESSAGE + }; + User user = new User + { + Id = creatorId, + }; + + this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(comment)); + this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(user)); + this.MapperMock.Setup(p => p.Map<ReadCommentServiceModel>(It.IsAny<Comment>())).Returns(commentServiceModel); + + ReadCommentServiceModel result = await this.CommentService.GetCommentById(new Guid()); + + Assert.AreEqual(MESSAGE, result.Message); + } + + [Test] + public void GetCommentById_ThorwsException_WhenTheUserDoesNotExist() + { + const string EXCEPTION_MESSAGE = "The user does not exist"; + Guid creatorId = new Guid(); + User creator = new User { Id = creatorId }; + Comment comment = new Comment + { + Message = MESSAGE, + Creator = creator + }; + + this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(comment)); + + Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.CommentService.GetCommentById(new Guid()), "GetCommentById does not throw exception when the user does not exist"); + + Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message); + } + + [Test] + public void GetCommentById_ThrowsException_WhenCommentDoesNotExist() + { + string exceptionMessage = "The comment does not exist"; + Guid creatorId = new Guid(); + User user = new User + { + Id = creatorId, + }; + + this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult<Comment>(null)); + this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(user)); + + Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.CommentService.GetCommentById(new Guid())); + + Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); + } + #endregion + + #region UpdateComment + [Test] + public async Task UpdateComment_ReturnsTheIdOfTheComment_WhenUpdatedSuccessfully() + { + Guid id = Guid.NewGuid(); + Comment comment = new Comment + { + Id = id, + Message = MESSAGE + }; + UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel + { + CommentId = id, + NewMessage = MESSAGE + }; + + this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny<Guid>())).Returns(Task.FromResult(true)); + this.CommentRepositoryMock.Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Comment>())).Returns(Task.FromResult(true)); + this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(comment)); + this.MapperMock.Setup(p => p.Map<Comment>(It.IsAny<UpdateCommentServiceModel>())).Returns(comment); + + Guid result = await this.CommentService.UpdateComment(updateCommentServiceModel); + + Assert.AreEqual(updateCommentServiceModel.CommentId, result); + } + + [Test] + public async Task UpdateComment_ReturnsEmptyId_WhenTheCommentIsNotUpdatedSuccessfully() + { + Comment comment = new Comment + { + Message = MESSAGE + }; + UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel + { + CommentId = Guid.NewGuid(), + NewMessage = MESSAGE + }; + + this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny<Guid>())).Returns(Task.FromResult(true)); + this.CommentRepositoryMock.Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Comment>())).Returns(Task.FromResult(false)); + this.MapperMock.Setup(p => p.Map<Comment>(It.IsAny<UpdateCommentServiceModel>())).Returns(comment); + + Guid result = await this.CommentService.UpdateComment(updateCommentServiceModel); + + Assert.AreEqual(Guid.Empty, result); + } + + [Test] + public void UpdateComment_ThrowsArgumentException_WhenCommentDoesNotExist() + { + string exceptionMessage = "Comment does not exist!"; + UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel + { + }; + + this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny<Guid>())).Returns(Task.FromResult(false)); + + Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.CommentService.UpdateComment(updateCommentServiceModel)); + + Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); + } + #endregion + + #region DeleteComment + [Test] + [TestCase(true)] + [TestCase(false)] + public async Task DeleteComment_ShouldReturnIfDeletionIsSuccessfull_WhenCommentExists(bool shouldPass) + { + Guid id = new Guid(); + Comment comment = new Comment(); + + this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny<Guid>())).Returns(Task.FromResult(true)); + this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(comment)); + this.CommentRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny<Comment>())).Returns(Task.FromResult(shouldPass)); + + bool result = await this.CommentService.DeleteComment(id); + + Assert.AreEqual(shouldPass, result); + } + + [Test] + public void DeleteComment_ThrowsException_WhenCommentDoesNotExist() + { + string exceptionMessage = "Comment does not exist!"; + Guid id = new Guid(); + + this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny<Guid>())).Returns(Task.FromResult(false)); + + Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.CommentService.DeleteComment(id)); + + Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); + } + #endregion + } +} diff --git a/src/DevHive.Tests/DevHive.Services.Tests/FeedService.Tests.cs b/src/DevHive.Tests/DevHive.Services.Tests/FeedService.Tests.cs index 36cb838..e4020c5 100644 --- a/src/DevHive.Tests/DevHive.Services.Tests/FeedService.Tests.cs +++ b/src/DevHive.Tests/DevHive.Services.Tests/FeedService.Tests.cs @@ -1,155 +1,155 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using AutoMapper; -using DevHive.Data.Interfaces.Repositories; -using DevHive.Data.Models; -using DevHive.Services.Models; -using DevHive.Services.Models.Post.Post; -using DevHive.Services.Services; -using Moq; -using NUnit.Framework; - -namespace DevHive.Services.Tests -{ - [TestFixture] - public class FeedServiceTests - { - private Mock<IFeedRepository> FeedRepositoryMock { get; set; } - private Mock<IUserRepository> UserRepositoryMock { get; set; } - private Mock<IMapper> MapperMock { get; set; } - private FeedService FeedService { get; set; } - - #region SetUps - [SetUp] - public void Setup() - { - this.FeedRepositoryMock = new Mock<IFeedRepository>(); - this.UserRepositoryMock = new Mock<IUserRepository>(); - this.MapperMock = new Mock<IMapper>(); - this.FeedService = new FeedService(this.FeedRepositoryMock.Object, this.UserRepositoryMock.Object, this.MapperMock.Object); - } - #endregion - - #region GetPage - [Test] - public async Task GetPage_ReturnsReadPageServiceModel_WhenSuitablePostsExist() - { - GetPageServiceModel getPageServiceModel = new GetPageServiceModel - { - UserId = Guid.NewGuid() - }; - - User dummyUser = CreateDummyUser(); - User anotherDummyUser = CreateAnotherDummyUser(); - HashSet<User> friends = new HashSet<User>(); - friends.Add(anotherDummyUser); - dummyUser.Friends = friends; - - List<Post> posts = new List<Post> - { - new Post{ Message = "Message"} - }; - - ReadPostServiceModel readPostServiceModel = new ReadPostServiceModel - { - PostId = Guid.NewGuid(), - Message = "Message" - }; - List<ReadPostServiceModel> readPostServiceModels = new List<ReadPostServiceModel>(); - readPostServiceModels.Add(readPostServiceModel); - ReadPageServiceModel readPageServiceModel = new ReadPageServiceModel - { - Posts = readPostServiceModels - }; - - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(dummyUser)); - this.FeedRepositoryMock.Setup(p => p.GetFriendsPosts(It.IsAny<List<User>>(), It.IsAny<DateTime>(), It.IsAny<int>(), It.IsAny<int>())).Returns(Task.FromResult(posts)); - this.MapperMock.Setup(p => p.Map<ReadPostServiceModel>(It.IsAny<Post>())).Returns(readPostServiceModel); - - ReadPageServiceModel result = await this.FeedService.GetPage(getPageServiceModel); - - Assert.GreaterOrEqual(1, result.Posts.Count, "GetPage does not correctly return the posts"); - } - - [Test] - public void GetPage_ThrowsException_WhenNoSuitablePostsExist() - { - const string EXCEPTION_MESSAGE = "No friends of user have posted anything yet!"; - GetPageServiceModel getPageServiceModel = new GetPageServiceModel - { - UserId = Guid.NewGuid() - }; - - User dummyUser = CreateDummyUser(); - User anotherDummyUser = CreateAnotherDummyUser(); - HashSet<User> friends = new HashSet<User>(); - friends.Add(anotherDummyUser); - dummyUser.Friends = friends; - - ReadPostServiceModel readPostServiceModel = new ReadPostServiceModel - { - PostId = Guid.NewGuid(), - Message = "Message" - }; - List<ReadPostServiceModel> readPostServiceModels = new List<ReadPostServiceModel>(); - readPostServiceModels.Add(readPostServiceModel); - ReadPageServiceModel readPageServiceModel = new ReadPageServiceModel - { - Posts = readPostServiceModels - }; - - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(dummyUser)); - this.FeedRepositoryMock.Setup(p => p.GetFriendsPosts(It.IsAny<List<User>>(), It.IsAny<DateTime>(), It.IsAny<int>(), It.IsAny<int>())).Returns(Task.FromResult(new List<Post>())); - - Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.FeedService.GetPage(getPageServiceModel)); - - Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Wrong exception message"); - } - - [Test] - public void GetPage_ThrowsException_WhenUserHasNoFriendsToGetPostsFrom() - { - const string EXCEPTION_MESSAGE = "User has no friends to get feed from!"; - GetPageServiceModel getPageServiceModel = new GetPageServiceModel - { - UserId = Guid.NewGuid() - }; - - User dummyUser = CreateDummyUser(); - - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(dummyUser)); - - Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.FeedService.GetPage(getPageServiceModel)); - - Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Wrong exception message"); - } - #endregion - - #region HelperMethods - private User CreateDummyUser() - { - return new() - { - Id = Guid.NewGuid(), - UserName = "dummyUser", - FirstName = "Spas", - LastName = "Spasov", - Email = "abv@abv.bg", - }; - } - - private User CreateAnotherDummyUser() - { - return new() - { - Id = Guid.NewGuid(), - UserName = "anotherDummyUser", - FirstName = "Alex", - LastName = "Spiridonov", - Email = "a_spiridonov@abv.bg", - }; - } - #endregion - } -} +//using System; +//using System.Collections.Generic; +//using System.Threading.Tasks; +//using AutoMapper; +//using DevHive.Data.Interfaces.Repositories; +//using DevHive.Data.Models; +//using DevHive.Services.Models; +//using DevHive.Services.Models.Post; +//using DevHive.Services.Services; +//using Moq; +//using NUnit.Framework; + +//namespace DevHive.Services.Tests +//{ +// [TestFixture] +// public class FeedServiceTests +// { +// private Mock<IFeedRepository> FeedRepositoryMock { get; set; } +// private Mock<IUserRepository> UserRepositoryMock { get; set; } +// private Mock<IMapper> MapperMock { get; set; } +// private FeedService FeedService { get; set; } + +// #region SetUps +// [SetUp] +// public void Setup() +// { +// this.FeedRepositoryMock = new Mock<IFeedRepository>(); +// this.UserRepositoryMock = new Mock<IUserRepository>(); +// this.MapperMock = new Mock<IMapper>(); +// this.FeedService = new FeedService(this.FeedRepositoryMock.Object, this.UserRepositoryMock.Object, this.MapperMock.Object); +// } +// #endregion + +// #region GetPage +// [Test] +// public async Task GetPage_ReturnsReadPageServiceModel_WhenSuitablePostsExist() +// { +// GetPageServiceModel getPageServiceModel = new GetPageServiceModel +// { +// UserId = Guid.NewGuid() +// }; + +// User dummyUser = CreateDummyUser(); +// User anotherDummyUser = CreateAnotherDummyUser(); +// HashSet<User> friends = new HashSet<User>(); +// friends.Add(anotherDummyUser); +// dummyUser.Friends = friends; + +// List<Post> posts = new List<Post> +// { +// new Post{ Message = "Message"} +// }; + +// ReadPostServiceModel readPostServiceModel = new ReadPostServiceModel +// { +// PostId = Guid.NewGuid(), +// Message = "Message" +// }; +// List<ReadPostServiceModel> readPostServiceModels = new List<ReadPostServiceModel>(); +// readPostServiceModels.Add(readPostServiceModel); +// ReadPageServiceModel readPageServiceModel = new ReadPageServiceModel +// { +// Posts = readPostServiceModels +// }; + +// this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(dummyUser)); +// this.FeedRepositoryMock.Setup(p => p.GetFriendsPosts(It.IsAny<List<User>>(), It.IsAny<DateTime>(), It.IsAny<int>(), It.IsAny<int>())).Returns(Task.FromResult(posts)); +// this.MapperMock.Setup(p => p.Map<ReadPostServiceModel>(It.IsAny<Post>())).Returns(readPostServiceModel); + +// ReadPageServiceModel result = await this.FeedService.GetPage(getPageServiceModel); + +// Assert.GreaterOrEqual(1, result.Posts.Count, "GetPage does not correctly return the posts"); +// } + +// [Test] +// public void GetPage_ThrowsException_WhenNoSuitablePostsExist() +// { +// const string EXCEPTION_MESSAGE = "No friends of user have posted anything yet!"; +// GetPageServiceModel getPageServiceModel = new GetPageServiceModel +// { +// UserId = Guid.NewGuid() +// }; + +// User dummyUser = CreateDummyUser(); +// User anotherDummyUser = CreateAnotherDummyUser(); +// HashSet<User> friends = new HashSet<User>(); +// friends.Add(anotherDummyUser); +// dummyUser.Friends = friends; + +// ReadPostServiceModel readPostServiceModel = new ReadPostServiceModel +// { +// PostId = Guid.NewGuid(), +// Message = "Message" +// }; +// List<ReadPostServiceModel> readPostServiceModels = new List<ReadPostServiceModel>(); +// readPostServiceModels.Add(readPostServiceModel); +// ReadPageServiceModel readPageServiceModel = new ReadPageServiceModel +// { +// Posts = readPostServiceModels +// }; + +// this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(dummyUser)); +// this.FeedRepositoryMock.Setup(p => p.GetFriendsPosts(It.IsAny<List<User>>(), It.IsAny<DateTime>(), It.IsAny<int>(), It.IsAny<int>())).Returns(Task.FromResult(new List<Post>())); + +// Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.FeedService.GetPage(getPageServiceModel)); + +// Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Wrong exception message"); +// } + +// [Test] +// public void GetPage_ThrowsException_WhenUserHasNoFriendsToGetPostsFrom() +// { +// const string EXCEPTION_MESSAGE = "User has no friends to get feed from!"; +// GetPageServiceModel getPageServiceModel = new GetPageServiceModel +// { +// UserId = Guid.NewGuid() +// }; + +// User dummyUser = CreateDummyUser(); + +// this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(dummyUser)); + +// Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.FeedService.GetPage(getPageServiceModel)); + +// Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Wrong exception message"); +// } +// #endregion + +// #region HelperMethods +// private User CreateDummyUser() +// { +// return new() +// { +// Id = Guid.NewGuid(), +// UserName = "dummyUser", +// FirstName = "Spas", +// LastName = "Spasov", +// Email = "abv@abv.bg", +// }; +// } + +// private User CreateAnotherDummyUser() +// { +// return new() +// { +// Id = Guid.NewGuid(), +// UserName = "anotherDummyUser", +// FirstName = "Alex", +// LastName = "Spiridonov", +// Email = "a_spiridonov@abv.bg", +// }; +// } +// #endregion +// } +//} diff --git a/src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs b/src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs index b47c8bc..900608c 100644 --- a/src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs +++ b/src/DevHive.Tests/DevHive.Services.Tests/PostService.Tests.cs @@ -1,11 +1,13 @@ using System; +using System.Collections.Generic; 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.Interfaces; +using DevHive.Services.Models.Post; using DevHive.Services.Services; +using Microsoft.AspNetCore.Http; using Moq; using NUnit.Framework; @@ -15,9 +17,10 @@ namespace DevHive.Services.Tests public class PostServiceTests { private const string MESSAGE = "Gosho Trapov"; + private Mock<ICloudService> CloudServiceMock { get; set; } private Mock<IPostRepository> PostRepositoryMock { get; set; } - private Mock<IUserRepository> UserRepositoryMock { get; set; } private Mock<ICommentRepository> CommentRepositoryMock { get; set; } + private Mock<IUserRepository> UserRepositoryMock { get; set; } private Mock<IMapper> MapperMock { get; set; } private PostService PostService { get; set; } @@ -26,247 +29,14 @@ namespace DevHive.Services.Tests public void Setup() { this.PostRepositoryMock = new Mock<IPostRepository>(); + this.CloudServiceMock = new Mock<ICloudService>(); this.UserRepositoryMock = new Mock<IUserRepository>(); this.CommentRepositoryMock = new Mock<ICommentRepository>(); this.MapperMock = new Mock<IMapper>(); - this.PostService = new PostService(this.UserRepositoryMock.Object, this.PostRepositoryMock.Object, this.CommentRepositoryMock.Object, this.MapperMock.Object); + this.PostService = new PostService(this.CloudServiceMock.Object, this.UserRepositoryMock.Object, this.PostRepositoryMock.Object, this.CommentRepositoryMock.Object, this.MapperMock.Object); } #endregion - #region Comment - #region AddComment - [Test] - public async Task AddComment_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully() - { - Guid id = Guid.NewGuid(); - User creator = new User { Id = Guid.NewGuid() }; - CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel - { - Message = MESSAGE - }; - Comment comment = new Comment - { - Message = MESSAGE, - Id = id, - }; - - this.CommentRepositoryMock.Setup(p => p.AddAsync(It.IsAny<Comment>())).Returns(Task.FromResult(true)); - this.CommentRepositoryMock.Setup(p => p.GetCommentByIssuerAndTimeCreatedAsync(It.IsAny<Guid>(), It.IsAny<DateTime>())).Returns(Task.FromResult(comment)); - this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny<Guid>())).Returns(Task.FromResult(true)); - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(creator)); - this.MapperMock.Setup(p => p.Map<Comment>(It.IsAny<CreateCommentServiceModel>())).Returns(comment); - - Guid result = await this.PostService.AddComment(createCommentServiceModel); - - Assert.AreEqual(id, result); - } - - [Test] - public async Task AddComment_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully() - { - CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel - { - Message = MESSAGE - }; - Comment comment = new Comment - { - Message = MESSAGE, - }; - - this.CommentRepositoryMock.Setup(p => p.AddAsync(It.IsAny<Comment>())).Returns(Task.FromResult(false)); - this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny<Guid>())).Returns(Task.FromResult(true)); - this.MapperMock.Setup(p => p.Map<Comment>(It.IsAny<CreateCommentServiceModel>())).Returns(comment); - - Guid result = await this.PostService.AddComment(createCommentServiceModel); - - Assert.IsTrue(result == Guid.Empty); - } - - [Test] - public void AddComment_ThrowsException_WhenPostDoesNotExist() - { - const string EXCEPTION_MESSAGE = "Post does not exist!"; - - CreateCommentServiceModel createCommentServiceModel = new CreateCommentServiceModel - { - Message = MESSAGE - }; - - Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.AddComment(createCommentServiceModel), "AddComment does not throw excpeion when the post does not exist"); - - Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message, "Incorecct exception message"); - } - #endregion - - #region GetCommentById - [Test] - public async Task GetCommentById_ReturnsTheComment_WhenItExists() - { - Guid creatorId = new Guid(); - User creator = new User { Id = creatorId }; - Comment comment = new Comment - { - Message = MESSAGE, - Creator = creator - }; - ReadCommentServiceModel commentServiceModel = new ReadCommentServiceModel - { - Message = MESSAGE - }; - User user = new User - { - Id = creatorId, - }; - - this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(comment)); - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(user)); - this.MapperMock.Setup(p => p.Map<ReadCommentServiceModel>(It.IsAny<Comment>())).Returns(commentServiceModel); - - ReadCommentServiceModel result = await this.PostService.GetCommentById(new Guid()); - - Assert.AreEqual(MESSAGE, result.Message); - } - - [Test] - public void GetCommentById_ThorwsException_WhenTheUserDoesNotExist() - { - const string EXCEPTION_MESSAGE = "The user does not exist"; - Guid creatorId = new Guid(); - User creator = new User { Id = creatorId }; - Comment comment = new Comment - { - Message = MESSAGE, - Creator = creator - }; - - this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(comment)); - - Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.GetCommentById(new Guid()), "GetCommentById does not throw exception when the user does not exist"); - - Assert.AreEqual(EXCEPTION_MESSAGE, ex.Message); - } - - [Test] - public void GetCommentById_ThrowsException_WhenCommentDoesNotExist() - { - string exceptionMessage = "The comment does not exist"; - Guid creatorId = new Guid(); - User user = new User - { - Id = creatorId, - }; - - this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult<Comment>(null)); - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(user)); - - Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.GetCommentById(new Guid())); - - Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); - } - #endregion - - #region UpdateComment - [Test] - public async Task UpdateComment_ReturnsTheIdOfTheComment_WhenUpdatedSuccessfully() - { - Guid id = Guid.NewGuid(); - Comment comment = new Comment - { - Id = id, - Message = MESSAGE - }; - UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel - { - CommentId = id, - NewMessage = MESSAGE - }; - - this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny<Guid>())).Returns(Task.FromResult(true)); - this.CommentRepositoryMock.Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Comment>())).Returns(Task.FromResult(true)); - this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(comment)); - this.MapperMock.Setup(p => p.Map<Comment>(It.IsAny<UpdateCommentServiceModel>())).Returns(comment); - - Guid result = await this.PostService.UpdateComment(updateCommentServiceModel); - - Assert.AreEqual(updateCommentServiceModel.CommentId, result); - } - - [Test] - public async Task UpdateComment_ReturnsEmptyId_WhenTheCommentIsNotUpdatedSuccessfully() - { - Comment comment = new Comment - { - Message = MESSAGE - }; - UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel - { - CommentId = Guid.NewGuid(), - NewMessage = MESSAGE - }; - - this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny<Guid>())).Returns(Task.FromResult(true)); - this.CommentRepositoryMock.Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<Comment>())).Returns(Task.FromResult(false)); - this.MapperMock.Setup(p => p.Map<Comment>(It.IsAny<UpdateCommentServiceModel>())).Returns(comment); - - Guid result = await this.PostService.UpdateComment(updateCommentServiceModel); - - Assert.AreEqual(Guid.Empty, result); - } - - [Test] - public void UpdateComment_ThrowsArgumentException_WhenCommentDoesNotExist() - { - string exceptionMessage = "Comment does not exist!"; - UpdateCommentServiceModel updateCommentServiceModel = new UpdateCommentServiceModel - { - }; - - this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny<Guid>())).Returns(Task.FromResult(false)); - - Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.UpdateComment(updateCommentServiceModel)); - - Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); - } - #endregion - - #region DeleteComment - [Test] - [TestCase(true)] - [TestCase(false)] - public async Task DeleteComment_ShouldReturnIfDeletionIsSuccessfull_WhenCommentExists(bool shouldPass) - { - Guid id = new Guid(); - Comment comment = new Comment(); - - this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny<Guid>())).Returns(Task.FromResult(true)); - this.CommentRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(comment)); - this.CommentRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny<Comment>())).Returns(Task.FromResult(shouldPass)); - - bool result = await this.PostService.DeleteComment(id); - - Assert.AreEqual(shouldPass, result); - } - - [Test] - public void DeleteComment_ThrowsException_WhenCommentDoesNotExist() - { - string exceptionMessage = "Comment does not exist!"; - Guid id = new Guid(); - - this.CommentRepositoryMock.Setup(p => p.DoesCommentExist(It.IsAny<Guid>())).Returns(Task.FromResult(false)); - - Exception ex = Assert.ThrowsAsync<ArgumentException>(() => this.PostService.DeleteComment(id)); - - Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); - } - #endregion - - #region ValidateJwtForComment - //TO DO: Implement - #endregion - #endregion - - #region Posts #region CreatePost [Test] public async Task CreatePost_ReturnsIdOfThePost_WhenItIsSuccessfullyCreated() @@ -275,11 +45,12 @@ namespace DevHive.Services.Tests User creator = new User { Id = Guid.NewGuid() }; CreatePostServiceModel createPostServiceModel = new CreatePostServiceModel { + Files = new List<IFormFile>() }; Post post = new Post { Message = MESSAGE, - Id = postId + Id = postId, }; this.PostRepositoryMock.Setup(p => p.AddAsync(It.IsAny<Post>())).Returns(Task.FromResult(true)); @@ -298,6 +69,7 @@ namespace DevHive.Services.Tests { CreatePostServiceModel createPostServiceModel = new CreatePostServiceModel { + Files = new List<IFormFile>() }; Post post = new Post { @@ -411,7 +183,8 @@ namespace DevHive.Services.Tests UpdatePostServiceModel updatePostServiceModel = new UpdatePostServiceModel { PostId = id, - NewMessage = MESSAGE + NewMessage = MESSAGE, + Files = new List<IFormFile>() }; this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny<Guid>())).Returns(Task.FromResult(true)); @@ -434,7 +207,8 @@ namespace DevHive.Services.Tests UpdatePostServiceModel updatePostServiceModel = new UpdatePostServiceModel { PostId = Guid.NewGuid(), - NewMessage = MESSAGE + NewMessage = MESSAGE, + Files = new List<IFormFile>() }; this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny<Guid>())).Returns(Task.FromResult(true)); @@ -493,10 +267,5 @@ namespace DevHive.Services.Tests Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } #endregion - - #region ValidateJwtForPost - //TO DO: Implement - #endregion - #endregion } } diff --git a/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs b/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs index 7573632..e671adb 100644 --- a/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs +++ b/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs @@ -110,15 +110,15 @@ namespace DevHive.Services.Tests { Name = name }; - CreateTechnologyServiceModel createTechnologyServiceModel = new() + ReadTechnologyServiceModel readTechnologyServiceModel = new() { Name = name }; this.TechnologyRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(technology)); - this.MapperMock.Setup(p => p.Map<CreateTechnologyServiceModel>(It.IsAny<Technology>())).Returns(createTechnologyServiceModel); + this.MapperMock.Setup(p => p.Map<ReadTechnologyServiceModel>(It.IsAny<Technology>())).Returns(readTechnologyServiceModel); - CreateTechnologyServiceModel result = await this.TechnologyService.GetTechnologyById(id); + ReadTechnologyServiceModel result = await this.TechnologyService.GetTechnologyById(id); Assert.AreEqual(name, result.Name); } diff --git a/src/DevHive.Tests/DevHive.Services.Tests/UserService.Tests.cs b/src/DevHive.Tests/DevHive.Services.Tests/UserService.Tests.cs index 1abc0f1..61eb449 100644 --- a/src/DevHive.Tests/DevHive.Services.Tests/UserService.Tests.cs +++ b/src/DevHive.Tests/DevHive.Services.Tests/UserService.Tests.cs @@ -9,6 +9,7 @@ using DevHive.Common.Models.Identity; using DevHive.Common.Models.Misc; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; +using DevHive.Services.Interfaces; using DevHive.Services.Models.Identity.User; using DevHive.Services.Options; using DevHive.Services.Services; @@ -21,6 +22,7 @@ namespace DevHive.Services.Tests [TestFixture] public class UserServiceTests { + private Mock<ICloudService> CloudServiceMock { get; set; } private Mock<IUserRepository> UserRepositoryMock { get; set; } private Mock<IRoleRepository> RoleRepositoryMock { get; set; } private Mock<ILanguageRepository> LanguageRepositoryMock { get; set; } @@ -35,11 +37,12 @@ namespace DevHive.Services.Tests { this.UserRepositoryMock = new Mock<IUserRepository>(); this.RoleRepositoryMock = new Mock<IRoleRepository>(); + this.CloudServiceMock = new Mock<ICloudService>(); this.LanguageRepositoryMock = new Mock<ILanguageRepository>(); this.TechnologyRepositoryMock = new Mock<ITechnologyRepository>(); this.JWTOptions = new JWTOptions("gXfQlU6qpDleFWyimscjYcT3tgFsQg3yoFjcvSLxG56n1Vu2yptdIUq254wlJWjm"); this.MapperMock = new Mock<IMapper>(); - this.UserService = new UserService(this.UserRepositoryMock.Object, this.LanguageRepositoryMock.Object, this.RoleRepositoryMock.Object, this.TechnologyRepositoryMock.Object, this.MapperMock.Object, JWTOptions); + this.UserService = new UserService(this.UserRepositoryMock.Object, this.LanguageRepositoryMock.Object, this.RoleRepositoryMock.Object, this.TechnologyRepositoryMock.Object, this.MapperMock.Object, JWTOptions, this.CloudServiceMock.Object); } #endregion @@ -48,6 +51,7 @@ namespace DevHive.Services.Tests public async Task LoginUser_ReturnsTokenModel_WhenLoggingUserIn() { string somePassword = "GoshoTrapovImaGolemChep"; + const string name = "GoshoTrapov"; string hashedPassword = PasswordModifications.GeneratePasswordHash(somePassword); LoginServiceModel loginServiceModel = new LoginServiceModel { @@ -56,13 +60,14 @@ namespace DevHive.Services.Tests User user = new User { Id = Guid.NewGuid(), - PasswordHash = hashedPassword + PasswordHash = hashedPassword, + UserName = name }; this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(true)); this.UserRepositoryMock.Setup(p => p.GetByUsernameAsync(It.IsAny<string>())).Returns(Task.FromResult(user)); - string JWTSecurityToken = this.WriteJWTSecurityToken(user.Id, user.Roles); + string JWTSecurityToken = this.WriteJWTSecurityToken(user.Id, user.UserName, user.Roles); TokenModel tokenModel = await this.UserService.LoginUser(loginServiceModel); @@ -113,13 +118,15 @@ namespace DevHive.Services.Tests public async Task RegisterUser_ReturnsTokenModel_WhenUserIsSuccessfull() { string somePassword = "GoshoTrapovImaGolemChep"; + const string name = "GoshoTrapov"; RegisterServiceModel registerServiceModel = new RegisterServiceModel { Password = somePassword }; User user = new User { - Id = Guid.NewGuid() + Id = Guid.NewGuid(), + UserName = name }; Role role = new Role { Name = Role.DefaultRole }; HashSet<Role> roles = new HashSet<Role> { role }; @@ -131,7 +138,7 @@ namespace DevHive.Services.Tests this.MapperMock.Setup(p => p.Map<User>(It.IsAny<RegisterServiceModel>())).Returns(user); this.UserRepositoryMock.Setup(p => p.AddAsync(It.IsAny<User>())).Verifiable(); - string JWTSecurityToken = this.WriteJWTSecurityToken(user.Id, roles); + string JWTSecurityToken = this.WriteJWTSecurityToken(user.Id, user.UserName, roles); TokenModel tokenModel = await this.UserService.RegisterUser(registerServiceModel); @@ -353,13 +360,13 @@ namespace DevHive.Services.Tests #endregion #region HelperMethods - private string WriteJWTSecurityToken(Guid userId, HashSet<Role> roles) + private string WriteJWTSecurityToken(Guid userId, string username, HashSet<Role> roles) { byte[] signingKey = Encoding.ASCII.GetBytes(this.JWTOptions.Secret); - HashSet<Claim> claims = new() { new Claim("ID", $"{userId}"), + new Claim("Username", username), }; foreach (var role in roles) |
