Esempio n. 1
0
        public void CallFindByUserNameAndAddOnce_WhenCalledWithExistingUserName()
        {
            // Arrange
            var postTitle       = "Post title";
            var postDescription = "Post description";
            var postContent     = "Post content.";
            var postAuthor      = "Post Author";
            var userServiceMock = new Mock <IUserService>();

            userServiceMock.Setup(m => m.FindByUserName(It.IsAny <string>()))
            .Returns(new User()
            {
                UserName = postAuthor
            });
            var setWrapperMock = new Mock <IEfDbSetWrapper <Post> >();

            setWrapperMock.Setup(m => m.Add(It.IsAny <Post>()));
            PostService postService = new PostService(setWrapperMock.Object, userServiceMock.Object);

            // Act
            var result = postService.AddNewPost(postTitle, postDescription, postContent, postAuthor);

            // Assert
            userServiceMock.Verify(m => m.FindByUserName(It.IsAny <string>()), Times.Once);
            setWrapperMock.Verify(m => m.Add(It.IsAny <Post>()), Times.Once);
            Assert.AreEqual(postTitle, result.Title);
            Assert.AreEqual(postDescription, result.Description);
            Assert.AreEqual(postContent, result.Content);
            Assert.AreEqual(postAuthor, result.Author.UserName);
        }
Esempio n. 2
0
        public void BeforePostMadeCancel()
        {
            var postRepository              = Substitute.For <IPostRepository>();
            var topicRepository             = Substitute.For <ITopicRepository>();
            var roleService                 = Substitute.For <IRoleService>();
            var membershipUserPointsService = Substitute.For <IMembershipUserPointsService>();
            var settingsService             = Substitute.For <ISettingsService>();

            var localisationService = Substitute.For <ILocalizationService>();
            var postService         = new PostService(membershipUserPointsService, settingsService, roleService, postRepository, topicRepository, localisationService, _api);

            var category = new Category();
            var role     = new MembershipRole {
                RoleName = "TestRole"
            };

            var categoryPermissionForRoleSet = new List <CategoryPermissionForRole>
            {
                new CategoryPermissionForRole {
                    Permission = new Permission {
                        Name = AppConstants.PermissionEditPosts
                    }, IsTicked = true
                },
                new CategoryPermissionForRole {
                    Permission = new Permission {
                        Name = AppConstants.PermissionDenyAccess
                    }, IsTicked = false
                },
                new CategoryPermissionForRole {
                    Permission = new Permission {
                        Name = AppConstants.PermissionReadOnly
                    }, IsTicked = false
                }
            };

            var permissionSet = new PermissionSet(categoryPermissionForRoleSet);

            roleService.GetPermissions(category, role).Returns(permissionSet);

            var topic = new Topic {
                Name = "Captain America", Category = category
            };
            var user = new MembershipUser
            {
                UserName = "******",
                Roles    = new List <MembershipRole>()
                {
                    role
                }
            };

            EventManager.Instance.BeforePostMade += eventsService_BeforePostMadeCancel;
            postService.AddNewPost("A test post", topic, user, out permissionSet);

            membershipUserPointsService.DidNotReceive().Add(Arg.Is <MembershipUserPoints>(x => x.User == user));
            EventManager.Instance.BeforePostMade -= eventsService_BeforePostMadeCancel;
        }
Esempio n. 3
0
        public void AddPost()
        {
            var postRepository              = Substitute.For <IPostRepository>();
            var topicRepository             = Substitute.For <ITopicRepository>();
            var roleService                 = Substitute.For <IRoleService>();
            var membershipUserPointsService = Substitute.For <IMembershipUserPointsService>();
            var settingsService             = Substitute.For <ISettingsService>();

            settingsService.GetSettings().Returns(new Settings {
                PointsAddedPerPost = 20
            });
            var localisationService = Substitute.For <ILocalizationService>();
            var postService         = new PostService(membershipUserPointsService, settingsService, roleService, postRepository, topicRepository, localisationService, _api);

            var category = new Category();
            var role     = new MembershipRole {
                RoleName = "TestRole"
            };

            var categoryPermissionForRoleSet = new List <CategoryPermissionForRole>
            {
                new CategoryPermissionForRole {
                    Permission = new Permission {
                        Name = AppConstants.PermissionEditPosts
                    }, IsTicked = true
                },
                new CategoryPermissionForRole {
                    Permission = new Permission {
                        Name = AppConstants.PermissionDenyAccess
                    }, IsTicked = false
                },
                new CategoryPermissionForRole {
                    Permission = new Permission {
                        Name = AppConstants.PermissionReadOnly
                    }, IsTicked = false
                }
            };

            var permissionSet = new PermissionSet(categoryPermissionForRoleSet);

            roleService.GetPermissions(category, role).Returns(permissionSet);

            var topic = new Topic {
                Name = "Captain America", Category = category
            };
            var user = new MembershipUser {
                UserName = "******",
                Roles    = new List <MembershipRole> {
                    role
                }
            };

            var newPost = postService.AddNewPost("A test post", topic, user, out permissionSet);

            Assert.AreEqual(newPost.User, user);
            Assert.AreEqual(newPost.Topic, topic);
        }
        public async Task <IActionResult> OnPostAdd([FromServices] UserManager <User> userManager)
        {
            User = await userManager.GetUserAsync(HttpContext.User);

            Roles = await userManager.GetRolesAsync(User);

            var PostContent = Request.Form["NewPostContent"];

            if (PostContent != "")
            {
                Post createPost = new Post()
                {
                    Content = PostContent,
                    UserId  = User.Id,
                };
                _postService.AddNewPost(createPost);
                return(RedirectToPage("./Index"));
            }
            return(RedirectToPage("./Index"));
        }
Esempio n. 5
0
        public void ShouldThrow_WhenUserServiceFindByUserNameReturnsNull()
        {
            // Arrange
            var  postTitle       = "Post title";
            var  postDescription = "Post description";
            var  postContent     = "Post content.";
            var  postAuthor      = "Post Author";
            var  userServiceMock = new Mock <IUserService>();
            User user            = null;

            userServiceMock.Setup(m => m.FindByUserName(It.IsAny <string>()))
            .Returns(user);
            var setWrapperMock = new Mock <IEfDbSetWrapper <Post> >();

            setWrapperMock.Setup(m => m.Add(It.IsAny <Post>()));
            PostService postService = new PostService(setWrapperMock.Object, userServiceMock.Object);

            // Act & Assert
            Assert.Throws <ArgumentNullException>(() => postService
                                                  .AddNewPost(postTitle, postDescription, postContent, postAuthor));
        }
        public PartialViewResult CreatePost(CreateAjaxPostViewModel post)
        {
            // Make sure correct culture on Ajax Call

            PermissionSet permissions;
            Post          newPost;
            Topic         topic;
            string        postContent;

            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                // Quick check to see if user is locked out, when logged in
                if (CurrentMember.IsLockedOut | !CurrentMember.IsApproved)
                {
                    MemberService.LogOff();
                    throw new Exception(Lang("Errors.NoAccess"));
                }

                // Check for banned links
                if (BannedLinkService.ContainsBannedLink(post.PostContent))
                {
                    throw new Exception(Lang("Errors.BannedLink"));
                }

                topic = TopicService.Get(post.Topic);

                postContent = BannedWordService.SanitiseBannedWords(post.PostContent);

                var akismetHelper = new AkismetHelper();

                // Create the new post
                newPost = PostService.AddNewPost(postContent, topic, CurrentMember, PermissionService, MemberService, CategoryPermissionService, MemberPointsService, out permissions);

                if (!akismetHelper.IsSpam(newPost))
                {
                    try
                    {
                        unitOfWork.Commit();
                    }
                    catch (Exception ex)
                    {
                        unitOfWork.Rollback();
                        LogError(ex);
                        throw new Exception(Lang("Errors.GenericMessage"));
                    }
                }
                else
                {
                    unitOfWork.Rollback();
                    throw new Exception(Lang("Errors.PossibleSpam"));
                }
            }

            //Check for moderation
            if (newPost.Pending)
            {
                return(PartialView(PathHelper.GetThemePartialViewPath("PostModeration")));
            }

            // All good send the notifications and send the post back
            using (UnitOfWorkManager.NewUnitOfWork())
            {
                // Create the view model
                var viewModel = PostMapper.MapPostViewModel(permissions, newPost, CurrentMember, Settings, topic, new List <Vote>(), new List <Favourite>());

                // Success send any notifications
                NotifyNewTopics(topic, postContent);

                return(PartialView(PathHelper.GetThemePartialViewPath("Post"), viewModel));
            }
        }