Exemple #1
0
 public ForumNewsGroup GetForumNewsGroupByName(string discussionGroupName)
 {
     lock (this)
     {
         return(RetryCall(() => ForumsService.GetForumNewsGroupByName(discussionGroupName)));
     }
 }
        public async Task LikeShouldAddALikeToTheGivenPostFromTheGivenUser()
        {
            var user = new ApplicationUser()
            {
                Email = "*****@*****.**",
            };
            var topic    = "TestTopic";
            var text     = "TestText";
            var newForum = new Forum()
            {
                ForumTopic = topic,
                ForumText  = text,
            };

            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase("forumTestLikePOST");
            var db = new ApplicationDbContext(options.Options);

            await db.Users.AddAsync(user);

            await db.Forums.AddAsync(newForum);

            await db.SaveChangesAsync();

            int expectedLikes = 1;

            var service = new ForumsService(db);

            await service.Like(newForum.ForumId, user.Id);

            var likedForum = await db.Forums.FirstOrDefaultAsync(f => f.ForumId == newForum.ForumId);

            Assert.NotNull(likedForum);
            Assert.Equal(expectedLikes, likedForum.Likes);
        }
        public async Task DeletePostShouldRemoveThePostIfGivenValidForumId()
        {
            int defaultLikes = 0;
            var topic        = "TestTopic";
            var text         = "TestText";

            var forum = new Forum()
            {
                ForumText  = text,
                ForumTopic = topic,
                Likes      = defaultLikes,
            };

            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase("forumTestAddPost");
            var db = new ApplicationDbContext(options.Options);

            await db.Forums.AddAsync(forum);

            await db.SaveChangesAsync();

            int countBeforeDelete = db.Forums.Count();
            var service           = new ForumsService(db);

            await service.DeletePost(forum.ForumId);

            var forumDb = db.Forums.FirstOrDefault(f => f.ForumId == forum.ForumId);

            Assert.Null(forumDb);
            Assert.Equal(countBeforeDelete - 1, db.Forums.Count());
        }
        public async Task AddPostShouldAddAPostWithTheGivenInputModelAndReturnPostId()
        {
            int defaultLikes = 0;

            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase("forumTest");
            var db = new ApplicationDbContext(options.Options);

            var topic = "TestTopic";
            var text  = "TestText";

            var newInputAdd = new AddForumModel()
            {
                Topic = topic,
                Text  = text,
            };

            var countBeforeAdding = db.Forums.Count();
            var service           = new ForumsService(db);

            var postId = await service.AddPost(newInputAdd);

            var postDb = db.Forums.FirstOrDefault(f => f.ForumId == postId);

            Assert.Equal(countBeforeAdding + 1, db.Forums.Count());
            Assert.NotNull(postDb);
            Assert.Equal(defaultLikes, postDb.Likes);
        }
        public async Task GetPostShouldReturnThePostWithTheGivenId()
        {
            var topic    = "TestTopic";
            var text     = "TestText";
            var newForum = new Forum()
            {
                ForumTopic = topic,
                ForumText  = text,
            };

            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase("forumTestGetPost");
            var db = new ApplicationDbContext(options.Options);

            await db.Forums.AddAsync(newForum);

            await db.SaveChangesAsync();

            var service = new ForumsService(db);

            var result = service.GetPost(newForum.ForumId);

            Assert.NotNull(result);
            Assert.IsType <EditForumViewModel>(result);
            Assert.Equal(topic, result.Topic);
            Assert.Equal(text, result.Text);
        }
Exemple #6
0
        public ForumNewsGroup GetForumNewsGroup(Guid id)
        {
#if PASSPORT_HEADER_ANALYSIS
            lock (this)
#endif
            {
                return(RetryCall(() => ForumsService.GetForumNewsGroup(id)));
            }
        }
Exemple #7
0
        public Message GetMessage(System.Guid forumId, System.Guid messageId)
        {
#if PASSPORT_HEADER_ANALYSIS
            lock (this)
#endif
            {
                return(RetryCall(() => ForumsService.GetMessage(forumId, messageId)));
            }
        }
Exemple #8
0
        public Guid CreateQuestionThread(Guid forumId, string title, string body)
        {
#if PASSPORT_HEADER_ANALYSIS
            lock (this)
#endif
            {
                return(RetryCall(() => ForumsService.CreateQuestionThread(forumId, title, body)));
            }
        }
Exemple #9
0
        public Guid CreateReply(Guid forumId, Guid threadId, Guid postId, string body)
        {
#if PASSPORT_HEADER_ANALYSIS
            lock (this)
#endif
            {
                return(RetryCall(() => ForumsService.CreateReply(forumId, threadId, postId, body)));
            }
        }
Exemple #10
0
        public ForumNewsGroupsContainer GetAllForumNewsGroupsByBrand(string brandName, int pageIndex, int pageSize)
        {
#if PASSPORT_HEADER_ANALYSIS
            lock (this)
#endif
            {
                return(RetryCall(() => ForumsService.GetAllForumNewsGroupsByBrand(brandName, pageIndex, pageSize)));
            }
        }
Exemple #11
0
        public IList <string> GetActiveBrands()
        {
#if PASSPORT_HEADER_ANALYSIS
            lock (this)
#endif
            {
                return(RetryCall(() => ForumsService.GetActiveBrands()));
            }
        }
        public async Task DeletePostShouldThrowInvalidOperationExceptionIfGivenInvalidForumId()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase("forumTestAddPost");
            var db = new ApplicationDbContext(options.Options);

            var service = new ForumsService(db);

            await Assert.ThrowsAsync <InvalidOperationException>(async() => await service.DeletePost("fake_forum_id"));
        }
        public async Task AddPostToUserShouldThrowArgumentNullExceptionIfGivenForumIdIsNullOrEmpty()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase("forumTestAddPost");
            var db = new ApplicationDbContext(options.Options);

            var service = new ForumsService(db);

            await Assert.ThrowsAsync <ArgumentNullException>(async() => await service.AddPostToUser("real_user_id", null));
        }
        public void GetPostShouldThrowArgumentNullExceptionIfGivenInvalidId()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase("forumTestGetPost");
            var db = new ApplicationDbContext(options.Options);

            var service = new ForumsService(db);

            Assert.Throws <ArgumentNullException>(() => service.GetPost("fake_post_id"));
        }
        public async Task AddPostShouldThrowArgumentNullExceptionIfNotGivenInput()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase("forumTest");
            var db = new ApplicationDbContext(options.Options);

            var service = new ForumsService(db);

            await Assert.ThrowsAsync <ArgumentNullException>(async() => await service.AddPost(null));
        }
        public async Task LikeShouldThrowArgumentNullExceptionIfGivenInvalidForumId()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase("forumTestLikePOST");
            var db = new ApplicationDbContext(options.Options);

            var service = new ForumsService(db);

            await Assert.ThrowsAsync <ArgumentNullException>(async() => await service.Like("fake_forum_id", "real_user_id"));
        }
Exemple #17
0
        public BriefMessagesContainer GetForumMessagesBriefForNNTP(Guid forumId, int nntpMessageStartIndex,
                                                                   int nntpMessageEndIndex, bool loadBody)
        {
#if PASSPORT_HEADER_ANALYSIS
            lock (this)
#endif
            {
                return(RetryCall(() => ForumsService.GetForumMessagesBriefForNNTP(forumId, nntpMessageStartIndex, nntpMessageEndIndex,
                                                                                  loadBody)));
            }
        }
        public async Task GetPersonalPostsShouldReturnCollectionOfAllForumPostsMadeByTheUser()
        {
            var user = new ApplicationUser()
            {
                Email = "*****@*****.**",
            };
            var topic        = "TestTopic";
            var text         = "TestText";
            var newInputAddF = new Forum()
            {
                ForumTopic = topic,
                ForumText  = text,
            };
            var newInputAddS = new Forum()
            {
                ForumTopic = topic,
                ForumText  = text,
            };

            var forums = new List <Forum>();

            forums.Add(newInputAddF);
            forums.Add(newInputAddS);

            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase("forumTestGetPosts");
            var db = new ApplicationDbContext(options.Options);

            await db.Users.AddAsync(user);

            await db.Forums.AddRangeAsync(forums);

            await db.SaveChangesAsync();

            int expectedCount = forums.Count();

            var service = new ForumsService(db);

            // Setup

            await service.AddPostToUser(user.Id, newInputAddF.ForumId);

            await service.AddPostToUser(user.Id, newInputAddS.ForumId);

            // Testing

            var result = service.GetPersonalPosts(user.Id);

            Assert.NotNull(result);
            Assert.Equal(expectedCount, result.Count());
            Assert.IsType <PersonalForumViewModel>(result.First());
        }
Exemple #19
0
        public void SetUp()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .EnableDetailedErrors();

            this.forumsRepository          = new EfDeletableEntityRepository <Forum>(new ApplicationDbContext(options.Options));
            this.postsRepository           = new EfDeletableEntityRepository <Post>(new ApplicationDbContext(options.Options));
            this.repliesRepository         = new EfDeletableEntityRepository <Reply>(new ApplicationDbContext(options.Options));
            this.forumCategoriesRepository = new EfRepository <ForumCategory>(new ApplicationDbContext(options.Options));
            this.forumsService             = new ForumsService(this.forumsRepository, this.forumCategoriesRepository, this.postsRepository, this.repliesRepository);
            AutoMapperConfig.RegisterMappings(typeof(TestForum).Assembly);
        }
Exemple #20
0
        public async Task GetPostShouldReturnThePostWithTheGivenId()
        {
            var user = new ApplicationUser()
            {
                Email    = "[email protected]",
                UserName = "******",
            };
            var topic    = "TestTopic";
            var text     = "TestText";
            var newForum = new Forum()
            {
                ForumTopic = topic,
                ForumText  = text,
            };
            var userForum = new UserForums()
            {
                ForumId = newForum.ForumId,
                Forum   = newForum,
                User    = user,
                UserId  = user.Id
            };

            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase("forumTestGetPostNew");
            var db = new ApplicationDbContext(options.Options);

            await db.Forums.AddAsync(newForum);

            await db.Users.AddAsync(user);

            await db.UserForums.AddAsync(userForum);

            await db.SaveChangesAsync();

            var service = new ForumsService(db);

            var result = service.GetPost(newForum.ForumId);

            Assert.NotNull(result);
            Assert.IsType <EditForumViewModel>(result);
            Assert.Equal(topic, result.Topic);
            Assert.Equal(text, result.Text);
            Assert.Equal(user.UserName, result.OwnerName);
        }
        public async Task EditPostShouldThrowInvalidOperationExceptionIfGivenInvalidForumId()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase("forumTestAddPost");
            var db = new ApplicationDbContext(options.Options);

            var service = new ForumsService(db);

            string editedTopic = "EditedTopic";
            string editedText  = "EditedText";

            var editInput = new EditForumInputModel()
            {
                ForumId = "fake_forum_id",
                Topic   = editedTopic,
                Text    = editedText,
            };

            await Assert.ThrowsAsync <InvalidOperationException>(async() => await service.EditPost(editInput));
        }
        public async Task EditPostShouldEditTheGivenPostWithTheGivenEditForumInputModel()
        {
            int defaultLikes = 0;
            var topic        = "TestTopic";
            var text         = "TestText";

            var forum = new Forum()
            {
                ForumText  = text,
                ForumTopic = topic,
                Likes      = defaultLikes,
            };

            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase("forumTestAddPost");
            var db = new ApplicationDbContext(options.Options);

            await db.Forums.AddAsync(forum);

            await db.SaveChangesAsync();

            var service = new ForumsService(db);

            string editedTopic = "EditedTopic";
            string editedText  = "EditedText";

            var editInput = new EditForumInputModel()
            {
                ForumId = forum.ForumId,
                Topic   = editedTopic,
                Text    = editedText,
            };

            await service.EditPost(editInput);

            var forumDb = await db.Forums.FirstOrDefaultAsync(f => f.ForumId == forum.ForumId);

            Assert.NotNull(forumDb);
            Assert.Equal(editedTopic, forumDb.ForumTopic);
            Assert.Equal(editedText, forumDb.ForumText);
        }
        //public IList<T> GetAvailableForums2<T>(
        //    string brand,
        //    Func<Forum, T> converter,
        //    Action<int, int> progress)
        //    where T : class
        //{
        //    if (string.IsNullOrEmpty(brand)) throw new ArgumentNullException("brand");

        //    return BuildResultsInBatch(converter,
        //        delegate(int index, int batchSize)
        //        {
        //            var results = ForumsService.GetAllForumsByBrand(brand, index, batchSize);
        //            //var results = ForumsService.GetAllForumNewsGroupsByBrand(brand, index, batchSize);
        //            var res = new List<Forum>(results.Results);
        //            if (progress != null)
        //                progress((batchSize * index) + res.Count, results.TotalRecords);
        //            return res;
        //        });
        //}

        /// <summary>
        /// Gets a list of articles
        /// </summary>
        /// <param name="lastArticle"></param>
        /// <param name="loadBody"></param>
        /// <param name="converter"></param>
        /// <param name="progress"></param>
        /// <param name="forumId"></param>
        /// <param name="firstArticle"></param>
        /// <returns>List of Forums for the given brand and locale</returns>
        public IList <T> GetForumMessagesBriefForNntp <T>(
            Guid forumId,
            int firstArticle,
            int lastArticle,
            bool loadBody,
            Func <BriefMessage, T> converter,
            Action <IList <T> > progress)
            where T : class
        {
            const int myBatchSize = 150;
            int       lastFromIdx = -1;

            return(BuildResultsInBatch_(converter,
                                        delegate(int index, int batchSize, out bool finished)
            {
                finished = false;
                int fromIdx = firstArticle + (index * myBatchSize);
                int toIdx = Math.Min(fromIdx + myBatchSize - 1, lastArticle);

                if ((fromIdx > toIdx) || (fromIdx == lastFromIdx))
                {
                    finished = true;
                    return null;
                }

                var results = ForumsService.GetForumMessagesBriefForNNTP(forumId, fromIdx, toIdx, loadBody);
                var res = new List <BriefMessage>(results.Results);
                lastFromIdx = fromIdx;

                return res;
            },
                                        r =>
            {
                if (progress != null)
                {
                    progress(r);
                }
            }
                                        ));
        }
Exemple #24
0
        public async Task GetPostShouldThrowArgumentExceptionIfPostDoesntBelongToUser()
        {
            var topic    = "TestTopic";
            var text     = "TestText";
            var newForum = new Forum()
            {
                ForumTopic = topic,
                ForumText  = text,
            };

            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase("forumTestGetPostNew");
            var db = new ApplicationDbContext(options.Options);

            await db.Forums.AddAsync(newForum);

            await db.SaveChangesAsync();

            var service = new ForumsService(db);

            Assert.Throws <ArgumentException>(() => service.GetPost(newForum.ForumId));
        }
        public async Task AddPostToUserShouldAddTheGivenForumIdToTheGivenUserWithId()
        {
            int defaultLikes = 0;
            var topic        = "TestTopic";
            var text         = "TestText";

            var forum = new Forum()
            {
                ForumText  = text,
                ForumTopic = topic,
                Likes      = defaultLikes,
            };

            var user = new ApplicationUser()
            {
                Email = "*****@*****.**",
            };

            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase("forumTestAddPost");
            var db = new ApplicationDbContext(options.Options);

            await db.Forums.AddAsync(forum);

            await db.Users.AddAsync(user);

            await db.SaveChangesAsync();

            var service = new ForumsService(db);

            int countBeforeAdd = db.UserForums.Count();

            await service.AddPostToUser(user.Id, forum.ForumId);

            var uf = db.UserForums.FirstOrDefault(uf => uf.ForumId == forum.ForumId && uf.UserId == user.Id);

            Assert.NotNull(uf);
            Assert.Equal(countBeforeAdd + 1, db.UserForums.Count());
        }
        public IList <T> GetAvailableForumSince <T>(
            string brand,
            DateTime createdSince,
            Func <ForumNewsGroup, T> converter,
            Action <int, int> progress)
            where T : class
        {
            if (string.IsNullOrEmpty(brand))
            {
                throw new ArgumentNullException("brand");
            }

            return(BuildResultsInBatch(converter,
                                       delegate(int index, int batchSize)
            {
                var results = ForumsService.GetAllNewForumNewsGroupsByBrand(brand, index, batchSize, createdSince);
                var res = new List <ForumNewsGroup>(results.Results);
                if (progress != null)
                {
                    progress((batchSize * index) + res.Count, results.TotalRecords);
                }
                return res;
            }));
        }
        public async Task GetForumStatsAsync_ShouldReturnCorrectResult()
        {
            // Arrange
            var serviceFactory     = new ServiceFactory();
            var userRepository     = new EfDeletableEntityRepository <ApplicationUser>(serviceFactory.Context);
            var categoryRepository = new EfDeletableEntityRepository <Category>(serviceFactory.Context);
            var topicRepository    = new EfDeletableEntityRepository <Topic>(serviceFactory.Context);

            var forumsService = new ForumsService(
                topicRepository,
                userRepository,
                categoryRepository,
                serviceFactory.UserManager);

            await serviceFactory.SeedRoleAsync(GlobalConstants.AdministratorRoleName);

            await serviceFactory.SeedRoleAsync(GlobalConstants.ModeratorRoleName);

            await topicRepository.AddAsync(new Topic()
            {
                Title = "TestTitle",
            });

            await topicRepository.AddAsync(new Topic()
            {
                Title = "TestTitle",
            });

            await topicRepository.SaveChangesAsync();

            var firstUser = new ApplicationUser()
            {
                UserName = "******",
            };

            var secondUser = new ApplicationUser()
            {
                UserName = "******",
            };

            await userRepository.AddAsync(firstUser);

            await userRepository.AddAsync(secondUser);

            await userRepository.SaveChangesAsync();

            await categoryRepository.AddAsync(new Category()
            {
                Name = "TestName",
            });

            await categoryRepository.SaveChangesAsync();

            await serviceFactory.UserManager.AddToRoleAsync(firstUser, GlobalConstants.AdministratorRoleName);

            await serviceFactory.UserManager.AddToRoleAsync(secondUser, GlobalConstants.ModeratorRoleName);

            // Act
            var expectedTopicsCount     = 2;
            var expectedCategoriesCount = 1;
            var expectedUsersCount      = 2;
            var expectedAdminsCount     = 1;
            var expectedModeratorsCount = 1;
            var actualResult            = await forumsService.GetForumStatsAsync();

            // Assert
            Assert.Equal(expectedTopicsCount, actualResult.TotalTopicsCount);
            Assert.Equal(expectedCategoriesCount, actualResult.TotalCategoriesCount);
            Assert.Equal(expectedUsersCount, actualResult.TotalUsersCount);
            Assert.Equal(expectedAdminsCount, actualResult.TotalAdminsCount);
            Assert.Equal(expectedModeratorsCount, actualResult.TotalModeratorsCount);
        }
Exemple #28
0
 public ForumsController(ForumsService service)
 {
     _forumsService = service;
 }