001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Thomas Roger
018 */
019package org.nuxeo.functionaltests.pages.tabs;
020
021import java.util.List;
022
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;
032
033/**
034 * @since 7.10
035 */
036public class PermissionsSubPage extends AbstractPage {
037
038    @Required
039    @FindBy(xpath = "//div[contains(@class, 'jsLocalPermissions')]/*/paper-button")
040    WebElement newPermission;
041
042    public PermissionsSubPage(WebDriver driver) {
043        super(driver);
044    }
045
046    public boolean hasPermissionForUser(String permission, String username) {
047        List<WebElement> elements = driver.findElements(By.xpath("//div[contains(@class, 'acl-table-row effective')]"));
048        boolean hasPermission = false;
049        for (WebElement element : elements) {
050            List<WebElement> names = element.findElements(By.xpath(".//span[contains(@class, 'tag user')]"));
051            List<WebElement> perms = element.findElements(By.className("label"));
052            if (names.size() > 0 && perms.size() > 0) {
053                String title = names.get(0).getAttribute("title");
054                String perm = perms.get(0).getText();
055                if (title.startsWith(username) && permission.equalsIgnoreCase(perm)) {
056                    hasPermission = true;
057                    break;
058                }
059            }
060        }
061        return hasPermission;
062    }
063
064    public PermissionsSubPage grantPermissionForUser(String permission, String username) {
065
066        newPermission.click();
067
068        WebElement addPermissionH2 = findElementWithTimeout(By.xpath("//h2[text()='Add a Permission']"));
069        WebElement popup = addPermissionH2.findElement(By.xpath(".."));
070
071        Select2WidgetElement userSelection = new Select2WidgetElement(driver,
072                popup.findElement(By.className("select2-container")), false);
073        userSelection.selectValue(username);
074
075        // select the permission
076        popup.findElement(By.tagName("iron-icon")).click();
077        Locator.waitUntilGivenFunction(input -> {
078            try {
079                WebElement el = popup.findElement(By.tagName("paper-item"));
080                return el.isDisplayed();
081            } catch (NoSuchElementException e) {
082                return false;
083            }
084        });
085        List<WebElement> elements = popup.findElements(By.tagName("paper-item"));
086        for (WebElement element : elements) {
087            if (permission.equalsIgnoreCase(element.getText())) {
088                element.click();
089                break;
090            }
091        }
092
093        // click on Create
094        popup.findElement(By.xpath(".//paper-button[text()='Create']")).click();
095        waitForPermissionAdded(permission, username);
096
097        return asPage(PermissionsSubPage.class);
098    }
099
100    protected void waitForPermissionAdded(String permission, String username) {
101        Locator.waitUntilGivenFunction(input -> hasPermissionForUser(permission, username));
102    }
103}