Skip to content

Commit

Permalink
test(TypeScript): verify endpoint calls work after new session load (…
Browse files Browse the repository at this point in the history
…logout)

Connected to vaadin/flow#9164
  • Loading branch information
platosha committed Nov 5, 2020
1 parent e1b2967 commit e8415ff
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

import java.util.regex.Pattern;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;

import com.vaadin.flow.testutil.ChromeBrowserTest;
Expand Down Expand Up @@ -60,4 +62,29 @@ public void should_requestAnonymously_connect_service() {
"\\[LOG] AppEndpoint/helloAnonymous took \\d+ ms")),
25);
}

@Test
public void should_requestAnonymously_after_logout() throws Exception {
String originalCsrfToken = executeScript(
"return self.Vaadin.TypeScript.csrfToken").toString();

openTestUrl("/logout");
openTestUrl("/");

String csrfToken = executeScript(
"return self.Vaadin.TypeScript.csrfToken").toString();
Assert.assertNotEquals("CSRF token should change for the new session",
originalCsrfToken, csrfToken);

mainView = $("main-view").waitForFirst();

TestBenchElement button = mainView.$(TestBenchElement.class)
.id("helloAnonymous");
button.click();

// Wait for the server connect response
waitUntil(ExpectedConditions.textToBePresentInElement(
mainView.$(TestBenchElement.class).id("content"),
"Hello, stranger!"), 25);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,12 @@ private void openTestUrl(String url) {
@Before
public void setup() throws Exception {
super.setup();
openTestUrl("/");
testComponent = $("test-component").first();
content = testComponent.$(TestBenchElement.class).id("content");
load();
}

@After
public void tearDown() {
openTestUrl("/logout");
logout();
}

/**
Expand Down Expand Up @@ -193,6 +191,32 @@ public void should_updateTitleInDOMWithInjectedService(){
driver.findElement(By.tagName("title")).getAttribute("textContent"));
}

@Test
public void should_requestAnonymously_after_logout() throws Exception {
String originalCsrfToken = executeScript(
"return self.Vaadin.TypeScript.csrfToken").toString();
logout();
load();

String csrfToken = executeScript(
"return self.Vaadin.TypeScript.csrfToken").toString();
Assert.assertNotEquals("CSRF token should change for the new session",
originalCsrfToken, csrfToken);

WebElement button = testComponent.$(TestBenchElement.class).id(
"helloAnonymous");
button.click();

// Wait for the server connect response
verifyContent("Hello, stranger!");
}

private void load() {
openTestUrl("/");
testComponent = $("test-component").waitForFirst();
content = testComponent.$(TestBenchElement.class).id("content");
}

private void login(String user) {
// Use form in the test component
testComponent.$(TestBenchElement.class).id("username").sendKeys(user);
Expand All @@ -202,6 +226,10 @@ private void login(String user) {
content = testComponent.$(TestBenchElement.class).id("content");
}

private void logout() {
openTestUrl("/logout");
}

private void verifyCallingAdminService(String expectedMessage) {
testComponent.$(TestBenchElement.class).id("helloAdmin").click();
verifyContent(expectedMessage);
Expand Down

0 comments on commit e8415ff

Please sign in to comment.