Skip to content

Commit 6951263

Browse files
Add tests for Messages.
Update BaseUpdatePage to have a getter for the title and body.
1 parent fafd0f7 commit 6951263

2 files changed

Lines changed: 178 additions & 5 deletions

File tree

src/org/labkey/test/pages/announcements/BaseUpdatePage.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,22 @@ public PAGE setTitle(String title)
4242
return getThis();
4343
}
4444

45+
public String getTitle()
46+
{
47+
return elementCache().titleInput.get();
48+
}
49+
4550
public PAGE setBody(String body)
4651
{
4752
elementCache().bodyInput.set(body);
4853
return getThis();
4954
}
5055

56+
public String getBody()
57+
{
58+
return elementCache().bodyInput.get();
59+
}
60+
5161
public PAGE setRenderAs(WikiRendererType renderAs)
5262
{
5363
elementCache().rendererSelect.set(renderAs);

src/org/labkey/test/tests/core/admin/AllowedFileExtensionAdminTest.java

Lines changed: 168 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
import org.labkey.test.pages.core.admin.AllowedFileExtensionAdminPage;
1717
import org.labkey.test.params.FieldDefinition;
1818
import org.labkey.test.params.experiment.SampleTypeDefinition;
19-
import org.labkey.test.params.list.IntListDefinition;
19+
import org.labkey.test.util.DataRegionTable;
2020
import org.labkey.test.util.Ext4Helper;
2121
import org.labkey.test.util.PortalHelper;
2222
import org.labkey.test.util.SampleTypeHelper;
23-
import org.labkey.test.util.TestDataGenerator;
2423
import org.labkey.test.util.exp.SampleTypeAPIHelper;
24+
import org.labkey.test.pages.announcements.InsertPage;
25+
import org.openqa.selenium.WebElement;
26+
import org.openqa.selenium.support.ui.ExpectedConditions;
2527

2628
import java.io.File;
2729
import java.io.IOException;
@@ -32,7 +34,7 @@
3234
import java.util.List;
3335
import java.util.Map;
3436

35-
import static org.labkey.test.util.exp.SampleTypeAPIHelper.SAMPLE_TYPE_DOMAIN_KIND;
37+
import static org.labkey.test.util.DataRegionTable.DataRegion;
3638

3739
@Category({Git.class})
3840
public class AllowedFileExtensionAdminTest extends BaseWebDriverTest
@@ -301,8 +303,22 @@ private void uploadToFileWebPartExcluded(List<String> excludedTypes)
301303

302304
}
303305

306+
/**
307+
* <p>
308+
* Using a list validate that attachments work correctly with the allowed files list.
309+
* </p>
310+
* <p>
311+
* After creating a list that has an auto-index and a attachment field as the only field this test will:
312+
* <ul>
313+
* <li>Can insert an element into the list with the allowed file type.</li>
314+
* <li>Validate that a file of the type not allowed is not allowed.</li>
315+
* <li>Remove the bad file and resubmit with a valid file type.</li>
316+
* </ul>
317+
* </p>
318+
*
319+
*/
304320
@Test
305-
public void testAllowedFileExtensionsInLists() throws IOException, CommandException
321+
public void testAllowedFileExtensionsInLists()
306322
{
307323

308324
List<String> allowedTypes = new ArrayList<>();
@@ -335,7 +351,7 @@ public void testAllowedFileExtensionsInLists() throws IOException, CommandExcept
335351
{
336352
_listHelper.insertNewRow(Map.of(attachmentField, fileMap.get(allowedType).getAbsolutePath()), false);
337353
// Add a space before the name to allow for the icon.
338-
expectedData [index++][0] = String.format(" %s", fileMap.get(allowedType).getName());
354+
expectedData[index++][0] = String.format(" %s", fileMap.get(allowedType).getName());
339355
}
340356

341357
goToManageLists();
@@ -348,6 +364,31 @@ public void testAllowedFileExtensionsInLists() throws IOException, CommandExcept
348364
// Not sure why we record two exceptions.
349365
checkExpectedErrors(2);
350366

367+
log("Click 'Back' button and select a file type that is allowed.");
368+
waitForElement(Locator.button("Back"));
369+
clickButton("Back");
370+
371+
waitForElement(Locator.name("quf_" + attachmentField));
372+
373+
// Same as Issue 53026, the fields are cleared after hitting back button. Covewrage for that issue is in
374+
// testAllowedFileExtensionsInSampleType test.
375+
log("Clear the file field.");
376+
FileInput el = FileInput.FileInput(Locator.name("quf_" + attachmentField), getDriver()).findWhenNeeded();
377+
executeScript("arguments[0].value = '';", el.getComponentElement());
378+
379+
File fileAgain = fileMap.get(".txt");
380+
log(String.format("Add the '%s' file to the list again (it is an allowed file).", fileAgain.getName()));
381+
el.set(fileAgain.getAbsolutePath());
382+
383+
clickButton("Submit");
384+
385+
DataRegionTable dataRegion = DataRegion(getDriver()).withName("query").find();
386+
List<String> actualData = dataRegion.getColumnDataAsText(attachmentField);
387+
388+
checker().withScreenshot()
389+
.verifyEquals(String.format("The file '%s' should be in the list twice.", fileAgain.getName()),
390+
2, Collections.frequency(actualData, String.format(" %s", fileAgain.getName())));
391+
351392
}
352393

353394
/**
@@ -471,6 +512,128 @@ public void testAllowedFileExtensionsInSampleType()
471512

472513
}
473514

515+
/**
516+
* <p>
517+
* Validate that message attachments work correctly with the allowed files list.
518+
* </p>
519+
* <p>
520+
* This test will:
521+
* <ul>
522+
* <li>Create a message with several allowed files as attachments.</li>
523+
* <li>Verify a message created with a disallowed file type is not allowed.</li>
524+
* <li>Remove the disallowed file from the list of attachments and resubmit.</li>
525+
* </ul>
526+
* </p>
527+
*/
528+
@Test
529+
public void testAllowedFilesInMessages()
530+
{
531+
532+
List<String> allowedTypes = new ArrayList<>();
533+
String excludedType = ".tsv";
534+
535+
for (String extension : fileMap.keySet())
536+
{
537+
if (!extension.equals(excludedType))
538+
allowedTypes.add(extension);
539+
}
540+
541+
log(String.format("Add the following as allowed extensions: %s", allowedTypes));
542+
setAllowedExtensions(allowedTypes, allowedTypes);
543+
544+
goToProjectHome();
545+
546+
String allowedTitle = "Allowed Files Attachment";
547+
548+
InsertPage page = InsertPage.beginAt(this)
549+
.setTitle(allowedTitle)
550+
.setBody("These attachments should be allowed.");
551+
552+
for (String allowedType : allowedTypes)
553+
{
554+
page.addAttachments(fileMap.get(allowedType));
555+
}
556+
557+
log(String.format("Create a message with title of '%s' and several allowed files as attachments.", allowedTitle));
558+
559+
page.submit();
560+
561+
String notAllowedTitle = "Not Allowed Files Attachment";
562+
String notAllowedBody = "At least one of these attachments should not be allowed.";
563+
564+
page = InsertPage.beginAt(this)
565+
.setTitle(notAllowedTitle)
566+
.setBody(notAllowedBody);
567+
568+
for (Map.Entry<String, File> entry : fileMap.entrySet())
569+
{
570+
page.addAttachments(entry.getValue());
571+
}
572+
573+
List<String> notAllowedAttachments = Locator.tagWithAttributeContaining("span", "id","filename")
574+
.findElements(getDriver())
575+
.stream().map(WebElement::getText).toList();
576+
577+
log(String.format("Try to create a message with title of '%s' with several allowed files as attachments, and one disallowed file type.", allowedTitle));
578+
579+
page.submit();
580+
581+
// Not sure why we record two exceptions.
582+
checkExpectedErrors(2);
583+
584+
// Issue 53026 Fields cleared after clicking 'Back' button.
585+
log("Click 'Back' button and validate that the fields still have all the expected values.");
586+
waitForElement(Locator.button("Back"));
587+
clickButton("Back");
588+
589+
waitForElement(Locator.tagWithId("textarea", "body"));
590+
591+
page = new InsertPage(getDriver());
592+
593+
checker().verifyEquals("Message title was not persisted.",
594+
notAllowedTitle, page.getTitle());
595+
596+
checker().verifyEquals("Message body was not persisted.",
597+
notAllowedBody, page.getBody());
598+
599+
List<WebElement> attachmentElements = Locator.tagWithAttributeContaining("span", "id","filename")
600+
.findElements(getDriver());
601+
602+
checker().verifyEqualsSorted("None of the previous attachments are still present.",
603+
notAllowedAttachments, attachmentElements
604+
.stream().map(WebElement::getText).toList());
605+
606+
checker().screenShotIfNewError("Field_Values_After_Disallowed_Error");
607+
608+
log("Remove the offending file and resubmit the message.");
609+
610+
// This did not work for me. The index is correct but fails saying could not find link with text "remove".
611+
// page.removeAttachment(attachmentElements
612+
// .stream().map(WebElement::getText).toList()
613+
// .indexOf(fileMap.get(excludedType).getName()));
614+
615+
List<WebElement> removeLinks = Locator.linkContainingText("remove").findElements(getDriver());
616+
617+
WebElement removeLink = removeLinks.get(attachmentElements
618+
.stream().map(WebElement::getText).toList()
619+
.indexOf(fileMap.get(excludedType).getName()));
620+
621+
removeLink.click();
622+
623+
shortWait().until(ExpectedConditions.stalenessOf(removeLink));
624+
625+
page.submit();
626+
627+
goToProjectHome();
628+
629+
DataRegionTable dataRegion = DataRegion(getDriver()).withName("Announcements").find();
630+
631+
checker().withScreenshot()
632+
.verifyEqualsSorted("Not all messages are there.",
633+
List.of(allowedTitle, notAllowedTitle), dataRegion.getColumnDataAsText("Title"));
634+
635+
}
636+
474637
private AllowedFileExtensionAdminPage setAllowedExtensions(List<String> allowedTypes, List<String> expectedTypes)
475638
{
476639
AllowedFileExtensionAdminPage allowedFileExtensionAdminPage = goToAdminConsole().clickAllowedFileExtensions();

0 commit comments

Comments
 (0)