public async Task CascadeDeletionOfImageInCardPreviousVersion_ImageChangesSide() { var db = DbHelper.GetEmptyTestDB(); var user = await UserHelper.CreateInDbAsync(db); var languageId = await CardLanguagHelper.CreateAsync(db); var image1 = await ImageHelper.CreateAsync(db, user); var image2 = await ImageHelper.CreateAsync(db, user); var card = await CardHelper.CreateAsync(db, user, language : languageId, frontSideImages : image1.AsArray()); await UpdateCardHelper.RunAsync(db, UpdateCardHelper.RequestForImageChange(card, Array.Empty <Guid>(), image1.AsArray(), image2.AsArray())); var deletionDate = RandomHelper.Date(); using (var dbContext = new MemCheckDbContext(db)) { var deleter = new DeleteCards(dbContext.AsCallContext(), deletionDate); await deleter.RunAsync(new DeleteCards.Request(user, card.Id.AsArray())); } using (var dbContext = new MemCheckDbContext(db)) { Assert.AreEqual(3, dbContext.CardPreviousVersions.Count()); Assert.AreEqual(5, dbContext.ImagesInCardPreviousVersions.Count()); } using (var dbContext = new MemCheckDbContext(db)) await new DeleteCardPreviousVersionsOfDeletedCards(dbContext.AsCallContext(new TestRoleChecker(user))).RunAsync(new DeleteCardPreviousVersionsOfDeletedCards.Request(user, RandomHelper.Date(deletionDate))); using (var dbContext = new MemCheckDbContext(db)) { Assert.AreEqual(0, dbContext.CardPreviousVersions.Count()); Assert.AreEqual(0, dbContext.ImagesInCardPreviousVersions.Count()); } }
public async Task CascadeDeletionOfUserWithViewOnCardPreviousVersion() { var db = DbHelper.GetEmptyTestDB(); var user = await UserHelper.CreateInDbAsync(db); var otherUser = await UserHelper.CreateInDbAsync(db); var languageId = await CardLanguagHelper.CreateAsync(db); var card = await CardHelper.CreateAsync(db, user, language : languageId, userWithViewIds : user.AsArray()); await UpdateCardHelper.RunAsync(db, UpdateCardHelper.RequestForVisibilityChange(card, new[] { user, otherUser })); var deletionDate = RandomHelper.Date(); using (var dbContext = new MemCheckDbContext(db)) { var deleter = new DeleteCards(dbContext.AsCallContext(), deletionDate); await deleter.RunAsync(new DeleteCards.Request(user, card.Id.AsArray())); } using (var dbContext = new MemCheckDbContext(db)) { Assert.AreEqual(3, dbContext.CardPreviousVersions.Count()); Assert.AreEqual(5, dbContext.UsersWithViewOnCardPreviousVersions.Count()); } using (var dbContext = new MemCheckDbContext(db)) await new DeleteCardPreviousVersionsOfDeletedCards(dbContext.AsCallContext(new TestRoleChecker(user))).RunAsync(new DeleteCardPreviousVersionsOfDeletedCards.Request(user, RandomHelper.Date(deletionDate))); using (var dbContext = new MemCheckDbContext(db)) { Assert.AreEqual(0, dbContext.CardPreviousVersions.Count()); Assert.AreEqual(0, dbContext.UsersWithViewOnCardPreviousVersions.Count()); } }
public async Task SameImageUsedTwice() { var db = DbHelper.GetEmptyTestDB(); var user = await UserHelper.CreateInDbAsync(db); var language = await CardLanguagHelper.CreateAsync(db); var image = await ImageHelper.CreateAsync(db, user); var request = new CreateCard.Request( user, RandomHelper.String(), Array.Empty <Guid>(), RandomHelper.String(), image.AsArray(), RandomHelper.String(), image.AsArray(), language, Array.Empty <Guid>(), Array.Empty <Guid>(), RandomHelper.String()); using var dbContext = new MemCheckDbContext(db); await Assert.ThrowsExceptionAsync <RequestInputException>(async() => await new CreateCard(dbContext.AsCallContext()).RunAsync(request)); }
public async Task CardWithIntermediaryVersionsToBeDeletedFromPreviousVersions() { var db = DbHelper.GetEmptyTestDB(); var user = await UserHelper.CreateInDbAsync(db); var languageId = await CardLanguagHelper.CreateAsync(db); var card = await CardHelper.CreateAsync(db, user, language : languageId); await UpdateCardHelper.RunAsync(db, UpdateCardHelper.RequestForFrontSideChange(card, RandomHelper.String())); var runDate = RandomHelper.Date(); using (var dbContext = new MemCheckDbContext(db)) { var deleter = new DeleteCards(dbContext.AsCallContext(), RandomHelper.DateBefore(runDate)); await deleter.RunAsync(new DeleteCards.Request(user, card.Id.AsArray())); } using (var dbContext = new MemCheckDbContext(db)) Assert.AreEqual(3, dbContext.CardPreviousVersions.Count()); using (var dbContext = new MemCheckDbContext(db)) await new DeleteCardPreviousVersionsOfDeletedCards(dbContext.AsCallContext(new TestRoleChecker(user))).RunAsync(new DeleteCardPreviousVersionsOfDeletedCards.Request(user, runDate)); using (var dbContext = new MemCheckDbContext(db)) Assert.AreEqual(0, dbContext.CardPreviousVersions.Count()); }
public async Task UpdateNothing() { var db = DbHelper.GetEmptyTestDB(); var cardCreator = await UserHelper.CreateInDbAsync(db); var languageId = await CardLanguagHelper.CreateAsync(db); var card = await CardHelper.CreateAsync(db, cardCreator, language : languageId, userWithViewIds : Array.Empty <Guid>()); using var dbContext = new MemCheckDbContext(db); var request = new UpdateCard.Request( card.Id, card.VersionCreator.Id, card.FrontSide, card.Images.Where(i => i.CardSide == ImageInCard.FrontSide).Select(i => i.ImageId), card.BackSide, card.Images.Where(i => i.CardSide == ImageInCard.BackSide).Select(i => i.ImageId), card.AdditionalInfo, card.Images.Where(i => i.CardSide == ImageInCard.AdditionalInfo).Select(i => i.ImageId), card.CardLanguage.Id, card.TagsInCards.Select(t => t.TagId), card.UsersWithView.Select(uwv => uwv.UserId), RandomHelper.String()); await Assert.ThrowsExceptionAsync <RequestInputException>(async() => await new UpdateCard(dbContext.AsCallContext()).RunAsync(request)); }
public async Task ReduceVisibility_OtherUserHasView_NoUserHasInDeck_OnlyAuthor() { var db = DbHelper.GetEmptyTestDB(); var cardCreator = await UserHelper.CreateInDbAsync(db); var languageId = await CardLanguagHelper.CreateAsync(db); var otherUser = await UserHelper.CreateInDbAsync(db); var card = await CardHelper.CreateAsync(db, cardCreator, language : languageId, userWithViewIds : new[] { cardCreator, otherUser }); using (var dbContext = new MemCheckDbContext(db)) { CardVisibilityHelper.CheckUserIsAllowedToViewCards(dbContext, cardCreator, card.Id); CardVisibilityHelper.CheckUserIsAllowedToViewCards(dbContext, otherUser, card.Id); } using (var dbContext = new MemCheckDbContext(db)) { var r = UpdateCardHelper.RequestForVisibilityChange(card, cardCreator.AsArray()); await new UpdateCard(dbContext.AsCallContext()).RunAsync(r); } using (var dbContext = new MemCheckDbContext(db)) { CardVisibilityHelper.CheckUserIsAllowedToViewCards(dbContext, cardCreator, card.Id); Assert.ThrowsException <InvalidOperationException>(() => CardVisibilityHelper.CheckUserIsAllowedToViewCards(dbContext, otherUser, card.Id)); } }
public async Task CardWithPreviousVersion() { var db = DbHelper.GetEmptyTestDB(); var language = await CardLanguagHelper.CreateAsync(db); var firstVersionCreatorId = await UserHelper.CreateInDbAsync(db); var firstVersionDate = RandomHelper.Date(); var card = await CardHelper.CreateAsync(db, firstVersionCreatorId, language : language, versionDate : firstVersionDate); var lastVersionCreatorName = RandomHelper.String(); var lastVersionCreatorId = await UserHelper.CreateInDbAsync(db, userName : lastVersionCreatorName); var lastVersionDate = RandomHelper.Date(); var lastVersionDescription = RandomHelper.String(); var lastVersionFrontSide = RandomHelper.String(); using (var dbContext = new MemCheckDbContext(db)) await new UpdateCard(dbContext.AsCallContext(), lastVersionDate).RunAsync(UpdateCardHelper.RequestForFrontSideChange(card, lastVersionFrontSide, versionCreator: lastVersionCreatorId, versionDescription: lastVersionDescription)); var otherUserId = await UserHelper.CreateInDbAsync(db); using (var dbContext = new MemCheckDbContext(db)) { var loaded = await new GetCardForEdit(dbContext.AsCallContext()).RunAsync(new GetCardForEdit.Request(otherUserId, card.Id)); Assert.AreEqual(firstVersionDate, loaded.FirstVersionUtcDate); Assert.AreEqual(lastVersionDate, loaded.LastVersionUtcDate); Assert.AreEqual(lastVersionCreatorName, loaded.LastVersionCreatorName); Assert.AreEqual(lastVersionDescription, loaded.LastVersionDescription); Assert.AreEqual(lastVersionFrontSide, loaded.FrontSide); } }
public async Task WithOneImage() { var testDB = DbHelper.GetEmptyTestDB(); var ownerId = await UserHelper.CreateInDbAsync(testDB, subscribeToCardOnEdit : false); var languageId = await CardLanguagHelper.CreateAsync(testDB); var imageId = await ImageHelper.CreateAsync(testDB, ownerId); Guid cardGuid = Guid.Empty; using (var dbContext = new MemCheckDbContext(testDB)) { var request = new CreateCard.Request( ownerId, RandomHelper.String(), new Guid[] { imageId }, RandomHelper.String(), new Guid[0], RandomHelper.String(), new Guid[0], languageId, new Guid[0], new Guid[0], RandomHelper.String()); cardGuid = (await new CreateCard(dbContext.AsCallContext()).RunAsync(request)).CardId; } using (var dbContext = new MemCheckDbContext(testDB)) { var card = dbContext.Cards.Include(c => c.Images).Single(c => c.Id == cardGuid); Assert.AreEqual(ImageInCard.FrontSide, card.Images.Single(i => i.ImageId == imageId).CardSide); } }
public async Task FailIfUserCanNotViewCurrentVersion() { var db = DbHelper.GetEmptyTestDB(); var userName = RandomHelper.String(); var userId = await UserHelper.CreateInDbAsync(db, userName : userName); var originalVersionDate = RandomHelper.Date(); var originalFrontSide = RandomHelper.String(); var originalVersionDescription = RandomHelper.String(); var language = await CardLanguagHelper.CreateAsync(db); var card = await CardHelper.CreateAsync(db, userId, originalVersionDate, frontSide : originalFrontSide, language : language, versionDescription : originalVersionDescription); var newFrontSide = RandomHelper.String(); var newVersionDate = RandomHelper.Date(originalVersionDate); var newVersionDescription = RandomHelper.String(); using (var dbContext = new MemCheckDbContext(db)) await new UpdateCard(dbContext.AsCallContext(), newVersionDate).RunAsync(UpdateCardHelper.RequestForVisibilityChange(card, userWithViewIds: userId.AsArray())); Guid previousVersionId; using (var dbContext = new MemCheckDbContext(db)) previousVersionId = (await dbContext.CardPreviousVersions.Where(previous => previous.Card == card.Id).SingleAsync()).Id; var otherUserId = await UserHelper.CreateInDbAsync(db); using (var dbContext = new MemCheckDbContext(db)) await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => await new GetCardDiff(dbContext.AsCallContext()).RunAsync(new GetCardDiff.Request(otherUserId, card.Id, previousVersionId))); }
public async Task TwoCardsWithLanguages() { var db = DbHelper.GetEmptyTestDB(); var french = await CardLanguagHelper.CreateAsync(db, "Français"); var otherLanguage = await CardLanguagHelper.CreateAsync(db, RandomHelper.String()); var user = await UserHelper.CreateInDbAsync(db); var deck = await DeckHelper.CreateAsync(db, user); var frenchCreatedCard = await CardHelper.CreateAsync(db, user, language : french); var otherLanguageCard = await CardHelper.CreateAsync(db, user, language : otherLanguage); await DeckHelper.AddNeverLearntCardAsync(db, deck, frenchCreatedCard.Id); await DeckHelper.AddNeverLearntCardAsync(db, deck, otherLanguageCard.Id); using var dbContext = new MemCheckDbContext(db); var request = new GetUnknownCardsToLearn.Request(user, deck, Array.Empty <Guid>(), Array.Empty <Guid>(), 10); var cards = await new GetUnknownCardsToLearn(dbContext.AsCallContext()).RunAsync(request); Assert.IsTrue(cards.Cards.Single(card => card.CardId == frenchCreatedCard.Id).IsInFrench); Assert.IsFalse(cards.Cards.Single(card => card.CardId == otherLanguageCard.Id).IsInFrench); }
public async Task PrivateCardDeleted() { var db = DbHelper.GetEmptyTestDB(); var loggedUser = await UserHelper.CreateInDbAsync(db); var language = await CardLanguagHelper.CreateAsync(db); var userToDelete = await UserHelper.CreateInDbAsync(db); var card = await CardHelper.CreateAsync(db, userToDelete, language : language); //Public await UpdateCardHelper.RunAsync(db, UpdateCardHelper.RequestForVisibilityChange(card, userWithViewIds: userToDelete.AsArray())); //Private using (var dbContext = new MemCheckDbContext(db)) { using var userManager = UserHelper.GetUserManager(dbContext); await new DeleteUserAccount(dbContext.AsCallContext(new TestRoleChecker(loggedUser)), userManager).RunAsync(new DeleteUserAccount.Request(loggedUser, userToDelete)); } using (var dbContext = new MemCheckDbContext(db)) { Assert.IsFalse(dbContext.Cards.Any()); Assert.IsFalse(dbContext.CardPreviousVersions.Any()); } }
private static async Task <CardPreviousVersion> CreateCardPreviousVersionAsync(DbContextOptions <MemCheckDbContext> testDB, Guid versionCreatorId, Guid cardId, DateTime versionDate) { var cardLanguageId = await CardLanguagHelper.CreateAsync(testDB); using var dbContext = new MemCheckDbContext(testDB); var creator = await dbContext.Users.Where(u => u.Id == versionCreatorId).SingleAsync(); var cardLanguage = await dbContext.CardLanguages.SingleAsync(cardLanguage => cardLanguage.Id == cardLanguageId); var result = new CardPreviousVersion { Card = cardId, CardLanguage = cardLanguage, VersionCreator = creator, VersionUtcDate = versionDate, VersionType = CardPreviousVersionType.Creation, FrontSide = RandomHelper.String(), BackSide = RandomHelper.String(), AdditionalInfo = RandomHelper.String(), VersionDescription = RandomHelper.String() }; dbContext.CardPreviousVersions.Add(result); var card = await dbContext.Cards.Where(c => c.Id == cardId).SingleAsync(); card.PreviousVersion = result; card.VersionType = CardVersionType.Changes; await dbContext.SaveChangesAsync(); return(result); }
public async Task UserInVisibilityList() { var db = DbHelper.GetEmptyTestDB(); var cardCreator = await UserHelper.CreateInDbAsync(db); var languageId = await CardLanguagHelper.CreateAsync(db); var otherUser = await UserHelper.CreateInDbAsync(db); var card = await CardHelper.CreateAsync(db, cardCreator, language : languageId, userWithViewIds : new[] { cardCreator, otherUser }); var newFrontSide = RandomHelper.String(); using (var dbContext = new MemCheckDbContext(db)) { var r = UpdateCardHelper.RequestForFrontSideChange(card, newFrontSide, otherUser); await new UpdateCard(dbContext.AsCallContext()).RunAsync(r); } using (var dbContext = new MemCheckDbContext(db)) { var cardFromDb = dbContext.Cards.Single(c => c.Id == card.Id); Assert.AreEqual(newFrontSide, cardFromDb.FrontSide); } }
public async Task NonPrivateCardNotDeleted_UserOwnerOfCurrentButNotPrevious() { var db = DbHelper.GetEmptyTestDB(); var loggedUserName = RandomHelper.String(); var loggedUser = await UserHelper.CreateInDbAsync(db, userName : loggedUserName); var language = await CardLanguagHelper.CreateAsync(db); var userToDelete = await UserHelper.CreateInDbAsync(db); var card = await CardHelper.CreateAsync(db, loggedUser, language : language); await UpdateCardHelper.RunAsync(db, UpdateCardHelper.RequestForFrontSideChange(card, RandomHelper.String(), versionCreator: userToDelete)); using (var dbContext = new MemCheckDbContext(db)) { using var userManager = UserHelper.GetUserManager(dbContext); await new DeleteUserAccount(dbContext.AsCallContext(new TestRoleChecker(loggedUser)), userManager).RunAsync(new DeleteUserAccount.Request(loggedUser, userToDelete)); } using (var dbContext = new MemCheckDbContext(db)) { Assert.AreEqual(DeleteUserAccount.DeletedUserName, dbContext.Cards.Include(card => card.VersionCreator).Single(card => card.Id == card.Id).VersionCreator.UserName); Assert.AreEqual(loggedUserName, dbContext.CardPreviousVersions.Include(cardPreviousVersion => cardPreviousVersion.VersionCreator).Single(cardPreviousVersion => cardPreviousVersion.Card == card.Id).VersionCreator.UserName); } }
public async Task Complex() { var db = DbHelper.GetEmptyTestDB(); var language1Name = RandomHelper.String(); var language1 = await CardLanguagHelper.CreateAsync(db, language1Name); var language2Name = RandomHelper.String(); var language2 = await CardLanguagHelper.CreateAsync(db, language2Name); var user = await UserHelper.CreateInDbAsync(db); await CardHelper.CreateAsync(db, versionCreatorId : user, language : language1); await CardHelper.CreateAsync(db, versionCreatorId : user, language : language1); await CardHelper.CreateAsync(db, versionCreatorId : user, language : language1); await CardHelper.CreateAsync(db, versionCreatorId : user, language : language2); using var dbContext = new MemCheckDbContext(db); var result = await new GetAllLanguages(dbContext.AsCallContext()).RunAsync(new GetAllLanguages.Request()); var resultLang1 = result.Single(l => l.Id == language1); Assert.AreEqual(language1Name, resultLang1.Name); Assert.AreEqual(3, resultLang1.CardCount); var resultLang2 = result.Single(l => l.Id == language2); Assert.AreEqual(language2Name, resultLang2.Name); Assert.AreEqual(1, resultLang2.CardCount); }
public async Task CreatorNotInVisibilityList() { var testDB = DbHelper.GetEmptyTestDB(); var creatorId = await UserHelper.CreateInDbAsync(testDB); var otherUser = await UserHelper.CreateInDbAsync(testDB); var languageId = await CardLanguagHelper.CreateAsync(testDB); Guid cardGuid = Guid.Empty; using var dbContext = new MemCheckDbContext(testDB); var request = new CreateCard.Request( creatorId, RandomHelper.String(), Array.Empty <Guid>(), RandomHelper.String(), Array.Empty <Guid>(), RandomHelper.String(), Array.Empty <Guid>(), languageId, Array.Empty <Guid>(), new Guid[] { otherUser }, RandomHelper.String()); var ownerMustHaveVisibility = RandomHelper.String(); var localizer = new TestLocalizer(new KeyValuePair <string, string>("OwnerMustHaveVisibility", ownerMustHaveVisibility).AsArray()); var exception = await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => await new CreateCard(dbContext.AsCallContext(localizer)).RunAsync(request)); Assert.AreEqual(ownerMustHaveVisibility, exception.Message); }
public async Task WithUserSubscribingToCardOnEdit() { var testDB = DbHelper.GetEmptyTestDB(); var ownerId = await UserHelper.CreateInDbAsync(testDB, subscribeToCardOnEdit : true); var languageId = await CardLanguagHelper.CreateAsync(testDB); Guid cardGuid = Guid.Empty; using (var dbContext = new MemCheckDbContext(testDB)) { var request = new CreateCard.Request( ownerId, RandomHelper.String(), Array.Empty <Guid>(), RandomHelper.String(), Array.Empty <Guid>(), RandomHelper.String(), Array.Empty <Guid>(), languageId, Array.Empty <Guid>(), Array.Empty <Guid>(), RandomHelper.String()); cardGuid = (await new CreateCard(dbContext.AsCallContext()).RunAsync(request)).CardId; } Assert.IsTrue(await CardSubscriptionHelper.UserIsSubscribedToCardAsync(testDB, ownerId, cardGuid)); }
public async Task FailIfNotCreatorOfCurrentVersion() { var db = DbHelper.GetEmptyTestDB(); var firstVersionCreator = await UserHelper.CreateInDbAsync(db); var languageId = await CardLanguagHelper.CreateAsync(db); var card = await CardHelper.CreateAsync(db, firstVersionCreator, language : languageId); var lastVersionCreator = await UserHelper.CreateInDbAsync(db); using (var dbContext = new MemCheckDbContext(db)) await new UpdateCard(dbContext.AsCallContext()).RunAsync(UpdateCardHelper.RequestForFrontSideChange(card, RandomHelper.String(), lastVersionCreator)); using (var dbContext = new MemCheckDbContext(db)) { var deleter = new DeleteCards(dbContext.AsCallContext(new TestLocalizer(new System.Collections.Generic.KeyValuePair <string, string>("YouAreNotTheCreatorOfCurrentVersion", "YouAreNotTheCreatorOfCurrentVersion")))); var e = await Assert.ThrowsExceptionAsync <RequestInputException>(async() => await deleter.RunAsync(new DeleteCards.Request(firstVersionCreator, card.Id.AsArray()))); StringAssert.Contains(e.Message, "YouAreNotTheCreatorOfCurrentVersion"); } using (var dbContext = new MemCheckDbContext(db)) { Assert.AreEqual(card.Id, dbContext.Cards.Single().Id); Assert.AreEqual(card.Id, dbContext.CardPreviousVersions.Single().Card); } }
public async Task NewlyFoundToReport() { var db = DbHelper.GetEmptyTestDB(); var user = await UserHelper.CreateInDbAsync(db); var language = await CardLanguagHelper.CreateAsync(db); var card1 = await CardHelper.CreateAsync(db, user, versionDate : new DateTime(2050, 03, 01), language : language); var card2 = await CardHelper.CreateAsync(db, user, versionDate : new DateTime(2050, 04, 02), language : language); var someText = RandomHelper.String(); var subscriptionName = RandomHelper.String(); var subscription = await SearchSubscriptionHelper.CreateAsync(db, user, subscriptionName, someText); using (var dbContext = new MemCheckDbContext(db)) { var searchResult = await new UserSearchNotifier(dbContext.AsCallContext(), 10, new DateTime(2050, 05, 01)).RunAsync(subscription.Id); Assert.AreEqual(subscriptionName, searchResult.SubscriptionName); Assert.AreEqual(0, searchResult.TotalNewlyFoundCardCount); Assert.AreEqual(0, searchResult.NewlyFoundCards.Length); Assert.AreEqual(0, searchResult.CountOfCardsNotFoundAnymore_StillExists_UserAllowedToView); Assert.AreEqual(0, searchResult.CardsNotFoundAnymore_StillExists_UserAllowedToView.Length); Assert.AreEqual(0, searchResult.CountOfCardsNotFoundAnymore_Deleted_UserAllowedToView); Assert.AreEqual(0, searchResult.CardsNotFoundAnymore_Deleted_UserAllowedToView.Length); Assert.AreEqual(0, searchResult.CountOfCardsNotFoundAnymore_StillExists_UserNotAllowedToView); Assert.AreEqual(0, searchResult.CountOfCardsNotFoundAnymore_Deleted_UserNotAllowedToView); } using (var dbContext = new MemCheckDbContext(db)) { UpdateCard.Request updateRequest = new(card1.Id, user, someText, Array.Empty <Guid>(), RandomHelper.String(), Array.Empty <Guid>(), RandomHelper.String(), Array.Empty <Guid>(), language, Array.Empty <Guid>(), Array.Empty <Guid>(), RandomHelper.String()); await new UpdateCard(dbContext.AsCallContext(), new DateTime(2050, 05, 02)).RunAsync(updateRequest); } var runDate = new DateTime(2050, 05, 03); using (var dbContext = new MemCheckDbContext(db)) { var searchResult = await new UserSearchNotifier(dbContext.AsCallContext(), 100, runDate).RunAsync(subscription.Id); Assert.AreEqual(subscriptionName, searchResult.SubscriptionName); Assert.AreEqual(1, searchResult.TotalNewlyFoundCardCount); Assert.AreEqual(1, searchResult.NewlyFoundCards.Length); Assert.AreEqual(0, searchResult.CountOfCardsNotFoundAnymore_StillExists_UserAllowedToView); Assert.AreEqual(0, searchResult.CardsNotFoundAnymore_StillExists_UserAllowedToView.Length); Assert.AreEqual(0, searchResult.CountOfCardsNotFoundAnymore_Deleted_UserAllowedToView); Assert.AreEqual(0, searchResult.CardsNotFoundAnymore_Deleted_UserAllowedToView.Length); Assert.AreEqual(0, searchResult.CountOfCardsNotFoundAnymore_StillExists_UserNotAllowedToView); Assert.AreEqual(0, searchResult.CountOfCardsNotFoundAnymore_Deleted_UserNotAllowedToView); Assert.AreEqual(card1.Id, searchResult.NewlyFoundCards.Single().CardId); } using (var dbContext = new MemCheckDbContext(db)) Assert.AreEqual(runDate, dbContext.SearchSubscriptions.Single(s => s.Id == subscription.Id).LastRunUtcDate); }
public async Task ReduceVisibility_OtherUserHasInDeck_OtherAuthor() { var db = DbHelper.GetEmptyTestDB(); var cardCreator = await UserHelper.CreateInDbAsync(db); var languageId = await CardLanguagHelper.CreateAsync(db); var card = await CardHelper.CreateAsync(db, cardCreator, language : languageId, userWithViewIds : Array.Empty <Guid>()); var userWithCardInDeck = await UserHelper.CreateInDbAsync(db); var deck = await DeckHelper.CreateAsync(db, userWithCardInDeck); await DeckHelper.AddCardAsync(db, deck, card.Id, 0); var otherUser = await UserHelper.CreateInDbAsync(db); using (var dbContext = new MemCheckDbContext(db)) { var r = UpdateCardHelper.RequestForFrontSideChange(card, RandomHelper.String(), otherUser); await new UpdateCard(dbContext.AsCallContext()).RunAsync(r); } using (var dbContext = new MemCheckDbContext(db)) { var r = UpdateCardHelper.RequestForVisibilityChange(card, cardCreator.AsArray()); await Assert.ThrowsExceptionAsync <RequestInputException>(async() => await new UpdateCard(dbContext.AsCallContext()).RunAsync(r)); } using (var dbContext = new MemCheckDbContext(db)) { var r = UpdateCardHelper.RequestForVisibilityChange(card, otherUser.AsArray(), otherUser); await Assert.ThrowsExceptionAsync <RequestInputException>(async() => await new UpdateCard(dbContext.AsCallContext()).RunAsync(r)); } using (var dbContext = new MemCheckDbContext(db)) { var r = UpdateCardHelper.RequestForVisibilityChange(card, new[] { userWithCardInDeck }, userWithCardInDeck); await Assert.ThrowsExceptionAsync <RequestInputException>(async() => await new UpdateCard(dbContext.AsCallContext()).RunAsync(r)); } using (var dbContext = new MemCheckDbContext(db)) { var r = UpdateCardHelper.RequestForVisibilityChange(card, new[] { cardCreator, otherUser, userWithCardInDeck }); await new UpdateCard(dbContext.AsCallContext()).RunAsync(r); } using (var dbContext = new MemCheckDbContext(db)) { CardVisibilityHelper.CheckUserIsAllowedToViewCards(dbContext, cardCreator, card.Id); CardVisibilityHelper.CheckUserIsAllowedToViewCards(dbContext, otherUser, card.Id); CardVisibilityHelper.CheckUserIsAllowedToViewCards(dbContext, userWithCardInDeck, card.Id); } }
public async Task UserDoesNotExist() { var db = DbHelper.GetEmptyTestDB(); var user = await UserHelper.CreateInDbAsync(db); var languageId = await CardLanguagHelper.CreateAsync(db); var card = await CardHelper.CreateIdAsync(db, user, language : languageId); using var dbContext = new MemCheckDbContext(db); await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => await new SetCardRating(dbContext.AsCallContext()).RunAsync(new SetCardRating.Request(Guid.NewGuid(), card, RandomHelper.Rating()))); }
public async Task TooBig() { var db = DbHelper.GetEmptyTestDB(); var user = await UserHelper.CreateInDbAsync(db); var languageId = await CardLanguagHelper.CreateAsync(db); var card = await CardHelper.CreateIdAsync(db, user, language : languageId); using var dbContext = new MemCheckDbContext(db); await Assert.ThrowsExceptionAsync <RequestInputException>(async() => await new SetCardRating(dbContext.AsCallContext()).RunAsync(new SetCardRating.Request(user, card, 6))); }
public async Task CheckAllFields() { var db = DbHelper.GetEmptyTestDB(); var languageName = RandomHelper.String(); var language = await CardLanguagHelper.CreateAsync(db, languageName); var creatorName = RandomHelper.String(); var creatorId = await UserHelper.CreateInDbAsync(db, userName : creatorName); var creationDate = RandomHelper.Date(); var frontSide = RandomHelper.String(); var backSide = RandomHelper.String(); var additionalInfo = RandomHelper.String(); var tagName = RandomHelper.String(); var tag = await TagHelper.CreateAsync(db, tagName); var otherUserName = RandomHelper.String(); var otherUserId = await UserHelper.CreateInDbAsync(db, userName : otherUserName); var versionDescription = RandomHelper.String(); var card = await CardHelper.CreateAsync(db, creatorId, language : language, versionDate : creationDate, frontSide : frontSide, backSide : backSide, additionalInfo : additionalInfo, tagIds : tag.AsArray(), userWithViewIds : new[] { creatorId, otherUserId }, versionDescription : versionDescription); var deck = await DeckHelper.CreateAsync(db, otherUserId); await DeckHelper.AddCardAsync(db, deck, card.Id); using var dbContext = new MemCheckDbContext(db); var loaded = await new GetCardForEdit(dbContext.AsCallContext()).RunAsync(new GetCardForEdit.Request(creatorId, card.Id)); Assert.AreEqual(frontSide, loaded.FrontSide); Assert.AreEqual(backSide, loaded.BackSide); Assert.AreEqual(additionalInfo, loaded.AdditionalInfo); Assert.AreEqual(language, loaded.LanguageId); Assert.AreEqual(languageName, loaded.LanguageName); Assert.AreEqual(tag, loaded.Tags.Single().TagId); Assert.AreEqual(tagName, loaded.Tags.Single().TagName); Assert.AreEqual(2, loaded.UsersWithVisibility.Count()); Assert.IsTrue(loaded.UsersWithVisibility.Count(u => u.UserId == creatorId) == 1); Assert.AreEqual(creatorName, loaded.UsersWithVisibility.Single(u => u.UserId == creatorId).UserName); Assert.IsTrue(loaded.UsersWithVisibility.Count(u => u.UserId == otherUserId) == 1); Assert.AreEqual(otherUserName, loaded.UsersWithVisibility.Single(u => u.UserId == otherUserId).UserName); Assert.AreEqual(creationDate, loaded.FirstVersionUtcDate); Assert.AreEqual(creationDate, loaded.LastVersionUtcDate); Assert.AreEqual(creatorName, loaded.LastVersionCreatorName); Assert.AreEqual(versionDescription, loaded.LastVersionDescription); Assert.AreEqual(1, loaded.UsersOwningDeckIncluding.Count()); Assert.IsTrue(loaded.UsersOwningDeckIncluding.Single() == otherUserName); Assert.AreEqual(0, loaded.Images.Count()); Assert.AreEqual(0, loaded.UserRating); Assert.AreEqual(0, loaded.AverageRating); Assert.AreEqual(0, loaded.CountOfUserRatings); }
public async Task UserNotLoggedIn() { var db = DbHelper.GetEmptyTestDB(); var user = await UserHelper.CreateInDbAsync(db); var languageId = await CardLanguagHelper.CreateAsync(db); var card = await CardHelper.CreateAsync(db, user, language : languageId); using var dbContext = new MemCheckDbContext(db); var request = UpdateCardHelper.RequestForFrontSideChange(card, RandomHelper.String(), Guid.Empty); await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => await new UpdateCard(dbContext.AsCallContext()).RunAsync(request)); }
public async Task DescriptionTooLong() { var db = DbHelper.GetEmptyTestDB(); var cardCreator = await UserHelper.CreateInDbAsync(db); var languageId = await CardLanguagHelper.CreateAsync(db); var card = await CardHelper.CreateAsync(db, cardCreator, language : languageId, userWithViewIds : Array.Empty <Guid>()); var request = UpdateCardHelper.RequestForFrontSideChange(card, RandomHelper.String(), versionDescription: RandomHelper.String(CardInputValidator.MaxVersionDescriptionLength + 1)); using var dbContext = new MemCheckDbContext(db); await Assert.ThrowsExceptionAsync <RequestInputException>(async() => await new UpdateCard(dbContext.AsCallContext()).RunAsync(request)); }
public async Task FailIfUserCanNotView() { var db = DbHelper.GetEmptyTestDB(); var userId = await UserHelper.CreateInDbAsync(db); var language = await CardLanguagHelper.CreateAsync(db); var card = await CardHelper.CreateAsync(db, userId, language : language, userWithViewIds : userId.AsArray()); var otherUserId = await UserHelper.CreateInDbAsync(db); using var dbContext = new MemCheckDbContext(db); await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => await new GetCardForEdit(dbContext.AsCallContext()).RunAsync(new GetCardForEdit.Request(otherUserId, card.Id))); }
public async Task MultipleImagesOnAdditionalSide() { var db = DbHelper.GetEmptyTestDB(); var user = await UserHelper.CreateInDbAsync(db); var language = await CardLanguagHelper.CreateAsync(db); var image1 = await ImageHelper.CreateAsync(db, user); var image2 = await ImageHelper.CreateAsync(db, user); Guid cardGuid; var createRequest = new CreateCard.Request( user, RandomHelper.String(), Array.Empty <Guid>(), RandomHelper.String(), Array.Empty <Guid>(), RandomHelper.String(), new Guid[] { image1, image2 }, language, Array.Empty <Guid>(), Array.Empty <Guid>(), RandomHelper.String()); using (var dbContext = new MemCheckDbContext(db)) cardGuid = (await new CreateCard(dbContext.AsCallContext()).RunAsync(createRequest)).CardId; var deck = await DeckHelper.CreateAsync(db, user); var addToDeckDate = RandomHelper.Date(); await DeckHelper.AddCardAsync(db, deck, cardGuid, 1, addToDeckDate); using (var dbContext = new MemCheckDbContext(db)) { var request = new GetCardsToRepeat.Request(user, deck, Array.Empty <Guid>(), Array.Empty <Guid>(), 10); var card = (await new GetCardsToRepeat(dbContext.AsCallContext(), addToDeckDate.AddDays(1)).RunAsync(request)).Cards.Single(); var images = card.Images; Assert.AreEqual(2, images.Count()); var first = card.Images.First(); Assert.AreEqual(ImageInCard.AdditionalInfo, first.CardSide); Assert.AreEqual(image1, first.ImageId); var last = card.Images.Last(); Assert.AreEqual(ImageInCard.AdditionalInfo, last.CardSide); Assert.AreEqual(image2, last.ImageId); } }
public async Task MultipleCase() { var db = DbHelper.GetEmptyTestDB(); var user = await UserHelper.CreateInDbAsync(db); var languageId = await CardLanguagHelper.CreateAsync(db); var runDate = RandomHelper.Date(); var cardNotDeleted = await CardHelper.CreateAsync(db, user, language : languageId); await UpdateCardHelper.RunAsync(db, UpdateCardHelper.RequestForFrontSideChange(cardNotDeleted, RandomHelper.String())); using (var dbContext = new MemCheckDbContext(db)) Assert.AreEqual(1, dbContext.CardPreviousVersions.Count()); var cardDeletedAfterRunDate = await CardHelper.CreateAsync(db, user, language : languageId); using (var dbContext = new MemCheckDbContext(db)) { var deleter = new DeleteCards(dbContext.AsCallContext(), RandomHelper.Date(runDate)); await deleter.RunAsync(new DeleteCards.Request(user, cardDeletedAfterRunDate.Id.AsArray())); } using (var dbContext = new MemCheckDbContext(db)) Assert.AreEqual(3, dbContext.CardPreviousVersions.Count()); var cardDeletedBeforeRunDate = await CardHelper.CreateAsync(db, user, language : languageId); using (var dbContext = new MemCheckDbContext(db)) { var deleter = new DeleteCards(dbContext.AsCallContext(), RandomHelper.DateBefore(runDate)); await deleter.RunAsync(new DeleteCards.Request(user, cardDeletedBeforeRunDate.Id.AsArray())); } using (var dbContext = new MemCheckDbContext(db)) Assert.AreEqual(5, dbContext.CardPreviousVersions.Count()); using (var dbContext = new MemCheckDbContext(db)) await new DeleteCardPreviousVersionsOfDeletedCards(dbContext.AsCallContext(new TestRoleChecker(user))).RunAsync(new DeleteCardPreviousVersionsOfDeletedCards.Request(user, runDate)); using (var dbContext = new MemCheckDbContext(db)) { var previousVersions = await dbContext.CardPreviousVersions.ToListAsync(); Assert.AreEqual(3, previousVersions.Count); Assert.AreEqual(1, previousVersions.Where(pv => pv.Card == cardNotDeleted.Id).Count()); Assert.AreEqual(2, previousVersions.Where(pv => pv.Card == cardDeletedAfterRunDate.Id).Count()); } }
public async Task OneLanguageInDb() { var db = DbHelper.GetEmptyTestDB(); var name = RandomHelper.String(); var id = await CardLanguagHelper.CreateAsync(db, name); using var dbContext = new MemCheckDbContext(db); var result = await new GetAllLanguages(dbContext.AsCallContext()).RunAsync(new GetAllLanguages.Request()); var resultLang = result.Single(); Assert.AreEqual(id, resultLang.Id); Assert.AreEqual(name, resultLang.Name); Assert.AreEqual(0, resultLang.CardCount); }
public async Task TagsDiff() { var db = DbHelper.GetEmptyTestDB(); var userName = RandomHelper.String(); var userId = await UserHelper.CreateInDbAsync(db, userName : userName); var originalVersionDate = RandomHelper.Date(); var originalVersionDescription = RandomHelper.String(); var originalTagName1 = RandomHelper.String(); var originalTagId1 = await TagHelper.CreateAsync(db, originalTagName1); var originalTagName2 = RandomHelper.String(); var originalTagId2 = await TagHelper.CreateAsync(db, originalTagName2); var language = await CardLanguagHelper.CreateAsync(db); var card = await CardHelper.CreateAsync(db, userId, originalVersionDate, tagIds : new[] { originalTagId1, originalTagId2 }, language : language, versionDescription : originalVersionDescription); var newVersionDate = RandomHelper.Date(originalVersionDate); var newVersionDescription = RandomHelper.String(); using (var dbContext = new MemCheckDbContext(db)) await new UpdateCard(dbContext.AsCallContext(), newVersionDate).RunAsync(UpdateCardHelper.RequestForTagChange(card, originalTagId1.AsArray(), versionDescription: newVersionDescription)); Guid previousVersionId; using (var dbContext = new MemCheckDbContext(db)) previousVersionId = (await dbContext.CardPreviousVersions.Where(previous => previous.Card == card.Id).SingleAsync()).Id; using (var dbContext = new MemCheckDbContext(db)) { var diffRequest = new GetCardDiff.Request(userId, card.Id, previousVersionId); var result = await new GetCardDiff(dbContext.AsCallContext()).RunAsync(diffRequest); Assert.AreEqual(userName, result.CurrentVersionCreator); Assert.AreEqual(userName, result.OriginalVersionCreator); Assert.AreEqual(newVersionDate, result.CurrentVersionUtcDate); Assert.AreEqual(originalVersionDate, result.OriginalVersionUtcDate); Assert.AreEqual(newVersionDescription, result.CurrentVersionDescription); Assert.AreEqual(originalVersionDescription, result.OriginalVersionDescription); var expectedTagDiffString = string.Join(",", new[] { originalTagName1, originalTagName2 }.OrderBy(s => s)); Assert.AreEqual(new(originalTagName1, expectedTagDiffString), result.Tags); Assert.IsNull(result.FrontSide); Assert.IsNull(result.BackSide); Assert.IsNull(result.Language); Assert.IsNull(result.AdditionalInfo); Assert.IsNull(result.UsersWithView); Assert.IsNull(result.ImagesOnFrontSide); Assert.IsNull(result.ImagesOnBackSide); Assert.IsNull(result.ImagesOnAdditionalSide); } }