001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thomas Roger
016 */
017package org.nuxeo.functionaltests.pages.tabs;
018
019import java.util.List;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.functionaltests.Locator;
024import org.nuxeo.functionaltests.Required;
025import org.nuxeo.functionaltests.forms.Select2WidgetElement;
026import org.nuxeo.functionaltests.pages.AbstractPage;
027import org.openqa.selenium.By;
028import org.openqa.selenium.NoSuchElementException;
029import org.openqa.selenium.WebDriver;
030import org.openqa.selenium.WebElement;
031import org.openqa.selenium.support.FindBy;
032import org.openqa.selenium.support.pagefactory.ByChained;
033
034/**
035 * @since 7.10
036 */
037public class PermissionsSubPage extends AbstractPage {
038
039    private static final Log log = LogFactory.getLog(PermissionsSubPage.class);
040
041    @Required
042    @FindBy(xpath = "//paper-button[text()='New Permission']")
043    WebElement newPermission;
044
045    public PermissionsSubPage(WebDriver driver) {
046        super(driver);
047    }
048
049    public boolean hasPermissionForUser(String permission, String username) {
050        List<WebElement> elements = driver.findElements(By.className("acl-table-row"));
051        boolean hasPermission = false;
052        for (WebElement element : elements) {
053            List<WebElement> divs = element.findElements(new ByChained(By.tagName("div")));
054            if (divs.size() > 3) {
055                String aceUsername = divs.get(0).getText();
056                String aceRight = divs.get(1).getText();
057                if (username.equals(aceUsername) && permission.equalsIgnoreCase(aceRight)) {
058                    hasPermission = true;
059                }
060            }
061        }
062        return hasPermission;
063    }
064
065    public PermissionsSubPage grantPermissionForUser(String permission, String username) {
066
067        newPermission.click();
068
069        WebElement addPermissionH2 = findElementWithTimeout(By.xpath("//h2[text()='Add a Permission']"));
070        WebElement popup = addPermissionH2.findElement(By.xpath(".."));
071
072        Select2WidgetElement userSelection = new Select2WidgetElement(driver,
073                popup.findElement(By.className("select2-container")), false);
074        userSelection.selectValue(username);
075
076        // select the permission
077        popup.findElement(By.tagName("iron-icon")).click();
078        Locator.waitUntilGivenFunction(input -> {
079            try {
080                WebElement el = popup.findElement(By.tagName("paper-item"));
081                return el.isDisplayed();
082            } catch (NoSuchElementException e) {
083                return false;
084            }
085        });
086        List<WebElement> elements = popup.findElements(By.tagName("paper-item"));
087        for (WebElement element : elements) {
088            if (permission.equalsIgnoreCase(element.getText())) {
089                element.click();
090                break;
091            }
092        }
093
094        // click on Create
095        popup.findElement(By.xpath(".//paper-button[text()='Create']")).click();
096        waitForPermissionAdded();
097
098        return asPage(PermissionsSubPage.class);
099    }
100
101    protected void waitForPermissionAdded() {
102        Locator.waitUntilGivenFunction(input -> {
103            try {
104                WebElement element = driver.findElement(By.xpath("//h2[text()='Add a Permission']"));
105                return !element.isDisplayed();
106            } catch (NoSuchElementException e) {
107                return true;
108            }
109        });
110    }
111}