-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Different approach to fix secured issue (#734)
* Created BakeryUI with listener * Added test for access denied view
- Loading branch information
Tulio Garcia
authored
Feb 12, 2019
1 parent
8f8c9d7
commit a2b0739
Showing
7 changed files
with
89 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.vaadin.starter.bakery.ui; | ||
|
||
import com.vaadin.flow.component.UI; | ||
import com.vaadin.flow.server.VaadinRequest; | ||
import com.vaadin.starter.bakery.app.security.SecurityUtils; | ||
import com.vaadin.starter.bakery.ui.components.OfflineBanner; | ||
import com.vaadin.starter.bakery.ui.exceptions.AccessDeniedException; | ||
import com.vaadin.starter.bakery.ui.views.login.LoginView; | ||
|
||
public class BakeryUI extends UI { | ||
|
||
protected void init(VaadinRequest request) { | ||
add(new OfflineBanner()); | ||
addBeforeEnterListener(event -> { | ||
final boolean accessGranted = | ||
SecurityUtils.isAccessGranted(event.getNavigationTarget()); | ||
if (!accessGranted) { | ||
if (SecurityUtils.isUserLoggedIn()) { | ||
event.rerouteToError(AccessDeniedException.class); | ||
} | ||
else { | ||
event.rerouteTo(LoginView.class); | ||
} | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/main/resources/static/frontend/src/views/errors/access-denied-view.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,18 +8,21 @@ | |
import org.junit.Test; | ||
import org.openqa.selenium.Keys; | ||
|
||
import com.vaadin.flow.component.tabs.testbench.TabElement; | ||
import com.vaadin.flow.component.textfield.testbench.PasswordFieldElement; | ||
import com.vaadin.flow.component.textfield.testbench.TextFieldElement; | ||
import com.vaadin.starter.bakery.testbench.elements.ui.StorefrontViewElement; | ||
import com.vaadin.starter.bakery.testbench.elements.ui.UsersViewElement; | ||
import com.vaadin.testbench.TestBenchElement; | ||
|
||
public class UsersViewIT extends AbstractIT<UsersViewElement> { | ||
|
||
private static Random r = new Random(); | ||
|
||
@Override | ||
protected UsersViewElement openView() { | ||
StorefrontViewElement storefront = openLoginView().login("[email protected]", "admin"); | ||
StorefrontViewElement storefront = | ||
openLoginView().login("[email protected]", "admin"); | ||
return storefront.getMenu().navigateToUsers(); | ||
} | ||
|
||
|
@@ -31,7 +34,8 @@ public void updatePassword() { | |
|
||
String uniqueEmail = "e" + r.nextInt() + "@vaadin.com"; | ||
|
||
createUser(usersView, uniqueEmail, "Paul", "Irwin", "Vaadin10", "baker"); | ||
createUser( | ||
usersView, uniqueEmail, "Paul", "Irwin", "Vaadin10", "baker"); | ||
|
||
int rowNum = usersView.getGrid().getCell(uniqueEmail).getRow(); | ||
usersView.openRowForEditing(rowNum); | ||
|
@@ -83,8 +87,9 @@ public void updatePassword() { | |
Assert.assertEquals("", password.getAttribute("value")); | ||
} | ||
|
||
private void createUser(UsersViewElement usersView, String email, String firstName, String lastName, | ||
String password, String role) { | ||
private void createUser( | ||
UsersViewElement usersView, String email, String firstName, | ||
String lastName, String password, String role) { | ||
usersView.getSearchBar().getCreateNewButton().click(); | ||
Assert.assertTrue(usersView.isEditorOpen()); | ||
|
||
|
@@ -115,7 +120,8 @@ public void tryToUpdateLockedEntity() { | |
page.getEmailField().setValue("[email protected]"); | ||
page.getEditorSaveButton().click(); | ||
|
||
Assert.assertEquals(rowNum, page.getGrid().getCell("[email protected]").getRow()); | ||
Assert.assertEquals( | ||
rowNum, page.getGrid().getCell("[email protected]").getRow()); | ||
} | ||
|
||
@Test | ||
|
@@ -130,9 +136,10 @@ public void tryToDeleteLockedEntity() { | |
page.getEditorDeleteButton().click(); | ||
page.getDeleteConfirmDialog().getConfirmButton().click(); | ||
|
||
Assert.assertEquals(rowNum, page.getGrid().getCell("[email protected]").getRow()); | ||
Assert.assertEquals( | ||
rowNum, page.getGrid().getCell("[email protected]").getRow()); | ||
} | ||
|
||
@Test | ||
public void testCancelConfirmationMessage() { | ||
UsersViewElement page = openView(); | ||
|
@@ -141,6 +148,23 @@ public void testCancelConfirmationMessage() { | |
page.getFirstName().sendKeys("Some name"); | ||
page.getEditorCancelButton().click(); | ||
|
||
Assert.assertThat(page.getDiscardConfirmDialog().getMessageText(), containsString("Discard changes")); | ||
Assert.assertThat( | ||
page.getDiscardConfirmDialog().getMessageText(), | ||
containsString("Discard changes")); | ||
} | ||
|
||
@Test | ||
public void accessDenied() { | ||
StorefrontViewElement storefront = | ||
openLoginView().login("[email protected]", "barista"); | ||
Assert.assertEquals( | ||
3, storefront.getMenu().$(TabElement.class).all().size()); | ||
|
||
driver.get(APP_URL + "users"); | ||
TestBenchElement accessDeniedPage = | ||
$("access-denied-view").waitForFirst(); | ||
|
||
Assert.assertEquals("Access denied", | ||
accessDeniedPage.$("h3").first().getText()); | ||
} | ||
} |