Ejemplo n.º 1
0
        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));
            }
        }
Ejemplo n.º 2
0
        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);
            }
        }
Ejemplo n.º 3
0
        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 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());
            }
        }
Ejemplo n.º 6
0
        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());
            }
        }
Ejemplo n.º 7
0
        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 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());
        }
Ejemplo n.º 9
0
        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);
            }
        }
Ejemplo n.º 10
0
        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));
        }
Ejemplo n.º 11
0
        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 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());
            }
        }
Ejemplo n.º 13
0
        public async Task EmptyDB_OriginalVersionDoesNotExist()
        {
            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);

            using (var dbContext = new MemCheckDbContext(db))
                await new UpdateCard(dbContext.AsCallContext()).RunAsync(UpdateCardHelper.RequestForFrontSideChange(card, RandomHelper.String()));
            using (var dbContext = new MemCheckDbContext(db))
                await dbContext.CardPreviousVersions.Where(previous => previous.Card == card.Id).SingleAsync();
            using (var dbContext = new MemCheckDbContext(DbHelper.GetEmptyTestDB()))
                await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => await new GetCardDiff(dbContext.AsCallContext()).RunAsync(new GetCardDiff.Request(userId, card.Id, Guid.NewGuid())));
        }
Ejemplo n.º 14
0
        public async Task UserDoesNotExist()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var languageId = await CardLanguagHelper.CreateAsync(db);

            var frontSide = RandomHelper.String();
            var card      = await CardHelper.CreateAsync(db, user, language : languageId, frontSide : frontSide);

            using var dbContext = new MemCheckDbContext(db);
            var r = UpdateCardHelper.RequestForFrontSideChange(card, RandomHelper.String(), Guid.NewGuid());
            await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => await new UpdateCard(dbContext.AsCallContext()).RunAsync(r));

            await CardHelper.AssertCardHasFrontSide(db, card.Id, frontSide);
        }
Ejemplo n.º 15
0
        public async Task ReduceVisibility_NoUserHasInDeck_OtherAuthor()
        {
            var db = DbHelper.GetEmptyTestDB();
            var cardCreatorName = RandomHelper.String();
            var cardCreator     = await UserHelper.CreateInDbAsync(db, userName : cardCreatorName);

            var languageId = await CardLanguagHelper.CreateAsync(db);

            var card = await CardHelper.CreateAsync(db, cardCreator, language : languageId, userWithViewIds : Array.Empty <Guid>());

            var newVersionCreatorName = RandomHelper.String();
            var newVersionCreator     = await UserHelper.CreateInDbAsync(db, userName : newVersionCreatorName);

            using (var dbContext = new MemCheckDbContext(db))
            {
                var r = UpdateCardHelper.RequestForFrontSideChange(card, RandomHelper.String(), newVersionCreator);
                await new UpdateCard(dbContext.AsCallContext()).RunAsync(r);
            }

            using (var dbContext = new MemCheckDbContext(db))
            {
                var r = UpdateCardHelper.RequestForVisibilityChange(card, cardCreator.AsArray(), cardCreator);
                var e = await Assert.ThrowsExceptionAsync <RequestInputException>(async() => await new UpdateCard(dbContext.AsCallContext()).RunAsync(r));

                Assert.IsTrue(e.Message.Contains(newVersionCreatorName));
            }

            using (var dbContext = new MemCheckDbContext(db))
            {
                var r = UpdateCardHelper.RequestForVisibilityChange(card, new[] { newVersionCreator }, newVersionCreator);
                var e = await Assert.ThrowsExceptionAsync <RequestInputException>(async() => await new UpdateCard(dbContext.AsCallContext()).RunAsync(r));

                Assert.IsTrue(e.Message.Contains(cardCreatorName));
            }

            using (var dbContext = new MemCheckDbContext(db))
            {
                var r = UpdateCardHelper.RequestForVisibilityChange(card, new[] { cardCreator, newVersionCreator });
                await new UpdateCard(dbContext.AsCallContext()).RunAsync(r);
            }

            using (var dbContext = new MemCheckDbContext(db))
            {
                CardVisibilityHelper.CheckUserIsAllowedToViewCards(dbContext, cardCreator, card.Id);
                CardVisibilityHelper.CheckUserIsAllowedToViewCards(dbContext, newVersionCreator, card.Id);
            }
        }
Ejemplo n.º 16
0
        public async Task CardsNotFoundAnymore_StillExists_UserAllowedToView()
        {
            var db = DbHelper.GetEmptyTestDB();

            var cardCreator = await UserHelper.CreateInDbAsync(db);

            var language = await CardLanguagHelper.CreateAsync(db);

            var card = await CardHelper.CreateAsync(db, cardCreator, versionDate : new DateTime(2050, 03, 01), language : language);

            var subscriber = await UserHelper.CreateInDbAsync(db);

            var subscription = await SearchSubscriptionHelper.CreateAsync(db, subscriber, requiredText : card.FrontSide);

            using (var dbContext = new MemCheckDbContext(db))
            {
                var searchResult = await new UserSearchNotifier(dbContext.AsCallContext(), 100, new DateTime(2050, 05, 01)).RunAsync(subscription.Id);
                Assert.AreEqual(1, searchResult.TotalNewlyFoundCardCount);
            }

            using (var dbContext = new MemCheckDbContext(db))
            {
                var updateRequest = UpdateCardHelper.RequestForFrontSideChange(card, 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(0, searchResult.TotalNewlyFoundCardCount);
                Assert.AreEqual(0, searchResult.NewlyFoundCards.Length);
                Assert.AreEqual(1, searchResult.CountOfCardsNotFoundAnymore_StillExists_UserAllowedToView);
                Assert.AreEqual(1, searchResult.CardsNotFoundAnymore_StillExists_UserAllowedToView.Length);
                Assert.AreEqual(card.Id, searchResult.CardsNotFoundAnymore_StillExists_UserAllowedToView.Single().CardId);
                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))
                Assert.AreEqual(runDate, dbContext.SearchSubscriptions.Single(s => s.Id == subscription.Id).LastRunUtcDate);
        }
Ejemplo n.º 17
0
        public async Task UserNotAllowedToViewCard()
        {
            var db          = DbHelper.GetEmptyTestDB();
            var cardCreator = await UserHelper.CreateInDbAsync(db);

            var languageId = await CardLanguagHelper.CreateAsync(db);

            var frontSide = RandomHelper.String();
            var card      = await CardHelper.CreateAsync(db, cardCreator, language : languageId, userWithViewIds : cardCreator.AsArray(), frontSide : frontSide);

            var otherUser = await UserHelper.CreateInDbAsync(db);

            using var dbContext = new MemCheckDbContext(db);
            var r = UpdateCardHelper.RequestForFrontSideChange(card, RandomHelper.String(), otherUser);
            await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => await new UpdateCard(dbContext.AsCallContext()).RunAsync(r));

            await CardHelper.AssertCardHasFrontSide(db, card.Id, frontSide);
        }
        public async Task CardWithPreviousVersionsNotDeleted()
        {
            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()));

            using (var dbContext = new MemCheckDbContext(db))
                Assert.AreEqual(1, dbContext.CardPreviousVersions.Count());
            using (var dbContext = new MemCheckDbContext(db))
                await new DeleteCardPreviousVersionsOfDeletedCards(dbContext.AsCallContext(new TestRoleChecker(user))).RunAsync(new DeleteCardPreviousVersionsOfDeletedCards.Request(user, DateTime.MaxValue));
            using (var dbContext = new MemCheckDbContext(db))
                Assert.AreEqual(1, dbContext.CardPreviousVersions.Count());
        }
Ejemplo n.º 19
0
        public async Task UserNotInNewVisibilityList()
        {
            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 newVersionCreator = await UserHelper.CreateInDbAsync(db);

            var r = UpdateCardHelper.RequestForVisibilityChange(card, cardCreator.AsArray()) with {
                VersionCreatorId = newVersionCreator
            };

            using var dbContext = new MemCheckDbContext(db);
            await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => await new UpdateCard(dbContext.AsCallContext()).RunAsync(r));
        }
Ejemplo n.º 20
0
        public async Task FailIfUserCanNotViewCurrentVersion()
        {
            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);    //Created public

            using (var dbContext = new MemCheckDbContext(db))
                await new UpdateCard(dbContext.AsCallContext()).RunAsync(UpdateCardHelper.RequestForVisibilityChange(card, userWithViewIds: userId.AsArray()));    //Now private
            var otherUserId = await UserHelper.CreateInDbAsync(db);

            using (var dbContext = new MemCheckDbContext(db))
            {
                var versions = await new GetCardVersions(dbContext.AsCallContext()).RunAsync(new GetCardVersions.Request(userId, card.Id));
                Assert.AreEqual(2, versions.Count());
                await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => await new GetCardVersions(dbContext.AsCallContext()).RunAsync(new GetCardVersions.Request(otherUserId, card.Id)));
            }
        }
Ejemplo n.º 21
0
        public async Task UserNotSubscribingToCardOnEdit()
        {
            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);

            var newVersionCreator = await UserHelper.CreateInDbAsync(db, subscribeToCardOnEdit : false);

            using (var dbContext = new MemCheckDbContext(db))
            {
                var r = UpdateCardHelper.RequestForFrontSideChange(card, RandomHelper.String(), newVersionCreator);
                await new UpdateCard(dbContext.AsCallContext()).RunAsync(r);
            }

            Assert.IsFalse(await CardSubscriptionHelper.UserIsSubscribedToCardAsync(db, newVersionCreator, card.Id));
        }
Ejemplo n.º 22
0
        public async Task PublicCard()
        {
            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 otherUser = await UserHelper.CreateInDbAsync(db);

            var newFrontSide = RandomHelper.String();

            using (var dbContext = new MemCheckDbContext(db))
            {
                var r = UpdateCardHelper.RequestForFrontSideChange(card, newFrontSide, otherUser);
                await new UpdateCard(dbContext.AsCallContext()).RunAsync(r);
            }

            await CardHelper.AssertCardHasFrontSide(db, card.Id, newFrontSide);
        }
Ejemplo n.º 23
0
        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()); //Private

            using (var dbContext = new MemCheckDbContext(db))
                await new UpdateCard(dbContext.AsCallContext()).RunAsync(UpdateCardHelper.RequestForVisibilityChange(card, userWithViewIds: Array.Empty <Guid>()));   //Now public
            var otherUserId = await UserHelper.CreateInDbAsync(db);

            using (var dbContext = new MemCheckDbContext(db))
            {
                var versionId = (await new GetCardVersions(dbContext.AsCallContext()).RunAsync(new GetCardVersions.Request(otherUserId, card.Id))).Single(v => v.VersionId != null).VersionId !.Value;

                var version = await new GetCardVersion(dbContext.AsCallContext()).RunAsync(new GetCardVersion.Request(userId, versionId));
                Assert.AreEqual(1, version.UsersWithVisibility.Count());
                await Assert.ThrowsExceptionAsync <InvalidOperationException>(async() => await new GetCardVersion(dbContext.AsCallContext()).RunAsync(new GetCardVersion.Request(otherUserId, versionId)));
            }
        }
Ejemplo n.º 24
0
        public async Task SucceedsIfCreatorOfCurrentVersionIsDeleted()
        {
            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));
            await UserHelper.DeleteAsync(db, lastVersionCreator);

            using (var dbContext = new MemCheckDbContext(db))
                await new DeleteCards(dbContext.AsCallContext()).RunAsync(new DeleteCards.Request(firstVersionCreator, card.Id.AsArray()));
            using (var dbContext = new MemCheckDbContext(db))
            {
                Assert.IsFalse(dbContext.Cards.Any());
                Assert.AreEqual(3, dbContext.CardPreviousVersions.Count());
            }
        }
Ejemplo n.º 25
0
        public async Task PrivateCardDeletedWhileOthersExist()
        {
            var db         = DbHelper.GetEmptyTestDB();
            var loggedUser = await UserHelper.CreateInDbAsync(db);

            var language = await CardLanguagHelper.CreateAsync(db);

            var userToDelete = await UserHelper.CreateInDbAsync(db);

            var privateCardToDelete = await CardHelper.CreateAsync(db, userToDelete, language : language, userWithViewIds : userToDelete.AsArray());

            await UpdateCardHelper.RunAsync(db, UpdateCardHelper.RequestForBackSideChange(privateCardToDelete, RandomHelper.String()));

            var privateCardNotToDelete = await CardHelper.CreateAsync(db, loggedUser, language : language, userWithViewIds : loggedUser.AsArray());

            await UpdateCardHelper.RunAsync(db, UpdateCardHelper.RequestForBackSideChange(privateCardNotToDelete, RandomHelper.String()));

            var publicCard = await CardHelper.CreateAsync(db, userToDelete, language : language);

            await UpdateCardHelper.RunAsync(db, UpdateCardHelper.RequestForBackSideChange(publicCard, RandomHelper.String()));

            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(card => card.Id == privateCardToDelete.Id));
                Assert.IsFalse(dbContext.CardPreviousVersions.Any(previousVersion => previousVersion.Card == privateCardToDelete.Id));
                Assert.IsTrue(dbContext.Cards.Any(card => card.Id == privateCardNotToDelete.Id));
                Assert.IsTrue(dbContext.CardPreviousVersions.Any(previousVersion => previousVersion.Card == privateCardNotToDelete.Id));
                Assert.IsTrue(dbContext.Cards.Any(card => card.Id == publicCard.Id));
                Assert.IsTrue(dbContext.CardPreviousVersions.Any(previousVersion => previousVersion.Card == publicCard.Id));
            }
        }
        public async Task CascadeDeletionOfTagInPreviousCardVersion()
        {
            var db   = DbHelper.GetEmptyTestDB();
            var user = await UserHelper.CreateInDbAsync(db);

            var languageId = await CardLanguagHelper.CreateAsync(db);

            var tag1 = await TagHelper.CreateAsync(db);

            var tag2 = await TagHelper.CreateAsync(db);

            var card = await CardHelper.CreateAsync(db, user, language : languageId, tagIds : new[] { tag1, tag2 });

            var tag3 = await TagHelper.CreateAsync(db);

            await UpdateCardHelper.RunAsync(db, UpdateCardHelper.RequestForTagChange(card, tag3.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(4, dbContext.TagInPreviousCardVersions.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.TagInPreviousCardVersions.Count());
            }
        }
Ejemplo n.º 27
0
        public async Task UpdateDoesNotAlterRatings()
        {
            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);

            var rating = RandomHelper.Rating();

            using (var dbContext = new MemCheckDbContext(db))
                await new SetCardRating(dbContext.AsCallContext()).RunAsync(new SetCardRating.Request(user, card.Id, rating));

            using (var dbContext = new MemCheckDbContext(db))
                await new UpdateCard(dbContext.AsCallContext()).RunAsync(UpdateCardHelper.RequestForFrontSideChange(card, RandomHelper.String()));

            using (var dbContext = new MemCheckDbContext(db))
            {
                var loaded = dbContext.Cards.Single();
                Assert.AreEqual(1, loaded.RatingCount);
                Assert.AreEqual(rating, loaded.AverageRating);
            }
        }
Ejemplo n.º 28
0
        public async Task SucceedsIfDeletedUserHasCreatedAPreviousVersion()
        {
            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))
                using (var userManager = UserHelper.GetUserManager(dbContext))
                    await new DeleteUserAccount(dbContext.AsCallContext(new TestRoleChecker(lastVersionCreator)), userManager).RunAsync(new DeleteUserAccount.Request(lastVersionCreator, firstVersionCreator));
            using (var dbContext = new MemCheckDbContext(db))
                await new DeleteCards(dbContext.AsCallContext()).RunAsync(new DeleteCards.Request(lastVersionCreator, card.Id.AsArray()));
            using (var dbContext = new MemCheckDbContext(db))
            {
                Assert.IsFalse(dbContext.Cards.Any());
                Assert.AreEqual(3, dbContext.CardPreviousVersions.Count());
            }
        }
Ejemplo n.º 29
0
        public async Task MultipleVersions()
        {
            var db       = DbHelper.GetEmptyTestDB();
            var userName = RandomHelper.String();
            var userId   = await UserHelper.CreateInDbAsync(db, userName : userName);

            var language = await CardLanguagHelper.CreateAsync(db);

            var oldestDate        = RandomHelper.Date();
            var oldestDescription = RandomHelper.String();
            var card = await CardHelper.CreateAsync(db, userId, language : language, versionDate : oldestDate, versionDescription : oldestDescription);

            var otherUserName = RandomHelper.String();
            var otherUserId   = await UserHelper.CreateInDbAsync(db, userName : otherUserName);

            var intermediaryDate        = RandomHelper.Date();
            var intermediaryDescription = RandomHelper.String();

            using (var dbContext = new MemCheckDbContext(db))
            {
                var request = UpdateCardHelper.RequestForFrontSideChange(card, RandomHelper.String(), versionCreator: otherUserId, versionDescription: intermediaryDescription);
                request = request with {
                    AdditionalInfo = RandomHelper.String()
                };
                await new UpdateCard(dbContext.AsCallContext(), intermediaryDate).RunAsync(request);
            }

            var newestDate        = RandomHelper.Date();
            var newestDescription = RandomHelper.String();

            using (var dbContext = new MemCheckDbContext(db))
            {
                var cardFromDb = await dbContext.Cards
                                 .Include(c => c.VersionCreator)
                                 .Include(c => c.VersionCreator)
                                 .Include(c => c.Images)
                                 .Include(c => c.CardLanguage)
                                 .Include(c => c.TagsInCards)
                                 .Include(c => c.UsersWithView)
                                 .SingleAsync(c => c.Id == card.Id);

                await new UpdateCard(dbContext.AsCallContext(), newestDate).RunAsync(UpdateCardHelper.RequestForBackSideChange(cardFromDb, RandomHelper.String(), versionDescription: newestDescription, versionCreator: userId));
            }

            using (var dbContext = new MemCheckDbContext(db))
            {
                var versions = (await new GetCardVersions(dbContext.AsCallContext()).RunAsync(new GetCardVersions.Request(otherUserId, card.Id))).ToList();

                Assert.AreEqual((await dbContext.CardPreviousVersions.SingleAsync(c => c.VersionUtcDate == oldestDate)).Id, versions[2].VersionId);
                Assert.AreEqual(oldestDate, versions[2].VersionUtcDate);
                Assert.AreEqual(userName, versions[2].VersionCreator);
                Assert.AreEqual(oldestDescription, versions[2].VersionDescription);
                Assert.AreEqual(5, versions[2].ChangedFieldNames.Count());

                Assert.AreEqual((await dbContext.CardPreviousVersions.SingleAsync(c => c.VersionUtcDate == intermediaryDate)).Id, versions[1].VersionId);
                Assert.AreEqual(intermediaryDate, versions[1].VersionUtcDate);
                Assert.AreEqual(otherUserName, versions[1].VersionCreator);
                Assert.AreEqual(intermediaryDescription, versions[1].VersionDescription);
                Assert.AreEqual(2, versions[1].ChangedFieldNames.Count());
                Assert.IsTrue(versions[1].ChangedFieldNames.Any(f => f == "FrontSide"));
                Assert.IsTrue(versions[1].ChangedFieldNames.Any(f => f == "AdditionalInfo"));

                Assert.IsNull(versions[0].VersionId);
                Assert.AreEqual(newestDate, versions[0].VersionUtcDate);
                Assert.AreEqual(userName, versions[0].VersionCreator);
                Assert.AreEqual(newestDescription, versions[0].VersionDescription);
                Assert.AreEqual(1, versions[0].ChangedFieldNames.Count());
                Assert.AreEqual("BackSide", versions[0].ChangedFieldNames.First());
            }
        }
    }
Ejemplo n.º 30
0
        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);
            }
        }