public void PatchReordersAList() { var options = new DbContextOptionsBuilder <ReadingListApiContext>() .UseInMemoryDatabase("patch_reorders_a_list") .Options; using (var context = new ReadingListApiContext(options)) { ReadingList readingList = new ReadingListFixture().ReadingList(); User user = new UserFixture().User(); user.ReadingLists.Add(readingList); context.Add(user); context.SaveChanges(); Book book1 = readingList.Books[0]; Book book2 = readingList.Books[1]; Book book3 = readingList.Books[2]; SessionHelperStub session = new SessionHelperStub(user); ReadingListController controller = new ReadingListController(context, session); PatchData data = new PatchData { BookId = book1.BookId, Ranking = 2 }; JsonResult result = controller.Patch(readingList.ReadingListId, data) as JsonResult; Assert.Equal(2, book1.Ranking); Assert.Equal(1, book2.Ranking); Assert.Equal(3, book3.Ranking); } }
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); } }
public void DeletesAReadingList() { var options = new DbContextOptionsBuilder <ReadingListApiContext>() .UseInMemoryDatabase("deletes_a_reading_list") .Options; using (var context = new ReadingListApiContext(options)) { User user = new UserFixture().User(); ReadingList readingList = new ReadingListFixture().ReadingList(); user.ReadingLists.Add(readingList); context.Users.Add(user); context.SaveChanges(); SessionHelperStub session = new SessionHelperStub(user); ReadingListController controller = new ReadingListController(context, session); Guid readingListId = readingList.ReadingListId; var result = controller.Delete(readingList.ReadingListId); Assert.Equal(0, context.ReadingLists.Count()); Assert.Equal(null, context.ReadingLists.Where(r => r.ReadingListId == readingListId).FirstOrDefault()); } }
public void ShowReturns401WhenUserDoesntOwnList() { var options = new DbContextOptionsBuilder <ReadingListApiContext>() .UseInMemoryDatabase("show_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); var result = controller.Get(readingList.ReadingListId); Assert.IsType <NotFoundResult>(result); } }
public void CreatesNewReadingList() { var options = new DbContextOptionsBuilder <ReadingListApiContext>() .UseInMemoryDatabase("returns_users_reading_list") .Options; using (var context = new ReadingListApiContext(options)) { User user = new UserFixture().User(); context.Users.Add(user); context.SaveChanges(); SessionHelperStub session = new SessionHelperStub(user); ReadingListController controller = new ReadingListController(context, session); ReadingList readingList = new ReadingList { Title = "Existential Meltdown", Books = new List <Book>() { new BookFixture().Book() } }; JsonResult result = controller.Post(readingList) as JsonResult; Assert.Equal(1, context.ReadingLists.Count()); Assert.Equal(1, context.Books.Count()); Assert.Equal(readingList, result.Value); Assert.Equal(1, readingList.Books[0].Ranking); } }
public void HasManyBooks() { var options = new DbContextOptionsBuilder <ReadingListApiContext>() .UseInMemoryDatabase("has_many_books") .Options; using (var context = new ReadingListApiContext(options)) { context.ReadingLists.Add(new ReadingListFixture().ReadingList()); context.SaveChanges(); ReadingList readingList = context.ReadingLists.Last(); Assert.Equal(3, readingList.Books.Count()); } }
public void AddsUserToDatabase() { var options = new DbContextOptionsBuilder <ReadingListApiContext>() .UseInMemoryDatabase("adds_user_to_database") .Options; using (var context = new ReadingListApiContext(options)) { context.Users.Add(new UserFixture().User()); context.SaveChanges(); Assert.Equal(1, context.Users.Count()); Assert.Equal("test email", context.Users.Last().Email); } }
public void BelongsToReadingList() { var options = new DbContextOptionsBuilder <ReadingListApiContext>() .UseInMemoryDatabase("belongs_to_reading_list") .Options; using (var context = new ReadingListApiContext(options)) { context.ReadingLists.Add(new ReadingListFixture().ReadingList()); context.SaveChanges(); Book book = context.Books.Last(); ReadingList readingList = context.ReadingLists.Last(); Assert.Equal(book.ReadingList.Title, readingList.Title); } }
public void AddsBookToDatabase() { var options = new DbContextOptionsBuilder <ReadingListApiContext>() .UseInMemoryDatabase("adds_books_to_database") .Options; using (var context = new ReadingListApiContext(options)) { context.Books.Add(new BookFixture().Book()); context.SaveChanges(); Book book = context.Books.Last(); Assert.Equal(1, context.Books.Count()); Assert.Equal("On The Plurality Of Worlds", book.Title); Assert.Equal("David Lewis", book.Authors[0]); } }
public void BelongsToUser() { var options = new DbContextOptionsBuilder <ReadingListApiContext>() .UseInMemoryDatabase("belongs_to_user") .Options; using (var context = new ReadingListApiContext(options)) { context.Users.Add(new UserFixture().User()); context.SaveChanges(); ReadingList readingList = new ReadingListFixture().ReadingList(); context.ReadingLists.Add(readingList); User user = context.Users.Last(); user.ReadingLists.Add(readingList); context.SaveChanges(); Assert.Equal(user.UserId, readingList.User.UserId); } }
public void CanUpdateRankingsOnBookRemoval() { var options = new DbContextOptionsBuilder <ReadingListApiContext>() .UseInMemoryDatabase("can_update_rankings_on_book_removal") .Options; using (var context = new ReadingListApiContext(options)) { ReadingList readingList = new ReadingListFixture().ReadingList(); context.Add(readingList); context.SaveChanges(); Book book1 = readingList.Books[0]; Book book2 = readingList.Books[1]; Book book3 = readingList.Books[2]; readingList.RemoveBook(book1.BookId); Assert.Equal(2, readingList.Books.Count()); Assert.Equal(1, book2.Ranking); Assert.Equal(2, book3.Ranking); } }
public void CanPromoteBookRanking() { var options = new DbContextOptionsBuilder <ReadingListApiContext>() .UseInMemoryDatabase("can_promote_book_ranking") .Options; using (var context = new ReadingListApiContext(options)) { ReadingList readingList = new ReadingListFixture().ReadingList(); context.Add(readingList); context.SaveChanges(); Book book1 = readingList.Books[0]; Book book2 = readingList.Books[1]; Book book3 = readingList.Books[2]; readingList.UpdateRankings(book3.BookId, 2); Assert.Equal(1, book1.Ranking); Assert.Equal(3, book2.Ranking); Assert.Equal(2, book3.Ranking); } }
public void CreatesNewUser() { var options = new DbContextOptionsBuilder <ReadingListApiContext>() .UseInMemoryDatabase("creates_new_user") .Options; using (var context = new ReadingListApiContext(options)) { Payload payload = new Payload { Email = "test email", Picture = "test picture", }; AuthService auth = new AuthService(context); auth.Authenticate(payload); Assert.Equal(1, context.Users.Count()); Assert.Equal("test email", context.Users.Last().Email); } }
public void ReturnsAReadingList() { var options = new DbContextOptionsBuilder <ReadingListApiContext>() .UseInMemoryDatabase("returns_a_reading_list") .Options; using (var context = new ReadingListApiContext(options)) { User user = new UserFixture().User(); ReadingList readingList = new ReadingListFixture().ReadingList(); user.ReadingLists.Add(readingList); context.Users.Add(user); context.SaveChanges(); SessionHelperStub session = new SessionHelperStub(user); ReadingListController controller = new ReadingListController(context, session); JsonResult result = controller.Get(readingList.ReadingListId) as JsonResult; Assert.Equal(readingList, result.Value); } }
public AuthService(ReadingListApiContext context) { _context = context; }
public ReadingListController(ReadingListApiContext context, ISessionHelper session) { _context = context; _session = session; }
public SessionHelper(ReadingListApiContext dbContext, IHttpContextAccessor contextAccessor) { this._dbContext = dbContext; this._contextAccessor = contextAccessor; }