using System.Security.Claims; using System.Security.Principal; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Mvc; using NSubstitute; using NUnit.Framework; using YPS.Beer.Controllers; using YPS.Beer.DTOs.Requests; using YPS.Beer.Models; using YPS.Beer.Services; namespace YPS.Beer.Tests.Controllers; public class FavouritesControllerTests { [Test] public async Task GetFavourites_ReturnsEmptyArray_WhenUserHasNoFavourites() { var beerService = Substitute.For(); var punkService = Substitute.For(); beerService.GetUserById("123456").Returns(new User { Id = "123456", }); var identity = new GenericIdentity("adf"); identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "123456")); var controller = new FavouritesController(beerService, punkService) { ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = new ClaimsPrincipal(identity), } } }; var result = await controller.GetFavourites(); var resultObject = result as OkObjectResult; var resultValue = resultObject?.Value as IEnumerable; Assert.That(resultValue, Is.Empty); } [Test] public async Task GetFavourites_ReturnsFavouriteBeers_WhenUserHasFavourites() { var beerService = Substitute.For(); var punkService = Substitute.For(); beerService.GetUserById("123456").Returns(new User { Id = "123456", Favourites = { new Favourite { Id = 1, BeerId = 34, UserId = "123456", }, new Favourite { Id = 2, BeerId = 4, UserId = "123456", }, new Favourite { Id = 3, BeerId = 56, UserId = "654321", } } }); punkService.GetBeers(Arg.Is(ids => ids[0] == 34 && ids[1] == 4)).Returns(new List { new() { Id = 34, Name = "This beer!", }, new() { Id = 4, Name = "Another beer!", } }); var identity = new GenericIdentity("adf"); identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "123456")); var controller = new FavouritesController(beerService, punkService) { ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = new ClaimsPrincipal(identity), } } }; var result = await controller.GetFavourites(); var resultObject = result as OkObjectResult; var resultValue = resultObject?.Value as IEnumerable; var resultList = resultValue?.ToArray(); Assert.That(resultList, Is.Not.Null); Assert.That(resultList, Has.Length.EqualTo(2)); } [Test] public async Task AddFavourite_ReturnsBadRequest_WhenUserAlreadyHasFavourite() { var beerService = Substitute.For(); var punkService = Substitute.For(); beerService.GetUserById("123456").Returns(new User { Id = "123456", Favourites = { new Favourite { Id = 1, BeerId = 34, UserId = "123456", }, } }); var identity = new GenericIdentity("adf"); identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "123456")); var controller = new FavouritesController(beerService, punkService) { ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = new ClaimsPrincipal(identity), } } }; var result = await controller.AddFavourite(new AddFavouriteRequest { BeerId = 34, }); Assert.That(result, Is.TypeOf()); } [Test] public async Task AddFavourite_ReturnsCreated_WhenFavouriteAddedToUser() { var beerService = Substitute.For(); var punkService = Substitute.For(); beerService.GetUserById("123456").Returns(new User { Id = "123456", }); beerService.AddFavouriteToUser("123456", Arg.Is(f => f.BeerId == 34)).Returns(new Favourite { Id = 1, UserId = "123456", BeerId = 34, }); var identity = new GenericIdentity("adf"); identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "123456")); var controller = new FavouritesController(beerService, punkService) { ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = new ClaimsPrincipal(identity), } } }; var result = await controller.AddFavourite(new AddFavouriteRequest { BeerId = 34, }); Assert.That(result, Is.TypeOf()); await beerService.Received(1).AddFavouriteToUser("123456", Arg.Is(f => f.BeerId == 34)); } }