YPS-Beer/backend/YPS.Beer.Tests/Controllers/FavouritesControllerTests.cs

202 lines
5.8 KiB
C#
Raw Permalink Normal View History

2023-12-07 00:20:59 +00:00
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<IBeerService>();
var punkService = Substitute.For<IPunkService>();
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<Models.Beer>;
Assert.That(resultValue, Is.Empty);
}
[Test]
public async Task GetFavourites_ReturnsFavouriteBeers_WhenUserHasFavourites()
{
var beerService = Substitute.For<IBeerService>();
var punkService = Substitute.For<IPunkService>();
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<int[]>(ids => ids[0] == 34 && ids[1] == 4)).Returns(new List<Models.Beer>
{
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<Models.Beer>;
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<IBeerService>();
var punkService = Substitute.For<IPunkService>();
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<BadRequestResult>());
}
[Test]
public async Task AddFavourite_ReturnsCreated_WhenFavouriteAddedToUser()
{
var beerService = Substitute.For<IBeerService>();
var punkService = Substitute.For<IPunkService>();
beerService.GetUserById("123456").Returns(new User
{
Id = "123456",
});
beerService.AddFavouriteToUser("123456", Arg.Is<Favourite>(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<CreatedResult>());
await beerService.Received(1).AddFavouriteToUser("123456", Arg.Is<Favourite>(f => f.BeerId == 34));
}
}