PageRenderTime 21ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/integration-tests/src/test/java/it/com/atlassian/confluence/plugins/macros/html/RssMacroWhitelistTestCase.java

https://bitbucket.org/atlassian/confluence-html-macros
Java | 199 lines | 164 code | 34 blank | 1 comment | 2 complexity | 2b3d3612f1fa1361155e7a417b580114 MD5 | raw file
  1. package it.com.atlassian.confluence.plugins.macros.html;
  2. import com.atlassian.confluence.api.model.content.Content;
  3. import com.atlassian.confluence.api.model.content.ContentType;
  4. import com.atlassian.confluence.rest.api.model.ExpansionsParser;
  5. import com.atlassian.confluence.test.rest.api.ConfluenceRestClient;
  6. import com.atlassian.confluence.test.rest.api.plugin.PluginRest;
  7. import com.atlassian.confluence.test.stateless.ConfluenceStatelessTestRunner;
  8. import com.atlassian.confluence.test.stateless.fixtures.Fixture;
  9. import com.atlassian.confluence.test.stateless.fixtures.SpaceFixture;
  10. import com.atlassian.confluence.test.stateless.fixtures.UserFixture;
  11. import com.atlassian.confluence.webdriver.pageobjects.ConfluenceTestedProduct;
  12. import com.atlassian.pageobjects.elements.PageElement;
  13. import com.atlassian.plugins.whitelist.testing.WhitelistTestRule;
  14. import org.junit.After;
  15. import org.junit.AfterClass;
  16. import org.junit.Before;
  17. import org.junit.BeforeClass;
  18. import org.junit.Test;
  19. import org.junit.runner.RunWith;
  20. import org.openqa.selenium.By;
  21. import javax.inject.Inject;
  22. import java.io.IOException;
  23. import static com.atlassian.confluence.api.model.content.ContentRepresentation.WIKI;
  24. import static com.atlassian.confluence.test.rpc.api.permissions.SpacePermission.ANONYMOUS_PERMISSIONS;
  25. import static com.atlassian.confluence.test.rpc.api.permissions.SpacePermission.REGULAR_PERMISSIONS;
  26. import static com.atlassian.confluence.test.stateless.fixtures.SpaceFixture.spaceFixture;
  27. import static com.atlassian.confluence.test.stateless.fixtures.UserFixture.userFixture;
  28. import static com.atlassian.pageobjects.elements.query.Poller.waitUntil;
  29. import static com.atlassian.pageobjects.elements.query.Poller.waitUntilFalse;
  30. import static com.atlassian.pageobjects.elements.query.Poller.waitUntilTrue;
  31. import static org.hamcrest.CoreMatchers.equalTo;
  32. import static org.mockserver.model.MediaType.APPLICATION_XML;
  33. @RunWith(ConfluenceStatelessTestRunner.class)
  34. public class RssMacroWhitelistTestCase extends AbstractHtmlMacroTestCase {
  35. private static final String MACRO_NAME = "rss";
  36. private static final String REMOTE_RESOURCE = "/download/attachments/950274/rssfeed.xml";
  37. @Fixture
  38. private static final UserFixture user = userFixture().build();
  39. @Fixture
  40. private static final SpaceFixture space = spaceFixture()
  41. .permission(user, REGULAR_PERMISSIONS)
  42. .anonymousPermission(ANONYMOUS_PERMISSIONS)
  43. .build();
  44. @Inject
  45. private static ConfluenceTestedProduct product;
  46. @Inject
  47. private static ConfluenceRestClient restClient;
  48. private static Content page;
  49. private WhitelistTestRule whitelistTestRule;
  50. @BeforeClass
  51. public static void initialise() {
  52. PluginRest pluginRest = restClient.getAdminSession().getPluginComponent();
  53. if (!pluginRest.isPluginEnabled(HTML_MACRO_PLUGIN)) {
  54. pluginRest.enablePlugin(HTML_MACRO_PLUGIN);
  55. }
  56. pluginRest.enablePluginModule(HTML_MACRO_PLUGIN, MACRO_NAME);
  57. // create page fixture here as they require the plugin modules to be enabled
  58. page = restClient.createSession(user.get()).contentService().create(
  59. Content.builder()
  60. .space(space.get())
  61. .title("HtmlIncludeFuncTest")
  62. .type(ContentType.PAGE)
  63. .body("{" + MACRO_NAME + ":url=" + remoteBaseUrl + REMOTE_RESOURCE + "}", WIKI)
  64. .build(),
  65. ExpansionsParser.parse("body.wiki,space")
  66. );
  67. restClient.getAdminSession().permissions().enableAnonymousUseConfluence();
  68. }
  69. @AfterClass
  70. public static void afterClass() {
  71. PluginRest pluginRest = restClient.getAdminSession().getPluginComponent();
  72. if (pluginRest.isPluginEnabled(HTML_MACRO_PLUGIN)) {
  73. pluginRest.disablePluginModule(HTML_MACRO_PLUGIN, MACRO_NAME);
  74. pluginRest.disablePlugin(HTML_MACRO_PLUGIN);
  75. }
  76. restClient.getAdminSession().permissions().disableAnonymousUseConfluence();
  77. }
  78. @Before
  79. public void setup() {
  80. whitelistTestRule = WhitelistTestRule.withDefaultAdminLoginAndBaseUrl(urlSelector.getBaseUrl());
  81. }
  82. @Before
  83. public void configureMockServer() throws IOException {
  84. configureMockServer(REMOTE_RESOURCE, "/com/atlassian/confluence/plugins/macros/html/rssfeed.xml", APPLICATION_XML);
  85. }
  86. @After
  87. public void tearDown() {
  88. whitelistTestRule.removeCreatedWhitelistRules();
  89. product.logOutFast();
  90. }
  91. @Test
  92. public void testWhitelistDenyWithoutAnyRule() {
  93. assertRemoteContentBlocked();
  94. }
  95. @Test
  96. public void testWhitelistAllowUsingWildcard() {
  97. assertRemoteContentBlocked();
  98. whitelistTestRule.whitelistWildcard(remoteBaseUrl + "/*");
  99. assertRemoteContentIncluded();
  100. }
  101. @Test
  102. public void testWhitelistAllowUsingExactMatch() {
  103. assertRemoteContentBlocked();
  104. whitelistTestRule.whitelistExactUrl(remoteBaseUrl + REMOTE_RESOURCE);
  105. assertRemoteContentIncluded();
  106. }
  107. @Test
  108. public void testWhitelistAllowUsingRegex() {
  109. assertRemoteContentBlocked();
  110. whitelistTestRule.whitelistRegularExpression(remoteBaseUrl + "/download/attachments/[\\d]+/rssfeed.xml");
  111. assertRemoteContentIncluded();
  112. }
  113. @Test
  114. public void testWhiteListAnonymousDeny() {
  115. assertRemoteContentBlockedForAnonymous();
  116. whitelistTestRule.whitelistExactUrlDisallowAnonymous(remoteBaseUrl + REMOTE_RESOURCE);
  117. assertRemoteContentBlockedForAnonymous();
  118. }
  119. @Test
  120. public void testWhiteListAnonymousAllow() {
  121. assertRemoteContentBlockedForAnonymous();
  122. whitelistTestRule.whitelistExactUrl(remoteBaseUrl + REMOTE_RESOURCE);
  123. assertRemoteContentIncludedForAnonymous();
  124. }
  125. private void assertRemoteContentIncluded() {
  126. product.loginAndView(user.get(), page);
  127. assertRssFeedShown();
  128. }
  129. private void assertRemoteContentBlocked() {
  130. product.loginAndView(user.get(), page);
  131. assertRssFeedNotShown();
  132. }
  133. private void assertRemoteContentIncludedForAnonymous() {
  134. product.viewPage(page);
  135. assertRssFeedShown();
  136. }
  137. private void assertRemoteContentBlockedForAnonymous() {
  138. product.viewPage(page);
  139. assertRssFeedNotShown();
  140. }
  141. private void assertRssFeedNotShown() {
  142. PageElement pageElement = pageElementFinder.find(By.xpath("//div[@class='rssMacro']//tr[1]//a[text()='\"Test RSS Exploit showing\"']"));
  143. waitUntilFalse(pageElement.timed().isPresent());
  144. pageElement = pageElementFinder.find(By.xpath("//div[@class='wiki-content']//div[contains(@class,'errorBox')]//p//strong"));
  145. waitUntil(
  146. pageElement.timed().getText(),
  147. equalTo("Could not access the content at the URL because it is not from an allowed source.")
  148. );
  149. pageElement = pageElementFinder.find(By.xpath("//div[@class='wiki-content']//p[3]"));
  150. waitUntil(
  151. pageElement.timed().getText(),
  152. equalTo("You may contact your site administrator and request that this URL be added to the list of allowed sources.")
  153. );
  154. }
  155. private void assertRssFeedShown() {
  156. PageElement pageElement = pageElementFinder.find(By.xpath("//div[contains(@class,'rssMacro')]//tr[1]//a[text()='\"Test RSS Exploit showing\"']"));
  157. waitUntilTrue(pageElement.timed().isPresent());
  158. pageElement = pageElementFinder.find(By.xpath("//div[contains(@class,'rssMacro')]//tr[1]//span"));
  159. waitUntil(pageElement.timed().getText(), equalTo("This is description & test"));
  160. pageElement = pageElementFinder.find(By.xpath("//div[contains(@class,'rssMacro')]//tbody/tr//a"));
  161. waitUntil(pageElement.timed().getText(), equalTo("Items & Test"));
  162. pageElement = pageElementFinder.find(By.xpath("//div[contains(@class,'rssMacro')]//tbody/tr//span"));
  163. waitUntil(pageElement.timed().getText(), equalTo("Item description includes html markup"));
  164. }
  165. }