From f986ca67edd425c32eaec5a20fecdc5786f9d8e3 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Wed, 3 Mar 2021 10:27:26 +0200 Subject: Fixing bugs in rating layer --- src/Web/DevHive.Web.Models/Rating/UpdateRatingWebModel.cs | 4 ---- src/Web/DevHive.Web/Controllers/RatingController.cs | 3 ++- 2 files changed, 2 insertions(+), 5 deletions(-) (limited to 'src/Web') diff --git a/src/Web/DevHive.Web.Models/Rating/UpdateRatingWebModel.cs b/src/Web/DevHive.Web.Models/Rating/UpdateRatingWebModel.cs index 425c3e1..176c099 100644 --- a/src/Web/DevHive.Web.Models/Rating/UpdateRatingWebModel.cs +++ b/src/Web/DevHive.Web.Models/Rating/UpdateRatingWebModel.cs @@ -8,10 +8,6 @@ namespace DevHive.Web.Models.Rating { public class UpdateRatingWebModel { - public Guid Id { get; set; } - - public Guid PostId { get; set; } - public bool IsLike { get; set; } } } diff --git a/src/Web/DevHive.Web/Controllers/RatingController.cs b/src/Web/DevHive.Web/Controllers/RatingController.cs index 5716b85..8d3ac92 100644 --- a/src/Web/DevHive.Web/Controllers/RatingController.cs +++ b/src/Web/DevHive.Web/Controllers/RatingController.cs @@ -65,7 +65,7 @@ namespace DevHive.Web.Controllers } [HttpPut] - public async Task UpdateRating(Guid userId, [FromBody] UpdateRatingWebModel updateRatingWebModel, [FromHeader] string authorization) + public async Task UpdateRating(Guid userId, Guid postId, [FromBody] UpdateRatingWebModel updateRatingWebModel, [FromHeader] string authorization) { if (!this._jwtService.ValidateToken(userId, authorization)) return new UnauthorizedResult(); @@ -73,6 +73,7 @@ namespace DevHive.Web.Controllers UpdateRatingServiceModel updateRatingServiceModel = this._mapper.Map(updateRatingWebModel); updateRatingServiceModel.UserId = userId; + updateRatingServiceModel.PostId = postId; ReadRatingServiceModel readRatingServiceModel = await this._rateService.UpdateRating(updateRatingServiceModel); -- cgit v1.2.3 From 4612913c72c3d2b4d3e28629df9f2ed97f0c29a9 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Wed, 3 Mar 2021 10:55:28 +0200 Subject: Moved Rating service models outside of Post folder --- .../Post/Rating/CreateRatingServiceModel.cs | 13 ------------- .../Post/Rating/ReadRatingServiceModel.cs | 15 --------------- .../Post/Rating/UpdateRatingServiceModel.cs | 15 --------------- .../Rating/CreateRatingServiceModel.cs | 13 +++++++++++++ .../Rating/ReadRatingServiceModel.cs | 15 +++++++++++++++ .../Rating/UpdateRatingServiceModel.cs | 15 +++++++++++++++ .../Configurations/Mapping/RatingMappings.cs | 2 +- .../DevHive.Services/Interfaces/IRatingService.cs | 2 +- src/Services/DevHive.Services/Services/RatingService.cs | 2 +- .../DevHive.Web/Configurations/Mapping/RatingMappings.cs | 2 +- src/Web/DevHive.Web/Controllers/RatingController.cs | 2 +- 11 files changed, 48 insertions(+), 48 deletions(-) delete mode 100644 src/Services/DevHive.Services.Models/Post/Rating/CreateRatingServiceModel.cs delete mode 100644 src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs delete mode 100644 src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs create mode 100644 src/Services/DevHive.Services.Models/Rating/CreateRatingServiceModel.cs create mode 100644 src/Services/DevHive.Services.Models/Rating/ReadRatingServiceModel.cs create mode 100644 src/Services/DevHive.Services.Models/Rating/UpdateRatingServiceModel.cs (limited to 'src/Web') diff --git a/src/Services/DevHive.Services.Models/Post/Rating/CreateRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Post/Rating/CreateRatingServiceModel.cs deleted file mode 100644 index aaeab73..0000000 --- a/src/Services/DevHive.Services.Models/Post/Rating/CreateRatingServiceModel.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Post.Rating -{ - public class CreateRatingServiceModel - { - public Guid UserId { get; set; } - - public Guid PostId { get; set; } - - public bool IsLike { get; set; } - } -} diff --git a/src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs deleted file mode 100644 index 86b4957..0000000 --- a/src/Services/DevHive.Services.Models/Post/Rating/ReadRatingServiceModel.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Post.Rating -{ - public class ReadRatingServiceModel - { - public Guid Id { get; set; } - - public Guid PostId { get; set; } - - public Guid UserId { get; set; } - - public bool IsLike { get; set; } - } -} diff --git a/src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs deleted file mode 100644 index 1ea8d8f..0000000 --- a/src/Services/DevHive.Services.Models/Post/Rating/UpdateRatingServiceModel.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Post.Rating -{ - public class UpdateRatingServiceModel - { - public Guid Id { get; set; } - - public Guid UserId { get; set; } - - public Guid PostId { get; set; } - - public bool IsLike { get; set; } - } -} diff --git a/src/Services/DevHive.Services.Models/Rating/CreateRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Rating/CreateRatingServiceModel.cs new file mode 100644 index 0000000..486e3d2 --- /dev/null +++ b/src/Services/DevHive.Services.Models/Rating/CreateRatingServiceModel.cs @@ -0,0 +1,13 @@ +using System; + +namespace DevHive.Services.Models.Rating +{ + public class CreateRatingServiceModel + { + public Guid UserId { get; set; } + + public Guid PostId { get; set; } + + public bool IsLike { get; set; } + } +} diff --git a/src/Services/DevHive.Services.Models/Rating/ReadRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Rating/ReadRatingServiceModel.cs new file mode 100644 index 0000000..56a5ab0 --- /dev/null +++ b/src/Services/DevHive.Services.Models/Rating/ReadRatingServiceModel.cs @@ -0,0 +1,15 @@ +using System; + +namespace DevHive.Services.Models.Rating +{ + public class ReadRatingServiceModel + { + public Guid Id { get; set; } + + public Guid PostId { get; set; } + + public Guid UserId { get; set; } + + public bool IsLike { get; set; } + } +} diff --git a/src/Services/DevHive.Services.Models/Rating/UpdateRatingServiceModel.cs b/src/Services/DevHive.Services.Models/Rating/UpdateRatingServiceModel.cs new file mode 100644 index 0000000..923e789 --- /dev/null +++ b/src/Services/DevHive.Services.Models/Rating/UpdateRatingServiceModel.cs @@ -0,0 +1,15 @@ +using System; + +namespace DevHive.Services.Models.Rating +{ + public class UpdateRatingServiceModel + { + public Guid Id { get; set; } + + public Guid UserId { get; set; } + + public Guid PostId { get; set; } + + public bool IsLike { get; set; } + } +} diff --git a/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs b/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs index 4534511..9ad5f25 100644 --- a/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs +++ b/src/Services/DevHive.Services/Configurations/Mapping/RatingMappings.cs @@ -1,6 +1,6 @@ using AutoMapper; using DevHive.Data.Models; -using DevHive.Services.Models.Post.Rating; +using DevHive.Services.Models.Rating; namespace DevHive.Services.Configurations.Mapping { diff --git a/src/Services/DevHive.Services/Interfaces/IRatingService.cs b/src/Services/DevHive.Services/Interfaces/IRatingService.cs index b262f45..be33300 100644 --- a/src/Services/DevHive.Services/Interfaces/IRatingService.cs +++ b/src/Services/DevHive.Services/Interfaces/IRatingService.cs @@ -1,7 +1,7 @@ using System; using System.Threading.Tasks; using DevHive.Data.Models; -using DevHive.Services.Models.Post.Rating; +using DevHive.Services.Models.Rating; namespace DevHive.Services.Interfaces { diff --git a/src/Services/DevHive.Services/Services/RatingService.cs b/src/Services/DevHive.Services/Services/RatingService.cs index 6d0d0b9..1f77a6e 100644 --- a/src/Services/DevHive.Services/Services/RatingService.cs +++ b/src/Services/DevHive.Services/Services/RatingService.cs @@ -8,7 +8,7 @@ using AutoMapper; using DevHive.Data.Interfaces; using DevHive.Data.Models; using DevHive.Services.Interfaces; -using DevHive.Services.Models.Post.Rating; +using DevHive.Services.Models.Rating; namespace DevHive.Services.Services { diff --git a/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs b/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs index 23c3eeb..1d731d8 100644 --- a/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs +++ b/src/Web/DevHive.Web/Configurations/Mapping/RatingMappings.cs @@ -1,5 +1,5 @@ using AutoMapper; -using DevHive.Services.Models.Post.Rating; +using DevHive.Services.Models.Rating; using DevHive.Web.Models.Rating; namespace DevHive.Web.Configurations.Mapping diff --git a/src/Web/DevHive.Web/Controllers/RatingController.cs b/src/Web/DevHive.Web/Controllers/RatingController.cs index 8d3ac92..c93a56d 100644 --- a/src/Web/DevHive.Web/Controllers/RatingController.cs +++ b/src/Web/DevHive.Web/Controllers/RatingController.cs @@ -3,7 +3,7 @@ using System.Threading.Tasks; using AutoMapper; using DevHive.Common.Jwt.Interfaces; using DevHive.Services.Interfaces; -using DevHive.Services.Models.Post.Rating; +using DevHive.Services.Models.Rating; using DevHive.Web.Models.Rating; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -- cgit v1.2.3 From 95693f70a2314eab65ac289580d264fe12c42a06 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Wed, 3 Mar 2021 10:56:48 +0200 Subject: Adding authorization to Rating Controller --- src/Web/DevHive.Web/Controllers/RatingController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Web') diff --git a/src/Web/DevHive.Web/Controllers/RatingController.cs b/src/Web/DevHive.Web/Controllers/RatingController.cs index c93a56d..8c44243 100644 --- a/src/Web/DevHive.Web/Controllers/RatingController.cs +++ b/src/Web/DevHive.Web/Controllers/RatingController.cs @@ -11,7 +11,7 @@ using Microsoft.AspNetCore.Mvc; namespace DevHive.Web.Controllers { [ApiController] - //[Authorize(Roles = "Admin,User")] + [Authorize(Roles = "Admin,User")] [Route("api/[controller]")] public class RatingController { -- cgit v1.2.3 From 6cc1bceebfa1ed01472303db226c000c2345b016 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Wed, 3 Mar 2021 10:57:49 +0200 Subject: Removed _userService from RatingController --- src/Web/DevHive.Web/Controllers/RatingController.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src/Web') diff --git a/src/Web/DevHive.Web/Controllers/RatingController.cs b/src/Web/DevHive.Web/Controllers/RatingController.cs index 8c44243..f6ab0c5 100644 --- a/src/Web/DevHive.Web/Controllers/RatingController.cs +++ b/src/Web/DevHive.Web/Controllers/RatingController.cs @@ -16,14 +16,12 @@ namespace DevHive.Web.Controllers public class RatingController { private readonly IRatingService _rateService; - private readonly IUserService _userService; private readonly IMapper _mapper; private readonly IJwtService _jwtService; - public RatingController(IRatingService rateService, IUserService userService, IMapper mapper, IJwtService jwtService) + public RatingController(IRatingService rateService, IMapper mapper, IJwtService jwtService) { this._rateService = rateService; - this._userService = userService; this._mapper = mapper; this._jwtService = jwtService; } -- cgit v1.2.3 From b3fd74a4207334b7b31e9450a078bd10437648c3 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 13 Mar 2021 08:46:31 +0200 Subject: Fixed type in delete rating method name in rating controller --- src/Web/DevHive.Web/Controllers/RatingController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Web') diff --git a/src/Web/DevHive.Web/Controllers/RatingController.cs b/src/Web/DevHive.Web/Controllers/RatingController.cs index f6ab0c5..7d21795 100644 --- a/src/Web/DevHive.Web/Controllers/RatingController.cs +++ b/src/Web/DevHive.Web/Controllers/RatingController.cs @@ -85,7 +85,7 @@ namespace DevHive.Web.Controllers } [HttpDelete] - public async Task DeleteTating(Guid userId, Guid ratingId, [FromHeader] string authorization) + public async Task DeleteRating(Guid userId, Guid ratingId, [FromHeader] string authorization) { if (!this._jwtService.ValidateToken(userId, authorization)) return new UnauthorizedResult(); -- cgit v1.2.3 From b2d6b607a116f1d3d491ac24c484a26f0c956688 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 13 Mar 2021 08:46:50 +0200 Subject: Added tests for rating controller --- .../DevHive.Web.Tests/RatingController.Tests.cs | 257 +++++++++++++++++++++ 1 file changed, 257 insertions(+) create mode 100644 src/Web/DevHive.Web.Tests/RatingController.Tests.cs (limited to 'src/Web') diff --git a/src/Web/DevHive.Web.Tests/RatingController.Tests.cs b/src/Web/DevHive.Web.Tests/RatingController.Tests.cs new file mode 100644 index 0000000..dd8954f --- /dev/null +++ b/src/Web/DevHive.Web.Tests/RatingController.Tests.cs @@ -0,0 +1,257 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Common.Jwt.Interfaces; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.Rating; +using DevHive.Web.Controllers; +using DevHive.Web.Models.Rating; +using Microsoft.AspNetCore.Mvc; +using Moq; +using NUnit.Framework; + +namespace DevHive.Web.Tests +{ + [TestFixture] + public class RatingControllerTests + { + private Mock RatingServiceMock { get; set; } + private Mock MapperMock { get; set; } + private Mock JwtServiceMock { get; set; } + private RatingController RatingController { get; set; } + + [SetUp] + public void SetUp() + { + this.RatingServiceMock = new Mock(); + this.MapperMock = new Mock(); + this.JwtServiceMock = new Mock(); + this.RatingController = new RatingController(this.RatingServiceMock.Object, this.MapperMock.Object, this.JwtServiceMock.Object); + } + + #region Create + [Test] + public void CreateRating_ReturnsOkObjectResult_WhenRatingIsSuccessfullyCreated() + { + Guid postId = Guid.NewGuid(); + CreateRatingWebModel createRatingWebModel = new CreateRatingWebModel + { + PostId = postId, + IsLike = true + }; + CreateRatingServiceModel createRatingServiceModel = new CreateRatingServiceModel + { + PostId = postId, + IsLike = true + }; + Guid ratingId = Guid.NewGuid(); + + this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(createRatingServiceModel); + this.RatingServiceMock.Setup(p => p.RatePost(It.IsAny())).Returns(Task.FromResult(ratingId)); + + IActionResult result = this.RatingController.RatePost(Guid.Empty, createRatingWebModel, String.Empty).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(ratingId, resultId); + } + + [Test] + public void CreateRating_ReturnsBadRequestResult_WhenRatingIsNotSuccessfullyCreated() + { + Guid postId = Guid.NewGuid(); + CreateRatingWebModel createRatingWebModel = new CreateRatingWebModel + { + PostId = postId, + IsLike = true + }; + CreateRatingServiceModel createRatingServiceModel = new CreateRatingServiceModel + { + PostId = postId, + IsLike = true + }; + Guid ratingId = Guid.NewGuid(); + + this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(createRatingServiceModel); + this.RatingServiceMock.Setup(p => p.RatePost(It.IsAny())).Returns(Task.FromResult(Guid.Empty)); + + IActionResult result = this.RatingController.RatePost(Guid.Empty, createRatingWebModel, String.Empty).Result; + + Assert.IsInstanceOf(result); + } + #endregion + + #region Read + [Test] + public void GetRatingById_ReturnsTheRating_WhenItExists() + { + Guid id = Guid.NewGuid(); + Guid userId = Guid.NewGuid(); + Guid postId = Guid.NewGuid(); + ReadRatingWebModel readRatingWebModel = new ReadRatingWebModel + { + Id = id, + UserId = userId, + PostId = postId, + IsLike = true + }; + ReadRatingServiceModel readRatingServiceModel = new ReadRatingServiceModel + { + Id = id, + UserId = userId, + PostId = postId, + IsLike = true + }; + + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(readRatingServiceModel); + this.RatingServiceMock.Setup(p => p.GetRatingById(It.IsAny())).Returns(Task.FromResult(readRatingServiceModel)); + + IActionResult result = this.RatingController.GetRatingById(id).Result; + + Assert.IsInstanceOf(result); + } + + [Test] + public void GetRatingByUserAndPost_ReturnsTheRating_WhenItExists() + { + Guid id = Guid.NewGuid(); + Guid userId = Guid.NewGuid(); + Guid postId = Guid.NewGuid(); + ReadRatingWebModel readRatingWebModel = new ReadRatingWebModel + { + Id = id, + UserId = userId, + PostId = postId, + IsLike = true + }; + ReadRatingServiceModel readRatingServiceModel = new ReadRatingServiceModel + { + Id = id, + UserId = userId, + PostId = postId, + IsLike = true + }; + + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(readRatingServiceModel); + this.RatingServiceMock.Setup(p => p.GetRatingByPostAndUser(It.IsAny(), It.IsAny())).Returns(Task.FromResult(readRatingServiceModel)); + + IActionResult result = this.RatingController.GetRatingByUserAndPost(userId, postId).Result; + + Assert.IsInstanceOf(result); + } + + #endregion + + #region Update + [Test] + public void Update_ShouldReturnOkResult_WhenRatingIsUpdatedSuccessfully() + { + Guid id = Guid.NewGuid(); + Guid userId = Guid.NewGuid(); + Guid postId = Guid.NewGuid(); + UpdateRatingWebModel updateRatingWebModel = new UpdateRatingWebModel + { + IsLike = true + }; + UpdateRatingServiceModel updateRatingServiceModel = new UpdateRatingServiceModel + { + Id = id, + UserId = userId, + PostId = postId, + IsLike = true + }; + ReadRatingWebModel readRatingWebModel = new ReadRatingWebModel + { + Id = id, + UserId = userId, + PostId = postId, + IsLike = true + }; + ReadRatingServiceModel readRatingServiceModel = new ReadRatingServiceModel + { + Id = id, + UserId = userId, + PostId = postId, + IsLike = true + }; + + this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(updateRatingServiceModel); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(readRatingWebModel); + this.RatingServiceMock.Setup(p => p.UpdateRating(It.IsAny())).Returns(Task.FromResult(readRatingServiceModel)); + + IActionResult result = this.RatingController.UpdateRating(userId, postId, updateRatingWebModel, String.Empty).Result; + + Assert.IsInstanceOf(result); + } + + [Test] + public void Update_ShouldReturnBadObjectResult_WhenLanguageIsNotUpdatedSuccessfully() + { + Guid id = Guid.NewGuid(); + UpdateRatingWebModel updateRatingWebModel = new UpdateRatingWebModel + { + IsLike = true + }; + UpdateRatingServiceModel updateRatingServiceModel = new UpdateRatingServiceModel + { + Id = id, + IsLike = true + }; + + this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(updateRatingServiceModel); + this.RatingServiceMock.Setup(p => p.UpdateRating(It.IsAny())).Returns(Task.FromResult(null)); + + IActionResult result = this.RatingController.UpdateRating(Guid.Empty, Guid.Empty, updateRatingWebModel, String.Empty).Result; + + Assert.IsInstanceOf(result); + } + #endregion + + #region Delete + [Test] + public void Delete_ReturnsOkResult_WhenRatingIsDeletedSuccessfully() + { + Guid id = Guid.NewGuid(); + + this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); + this.RatingServiceMock.Setup(p => p.DeleteRating(It.IsAny())).Returns(Task.FromResult(true)); + + IActionResult result = this.RatingController.DeleteRating(Guid.Empty, Guid.Empty, String.Empty).Result; + + Assert.IsInstanceOf(result); + } + + [Test] + public void Delete_ReturnsBadRequestObjectResult_WhenRatingIsNotDeletedSuccessfully() + { + string message = "Could not delete Rating"; + Guid id = Guid.NewGuid(); + + this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); + this.RatingServiceMock.Setup(p => p.DeleteRating(It.IsAny())).Returns(Task.FromResult(false)); + + IActionResult result = this.RatingController.DeleteRating(Guid.Empty, Guid.Empty, String.Empty).Result; + + Assert.IsInstanceOf(result); + + BadRequestObjectResult badRequestObjectResult = result as BadRequestObjectResult; + string resultModel = badRequestObjectResult.Value.ToString(); + + Assert.AreEqual(message, resultModel); + } + #endregion + } +} -- cgit v1.2.3 From e68181d5452f8e2c553c1eb36e689bab1ae080e3 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Sun, 14 Mar 2021 21:16:53 +0200 Subject: added documentaion for rating repository --- .../DevHive.Data/Repositories/RatingRepository.cs | 22 ++++++++++++++++++++++ src/Web/DevHive.Web/Startup.cs | 1 - 2 files changed, 22 insertions(+), 1 deletion(-) (limited to 'src/Web') diff --git a/src/Data/DevHive.Data/Repositories/RatingRepository.cs b/src/Data/DevHive.Data/Repositories/RatingRepository.cs index 1784144..e4a5d5d 100644 --- a/src/Data/DevHive.Data/Repositories/RatingRepository.cs +++ b/src/Data/DevHive.Data/Repositories/RatingRepository.cs @@ -27,6 +27,11 @@ namespace DevHive.Data.Repositories .Include(x => x.Post) .FirstOrDefaultAsync(x => x.Id == id); } + /// + /// Gets all the ratings for a psot. + /// + /// Id of the post. + /// public async Task> GetRatingsByPostId(Guid postId) { return await this._context.Rating @@ -34,12 +39,24 @@ namespace DevHive.Data.Repositories .Include(x => x.Post) .Where(x => x.Post.Id == postId).ToListAsync(); } + /// + /// Checks if a user rated a given post. + /// + /// Id of the user. + /// Id of the psot. + /// True if the user has already rated the post and false if he hasn't. public async Task UserRatedPost(Guid userId, Guid postId) { return await this._context.Rating .Where(x => x.Post.Id == postId) .AnyAsync(x => x.User.Id == userId); } + /// + /// Gets a rating by the post to which the rating corresponds and the user who created it. + /// + /// Id of the user. + /// Id of the post. + /// Rating for the given post by the given user. public async Task GetRatingByUserAndPostId(Guid userId, Guid postId) { return await this._context.Rating @@ -48,6 +65,11 @@ namespace DevHive.Data.Repositories .FirstOrDefaultAsync(x => x.Post.Id == postId && x.User.Id == userId); } + /// + /// Checks if a given rating already exist + /// + /// Id of the rating + /// True if the rating exists and false if it does not. public async Task DoesRatingExist(Guid id) { return await this._context.Rating diff --git a/src/Web/DevHive.Web/Startup.cs b/src/Web/DevHive.Web/Startup.cs index 40f674d..1c714ec 100644 --- a/src/Web/DevHive.Web/Startup.cs +++ b/src/Web/DevHive.Web/Startup.cs @@ -5,7 +5,6 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using DevHive.Web.Configurations.Extensions; using Newtonsoft.Json; -using System.Threading.Tasks; namespace DevHive.Web { -- cgit v1.2.3 From 76f71bb8cab45922f3e4999253a1567a0e4af3db Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 14 Mar 2021 21:39:37 +0200 Subject: Updated rating tests to follow our current code style standards --- .../DevHive.Data.Tests/RatingRepository.Tests.cs | 46 ++--- .../DevHive.Services.Tests/RatingService.Tests.cs | 210 ++++++++++++++------- .../DevHive.Web.Tests/RatingController.Tests.cs | 118 ++++++++---- 3 files changed, 247 insertions(+), 127 deletions(-) (limited to 'src/Web') diff --git a/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs b/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs index 17616fe..2da90d9 100644 --- a/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs +++ b/src/Data/DevHive.Data.Tests/RatingRepository.Tests.cs @@ -12,8 +12,8 @@ namespace DevHive.Data.Tests [TestFixture] public class RatingRepositoryTests { - private DevHiveContext Context { get; set; } - private RatingRepository RatingRepository { get; set; } + private DevHiveContext _context; + private RatingRepository _ratingRepository; #region Setups [SetUp] @@ -22,14 +22,14 @@ namespace DevHive.Data.Tests var optionsBuilder = new DbContextOptionsBuilder() .UseInMemoryDatabase(databaseName: "DevHive_Test_Database"); - this.Context = new DevHiveContext(optionsBuilder.Options); - this.RatingRepository = new RatingRepository(this.Context, null); + this._context = new DevHiveContext(optionsBuilder.Options); + this._ratingRepository = new RatingRepository(this._context, null); } [TearDown] public void TearDown() { - this.Context.Database.EnsureDeleted(); + this._context.Database.EnsureDeleted(); } #endregion @@ -40,7 +40,7 @@ namespace DevHive.Data.Tests Guid ratingId = Guid.NewGuid(); await AddDummyRating(ratingId); - Rating ratingResult = await this.RatingRepository.GetByIdAsync(ratingId); + Rating ratingResult = await this._ratingRepository.GetByIdAsync(ratingId); Assert.AreEqual(ratingResult.Id, ratingId); } @@ -48,7 +48,7 @@ namespace DevHive.Data.Tests [Test] public async Task GetByIdAsync_ReturnsNull_IfRatingDoesNotExist() { - Rating ratingResult = await this.RatingRepository.GetByIdAsync(Guid.NewGuid()); + Rating ratingResult = await this._ratingRepository.GetByIdAsync(Guid.NewGuid()); Assert.IsNull(ratingResult); } @@ -63,7 +63,7 @@ namespace DevHive.Data.Tests await AddDummyRating(Guid.NewGuid(), postId); await AddDummyRating(Guid.NewGuid(), postId); - List result = await this.RatingRepository.GetRatingsByPostId(postId); + List result = await this._ratingRepository.GetRatingsByPostId(postId); Assert.IsNotEmpty(result); } @@ -71,7 +71,7 @@ namespace DevHive.Data.Tests [Test] public async Task GetRatingsByPostId_ReturnsEmptyList_WhenThereAreNoRatings() { - List result = await this.RatingRepository.GetRatingsByPostId(Guid.NewGuid()); + List result = await this._ratingRepository.GetRatingsByPostId(Guid.NewGuid()); Assert.IsEmpty(result); } @@ -88,7 +88,7 @@ namespace DevHive.Data.Tests await AddDummyUser(userId); await AddDummyRating(ratingId, postId, userId); - Rating result = await this.RatingRepository.GetRatingByUserAndPostId(userId, postId); + Rating result = await this._ratingRepository.GetRatingByUserAndPostId(userId, postId); Assert.AreEqual(result.Id, ratingId); } @@ -96,7 +96,7 @@ namespace DevHive.Data.Tests [Test] public async Task GetRatingByUserAndPostId_ReturnsNull_WhenRatingDoesNotExist() { - Rating result = await this.RatingRepository.GetRatingByUserAndPostId(Guid.NewGuid(), Guid.NewGuid()); + Rating result = await this._ratingRepository.GetRatingByUserAndPostId(Guid.NewGuid(), Guid.NewGuid()); Assert.IsNull(result); } @@ -112,7 +112,7 @@ namespace DevHive.Data.Tests await AddDummyUser(userId); await AddDummyRating(Guid.NewGuid(), postId, userId); - bool result = await this.RatingRepository.UserRatedPost(userId, postId); + bool result = await this._ratingRepository.UserRatedPost(userId, postId); Assert.IsTrue(result); } @@ -120,7 +120,7 @@ namespace DevHive.Data.Tests [Test] public async Task UserRatedPost_ReturnsFalse_WhenUserHasNotRatedPost() { - bool result = await this.RatingRepository.UserRatedPost(Guid.NewGuid(), Guid.NewGuid()); + bool result = await this._ratingRepository.UserRatedPost(Guid.NewGuid(), Guid.NewGuid()); Assert.IsFalse(result); } @@ -133,7 +133,7 @@ namespace DevHive.Data.Tests Guid ratingId = Guid.NewGuid(); await AddDummyRating(ratingId); - bool result = await this.RatingRepository.DoesRatingExist(ratingId); + bool result = await this._ratingRepository.DoesRatingExist(ratingId); Assert.IsTrue(result); } @@ -141,7 +141,7 @@ namespace DevHive.Data.Tests [Test] public async Task DoesRatingExist_ReturnsFalse_WhenRatingDoesNotExist() { - bool result = await this.RatingRepository.DoesRatingExist(Guid.NewGuid()); + bool result = await this._ratingRepository.DoesRatingExist(Guid.NewGuid()); Assert.IsFalse(result); } @@ -153,12 +153,12 @@ namespace DevHive.Data.Tests Rating rating = new Rating { Id = ratingId, - Post = this.Context.Posts.FirstOrDefault(x => x.Id == postId), - User = this.Context.Users.FirstOrDefault(x => x.Id == userId) + Post = this._context.Posts.FirstOrDefault(x => x.Id == postId), + User = this._context.Users.FirstOrDefault(x => x.Id == userId) }; - await this.Context.Rating.AddAsync(rating); - await this.Context.SaveChangesAsync(); + await this._context.Rating.AddAsync(rating); + await this._context.SaveChangesAsync(); } private async Task AddDummyPost(Guid postId) @@ -169,8 +169,8 @@ namespace DevHive.Data.Tests Message = "Never gonna give you up" }; - await this.Context.Posts.AddAsync(post); - await this.Context.SaveChangesAsync(); + await this._context.Posts.AddAsync(post); + await this._context.SaveChangesAsync(); } private async Task AddDummyUser(Guid userId) @@ -180,8 +180,8 @@ namespace DevHive.Data.Tests Id = userId }; - await this.Context.Users.AddAsync(user); - await this.Context.SaveChangesAsync(); + await this._context.Users.AddAsync(user); + await this._context.SaveChangesAsync(); } #endregion } diff --git a/src/Services/DevHive.Services.Tests/RatingService.Tests.cs b/src/Services/DevHive.Services.Tests/RatingService.Tests.cs index 89c1a2e..5f84530 100644 --- a/src/Services/DevHive.Services.Tests/RatingService.Tests.cs +++ b/src/Services/DevHive.Services.Tests/RatingService.Tests.cs @@ -13,21 +13,21 @@ namespace DevHive.Services.Tests [TestFixture] public class RatingServiceTests { - private Mock PostRepositoryMock { get; set; } - private Mock RatingRepositoryMock { get; set; } - private Mock UserRepositoryMock { get; set; } - private Mock MapperMock { get; set; } - private RatingService RatingService { get; set; } + private Mock _postRepositoryMock; + private Mock _ratingRepositoryMock; + private Mock _userRepositoryMock; + private Mock _mapperMock; + private RatingService _ratingService; #region SetUps [SetUp] public void SetUp() { - this.PostRepositoryMock = new Mock(); - this.RatingRepositoryMock = new Mock(); - this.UserRepositoryMock = new Mock(); - this.MapperMock = new Mock(); - this.RatingService = new RatingService(this.PostRepositoryMock.Object, this.RatingRepositoryMock.Object, this.UserRepositoryMock.Object, this.MapperMock.Object); + this._postRepositoryMock = new Mock(); + this._ratingRepositoryMock = new Mock(); + this._userRepositoryMock = new Mock(); + this._mapperMock = new Mock(); + this._ratingService = new RatingService(this._postRepositoryMock.Object, this._ratingRepositoryMock.Object, this._userRepositoryMock.Object, this._mapperMock.Object); } #endregion @@ -59,15 +59,29 @@ namespace DevHive.Services.Tests Id = postId }; - this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny())).Returns(Task.FromResult(true)); - this.RatingRepositoryMock.Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); - this.PostRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(post)); - this.RatingRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(true)); - this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())).Returns(Task.FromResult(rating)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(rating); - - Guid result = await this.RatingService.RatePost(createRatingServiceModel); + this._postRepositoryMock + .Setup(p => p.DoesPostExist(It.IsAny())) + .ReturnsAsync(true); + this._ratingRepositoryMock + .Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())) + .ReturnsAsync(false); + this._userRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .ReturnsAsync(user); + this._postRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .ReturnsAsync(post); + this._ratingRepositoryMock + .Setup(p => p.AddAsync(It.IsAny())) + .ReturnsAsync(true); + this._ratingRepositoryMock + .Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())) + .ReturnsAsync(rating); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(rating); + + Guid result = await this._ratingService.RatePost(createRatingServiceModel); Assert.AreEqual(id, result); } @@ -99,14 +113,26 @@ namespace DevHive.Services.Tests Id = postId }; - this.PostRepositoryMock.Setup(p => p.DoesPostExist(It.IsAny())).Returns(Task.FromResult(true)); - this.RatingRepositoryMock.Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); - this.PostRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(post)); - this.RatingRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(false)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(rating); - - Guid result = await this.RatingService.RatePost(createRatingServiceModel); + this._postRepositoryMock + .Setup(p => p.DoesPostExist(It.IsAny())) + .ReturnsAsync(true); + this._ratingRepositoryMock + .Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())) + .ReturnsAsync(false); + this._userRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .ReturnsAsync(user); + this._postRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .ReturnsAsync(post); + this._ratingRepositoryMock + .Setup(p => p.AddAsync(It.IsAny())) + .ReturnsAsync(false); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(rating); + + Guid result = await this._ratingService.RatePost(createRatingServiceModel); Assert.AreEqual(result, Guid.Empty); } @@ -134,10 +160,14 @@ namespace DevHive.Services.Tests IsLike = isLike }; - this.RatingRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(rating)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(readRatingServiceModel); + this._ratingRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .ReturnsAsync(rating); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(readRatingServiceModel); - ReadRatingServiceModel result = await this.RatingService.GetRatingById(id); + ReadRatingServiceModel result = await this._ratingService.GetRatingById(id); Assert.AreEqual(isLike, result.IsLike); } @@ -146,9 +176,11 @@ namespace DevHive.Services.Tests public void GetRatingById_ThrowsException_WhenRatingDoesNotExist() { string exceptionMessage = "The rating does not exist"; - this.RatingRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(null)); + this._ratingRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .Returns(Task.FromResult(null)); - Exception ex = Assert.ThrowsAsync(() => this.RatingService.GetRatingById(Guid.Empty)); + Exception ex = Assert.ThrowsAsync(() => this._ratingService.GetRatingById(Guid.Empty)); Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } @@ -174,10 +206,14 @@ namespace DevHive.Services.Tests IsLike = isLike }; - this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())).Returns(Task.FromResult(rating)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(readRatingServiceModel); + this._ratingRepositoryMock + .Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())) + .ReturnsAsync(rating); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(readRatingServiceModel); - ReadRatingServiceModel result = await this.RatingService.GetRatingByPostAndUser(user.Id, Guid.Empty); + ReadRatingServiceModel result = await this._ratingService.GetRatingByPostAndUser(user.Id, Guid.Empty); Assert.AreEqual(isLike, result.IsLike); } @@ -186,9 +222,11 @@ namespace DevHive.Services.Tests public void GetRatingByPostAndUser_ThrowsException_WhenRatingDoesNotExist() { string exceptionMessage = "The rating does not exist"; - this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())).Returns(Task.FromResult(null)); + this._ratingRepositoryMock + .Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(null)); - Exception ex = Assert.ThrowsAsync(() => this.RatingService.GetRatingById(Guid.Empty)); + Exception ex = Assert.ThrowsAsync(() => this._ratingService.GetRatingById(Guid.Empty)); Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } @@ -221,13 +259,23 @@ namespace DevHive.Services.Tests IsLike = isLike }; - this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())).Returns(Task.FromResult(rating)); - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); - this.RatingRepositoryMock.Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); - this.RatingRepositoryMock.Setup(p => p.EditAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(readRatingServiceModel); - - ReadRatingServiceModel result = await this.RatingService.UpdateRating(updateRatingServiceModel); + this._ratingRepositoryMock + .Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())) + .ReturnsAsync(rating); + this._userRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .ReturnsAsync(user); + this._ratingRepositoryMock + .Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())) + .ReturnsAsync(true); + this._ratingRepositoryMock + .Setup(p => p.EditAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(true); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(readRatingServiceModel); + + ReadRatingServiceModel result = await this._ratingService.UpdateRating(updateRatingServiceModel); Assert.AreEqual(result, readRatingServiceModel); } @@ -258,12 +306,20 @@ namespace DevHive.Services.Tests IsLike = isLike }; - this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())).Returns(Task.FromResult(rating)); - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); - this.RatingRepositoryMock.Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())).Returns(Task.FromResult(true)); - this.RatingRepositoryMock.Setup(p => p.EditAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); - - ReadRatingServiceModel result = await this.RatingService.UpdateRating(updateRatingServiceModel); + this._ratingRepositoryMock + .Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())) + .ReturnsAsync(rating); + this._userRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .ReturnsAsync(user); + this._ratingRepositoryMock + .Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())) + .ReturnsAsync(true); + this._ratingRepositoryMock + .Setup(p => p.EditAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(false); + + ReadRatingServiceModel result = await this._ratingService.UpdateRating(updateRatingServiceModel); Assert.IsNull(result); } @@ -278,9 +334,11 @@ namespace DevHive.Services.Tests IsLike = true }; - this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())).Returns(Task.FromResult(null)); + this._ratingRepositoryMock + .Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())) + .Returns(Task.FromResult(null)); - Exception ex = Assert.ThrowsAsync(() => this.RatingService.UpdateRating(updateRatingServiceModel)); + Exception ex = Assert.ThrowsAsync(() => this._ratingService.UpdateRating(updateRatingServiceModel)); Assert.AreEqual(ex.Message, exceptionMessage); } @@ -307,11 +365,17 @@ namespace DevHive.Services.Tests User = user }; - this.RatingRepositoryMock.Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())).Returns(Task.FromResult(rating)); - this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(user)); - this.RatingRepositoryMock.Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())).Returns(Task.FromResult(false)); + this._ratingRepositoryMock + .Setup(p => p.GetRatingByUserAndPostId(It.IsAny(), It.IsAny())) + .ReturnsAsync(rating); + this._userRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .ReturnsAsync(user); + this._ratingRepositoryMock + .Setup(p => p.UserRatedPost(It.IsAny(), It.IsAny())) + .ReturnsAsync(false); - Exception ex = Assert.ThrowsAsync(() => this.RatingService.UpdateRating(updateRatingServiceModel)); + Exception ex = Assert.ThrowsAsync(() => this._ratingService.UpdateRating(updateRatingServiceModel)); Assert.AreEqual(ex.Message, exceptionMessage); } @@ -327,11 +391,17 @@ namespace DevHive.Services.Tests Id = ratingId }; - this.RatingRepositoryMock.Setup(p => p.DoesRatingExist(It.IsAny())).Returns(Task.FromResult(true)); - this.RatingRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(null)); - this.RatingRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny())).Returns(Task.FromResult(true)); + this._ratingRepositoryMock + .Setup(p => p.DoesRatingExist(It.IsAny())) + .ReturnsAsync(true); + this._ratingRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .Returns(Task.FromResult(null)); + this._ratingRepositoryMock + .Setup(p => p.DeleteAsync(It.IsAny())) + .ReturnsAsync(true); - bool result = await this.RatingService.DeleteRating(ratingId); + bool result = await this._ratingService.DeleteRating(ratingId); Assert.IsTrue(result); } @@ -345,11 +415,17 @@ namespace DevHive.Services.Tests Id = ratingId }; - this.RatingRepositoryMock.Setup(p => p.DoesRatingExist(It.IsAny())).Returns(Task.FromResult(true)); - this.RatingRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(null)); - this.RatingRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny())).Returns(Task.FromResult(false)); + this._ratingRepositoryMock + .Setup(p => p.DoesRatingExist(It.IsAny())) + .ReturnsAsync(true); + this._ratingRepositoryMock + .Setup(p => p.GetByIdAsync(It.IsAny())) + .Returns(Task.FromResult(null)); + this._ratingRepositoryMock + .Setup(p => p.DeleteAsync(It.IsAny())) + .ReturnsAsync(false); - bool result = await this.RatingService.DeleteRating(ratingId); + bool result = await this._ratingService.DeleteRating(ratingId); Assert.IsFalse(result); } @@ -359,9 +435,11 @@ namespace DevHive.Services.Tests { string exceptionMessage = "Rating does not exist!"; - this.RatingRepositoryMock.Setup(p => p.DoesRatingExist(It.IsAny())).Returns(Task.FromResult(false)); + this._ratingRepositoryMock + .Setup(p => p.DoesRatingExist(It.IsAny())) + .ReturnsAsync(false); - Exception ex = Assert.ThrowsAsync(() => this.RatingService.DeleteRating(Guid.Empty)); + Exception ex = Assert.ThrowsAsync(() => this._ratingService.DeleteRating(Guid.Empty)); Assert.AreEqual(ex.Message, exceptionMessage); } diff --git a/src/Web/DevHive.Web.Tests/RatingController.Tests.cs b/src/Web/DevHive.Web.Tests/RatingController.Tests.cs index dd8954f..c7340a6 100644 --- a/src/Web/DevHive.Web.Tests/RatingController.Tests.cs +++ b/src/Web/DevHive.Web.Tests/RatingController.Tests.cs @@ -16,18 +16,18 @@ namespace DevHive.Web.Tests [TestFixture] public class RatingControllerTests { - private Mock RatingServiceMock { get; set; } - private Mock MapperMock { get; set; } - private Mock JwtServiceMock { get; set; } - private RatingController RatingController { get; set; } + private Mock _ratingServiceMock; + private Mock _mapperMock; + private Mock _jwtServiceMock; + private RatingController _ratingController; [SetUp] public void SetUp() { - this.RatingServiceMock = new Mock(); - this.MapperMock = new Mock(); - this.JwtServiceMock = new Mock(); - this.RatingController = new RatingController(this.RatingServiceMock.Object, this.MapperMock.Object, this.JwtServiceMock.Object); + this._ratingServiceMock = new Mock(); + this._mapperMock = new Mock(); + this._jwtServiceMock = new Mock(); + this._ratingController = new RatingController(this._ratingServiceMock.Object, this._mapperMock.Object, this._jwtServiceMock.Object); } #region Create @@ -47,11 +47,17 @@ namespace DevHive.Web.Tests }; Guid ratingId = Guid.NewGuid(); - this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(createRatingServiceModel); - this.RatingServiceMock.Setup(p => p.RatePost(It.IsAny())).Returns(Task.FromResult(ratingId)); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())) + .Returns(true); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(createRatingServiceModel); + this._ratingServiceMock + .Setup(p => p.RatePost(It.IsAny())) + .ReturnsAsync(ratingId); - IActionResult result = this.RatingController.RatePost(Guid.Empty, createRatingWebModel, String.Empty).Result; + IActionResult result = this._ratingController.RatePost(Guid.Empty, createRatingWebModel, String.Empty).Result; Assert.IsInstanceOf(result); @@ -82,11 +88,17 @@ namespace DevHive.Web.Tests }; Guid ratingId = Guid.NewGuid(); - this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(createRatingServiceModel); - this.RatingServiceMock.Setup(p => p.RatePost(It.IsAny())).Returns(Task.FromResult(Guid.Empty)); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())) + .Returns(true); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(createRatingServiceModel); + this._ratingServiceMock + .Setup(p => p.RatePost(It.IsAny())) + .ReturnsAsync(Guid.Empty); - IActionResult result = this.RatingController.RatePost(Guid.Empty, createRatingWebModel, String.Empty).Result; + IActionResult result = this._ratingController.RatePost(Guid.Empty, createRatingWebModel, String.Empty).Result; Assert.IsInstanceOf(result); } @@ -114,10 +126,14 @@ namespace DevHive.Web.Tests IsLike = true }; - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(readRatingServiceModel); - this.RatingServiceMock.Setup(p => p.GetRatingById(It.IsAny())).Returns(Task.FromResult(readRatingServiceModel)); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(readRatingServiceModel); + this._ratingServiceMock + .Setup(p => p.GetRatingById(It.IsAny())) + .ReturnsAsync(readRatingServiceModel); - IActionResult result = this.RatingController.GetRatingById(id).Result; + IActionResult result = this._ratingController.GetRatingById(id).Result; Assert.IsInstanceOf(result); } @@ -143,10 +159,14 @@ namespace DevHive.Web.Tests IsLike = true }; - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(readRatingServiceModel); - this.RatingServiceMock.Setup(p => p.GetRatingByPostAndUser(It.IsAny(), It.IsAny())).Returns(Task.FromResult(readRatingServiceModel)); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(readRatingServiceModel); + this._ratingServiceMock + .Setup(p => p.GetRatingByPostAndUser(It.IsAny(), It.IsAny())) + .ReturnsAsync(readRatingServiceModel); - IActionResult result = this.RatingController.GetRatingByUserAndPost(userId, postId).Result; + IActionResult result = this._ratingController.GetRatingByUserAndPost(userId, postId).Result; Assert.IsInstanceOf(result); } @@ -186,12 +206,20 @@ namespace DevHive.Web.Tests IsLike = true }; - this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(updateRatingServiceModel); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(readRatingWebModel); - this.RatingServiceMock.Setup(p => p.UpdateRating(It.IsAny())).Returns(Task.FromResult(readRatingServiceModel)); - - IActionResult result = this.RatingController.UpdateRating(userId, postId, updateRatingWebModel, String.Empty).Result; + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())) + .Returns(true); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(updateRatingServiceModel); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(readRatingWebModel); + this._ratingServiceMock + .Setup(p => p.UpdateRating(It.IsAny())) + .ReturnsAsync(readRatingServiceModel); + + IActionResult result = this._ratingController.UpdateRating(userId, postId, updateRatingWebModel, String.Empty).Result; Assert.IsInstanceOf(result); } @@ -210,11 +238,17 @@ namespace DevHive.Web.Tests IsLike = true }; - this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(updateRatingServiceModel); - this.RatingServiceMock.Setup(p => p.UpdateRating(It.IsAny())).Returns(Task.FromResult(null)); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())) + .Returns(true); + this._mapperMock + .Setup(p => p.Map(It.IsAny())) + .Returns(updateRatingServiceModel); + this._ratingServiceMock + .Setup(p => p.UpdateRating(It.IsAny())) + .Returns(Task.FromResult(null)); - IActionResult result = this.RatingController.UpdateRating(Guid.Empty, Guid.Empty, updateRatingWebModel, String.Empty).Result; + IActionResult result = this._ratingController.UpdateRating(Guid.Empty, Guid.Empty, updateRatingWebModel, String.Empty).Result; Assert.IsInstanceOf(result); } @@ -226,10 +260,14 @@ namespace DevHive.Web.Tests { Guid id = Guid.NewGuid(); - this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); - this.RatingServiceMock.Setup(p => p.DeleteRating(It.IsAny())).Returns(Task.FromResult(true)); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())) + .Returns(true); + this._ratingServiceMock + .Setup(p => p.DeleteRating(It.IsAny())) + .ReturnsAsync(true); - IActionResult result = this.RatingController.DeleteRating(Guid.Empty, Guid.Empty, String.Empty).Result; + IActionResult result = this._ratingController.DeleteRating(Guid.Empty, Guid.Empty, String.Empty).Result; Assert.IsInstanceOf(result); } @@ -240,10 +278,14 @@ namespace DevHive.Web.Tests string message = "Could not delete Rating"; Guid id = Guid.NewGuid(); - this.JwtServiceMock.Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())).Returns(true); - this.RatingServiceMock.Setup(p => p.DeleteRating(It.IsAny())).Returns(Task.FromResult(false)); + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny(), It.IsAny())) + .Returns(true); + this._ratingServiceMock + .Setup(p => p.DeleteRating(It.IsAny())) + .ReturnsAsync(false); - IActionResult result = this.RatingController.DeleteRating(Guid.Empty, Guid.Empty, String.Empty).Result; + IActionResult result = this._ratingController.DeleteRating(Guid.Empty, Guid.Empty, String.Empty).Result; Assert.IsInstanceOf(result); -- cgit v1.2.3