Example #1
0
        public async Task Test_05_AccessControl_GetBlogsAsync()
        {
            using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                BlogHandler blogHandler = new BlogHandler(dbContext);
                GroupHandler groupHandler = new GroupHandler(dbContext);
                GroupMemberHandler gmHandler = new GroupMemberHandler(dbContext);
                PersonHandler perHandler = new PersonHandler(dbContext);
                PersonXPersonHandler pxpHandler = new PersonXPersonHandler(dbContext);

                Person faker = CreatePerson("TestFaker", "TestFaker");
                Person marin = CreatePerson("TestMarin", "TestMarin");
                Person deft = CreatePerson("TestDeft", "TestDeft");

                Follow(faker.ID, marin.ID, deft.ID);

                //1. test access info MySelfOnly.
                await blogHandler.CreateBlogAsync(faker.ID, "TestContentByFakerOne", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(marin.ID, "TestContentByMarinOne", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(marin.ID, "TestContentByMarinTwo", BlogInfoAccessInfo.MyselfOnly);
                await blogHandler.CreateBlogAsync(deft.ID, "TestContentByDeftOne", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(deft.ID, "TestContentByDeftTwo", BlogInfoAccessInfo.MyselfOnly);

                List<Blog> blogs = await blogHandler.GetBlogsAsync(faker.ID);

                Assert.AreEqual(blogs.Count, 3);

                foreach (var blog in blogs)
                {
                    Assert.AreNotEqual(blog.Content, "TestContentByMarinTwo");
                    Assert.AreNotEqual(blog.Content, "TestContentByDeftTwo");
                }

                //2. test access info GroupOnly.
                Person paul = CreatePerson("TestPaul", "TestPaul");
                Person judy = CreatePerson("TestJudy", "TestJudy");
                Person anne = CreatePerson("TestAnne", "TestAnne");
                Person alisa = CreatePerson("TestAlisa", "TestAlisa");

                Follow(paul.ID, judy.ID, anne.ID, alisa.ID);

                //judy group not include paul.
                Group judyGroup = await groupHandler.CreateGroupAsync(judy.ID, "TestJudyGroup", GroupType.GroupList);

                //anne group include paul.
                Group anneGroup = await groupHandler.CreateGroupAsync(anne.ID, "TestAnneGroup", GroupType.GroupList);

                GroupMember groupMemberByAnne = new GroupMember()
                {
                    GroupID = anneGroup.ID,
                    PersonID = paul.ID
                };
                gmHandler.Add(groupMemberByAnne);
                gmHandler.SaveChanges();

                //alisa group include judy, anne but not paul.
                Group alisaGroup = await groupHandler.CreateGroupAsync(alisa.ID, "TestAlisaGroup", GroupType.GroupList);

                GroupMember groupMemberByAlisaOne = new GroupMember()
                {
                    GroupID = alisaGroup.ID,
                    PersonID = judy.ID
                };
                GroupMember groupMemberByAlisaTwo = new GroupMember()
                {
                    GroupID = alisaGroup.ID,
                    PersonID = anne.ID
                };

                gmHandler.Add(groupMemberByAlisaOne);
                gmHandler.Add(groupMemberByAlisaTwo);
                gmHandler.SaveChanges();

                await blogHandler.CreateBlogAsync(paul.ID, "TestContentByPaul", BlogInfoAccessInfo.MyselfOnly);
                await blogHandler.CreateBlogAsync(judy.ID, "TestContentByJudyOne", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(judy.ID, "TestContentByJudyTwo", BlogInfoAccessInfo.GroupOnly, judyGroup.ID);
                await blogHandler.CreateBlogAsync(anne.ID, "TestContentByAnne", BlogInfoAccessInfo.GroupOnly, anneGroup.ID);
                await blogHandler.CreateBlogAsync(alisa.ID, "TestContentByAlisa", BlogInfoAccessInfo.GroupOnly, alisaGroup.ID);

                blogs = await blogHandler.GetBlogsAsync(paul.ID);

                Assert.AreEqual(blogs.Count, 3);

                foreach (var blog in blogs)
                {
                    Assert.AreNotEqual(blog.Content, "TestContentByJudyTwo");
                    Assert.AreNotEqual(blog.Content, "TestContentByAlisa");
                }

                //3. test access info FriendOnly.
                Person sam = CreatePerson("TestSam", "TestSam");
                Person joan = CreatePerson("TestJoan", "TestJoan");
                Person lily = CreatePerson("TestLily", "TestLily");

                Follow(sam.ID, joan.ID, lily.ID);

                //3.1 test joan and sam is friend but not lily.
                Follow(joan.ID, sam.ID);

                await blogHandler.CreateBlogAsync(joan.ID, "TestContentByJoanOne", BlogInfoAccessInfo.FriendOnly);
                await blogHandler.CreateBlogAsync(joan.ID, "TestContentByJoanTwo", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(lily.ID, "TestContentByLilyOne", BlogInfoAccessInfo.FriendOnly);
                await blogHandler.CreateBlogAsync(lily.ID, "TestContentByLilyTwo", BlogInfoAccessInfo.FriendOnly);

                blogs = await blogHandler.GetBlogsAsync(sam.ID);

                Assert.AreEqual(blogs.Count, 2);

                foreach (var blog in blogs)
                {
                    Assert.AreNotEqual(blog.Content, "TestContentByLilyOne");
                    Assert.AreNotEqual(blog.Content, "TestContentByLilyTwo");
                }

                //3.2 now lily follow sam too.
                Follow(lily.ID, sam.ID);

                blogs = await blogHandler.GetBlogsAsync(sam.ID);

                Assert.AreEqual(blogs.Count, 4);
            }
        }
Example #2
0
        public async Task Test_01_AddCommentWithoutContent()
        {
            using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                BlogHandler blogHandler = new BlogHandler(dbContext);
                GroupHandler groupHandler = new GroupHandler(dbContext);
                GroupMemberHandler gmHandler = new GroupMemberHandler(dbContext);
                CommentHandler commentHandler = new CommentHandler(dbContext);

                Person sam = CreatePerson("TestSam", "TestSam", AllowablePersonForComment.All, true);

                Blog testBlog = await blogHandler.CreateBlogAsync(sam.ID, "TestCommentBlog", BlogInfoAccessInfo.MyselfOnly);

                Person shelly = CreatePerson("TestShelly", "TestShelly", AllowablePersonForComment.All, true);

                //1. normal test.
                Comment testComment = await commentHandler.AddCommentAsync(shelly.ID, testBlog.ID, "hello, i am a comment test");

                Comment testNormalComment = await commentHandler.GetByIdAsync(testComment.ID);

                Assert.IsNotNull(testNormalComment);
                Assert.AreEqual(testNormalComment.PersonID, shelly.ID);
                Assert.AreEqual(testNormalComment.BlogID, testBlog.ID);
                Assert.AreEqual(testNormalComment.Content, "hello, i am a comment test");

                //2. set error blog id and test it.
                bool isChecked = false;
                try
                {
                    Comment testComment_1 = await commentHandler.AddCommentAsync(commentPerson.ID, 12313121, "hello, i am a comment test");
                }
                catch (Exception ex)
                {
                    isChecked = true;
                    Assert.AreEqual(ex.GetType(), typeof(DisplayableException));
                    Assert.AreEqual(ex.Message, "要评论的Blog不存在或者已经被删除");
                }
                Assert.IsTrue(isChecked);

                //3. test black list comment.
                Group blackListGroup = new Group()
                {
                    PersonID = sam.ID,
                    Name = "TestBlackList",
                    Type = GroupType.BlackList
                };
                groupHandler.Add(blackListGroup);
                groupHandler.SaveChanges();

                GroupMember GroupMemberByBlack = new GroupMember()
                {
                    GroupID = blackListGroup.ID,
                    PersonID = commentPerson.ID
                };

                gmHandler.Add(GroupMemberByBlack);
                gmHandler.SaveChanges();

                isChecked = false;
                try
                {
                    Comment testComment_2 = await commentHandler.AddCommentAsync(commentPerson.ID, testBlog.ID, "hello, i am a comment test 4 black list.");
                }
                catch (Exception ex)
                {
                    isChecked = true;
                    Assert.AreEqual(ex.GetType(), typeof(DisplayableException));
                    Assert.AreEqual(ex.Message, "由于用户设置,你无法回复评论。");
                }
                Assert.IsTrue(isChecked);

                //4. remove black list member.
                gmHandler.MarkAsDeleted(GroupMemberByBlack);
                gmHandler.SaveChanges();
            }
        }
Example #3
0
        public async Task Test_04_Normal_GetBlogsAsync()
        {
            using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                BlogHandler blogHandler = new BlogHandler(dbContext);
                GroupHandler groupHandler = new GroupHandler(dbContext);
                GroupMemberHandler gmHandler = new GroupMemberHandler(dbContext);
                PersonHandler perHandler = new PersonHandler(dbContext);
                PersonXPersonHandler pxpHandler = new PersonXPersonHandler(dbContext);

                //1. test normal.
                bool isChecked = false;
                try
                {
                    List<Blog> invalidPersonIdBlogs = await blogHandler.GetBlogsAsync(999999999);
                }
                catch (Exception ex)
                {
                    isChecked = true;
                    Assert.AreEqual(ex.Message, "该用户不存在");
                }
                Assert.IsTrue(isChecked);

                Person master = CreatePerson("TestMasterPer", "TestMasterMind");

                Person mary = CreatePerson("TestMary", "TestMary");
                Person nick = CreatePerson("TestNick", "TestNick");
                Person tony = CreatePerson("TestTony", "TestTony");

                //1. create some blog and test it.
                await blogHandler.CreateBlogAsync(master.ID, "TestContentByMaster", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(mary.ID, "TestContentByMaryOne", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(mary.ID, "TestContentByMaryTwo", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(nick.ID, "TestContentByNickOne", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(tony.ID, "TestContentByTonyOne", BlogInfoAccessInfo.All);

                Follow(master.ID, mary.ID, nick.ID, tony.ID);
                
                List<Blog> blogs = await blogHandler.GetBlogsAsync(master.ID);
                
                Assert.AreEqual(blogs.Count, 5);
                Assert.AreEqual(blogs.Count(x => x.PersonID == mary.ID), 2);
                Assert.AreEqual(blogs.Count(x => x.PersonID == nick.ID), 1);
                Assert.AreEqual(blogs.Count(x => x.PersonID == tony.ID), 1);
                Assert.AreEqual(blogs.Count(x => x.PersonID == master.ID), 1);

                //2. add a group and add some group member test it.
                Group masterGroup = new Group()
                {
                    PersonID = master.ID,
                    Name = "TestMasterGroup",
                    Type = GroupType.GroupList
                };
                groupHandler.Add(masterGroup);
                groupHandler.SaveChanges();

                GroupMember GroupMemberByMary = new GroupMember()
                {
                    GroupID = masterGroup.ID,
                    PersonID = mary.ID
                };
                GroupMember GroupMemberByNick = new GroupMember()
                {
                    GroupID = masterGroup.ID,
                    PersonID = nick.ID
                };
                gmHandler.Add(GroupMemberByMary);
                gmHandler.Add(GroupMemberByNick);
                gmHandler.SaveChanges();

                //3. test get blog by group.
                blogs = await blogHandler.GetBlogsAsync(master.ID, masterGroup.ID);

                Assert.AreEqual(blogs.Count, 4);
                Assert.AreEqual(blogs.Count(x => x.PersonID == mary.ID), 2);
                Assert.AreEqual(blogs.Count(x => x.PersonID == nick.ID), 1);
                Assert.AreEqual(blogs.Count(x => x.PersonID == master.ID), 1);

                //4. add shield group and test it.
                Person mike = CreatePerson("TestMike", "TestMike");
                Person yoyo = CreatePerson("TestYOYO", "TestYOYO");
                Person pipi = CreatePerson("TestPIPI", "TestPIPI");
                Person poko = CreatePerson("TestPoko", "TestPoko");

                Follow(master.ID, mike.ID, yoyo.ID, pipi.ID, poko.ID);

                await blogHandler.CreateBlogAsync(mike.ID, "TestContentByMikeOne", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(mike.ID, "TestContentByMikeTwo", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(yoyo.ID, "TestContentByYoyoOne", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(yoyo.ID, "TestContentByYoyoTwo", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(pipi.ID, "TestContentByPipiOne", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(poko.ID, "TestContentByPokoOne", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(poko.ID, "TestContentByPokoTwo", BlogInfoAccessInfo.All);
                await blogHandler.CreateBlogAsync(poko.ID, "TestContentByPokoThree", BlogInfoAccessInfo.All);

                Group masterShieldGroup = new Group()
                {
                    PersonID = master.ID,
                    Name = "TestMasterShield",
                    Type = GroupType.ShieldList
                };
                groupHandler.Add(masterShieldGroup);
                groupHandler.SaveChanges();

                GroupMember GroupMemberByPoko = new GroupMember()
                {
                    GroupID = masterShieldGroup.ID,
                    PersonID = poko.ID
                };

                gmHandler.Add(GroupMemberByPoko);
                gmHandler.SaveChanges();

                blogs = await blogHandler.GetBlogsAsync(master.ID);

                Assert.AreEqual(blogs.Count, 10);

                foreach (var blog in blogs)
                {
                    Assert.AreNotEqual(blog.PersonID, poko.ID);
                    Assert.AreNotEqual(blog.Content, "TestContentByPokoOne");
                    Assert.AreNotEqual(blog.Content, "TestContentByPokoTwo");
                    Assert.AreNotEqual(blog.Content, "TestContentByPokoThree");
                }

                //5. add group member to normal group and test it.
                GroupMember GroupMemberByPoko_Normal = new GroupMember()
                {
                    GroupID = masterGroup.ID,
                    PersonID = poko.ID
                };
                gmHandler.Add(GroupMemberByMary);

                blogs = await blogHandler.GetBlogsAsync(master.ID, masterGroup.ID);

                Assert.AreEqual(blogs.Count, 4);
                Assert.AreEqual(blogs.Count(x => x.PersonID == mary.ID), 2);
                Assert.AreEqual(blogs.Count(x => x.PersonID == nick.ID), 1);
                Assert.AreEqual(blogs.Count(x => x.PersonID == master.ID), 1);
            }
        }
Example #4
0
        public async Task Test_04_FollowAsync()
        {
            using (KoalaBlogDbContext dbContext = new KoalaBlogDbContext())
            {
                PersonHandler perHandler = new PersonHandler(dbContext);
                GroupHandler groupHandler = new GroupHandler(dbContext);
                GroupMemberHandler gmHandler = new GroupMemberHandler(dbContext);

                //1. test normal.
                Person sam = CreatePerson("TestSam", "TestSam");
                Person joan = CreatePerson("TestJoan", "TestJoan");

                bool isSucceed = await perHandler.FollowAsync(sam.ID, joan.ID);

                sam = await perHandler.Include(x => x.MyFollowingPersons, x => x.MyFans).SingleOrDefaultAsync(x => x.ID == sam.ID);
                joan = await perHandler.Include(x => x.MyFollowingPersons, x => x.MyFans).SingleOrDefaultAsync(x => x.ID == joan.ID);

                Assert.IsTrue(isSucceed);
                Assert.AreEqual(sam.MyFollowingPersons.Count, 1);
                Assert.AreEqual(sam.MyFans.Count, 0);
                Assert.AreEqual(joan.MyFollowingPersons.Count, 0);
                Assert.AreEqual(joan.MyFans.Count, 1);

                //2. test one more time follow, and check it count was changed.
                isSucceed = await perHandler.FollowAsync(sam.ID, joan.ID);

                sam = await perHandler.Include(x => x.MyFollowingPersons, x => x.MyFans).SingleOrDefaultAsync(x => x.ID == sam.ID);
                joan = await perHandler.Include(x => x.MyFollowingPersons, x => x.MyFans).SingleOrDefaultAsync(x => x.ID == joan.ID);

                Assert.IsTrue(isSucceed);
                Assert.AreEqual(sam.MyFollowingPersons.Count, 1);
                Assert.AreEqual(sam.MyFollowingPersons.First().FollowingID, joan.ID);
                Assert.AreEqual(sam.MyFans.Count, 0);
                Assert.AreEqual(joan.MyFollowingPersons.Count, 0);
                Assert.AreEqual(joan.MyFans.Count, 1);
                Assert.AreEqual(joan.MyFans.First().FollowerID, sam.ID);

                //3. test black list.
                Person jay = CreatePerson("TestJay", "TestJay");
                Person ken = CreatePerson("TestKen", "TestKen");

                Group blackGroup = await groupHandler.CreateGroupAsync(ken.ID, "TestBGroup", GroupType.BlackList);

                GroupMember gm_sam = new GroupMember()
                {
                    PersonID = jay.ID,
                    GroupID = blackGroup.ID
                };
                gmHandler.Add(gm_sam);
                gmHandler.SaveChanges();

                bool isChecked = false;
                try
                {
                    isSucceed = await perHandler.FollowAsync(jay.ID, ken.ID);
                }
                catch (Exception ex)
                {
                    isChecked = true;
                    Assert.AreEqual(ex.GetType(), typeof(DisplayableException));
                    Assert.AreEqual(ex.Message, "由于用户设置,你无法关注。");
                }
                Assert.IsTrue(isChecked);

                jay = await perHandler.Include(x => x.MyFollowingPersons, x => x.MyFans).SingleOrDefaultAsync(x => x.ID == jay.ID);
                ken = await perHandler.Include(x => x.MyFollowingPersons, x => x.MyFans).SingleOrDefaultAsync(x => x.ID == ken.ID);

                Assert.AreEqual(jay.MyFollowingPersons.Count, 0);
                Assert.AreEqual(jay.MyFans.Count, 0);
                Assert.AreEqual(ken.MyFollowingPersons.Count, 0);
                Assert.AreEqual(ken.MyFans.Count, 0);
            }
        }