public async Task DeleteKitchenUserByKitchenUserId_ValidKitchenAndUser_ReturnsDeletedUser() { // generate test data to ensure atleast 1 user is in kitchen that can be removed InMemoryDataGenerator.AddNewRandomUsersToKitchens(_context, 1); Kitchen myKitchen = _userManager.TestUser.KitchenUser.Where(k => k.IsOwner).FirstOrDefault()?.Kitchen; // find a user in the kitchen to delete (that is not the test user) KitchenUser userToDelete = myKitchen?.KitchenUser.Where(k => k.UserId != _userManager.TestUser.Id && !k.IsOwner).FirstOrDefault(); if (userToDelete == null) { throw new Exception("user not setup for testing"); } int countBeforeDelete = myKitchen.KitchenUser.Count; ActionResult <KitchenUserDto> deleteResult = await _controller.DeleteKitchenUserByKitchenUserId(userToDelete.KitchenUserId); Assert.IsType <OkObjectResult>(deleteResult.Result); KitchenUserDto deletedKitchen = (deleteResult.Result as OkObjectResult).Value as KitchenUserDto; Assert.Equal(countBeforeDelete - 1, myKitchen.KitchenUser.Count); Assert.Equal(new KitchenUserDto(userToDelete).ToString(), deletedKitchen.ToString()); Assert.Equal(new KitchenUserDto(userToDelete).UserId, deletedKitchen.UserId); Assert.Equal(new KitchenUserDto(userToDelete).KitchenId, deletedKitchen.KitchenId); }
public async Task <ActionResult <List <KitchenUserDto> > > GetAcceptedUsersForKitchen(long kitchenId) { PantryPlannerUser user; List <KitchenUserDto> kitchenUsers; try { user = await _userManager.GetUserFromCookieOrJwtAsync(this.User); } catch (PermissionsException e) { // this will be thrown if the user is null return(Unauthorized(e.Message)); } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, e.Message)); } try { kitchenUsers = KitchenUserDto.ToList(_service.GetAcceptedUsersForKitchenById(kitchenId, user)); } catch (ArgumentNullException e) { return(BadRequest(e.Message)); } catch (KitchenNotFoundException e) { return(NotFound(e.Message)); } catch (PermissionsException e) { return(Unauthorized(e.Message)); } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, e.Message)); } if (kitchenUsers == null) { return(NotFound("No users found for kitchen")); } return(kitchenUsers); }
public async Task GetAcceptedUsersForKitchen_ValidKitchen_ReturnsCorrectResult() { Kitchen kitchen = _userManager.TestUser.KitchenUser.FirstOrDefault()?.Kitchen; if (kitchen == null) { throw new Exception("kitchen is not setup for testing"); } List <KitchenUserDto> expectedResult = KitchenUserDto.ToList(_context.KitchenUser.Where(k => k.KitchenId == kitchen.KitchenId && k.HasAcceptedInvite.Value).ToList()); ActionResult <List <KitchenUserDto> > allUsersResult = await _controller.GetAcceptedUsersForKitchen(kitchen.KitchenId); Assert.Equal(expectedResult.Count, allUsersResult.Value.Count); }
public async Task <ActionResult <List <KitchenUserDto> > > GetKitchenInvitesForLoggedInUser() { PantryPlannerUser user; List <KitchenUserDto> myInvites; try { user = await _userManager.GetUserFromCookieOrJwtAsync(this.User); } catch (PermissionsException e) { // this will be thrown if the user is null return(Unauthorized(e.Message)); } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, e.Message)); } try { myInvites = KitchenUserDto.ToList(_service.GetMyInvites(user)); } catch (ArgumentNullException e) { return(BadRequest(e.Message)); } catch (UserNotFoundException e) { return(NotFound(e.Message)); } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, e.Message)); } if (myInvites == null) { return(NotFound("No invites found for user")); } return(myInvites); }