PageRenderTime 24ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/jira-project/jira-functional-tests/jira-webdriver-tests/src/main/java/com/atlassian/jira/webtest/webdriver/tests/issue/TestAttachmentExpansion.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 265 lines | 230 code | 35 blank | 0 comment | 0 complexity | ce1e422aec21234f7da0d941047a7c3d MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.webtest.webdriver.tests.issue;
  2. import com.atlassian.integrationtesting.runner.restore.RestoreOnce;
  3. import com.atlassian.jira.functest.framework.backdoor.Backdoor;
  4. import com.atlassian.jira.functest.framework.navigation.issue.AttachmentsBlock.Sort;
  5. import com.atlassian.jira.functest.framework.navigation.issue.AttachmentsBlock.ViewMode;
  6. import com.atlassian.jira.functest.framework.suite.Category;
  7. import com.atlassian.jira.functest.framework.suite.WebTest;
  8. import com.atlassian.jira.functest.rule.Rules;
  9. import com.atlassian.jira.pageobjects.JiraWebDriverTest;
  10. import com.atlassian.jira.pageobjects.elements.ImageElement;
  11. import com.atlassian.jira.pageobjects.model.FileDownloadInfo;
  12. import com.atlassian.jira.pageobjects.pages.viewissue.ViewIssuePage;
  13. import com.atlassian.jira.pageobjects.pages.viewissue.attachment.ArchiveEntryItem;
  14. import com.atlassian.jira.pageobjects.pages.viewissue.attachment.ArchiveFooter;
  15. import com.atlassian.jira.pageobjects.pages.viewissue.attachment.AttachmentOptions;
  16. import com.atlassian.jira.pageobjects.pages.viewissue.attachment.AttachmentRow;
  17. import com.atlassian.jira.pageobjects.pages.viewissue.attachment.AttachmentSection;
  18. import com.atlassian.jira.pageobjects.pages.viewissue.attachment.FileLinkElement;
  19. import com.atlassian.jira.pageobjects.util.TraceContext;
  20. import com.atlassian.jira.pageobjects.util.Tracer;
  21. import com.atlassian.uri.Uri;
  22. import com.google.common.base.Optional;
  23. import org.junit.Before;
  24. import org.junit.ClassRule;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.junit.rules.TestRule;
  28. import javax.inject.Inject;
  29. import javax.ws.rs.core.MediaType;
  30. import java.util.Map;
  31. import static com.atlassian.uri.Uri.fromJavaUri;
  32. import static javax.ws.rs.core.UriBuilder.fromUri;
  33. import static org.hamcrest.Matchers.equalTo;
  34. import static org.hamcrest.Matchers.hasSize;
  35. import static org.hamcrest.Matchers.is;
  36. import static org.hamcrest.Matchers.notNullValue;
  37. import static org.junit.Assert.assertThat;
  38. @WebTest({Category.WEBDRIVER_TEST, Category.ATTACHMENTS})
  39. @RestoreOnce("TestAttachmentExpansion/TestAttachmentExpansion.xml")
  40. public class TestAttachmentExpansion extends JiraWebDriverTest {
  41. @ClassRule
  42. public static final TestRule prepareAttachments = (base, description) -> Rules.chain()
  43. .around(getBaseClassRule())
  44. .around(Rules.prepareAttachments(jira::environmentData, () -> new Backdoor(jira.environmentData()), "TestAttachmentExpansion/attachments"))
  45. .apply(base, description);
  46. private static final int ISSUE_ID = 10000;
  47. @Rule
  48. public final TestRule baseRule = getBaseRule();
  49. private Uri baseUri;
  50. private Map<String, AttachmentRow> attachmentRows;
  51. @Inject
  52. TraceContext traceContext;
  53. @Before
  54. public void visitViewIssuePage() throws Exception {
  55. baseUri = fromJavaUri(jira.environmentData().getBaseUrl().toURI());
  56. final ViewIssuePage viewIssuePage = jira.goToViewIssue("HSP-1");
  57. final AttachmentSection attachmentSection = viewIssuePage.getAttachmentSection();
  58. attachmentSection.openOptions().setViewMode(ViewMode.LIST);
  59. attachmentRows = attachmentSection.getAttachmentRowsByTitle();
  60. }
  61. @Test
  62. public void shouldExpandPetabyteBombFlatly() {
  63. final AttachmentRow row = getAttachmentRow("42-bomb.zip");
  64. assertEntryCount(row, 16);
  65. }
  66. private AttachmentRow getAttachmentRow(final String rowTitle) {
  67. final AttachmentRow row = attachmentRows.get(rowTitle);
  68. assertThat("an attachment titled " + rowTitle + " should exist", row, is(notNullValue()));
  69. return row;
  70. }
  71. private void assertEntryCount(final AttachmentRow row, final int expectedCount) {
  72. assertThat("entryCount", row.expandEntries(), hasSize(expectedCount));
  73. }
  74. @Test
  75. public void shouldEmitWarningForCorruptArchive() {
  76. final AttachmentRow row = getAttachmentRow("corrupt.zip");
  77. assertEntryCount(row, 0);
  78. assertWarning(row);
  79. }
  80. private void assertWarning(final AttachmentRow row) {
  81. assertThat("expansion warning", row.containsExpansionWarning(), is(true));
  82. }
  83. @Test
  84. public void shouldExpandArchiveWithLongPaths() {
  85. final AttachmentRow row = getAttachmentRow("deep.zip");
  86. assertEntryCount(row, 1);
  87. assertEntryTitle(row, 0, "long/path/thanks/to/.../reach/the/leaf.txt");
  88. }
  89. private void assertEntryTitle(final AttachmentRow row, final int entryIndex, final String expectedTitle) {
  90. final String actualTitle = entry(row, entryIndex).getTitle();
  91. assertThat("entry title " + entryIndex, actualTitle, is(equalTo(expectedTitle)));
  92. }
  93. private ArchiveEntryItem entry(final AttachmentRow row, final int index) {
  94. return row.expandEntries().get(index);
  95. }
  96. @Test
  97. public void shouldEmitWarningForEmptyArchive() {
  98. final AttachmentRow row = getAttachmentRow("empty.zip");
  99. assertEntryCount(row, 0);
  100. assertWarning(row);
  101. }
  102. @Test
  103. public void shouldExpandEncryptedArchive() {
  104. final AttachmentRow row = getAttachmentRow("encrypted.zip");
  105. assertEntryTitle(row, 0, "lorem-ipsum.txt");
  106. }
  107. @Test
  108. public void shouldProperlyEscapeHtml() {
  109. final AttachmentRow row = getAttachmentRow("escape & html.zip");
  110. assertEntryTitle(row, 0, "delta > 0.txt");
  111. assertEntryTitle(row, 1, "rock & roll.txt");
  112. assertEntryTitle(row, 2, "the amp here &amp; is expected.txt");
  113. }
  114. @Test
  115. public void shouldViewEntryIcons() {
  116. final AttachmentRow row = getAttachmentRow("real.zip");
  117. assertEntryIcon(row, 0, "aui-iconfont-devtools-file", "File");
  118. assertEntryIcon(row, 1, "aui-iconfont-file-txt", "Text File");
  119. assertEntryIcon(row, 2, "aui-iconfont-image", "JPEG File");
  120. }
  121. private void assertEntryIcon(
  122. final AttachmentRow row,
  123. final int entryIndex,
  124. final String expectedClassName,
  125. final String expectedAlternativeText
  126. ) {
  127. final ImageElement icon = entry(row, entryIndex).getIcon();
  128. assertThat("icon URI " + entryIndex, icon.getClassName(), is(equalTo(expectedClassName)));
  129. assertThat("alternative text " + entryIndex, icon.getAlternativeText(), is(equalTo(expectedAlternativeText)));
  130. }
  131. @Test
  132. public void shouldViewEntryFileSizes() {
  133. final AttachmentRow row = getAttachmentRow("real.zip");
  134. assertEntryFileSize(row, 0, "16 kB");
  135. assertEntryFileSize(row, 1, "2 kB");
  136. assertEntryFileSize(row, 2, "4 kB");
  137. }
  138. private void assertEntryFileSize(final AttachmentRow row, final int entryIndex, final String expectedFileSize) {
  139. assertThat("entry file size " + entryIndex, entry(row, entryIndex).getSize(), is(equalTo(expectedFileSize)));
  140. }
  141. @Test
  142. public void shouldExpandRecursiveBombFlatly() {
  143. final AttachmentRow row = getAttachmentRow("recursive-bomb.zip");
  144. assertEntryTitle(row, 0, "r/r.zip");
  145. assertEntryFileSize(row, 0, "0.4 kB");
  146. }
  147. @Test
  148. public void shouldLimitPresentedEntries() {
  149. final AttachmentRow row = getAttachmentRow("big-tree.zip");
  150. assertEntryCount(row, 30);
  151. final ArchiveFooter footer = row.getFooter();
  152. final Optional<String> limitInfo = footer.getLimitInfo();
  153. assertThat("footer limit info presence", limitInfo.isPresent(), is(true));
  154. assertThat("footer limit info label", limitInfo.get(), is(equalTo("Showing 30 of 50 items")));
  155. }
  156. @Test
  157. public void entriesShouldHaveProperIndexes() {
  158. final AttachmentRow row = getAttachmentRow("big-tree.zip");
  159. assertThat(entry(row, 1).getIndex(), is(equalTo(3)));
  160. }
  161. @Test
  162. public void shouldPreventMoreSophisticatedXss() {
  163. final AttachmentRow row = getAttachmentRow("XSS.zip");
  164. assertEntryTitle(row, 0, "\"<script>alert('XSS')</script>\".txt");
  165. assertEntryTitle(row, 1, "\"<script>alert('XSS')<:script>\".txt");
  166. assertEntryTitle(row, 2, "'<script>alert(\"XSS\")</script>'.txt");
  167. assertEntryTitle(row, 3, "'<script>alert(\"XSS\")<:script>'.txt");
  168. assertEntryTitle(row, 4, "<script>alert(\"XSS\")</script>.txt");
  169. assertEntryTitle(row, 4, "<script>alert(\"XSS\")</script>.txt");
  170. assertEntryTitle(row, 5, "<script>alert(\"XSS\")<:script>.txt");
  171. assertEntryTitle(row, 6, "<script>alert('XSS')</script>.txt");
  172. assertEntryTitle(row, 7, "<script>alert('XSS')<:script>.txt");
  173. }
  174. @Test
  175. public void entriesShouldBeDownloadableByClicking() throws Exception {
  176. final AttachmentRow row = getAttachmentRow("real.zip");
  177. final ArchiveEntryItem entry = entry(row, 0);
  178. final FileLinkElement link = entry.getFileLink();
  179. final Uri expectedFileUri = getExpectedDownloadUri(row.getAttachmentId(), entry.getIndex());
  180. assertThat(link.getFileUri(), is(equalTo(expectedFileUri)));
  181. }
  182. private Uri getExpectedDownloadUri(final long attachmentId, final int entryIndex) {
  183. return fromJavaUri(
  184. fromUri(baseUri.toJavaUri())
  185. .path("secure")
  186. .path("attachmentzip")
  187. .path("unzip")
  188. .path(String.valueOf(ISSUE_ID))
  189. .path(attachmentId + "[" + entryIndex + "]")
  190. .path("/")
  191. .build()
  192. );
  193. }
  194. @Test
  195. public void entriesShouldBeDownloadableByDragging() throws Exception {
  196. final AttachmentRow row = getAttachmentRow("real.zip");
  197. final ArchiveEntryItem entry = entry(row, 0);
  198. final FileLinkElement link = entry.getFileLink();
  199. final Uri expectedFileUri = getExpectedDownloadUri(row.getAttachmentId(), entry.getIndex());
  200. final FileDownloadInfo downloadInfo = link.getDownloadInfo();
  201. assertThat("download media type", downloadInfo.getMediaType(), is(equalTo(MediaType.valueOf("audio/mpeg"))));
  202. assertThat("download file name ", downloadInfo.getFileName(), is(equalTo("beep-01a.mp3")));
  203. assertThat("download URI", downloadInfo.getDownloadUri(), is(equalTo(expectedFileUri)));
  204. assertThat(link + " should be marked as draggable", link.isDraggable(), is(true));
  205. }
  206. @Test
  207. public void shouldWireExpansionAfterViewModeSwitch() {
  208. final AttachmentSection attachmentSection = pageBinder.bind(AttachmentSection.class);
  209. attachmentSection.openOptions().setViewMode(ViewMode.GALLERY);
  210. Tracer tracer = traceContext.checkpoint();
  211. final AttachmentOptions attachmentOptions = attachmentSection.openOptions();
  212. attachmentOptions.setViewMode(ViewMode.LIST);
  213. traceContext.waitFor(tracer, "jira.plugins.viewissue.expansion.wired");
  214. }
  215. @Test
  216. public void shouldWireExpansionAfterSortChange() {
  217. final AttachmentSection attachmentSection = pageBinder.bind(AttachmentSection.class);
  218. attachmentSection.openOptions().setViewMode(ViewMode.LIST);
  219. attachmentSection.openOptions().setSortBy(Sort.Key.NAME);
  220. attachmentSection.openOptions().setSortOrder(Sort.Direction.ASCENDING);
  221. Tracer tracer = traceContext.checkpoint();
  222. AttachmentOptions attachmentOptions = attachmentSection.openOptions();
  223. attachmentOptions.setSortBy(Sort.Key.DATE);
  224. traceContext.waitFor(tracer, "jira.plugins.viewissue.expansion.wired");
  225. tracer = traceContext.checkpoint();
  226. attachmentOptions = attachmentSection.openOptions();
  227. attachmentOptions.setSortOrder(Sort.Direction.DESCENDING);
  228. traceContext.waitFor(tracer, "jira.plugins.viewissue.expansion.wired");
  229. }
  230. }