diff options
| author | Danail Dimitrov <danaildimitrov321@gmail.com> | 2021-03-14 21:40:12 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-14 21:40:12 +0200 |
| commit | ac82c773a5ec43c6a59d3d0b7665b67ac9e6bdde (patch) | |
| tree | 8d57d347dd56642105998c3cc3d12bd16ac49aa1 /src/Web | |
| parent | 432fc5890814596d50fb409a6e5dc952d7bb4836 (diff) | |
| parent | 76f71bb8cab45922f3e4999253a1567a0e4af3db (diff) | |
| download | DevHive-ac82c773a5ec43c6a59d3d0b7665b67ac9e6bdde.tar DevHive-ac82c773a5ec43c6a59d3d0b7665b67ac9e6bdde.tar.gz DevHive-ac82c773a5ec43c6a59d3d0b7665b67ac9e6bdde.zip | |
Merge pull request #22 from Team-Kaleidoscope/rating_system
Rating system
Diffstat (limited to 'src/Web')
5 files changed, 306 insertions, 13 deletions
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.Tests/RatingController.Tests.cs b/src/Web/DevHive.Web.Tests/RatingController.Tests.cs new file mode 100644 index 0000000..c7340a6 --- /dev/null +++ b/src/Web/DevHive.Web.Tests/RatingController.Tests.cs @@ -0,0 +1,299 @@ +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<IRatingService> _ratingServiceMock; + private Mock<IMapper> _mapperMock; + private Mock<IJwtService> _jwtServiceMock; + private RatingController _ratingController; + + [SetUp] + public void SetUp() + { + this._ratingServiceMock = new Mock<IRatingService>(); + this._mapperMock = new Mock<IMapper>(); + this._jwtServiceMock = new Mock<IJwtService>(); + 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<Guid>(), It.IsAny<string>())) + .Returns(true); + this._mapperMock + .Setup(p => p.Map<CreateRatingServiceModel>(It.IsAny<CreateRatingWebModel>())) + .Returns(createRatingServiceModel); + this._ratingServiceMock + .Setup(p => p.RatePost(It.IsAny<CreateRatingServiceModel>())) + .ReturnsAsync(ratingId); + + IActionResult result = this._ratingController.RatePost(Guid.Empty, createRatingWebModel, String.Empty).Result; + + Assert.IsInstanceOf<OkObjectResult>(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<Guid>(), It.IsAny<string>())) + .Returns(true); + this._mapperMock + .Setup(p => p.Map<CreateRatingServiceModel>(It.IsAny<CreateRatingWebModel>())) + .Returns(createRatingServiceModel); + this._ratingServiceMock + .Setup(p => p.RatePost(It.IsAny<CreateRatingServiceModel>())) + .ReturnsAsync(Guid.Empty); + + IActionResult result = this._ratingController.RatePost(Guid.Empty, createRatingWebModel, String.Empty).Result; + + Assert.IsInstanceOf<BadRequestResult>(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<ReadRatingServiceModel>(It.IsAny<ReadRatingWebModel>())) + .Returns(readRatingServiceModel); + this._ratingServiceMock + .Setup(p => p.GetRatingById(It.IsAny<Guid>())) + .ReturnsAsync(readRatingServiceModel); + + IActionResult result = this._ratingController.GetRatingById(id).Result; + + Assert.IsInstanceOf<OkObjectResult>(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<ReadRatingServiceModel>(It.IsAny<ReadRatingWebModel>())) + .Returns(readRatingServiceModel); + this._ratingServiceMock + .Setup(p => p.GetRatingByPostAndUser(It.IsAny<Guid>(), It.IsAny<Guid>())) + .ReturnsAsync(readRatingServiceModel); + + IActionResult result = this._ratingController.GetRatingByUserAndPost(userId, postId).Result; + + Assert.IsInstanceOf<OkObjectResult>(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<Guid>(), It.IsAny<string>())) + .Returns(true); + this._mapperMock + .Setup(p => p.Map<UpdateRatingServiceModel>(It.IsAny<UpdateRatingWebModel>())) + .Returns(updateRatingServiceModel); + this._mapperMock + .Setup(p => p.Map<ReadRatingWebModel>(It.IsAny<ReadRatingServiceModel>())) + .Returns(readRatingWebModel); + this._ratingServiceMock + .Setup(p => p.UpdateRating(It.IsAny<UpdateRatingServiceModel>())) + .ReturnsAsync(readRatingServiceModel); + + IActionResult result = this._ratingController.UpdateRating(userId, postId, updateRatingWebModel, String.Empty).Result; + + Assert.IsInstanceOf<OkObjectResult>(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<Guid>(), It.IsAny<string>())) + .Returns(true); + this._mapperMock + .Setup(p => p.Map<UpdateRatingServiceModel>(It.IsAny<UpdateRatingWebModel>())) + .Returns(updateRatingServiceModel); + this._ratingServiceMock + .Setup(p => p.UpdateRating(It.IsAny<UpdateRatingServiceModel>())) + .Returns(Task.FromResult<ReadRatingServiceModel>(null)); + + IActionResult result = this._ratingController.UpdateRating(Guid.Empty, Guid.Empty, updateRatingWebModel, String.Empty).Result; + + Assert.IsInstanceOf<BadRequestResult>(result); + } + #endregion + + #region Delete + [Test] + public void Delete_ReturnsOkResult_WhenRatingIsDeletedSuccessfully() + { + Guid id = Guid.NewGuid(); + + this._jwtServiceMock + .Setup(p => p.ValidateToken(It.IsAny<Guid>(), It.IsAny<string>())) + .Returns(true); + this._ratingServiceMock + .Setup(p => p.DeleteRating(It.IsAny<Guid>())) + .ReturnsAsync(true); + + IActionResult result = this._ratingController.DeleteRating(Guid.Empty, Guid.Empty, String.Empty).Result; + + Assert.IsInstanceOf<OkResult>(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<Guid>(), It.IsAny<string>())) + .Returns(true); + this._ratingServiceMock + .Setup(p => p.DeleteRating(It.IsAny<Guid>())) + .ReturnsAsync(false); + + IActionResult result = this._ratingController.DeleteRating(Guid.Empty, Guid.Empty, String.Empty).Result; + + Assert.IsInstanceOf<BadRequestObjectResult>(result); + + BadRequestObjectResult badRequestObjectResult = result as BadRequestObjectResult; + string resultModel = badRequestObjectResult.Value.ToString(); + + Assert.AreEqual(message, resultModel); + } + #endregion + } +} 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 5716b85..7d21795 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; @@ -11,19 +11,17 @@ using Microsoft.AspNetCore.Mvc; namespace DevHive.Web.Controllers { [ApiController] - //[Authorize(Roles = "Admin,User")] + [Authorize(Roles = "Admin,User")] [Route("api/[controller]")] 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; } @@ -65,7 +63,7 @@ namespace DevHive.Web.Controllers } [HttpPut] - public async Task<IActionResult> UpdateRating(Guid userId, [FromBody] UpdateRatingWebModel updateRatingWebModel, [FromHeader] string authorization) + public async Task<IActionResult> UpdateRating(Guid userId, Guid postId, [FromBody] UpdateRatingWebModel updateRatingWebModel, [FromHeader] string authorization) { if (!this._jwtService.ValidateToken(userId, authorization)) return new UnauthorizedResult(); @@ -73,6 +71,7 @@ namespace DevHive.Web.Controllers UpdateRatingServiceModel updateRatingServiceModel = this._mapper.Map<UpdateRatingServiceModel>(updateRatingWebModel); updateRatingServiceModel.UserId = userId; + updateRatingServiceModel.PostId = postId; ReadRatingServiceModel readRatingServiceModel = await this._rateService.UpdateRating(updateRatingServiceModel); @@ -86,7 +85,7 @@ namespace DevHive.Web.Controllers } [HttpDelete] - public async Task<IActionResult> DeleteTating(Guid userId, Guid ratingId, [FromHeader] string authorization) + public async Task<IActionResult> DeleteRating(Guid userId, Guid ratingId, [FromHeader] string authorization) { if (!this._jwtService.ValidateToken(userId, authorization)) return new UnauthorizedResult(); diff --git a/src/Web/DevHive.Web/Startup.cs b/src/Web/DevHive.Web/Startup.cs index ebd091e..002c718 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 { |
