001/*
002 * (C) Copyright 2016 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 *     Yannis JULIENNE
019 */
020package org.nuxeo.functionaltests.pages.tabs;
021
022import java.io.File;
023import java.util.List;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.functionaltests.AjaxRequestManager;
028import org.nuxeo.functionaltests.Locator;
029import org.nuxeo.functionaltests.Required;
030import org.nuxeo.functionaltests.ScreenshotTaker;
031import org.nuxeo.functionaltests.WaitForJQueryAjaxOnLoading;
032import org.nuxeo.functionaltests.forms.Select2WidgetElement;
033import org.nuxeo.functionaltests.pages.AbstractPage;
034import org.openqa.selenium.By;
035import org.openqa.selenium.NoSuchElementException;
036import org.openqa.selenium.NotFoundException;
037import org.openqa.selenium.WebDriver;
038import org.openqa.selenium.WebElement;
039import org.openqa.selenium.support.FindBy;
040
041/**
042 * @since 7.10
043 */
044@WaitForJQueryAjaxOnLoading
045public class PermissionsSubPage extends AbstractPage {
046
047    private static final Log log = LogFactory.getLog(PermissionsSubPage.class);
048
049    // moved @Required on this element to allow read only view
050    @Required
051    @FindBy(xpath = "//div[contains(@class, 'jsLocalPermissions')]")
052    WebElement localPermissions;
053
054    @FindBy(xpath = "//div[contains(@class, 'jsLocalPermissions')]/*/paper-button")
055    WebElement newPermission;
056
057    @FindBy(xpath = "//paper-button[@id='block']")
058    WebElement blockPermissions;
059
060    @FindBy(xpath = "//paper-button[@id='unblock']")
061    WebElement unblockPermissions;
062
063    public PermissionsSubPage(WebDriver driver) {
064        super(driver);
065    }
066
067    /**
068     * @deprecated as of 8.3, replaced by {@link #hasPermission(String, String)}
069     */
070    @Deprecated
071    public boolean hasPermissionForUser(String permission, String username) {
072        return hasPermission(permission, username);
073    }
074
075    /**
076     * @since 8.3
077     */
078    public boolean hasPermission(String permission, String userOrGroupName) {
079        List<WebElement> elements = driver.findElements(By.xpath("//div[contains(@class, 'acl-table-row effective')]"));
080        boolean hasPermission = false;
081        for (WebElement element : elements) {
082            List<WebElement> names = element.findElements(By.xpath(".//span[contains(@class, 'tag')]"));
083            List<WebElement> perms = element.findElements(By.className("label"));
084            if (names.size() > 0 && perms.size() > 0) {
085                String title = names.get(0).getAttribute("title");
086                String perm = perms.get(0).getText();
087                if (title.startsWith(userOrGroupName) && permission.equalsIgnoreCase(perm)) {
088                    hasPermission = true;
089                    break;
090                }
091            }
092        }
093        return hasPermission;
094    }
095
096    /**
097     * @deprecated as of 8.3, replaced by {@link #grantPermission(String, String)}
098     */
099    @Deprecated
100    public PermissionsSubPage grantPermissionForUser(String permission, String username) {
101        return grantPermission(permission, username);
102    }
103
104    /**
105     * @since 8.3
106     */
107    public PermissionsSubPage grantPermission(String permission, String userOrGroupName) {
108
109        waitUntilEnabledAndClick(newPermission);
110
111        WebElement addPermissionH2 = findElementWithTimeout(By.xpath("//h2[text()='Add a Permission']"));
112        WebElement popup = addPermissionH2.findElement(By.xpath(".."));
113
114        Select2WidgetElement userSelection = new Select2WidgetElement(driver,
115                popup.findElement(By.className("select2-container")), false);
116        userSelection.selectValue(userOrGroupName);
117
118        // select the permission
119        popup.findElement(By.tagName("iron-icon")).click();
120        Locator.waitUntilGivenFunction(input -> {
121            try {
122                WebElement el = popup.findElement(By.tagName("paper-item"));
123                return el.isDisplayed();
124            } catch (NoSuchElementException e) {
125                return false;
126            }
127        });
128        List<WebElement> elements = popup.findElements(By.tagName("paper-item"));
129        for (WebElement element : elements) {
130            if (permission.equalsIgnoreCase(element.getText())) {
131                element.click();
132                break;
133            }
134        }
135
136        // NXP-19932: take screenshot to help understanding potential randoms on click
137        ScreenshotTaker taker = new ScreenshotTaker();
138        File screenShot = taker.takeScreenshot(driver, "PermissionSubPage-add-permission-");
139        log.warn("Screenshot taken : " + screenShot.getAbsolutePath());
140        // click on Create
141        Locator.findElementWaitUntilEnabledAndClick(popup, By.xpath(".//paper-button[text()='Create']"));
142        waitForPermissionAdded(permission, userOrGroupName);
143
144        return asPage(PermissionsSubPage.class);
145    }
146
147    protected void waitForPermissionAdded(String permission, String userOrGroupName) {
148        // wait for any JQuery ajax request to complete
149        new AjaxRequestManager(driver).waitForJQueryRequests();
150        Locator.waitUntilGivenFunction(input -> hasPermission(permission, userOrGroupName));
151    }
152
153    /**
154     * @since 8.3
155     */
156    public PermissionsSubPage blockPermissions() {
157        waitUntilEnabledAndClick(blockPermissions);
158        Locator.waitUntilElementPresent(By.xpath("//paper-button[@id='unblock']"));
159        return asPage(PermissionsSubPage.class);
160    }
161
162    /**
163     * @since 8.3
164     */
165    public PermissionsSubPage unblockPermissions() {
166        waitUntilEnabledAndClick(unblockPermissions);
167        Locator.waitUntilElementPresent(By.xpath("//paper-button[@id='block']"));
168        return asPage(PermissionsSubPage.class);
169    }
170
171    /**
172     * @since 8.3
173     */
174    public PermissionsSubPage deletePermission(String permission, String userOrGroupName) {
175        WebElement deleteButton = findDeleteButton(permission, userOrGroupName);
176        if (deleteButton != null) {
177            Locator.waitUntilEnabledAndClick(deleteButton);
178            Locator.waitUntilElementPresent(
179                    By.xpath("//h2[contains(text(), 'The following permission will be deleted')]"));
180            findElementWaitUntilEnabledAndClick(By.xpath("//paper-button[text()='Delete']"));
181            Locator.waitUntilElementPresent(By.xpath("//span[text()='Permission deleted.']"));
182        }
183        return asPage(PermissionsSubPage.class);
184    }
185
186    private WebElement findDeleteButton(String permission, String userOrGroupName) {
187        List<WebElement> elements = driver.findElements(By.xpath("//div[contains(@class, 'acl-table-row effective')]"));
188        for (WebElement element : elements) {
189            List<WebElement> names = element.findElements(By.xpath(".//span[contains(@class, 'tag')]"));
190            List<WebElement> perms = element.findElements(By.className("label"));
191            if (names.size() > 0 && perms.size() > 0) {
192                String title = names.get(0).getAttribute("title");
193                String perm = perms.get(0).getText();
194                if (title.startsWith(userOrGroupName) && permission.equalsIgnoreCase(perm)) {
195                    return element.findElement(By.xpath(".//paper-icon-button[@icon='delete']"));
196                }
197            }
198        }
199        return null;
200    }
201
202    /**
203     * @since 8.3
204     */
205    public boolean hasNewPermissionButton() {
206        try {
207            waitUntilEnabled(newPermission);
208            return true;
209        } catch (NotFoundException e) {
210            return false;
211        }
212    }
213}