public async Task RequestFriendship_ShouldReturnForbiddenResult_WhenWhenRequesterAndAddresseeIdAreTheSame() { // Arrange RequestFriendshipBody body = new RequestFriendshipBody { AddresseeId = 1 }; Claim expectedNameIdentifierClaim = new Claim(ClaimTypes.NameIdentifier, body.AddresseeId.ToString()); Mock <ClaimsPrincipal> userMock = new Mock <ClaimsPrincipal>(); userMock .Setup(m => m.FindFirst(ClaimTypes.NameIdentifier)) .Returns(expectedNameIdentifierClaim); FriendshipController controller = new FriendshipController(null, null) { ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = userMock.Object } } }; // Act ActionResult <FriendshipResource> response = await controller.RequestFriendship(body); // Assert ObjectResult result = Assert.IsType <ObjectResult>(response.Result); ErrorResource error = Assert.IsType <ErrorResource>(result.Value); Assert.Equal(StatusCodes.Status403Forbidden, error.StatusCode); }
public void Setup() { _controller = new FriendshipController( new MockFriendshipRepo(), null ); }
public async Task UpdateFriendshipStatus_ShouldReturnNotFound_WhenFriendshipDoesNotExist() { // Arrange const int friendshipId = 87921; UpdateFriendshipStatusBody model = new UpdateFriendshipStatusBody { FriendshipStatusId = FriendshipStatusId.Accepted }; Mock <IMediator> mediatorMock = new Mock <IMediator>(); mediatorMock .Setup(m => m.Send(It.IsAny <FriendshipExistsQuery>(), It.IsAny <CancellationToken>())) .ReturnsAsync(false); FriendshipController controller = new FriendshipController(mediatorMock.Object, null); // Act ActionResult response = await controller.UpdateFriendshipStatus(friendshipId, model); // Assert NotFoundObjectResult result = Assert.IsType <NotFoundObjectResult>(response); ErrorResource error = Assert.IsType <ErrorResource>(result.Value); Assert.Equal(StatusCodes.Status404NotFound, error.StatusCode); }
public async Task UpdateFriendshipStatus_ShouldReturnNoContentResult() { // Arrange const int friendshipId = 1; UpdateFriendshipStatusBody model = new UpdateFriendshipStatusBody { FriendshipStatusId = FriendshipStatusId.Accepted }; Mock <IMediator> mediatorMock = new Mock <IMediator>(); mediatorMock .Setup(m => m.Send(It.IsAny <FriendshipExistsQuery>(), It.IsAny <CancellationToken>())) .ReturnsAsync(true); mediatorMock .Setup(m => m.Send(It.IsAny <UpdateFriendshipStatusCommand>(), It.IsAny <CancellationToken>())) .Returns(Unit.Task); FriendshipController controller = new FriendshipController(mediatorMock.Object, null); // Act ActionResult response = await controller.UpdateFriendshipStatus(friendshipId, model); // Assert Assert.IsType <NoContentResult>(response); mediatorMock.Verify(m => m.Send(It.IsAny <UpdateFriendshipStatusCommand>(), It.IsAny <CancellationToken>()), Times.Once); }
public async Task GetFriendshipById_ShouldReturnOkResult_WhenFriendshipExists() { // Arrange const int friendshipId = 1; FriendshipResource expectedFriendship = new FriendshipResource { FriendshipId = friendshipId }; Mock <IMediator> mediatorMock = new Mock <IMediator>(); mediatorMock .Setup(m => m.Send(It.IsAny <GetFriendshipByIdQuery>(), It.IsAny <CancellationToken>())) .ReturnsAsync(expectedFriendship); FriendshipController controller = new FriendshipController(mediatorMock.Object, null); // Act ActionResult <FriendshipResource> response = await controller.GetFriendshipById(friendshipId); // Assert OkObjectResult result = Assert.IsType <OkObjectResult>(response.Result); FriendshipResource friendship = Assert.IsType <FriendshipResource>(result.Value); Assert.Equal(friendshipId, friendship.FriendshipId); }
static void CliqueStuff() { var fileName = "friendships.txt"; var people = FriendshipController.LoadPeople(fileName); int a = 0; foreach (var clique in FriendshipController.GetCliques(people, people[0])) { if (a == 3) { break; } string cliqueString = ""; foreach (var person in clique) { cliqueString += person.Name; } Console.WriteLine(cliqueString); } Console.Read(); foreach (var person in people) { Console.WriteLine(person.Name + " has " + person.Friends.Count + " friends."); } Console.WriteLine(fileName); }
public async Task UpdateFriendshipStatus_ShouldReturnBadRequestResult_WhenModelValidationFails() { // Arrange FriendshipController controller = new FriendshipController(null, null); controller.ModelState.AddModelError("", ""); // Act ActionResult response = await controller.UpdateFriendshipStatus(0, null); // Assert Assert.IsType <BadRequestObjectResult>(response); }
public async Task RequestFriendship_ShouldReturnBadRequestResult_WhenModelValidationFails() { // Arrange RequestFriendshipBody model = new RequestFriendshipBody { AddresseeId = -123 }; FriendshipController controller = new FriendshipController(null, null); controller.ModelState.AddModelError("", ""); // Act ActionResult <FriendshipResource> response = await controller.RequestFriendship(model); // Assert Assert.IsType <BadRequestObjectResult>(response.Result); }
public async Task RequestFriendship_ShouldReturnForbiddenResult_WhenUserCombinationAlreadyExists() { // Arrange RequestFriendshipBody body = new RequestFriendshipBody { AddresseeId = 1 }; Claim expectedNameIdentifierClaim = new Claim(ClaimTypes.NameIdentifier, "2"); Mock <ClaimsPrincipal> userMock = new Mock <ClaimsPrincipal>(); userMock .Setup(m => m.FindFirst(ClaimTypes.NameIdentifier)) .Returns(expectedNameIdentifierClaim); Mock <IMediator> mediatorMock = new Mock <IMediator>(); mediatorMock .Setup(m => m.Send(It.IsAny <UserExistsQuery>(), It.IsAny <CancellationToken>())) .ReturnsAsync(true); mediatorMock .Setup(m => m.Send(It.IsAny <FriendshipCombinationExistsQuery>(), It.IsAny <CancellationToken>())) .ReturnsAsync(true); FriendshipController controller = new FriendshipController(mediatorMock.Object, null) { ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = userMock.Object } } }; // Act ActionResult <FriendshipResource> response = await controller.RequestFriendship(body); // Assert ObjectResult result = Assert.IsType <ObjectResult>(response.Result); ErrorResource error = Assert.IsType <ErrorResource>(result.Value); Assert.Equal(StatusCodes.Status403Forbidden, error.StatusCode); }
public async Task GetSentFriendShips_HaveSentFriendships_ReturnOkObjectResultWithListOfSentFriendsipDtos() { // Arrange var friendship1 = new Friendship { FromUserId = user1.Id, FromUser = user1, ToUserId = user2.Id, ToUser = user2, State = FriendshipState.Accepted, Id = Guid.NewGuid(), }; var friendship2 = new Friendship { FromUserId = user1.Id, FromUser = user1, ToUserId = user3.Id, ToUser = user3, State = FriendshipState.Accepted, Id = Guid.NewGuid(), }; var mockService = new Mock <IFriendshipService>(); mockService.Setup(service => service.GetSentFriendshipsAsync()) .ReturnsAsync(new List <Friendship> { friendship1, friendship2 }); var controller = new FriendshipController(mockService.Object, mapper); // Act var result = await controller.GetSentFriendShips(); // Assert Assert.IsNotNull(result); Assert.IsInstanceOf <ActionResult>(result.Result); Assert.IsInstanceOf <OkObjectResult>(result.Result); var objectResult = (result.Result as ObjectResult); Assert.AreEqual(objectResult.StatusCode, 200); var sentFriendshipDtos = (IEnumerable <SentFriendshipDto>)(result.Result as OkObjectResult).Value; Assert.AreEqual(sentFriendshipDtos.Count(), 2); }
public async Task GetSentFriendShips_HaveNotSentFriendships_ReturnOkObjectResultWithEmptyList() { // Arrange var mockService = new Mock <IFriendshipService>(); var controller = new FriendshipController(mockService.Object, mapper); // Act var result = await controller.GetSentFriendShips(); // Assert Assert.IsNotNull(result); Assert.IsInstanceOf <ActionResult>(result.Result); Assert.IsInstanceOf <OkObjectResult>(result.Result); var objectResult = (result.Result as ObjectResult); Assert.AreEqual(objectResult.StatusCode, 200); var sentFriendshipDtos = (IEnumerable <SentFriendshipDto>)(result.Result as OkObjectResult).Value; Assert.IsEmpty(sentFriendshipDtos); }
public async Task GetFriendshipById_ShouldReturnNotFoundResult_WhenFriendshipIsNotFound() { // Arrange const int friendshipId = 632; Mock <IMediator> mediatorMock = new Mock <IMediator>(); mediatorMock .Setup(m => m.Send(It.IsAny <GetFriendshipByIdQuery>(), It.IsAny <CancellationToken>())) .ReturnsAsync((FriendshipResource)null); FriendshipController controller = new FriendshipController(mediatorMock.Object, null); // Act ActionResult <FriendshipResource> response = await controller.GetFriendshipById(friendshipId); // Assert NotFoundObjectResult result = Assert.IsType <NotFoundObjectResult>(response.Result); ErrorResource error = Assert.IsType <ErrorResource>(result.Value); Assert.Equal(StatusCodes.Status404NotFound, error.StatusCode); }
public async Task RequestFriendship_ShouldReturnCreatedResult_WithCreatedResource() { // Arrange RequestFriendshipBody body = new RequestFriendshipBody { AddresseeId = 2 }; FriendshipResource expectedFriendship = new FriendshipResource { AddresseeId = 2, RequesterId = 1, FriendshipId = 1 }; Claim expectedNameIdentifierClaim = new Claim(ClaimTypes.NameIdentifier, "1"); Mock <ClaimsPrincipal> userMock = new Mock <ClaimsPrincipal>(); userMock .Setup(m => m.FindFirst(ClaimTypes.NameIdentifier)) .Returns(expectedNameIdentifierClaim); Mock <IMediator> mediatorMock = new Mock <IMediator>(); mediatorMock .Setup(m => m.Send(It.IsAny <RequestFriendshipCommand>(), It.IsAny <CancellationToken>())) .ReturnsAsync(expectedFriendship); mediatorMock .Setup(m => m.Send(It.IsAny <UserExistsQuery>(), It.IsAny <CancellationToken>())) .ReturnsAsync(true); mediatorMock .Setup(m => m.Send(It.IsAny <FriendshipCombinationExistsQuery>(), It.IsAny <CancellationToken>())) .ReturnsAsync(false); MapperConfiguration mapperConfiguration = new MapperConfiguration(config => { config.CreateMap <RequestFriendshipBody, RequestFriendshipCommand>(); }); IMapper mapperMock = mapperConfiguration.CreateMapper(); FriendshipController controller = new FriendshipController(mediatorMock.Object, mapperMock) { ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext { User = userMock.Object } } }; // Act ActionResult <FriendshipResource> response = await controller.RequestFriendship(body); // Assert CreatedAtActionResult result = Assert.IsType <CreatedAtActionResult>(response.Result); Assert.Equal(expectedFriendship, result.Value); }