/src/test/java/net/jforum/actions/ModerationActionsTestCase.java

https://github.com/almassari/jforum3 · Java · 180 lines · 140 code · 27 blank · 13 comment · 0 complexity · 7d4bf75f9e3d45c6b551ba73b11c97b1 MD5 · raw file

  1. /*
  2. * Copyright (c) JForum Team. All rights reserved.
  3. *
  4. * The software in this package is published under the terms of the LGPL
  5. * license a copy of which has been included with this distribution in the
  6. * license.txt file.
  7. *
  8. * The JForum Project
  9. * http://www.jforum.net
  10. */
  11. package net.jforum.actions;
  12. import java.util.ArrayList;
  13. import java.util.Arrays;
  14. import net.jforum.actions.helpers.Actions;
  15. import net.jforum.actions.helpers.ApproveInfo;
  16. import net.jforum.actions.helpers.Domain;
  17. import net.jforum.core.support.vraptor.ViewPropertyBag;
  18. import net.jforum.entities.Category;
  19. import net.jforum.entities.ModerationLog;
  20. import net.jforum.entities.Topic;
  21. import net.jforum.entities.User;
  22. import net.jforum.entities.UserSession;
  23. import net.jforum.repository.CategoryRepository;
  24. import net.jforum.repository.ModerationLogRepository;
  25. import net.jforum.repository.TopicRepository;
  26. import net.jforum.security.RoleManager;
  27. import net.jforum.services.ModerationService;
  28. import net.jforum.services.ViewService;
  29. import net.jforum.util.JForumConfig;
  30. import net.jforum.util.TestCaseUtils;
  31. import org.jmock.Expectations;
  32. import org.jmock.Mockery;
  33. import org.junit.Test;
  34. /**
  35. * @author Rafael Steil
  36. */
  37. public class ModerationActionsTestCase {
  38. private Mockery context = TestCaseUtils.newMockery();
  39. private JForumConfig jForumConfig = context.mock(JForumConfig.class);
  40. private ViewService viewService = context.mock(ViewService.class);
  41. private RoleManager roleManager = context.mock(RoleManager.class);
  42. private ModerationService service = context.mock(ModerationService.class);
  43. private ModerationLog moderationLog = new ModerationLog();
  44. private CategoryRepository categoryRepository = context.mock(CategoryRepository.class);
  45. private ViewPropertyBag propertyBag = context.mock(ViewPropertyBag.class);
  46. private UserSession userSession = context.mock(UserSession.class);
  47. private TopicRepository topicRepository = context.mock(TopicRepository.class);
  48. private ModerationLogRepository moderationLogRepository = context.mock(ModerationLogRepository.class);
  49. private User user = new User();
  50. private ModerationActions action = new ModerationActions(viewService, roleManager, service,
  51. categoryRepository, propertyBag, topicRepository, jForumConfig, moderationLogRepository, userSession);
  52. @Test
  53. public void moveTopics() {
  54. context.checking(new Expectations() {{
  55. one(userSession).getUser(); will(returnValue(user));
  56. one(roleManager).getCanMoveTopics(); will(returnValue(true));
  57. one(service).moveTopics(1, moderationLog, 2, 3, 4);
  58. one(viewService).redirect("return path");
  59. }});
  60. action.moveTopics(1, "return path", moderationLog, 2, 3, 4);
  61. context.assertIsSatisfied();
  62. }
  63. @Test
  64. public void moveTopicsDoesNotHaveRoleShouldIgnore() {
  65. context.checking(new Expectations() {{
  66. one(roleManager).getCanMoveTopics(); will(returnValue(false));
  67. one(viewService).redirect("return path");
  68. }});
  69. action.moveTopics(1, "return path", moderationLog, 1, 2);
  70. context.assertIsSatisfied();
  71. }
  72. @Test
  73. public void askMoveDestination() {
  74. context.checking(new Expectations() {{
  75. one(roleManager).getCanMoveTopics(); will(returnValue(true));
  76. one(categoryRepository).getAllCategories(); will(returnValue(new ArrayList<Category>()));
  77. one(propertyBag).put("topicIds", new int[] { 1, 2, 3 });
  78. one(propertyBag).put("fromForumId", 10);
  79. one(propertyBag).put("returnUrl", "return path");
  80. one(propertyBag).put("categories", new ArrayList<Category>());
  81. }});
  82. action.askMoveDestination("return path", 10, 1, 2, 3);
  83. context.assertIsSatisfied();
  84. }
  85. @Test
  86. public void askMoveDestinationDoesNotHaveRoleShouldIgnore() {
  87. context.checking(new Expectations() {{
  88. one(roleManager).getCanMoveTopics(); will(returnValue(false));
  89. one(viewService).redirect("return path");
  90. }});
  91. action.askMoveDestination("return path", 1, 2, 3);
  92. context.assertIsSatisfied();
  93. }
  94. @Test
  95. public void lockUnlock() {
  96. context.checking(new Expectations() {{
  97. one(userSession).getUser(); will(returnValue(user));
  98. one(roleManager).getCanLockUnlockTopics(); will(returnValue(true));
  99. one(service).lockUnlock(new int[]{1, 2, 3}, moderationLog);
  100. ignoring(viewService);
  101. }});
  102. action.lockUnlock(1, null, moderationLog, new int[]{1, 2, 3});
  103. context.assertIsSatisfied();
  104. }
  105. @Test
  106. public void lockUnlockDoesNotHaveRoleShouldIgnore() {
  107. context.checking(new Expectations() {{
  108. one(roleManager).getCanLockUnlockTopics(); will(returnValue(false));
  109. ignoring(viewService);
  110. }});
  111. action.lockUnlock(1, null, moderationLog, new int[]{1});
  112. context.assertIsSatisfied();
  113. }
  114. @Test
  115. public void deleteTopicsExpectSuccess() {
  116. context.checking(new Expectations() {{
  117. one(userSession).getUser(); will(returnValue(user));
  118. one(roleManager).getCanDeletePosts(); will(returnValue(true));
  119. one(topicRepository).get(4); will(returnValue(new Topic()));
  120. one(topicRepository).get(5); will(returnValue(new Topic()));
  121. one(service).deleteTopics(Arrays.asList(new Topic(), new Topic()), moderationLog);
  122. one(viewService).redirectToAction(Domain.FORUMS, Actions.SHOW, 1);
  123. }});
  124. action.deleteTopics(1, null, new int[]{4, 5}, moderationLog);
  125. context.assertIsSatisfied();
  126. }
  127. @Test
  128. public void deleteTopicsDoesNotHaveRoleShouldIgnore() {
  129. context.checking(new Expectations() {{
  130. one(roleManager).getCanDeletePosts(); will(returnValue(false));
  131. one(viewService).redirectToAction(Domain.FORUMS, Actions.SHOW, 1);
  132. }});
  133. action.deleteTopics(1, null, new int[]{4}, moderationLog);
  134. context.assertIsSatisfied();
  135. }
  136. @Test
  137. public void approveExpectSuccess() {
  138. context.checking(new Expectations() {{
  139. one(roleManager).getCanApproveMessages(); will(returnValue(true));
  140. one(service).doApproval(1, Arrays.asList(new ApproveInfo[0]));
  141. one(viewService).redirectToAction(Domain.FORUMS, Actions.SHOW, 1);
  142. }});
  143. action.approve(1, Arrays.asList(new ApproveInfo[0]));
  144. }
  145. @Test
  146. public void approveDoesNotHaveRequiredRoleShouldIgnore() {
  147. context.checking(new Expectations() {{
  148. one(roleManager).getCanApproveMessages(); will(returnValue(false));
  149. one(viewService).redirectToAction(Domain.FORUMS, Actions.SHOW, 1);
  150. }});
  151. action.approve(1, Arrays.asList(new ApproveInfo[0]));
  152. }
  153. }