public void CreateReturns401WhenUserDoesntOwnList() { var options = new DbContextOptionsBuilder <ReadingListApiContext>() .UseInMemoryDatabase("create_returns_401") .Options; using (var context = new ReadingListApiContext(options)) { ReadingList readingList = new ReadingListFixture().ReadingList(); User user = new UserFixture().User(); User unauthorizedUser = new User { Email = "unauthorized test email", Avatar = "unauthorized test avatar", }; user.ReadingLists.Add(readingList); context.Users.AddRange(new List <User>() { user, unauthorizedUser }); context.SaveChanges(); SessionHelperStub session = new SessionHelperStub(unauthorizedUser); ReadingListController controller = new ReadingListController(context, session); Book newBook = new BookFixture().Book(); var result = controller.Put(readingList.ReadingListId, newBook); Assert.IsType <NotFoundResult>(result); } }
public void AddsBooksToExistingReadingList() { var options = new DbContextOptionsBuilder <ReadingListApiContext>() .UseInMemoryDatabase("adds_book_to_existing_reading_list") .Options; using (var context = new ReadingListApiContext(options)) { ReadingList readingList = new ReadingListFixture().ReadingList(); User user = new UserFixture().User(); user.ReadingLists.Add(readingList); context.Users.Add(user); context.SaveChanges(); SessionHelperStub session = new SessionHelperStub(user); ReadingListController controller = new ReadingListController(context, session); Book newBook = new BookFixture().Book(); JsonResult result = controller.Put(readingList.ReadingListId, newBook) as JsonResult; List <Book> expectedResult = context.ReadingLists .Where(r => r.ReadingListId == readingList.ReadingListId) .FirstOrDefault() .Books; Assert.Equal(readingList, result.Value); Assert.Equal(4, context.Books.Count()); Assert.Contains(newBook, expectedResult); Assert.Equal(4, readingList.Books[3].Ranking); } }