1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.test.selenium

File UsersGroupsRightsManagementTest.java

 

Code metrics

4
155
24
1
421
250
30
0.19
6.46
24
1.25

Classes

Class Line # Actions
UsersGroupsRightsManagementTest 32 155 0% 30 0
1.0100%
 

Contributing tests

This file is covered by 8 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.test.selenium;
21   
22    import org.junit.Test;
23    import org.xwiki.test.selenium.framework.AbstractXWikiTestCase;
24   
25    import static org.junit.Assert.*;
26   
27    /**
28    * Verify the Users, Groups and Rights Management features of XWiki.
29    *
30    * @version $Id: c16068f7d219b0ff930b8e1be15a2ec8edf7edc3 $
31    */
 
32    public class UsersGroupsRightsManagementTest extends AbstractXWikiTestCase
33    {
34    /**
35    * <ul>
36    * <li>Validate group creation.</li>
37    * <li>Validate groups administration print "0" members for empty group.</li>
38    * <li>Validate group deletion.</li>
39    * <li>Validate rights automatically cleaned from deleted groups.</li>
40    * </ul>
41    */
 
42  1 toggle @Test
43    public void testCreateAndDeleteGroup()
44    {
45    // Make sure there's no XWikiNewGroup before we try to create it
46  1 deleteGroup("XWikiNewGroup", true);
47  1 createGroup("XWikiNewGroup");
48   
49    // Validate XWIKI-1903: Empty group shows 1 member.
50  1 assertEquals("Group XWikiNewGroup which is empty print more than 0 members", 0,
51    getGroupMembersCount("XWikiNewGroup"));
52   
53    // Give "view" global right to XWikiNewGroup on wiki
54  1 openGlobalRightsPage();
55  1 clickGroupsRadioButton();
56  1 clickViewRightsCheckbox("XWikiNewGroup", "allow");
57   
58    // Give "comment" page right to XWikiNewGroup on Test.TestCreateAndDeleteGroup page
59  1 createPage("Test", "TestCreateAndDeleteGroup", "whatever");
60  1 clickEditPageAccessRights();
61  1 clickGroupsRadioButton();
62  1 clickCommentRightsCheckbox("XWikiNewGroup", "allow");
63   
64    // Delete the newly created group and see if rights are cleaned
65  1 deleteGroup("XWikiNewGroup", false);
66   
67    // Validate XWIKI-2304: When a user or a group is removed it's not removed from rights objects
68  1 open("XWiki", "XWikiPreferences", "edit", "editor=object");
69  1 assertTextNotPresent("XWikiNewGroup");
70  1 open("Test", "TestCreateAndDeleteGroup", "edit", "editor=object");
71  1 assertTextNotPresent("XWikiNewGroup");
72    }
73   
74    /**
75    * Validate that administration show error when trying to create an existing group.
76    */
 
77  1 toggle @Test
78    public void testCreateGroupWhenGroupAlreadyExists()
79    {
80  1 open("XWiki", "testCreateGroupWhenGroupAlreadyExists", "edit", "editor=wiki");
81  1 clickEditSaveAndView();
82  1 openGroupsPage();
83  1 clickLinkWithText("Add new group", false);
84  1 waitForLightbox("Create new group".toUpperCase());
85  1 setFieldValue("newgroupi", "testCreateGroupWhenGroupAlreadyExists");
86  1 getSelenium().click("//input[@value='Create group']");
87    // We need to wait till the alert appears since when the user clicks on the "Create Group" button there's
88    // an Ajax call made to the server.
89  1 waitForAlert();
90  1 assertEquals("testCreateGroupWhenGroupAlreadyExists cannot be used for the "
91    + "group name, as another page with this name already exists.", getSelenium().getAlert());
92    }
93   
94    /**
95    * <ul>
96    * <li>Validate user creation.</li>
97    * <li>Validate user deletion.</li>
98    * <li>Validate groups automatically cleaned from deleted users.</li>
99    * </ul>
100    */
 
101  1 toggle @Test
102    public void testCreateAndDeleteUser()
103    {
104    // Make sure there's no XWikiNewUser user before we try to create it
105  1 deleteUser("XWikiNewUser", true);
106  1 createUser("XWikiNewUser", "XWikiNewUser");
107   
108    // Verify that new users are automatically added to the XWikiAllGroup group.
109  1 open("XWiki", "XWikiAllGroup");
110  1 waitForGroupUsersLiveTable();
111  1 assertTextPresent("XWiki.XWikiNewUser");
112   
113    // Delete the newly created user and see if groups are cleaned
114  1 deleteUser("XWikiNewUser", false);
115   
116    // Verify that when a user is removed he's removed from the groups he belongs to.
117  1 open("XWiki", "XWikiAllGroup");
118  1 waitForGroupUsersLiveTable();
119  1 assertTextNotPresent("XWiki.XWikiNewUser");
120    }
121   
122    /**
123    * Test that the Ajax registration tool accepts non-ASCII symbols.
124    */
 
125  1 toggle @Test
126    public void testCreateNonAsciiUser()
127    {
128    // Make sure there's no AccentUser user before we try to create it
129  1 deleteUser("AccentUser", true);
130   
131    // Use ISO-8859-1 symbols to make sure that the test works both in ISO-8859-1 and UTF8
132  1 createUser("AccentUser", "AccentUser", "a\u00e9b", "c\u00e0d");
133   
134    // Verify that the user is present in the table
135  1 assertTextPresent("AccentUser");
136    // Verify that the correct symbols appear
137  1 assertTextPresent("a\u00e9b");
138  1 assertTextPresent("c\u00e0d");
139    }
140   
141    /**
142    * Validate group rights. Validate XWIKI-2375: Group and user access rights problem with a name which includes space
143    * characters
144    */
 
145  1 toggle @Test
146    public void testGroupRights()
147    {
148  1 String username = "TestUser";
149    // Voluntarily put a space in the group name.
150  1 String groupname = "Test Group";
151   
152    // Make sure there's no "TestUser" user and no "Test Group" user before we try to create it
153  1 deleteUser(username, true);
154  1 deleteGroup(groupname, true);
155   
156    // Create a new user, a new group, make the user part of that group and create a new page
157  1 createUser(username, username);
158  1 createGroup(groupname);
159  1 addUserToGroup(username, groupname);
160  1 createPage("Test", "TestGroupRights", "Some content");
161   
162    // Deny view rights to the group on the newly created page
163  1 open("Test", "TestGroupRights", "edit", "editor=rights");
164  1 clickGroupsRadioButton();
165    // Click a first time to allow view and a second time to deny it.
166  1 clickViewRightsCheckbox(groupname, "allow");
167  1 clickViewRightsCheckbox(groupname, "deny1");
168   
169    // Make sure that Admins can still view the page
170  1 open("Test", "TestGroupRights");
171  1 assertTextPresent("Some content");
172   
173    // And ensure that the newly created user cannot view it
174  1 login(username, username, false);
175  1 open("Test", "TestGroupRights");
176  1 assertTextPresent("not allowed");
177   
178    // Cleanup
179  1 loginAsAdmin();
180  1 deleteUser(username, false);
181  1 deleteGroup(groupname, false);
182    }
183   
184    /**
185    * Test adding a group to a group. Specifically, assert that the group is added as a member itself, not adding all
186    * its members one by one.
187    */
 
188  1 toggle @Test
189    public void testAddGroupToGroup()
190    {
191  1 String group = "GroupWithGroup";
192  1 createGroup(group);
193  1 openGroupsPage();
194  1 String xpath = "//tbody/tr[td/a='" + group + "']/td[3]/img[@title='Edit']";
195  1 System.out.println("XPATH: " + xpath);
196  1 waitForCondition("selenium.isElementPresent(\"" + xpath + "\")");
197  1 getSelenium().click("//tbody/tr[td/a=\"" + group + "\"]/td[3]/img[@title=\"Edit\"]");
198  1 waitForLightbox("SUBGROUPS TO ADD");
199  1 setFieldValue("groupInput", "XWiki.XWikiAllGroup");
200  1 clickLinkWithLocator("addMembers", false);
201  1 String xpathPrefix = "//div[@id='lb-content']/div/div/table/tbody/tr/td/table/tbody/tr";
202  1 String adminGroupXPath =
203    xpathPrefix + "/td[contains(@class, 'member')]/a[@href='/xwiki/bin/view/XWiki/XWikiAllGroup']";
204    // this xpath expression is fragile, but we have to start as up as the lightbox does, because
205    // the same table with same ids and classes is already displayed in the Preferences page
206    // (that is, the list of existing groups).
207  1 waitForCondition("selenium.isElementPresent(\"" + adminGroupXPath + "\")");
208    // Now assert that XWiki.Admin, member of XWikiAdminGroup is not added as a member of our created group
209  1 assertElementNotPresent(xpathPrefix + "/td[contains(@class, 'member')]/a[@href='/xwiki/bin/view/XWiki/Admin']");
210  1 clickLinkWithLocator("lb-close");
211   
212    // Now same test, but from the group document UI in inline mode
213  1 clickLinkWithText(group);
214  1 clickEditPageInlineForm();
215  1 setFieldValue("groupInput", "XWiki.XWikiAdminGroup");
216  1 clickLinkWithLocator("addMembers", false);
217  1 waitForTextContains("id=groupusers", "XWiki.XWikiAdminGroup");
218   
219    // cleanup
220  1 deleteGroup(group, false);
221    }
222   
223    /**
224    * Validate adding a member to a group via the administration.
225    */
 
226  1 toggle @Test
227    public void testAddUserToGroup()
228    {
229    // Make sure there's no XWikiNewUser user before we try to create it
230  1 deleteUser("XWikiTestUser", true);
231  1 createUser("XWikiTestUser", "XWikiTestUser");
232   
233  1 addUserToGroup("XWikiTestUser", "XWikiAdminGroup");
234   
235  1 deleteUser("XWikiTestUser", true);
236    }
237   
238    /**
239    * Validate member filtering on group sheet.
240    */
 
241  1 toggle @Test
242    public void testFilteringOnGroupSheet()
243    {
244  1 openGroupsPage();
245  1 String rowXPath = "//td[contains(@class, 'member')]/a[@href='/xwiki/bin/view/XWiki/Admin']";
246  1 this.clickLinkWithText("XWikiAdminGroup");
247  1 this.waitForCondition("selenium.isElementPresent(\"" + rowXPath + "\")");
248   
249  1 this.getSelenium().focus("member");
250  1 this.getSelenium().typeKeys("member", "zzz");
251  1 this.waitForCondition("!selenium.isElementPresent(\"" + rowXPath + "\")");
252   
253  1 this.getSelenium().focus("member");
254    // Type Backspace 3 times to delete the previous text.
255  1 this.getSelenium().typeKeys("member", "\b\b\bAd");
256  1 this.waitForCondition("selenium.isElementPresent(\"" + rowXPath + "\")");
257    }
258   
259    // Helper methods
260   
 
261  3 toggle private void createGroup(String groupname)
262    {
263  3 openGroupsPage();
264  3 clickLinkWithText("Add new group", false);
265  3 waitForLightbox("Create new group".toUpperCase());
266  3 setFieldValue("newgroupi", groupname);
267  3 clickLinkWithXPath("//input[@value='Create group']", true);
268  3 waitForTextContains("id=groupstable", groupname);
269    }
270   
271    /**
272    * @param deleteOnlyIfExists if true then only delete the group if it exists
273    */
 
274  5 toggle private void deleteGroup(String groupname, boolean deleteOnlyIfExists)
275    {
276  5 if (!deleteOnlyIfExists || (deleteOnlyIfExists && isExistingPage("XWiki", groupname))) {
277  3 openGroupsPage();
278  3 getSelenium().chooseOkOnNextConfirmation();
279  3 clickLinkWithLocator("//tbody/tr[td/a='" + groupname + "']//img[@title='Delete']", false);
280  3 waitForConfirmation();
281  3 assertEquals("The group XWiki." + groupname + " will be deleted. Are you sure you want to proceed?",
282    getSelenium().getConfirmation());
283    // Wait till the group has been deleted.
284  3 waitForCondition("!selenium.isElementPresent('//tbody/tr[td/a=\"" + groupname + "\"]')");
285    }
286    }
287   
 
288  3 toggle private void createUser(String login, String pwd)
289    {
290  3 createUser(login, pwd, "New", "User");
291    }
292   
 
293  4 toggle private void createUser(String login, String pwd, String fname, String lname)
294    {
295  4 openUsersPage();
296  4 clickLinkWithText("Add new user", false);
297  4 waitForElement("//input[@id='register_first_name']");
298  4 setFieldValue("register_first_name", fname);
299  4 setFieldValue("register_last_name", lname);
300  4 setFieldValue("xwikiname", login);
301  4 setFieldValue("register_password", pwd);
302  4 setFieldValue("register2_password", pwd);
303  4 setFieldValue("register_email", "new.user@xwiki.org");
304  4 getSelenium().click("//input[@value='Save']");
305    // Wait till the user is displayed.
306  4 waitForTextContains("id=userstable", login);
307    }
308   
 
309  7 toggle private void deleteUser(String login, boolean deleteOnlyIfExists)
310    {
311  7 if (!deleteOnlyIfExists || (deleteOnlyIfExists && isExistingPage("XWiki", login))) {
312  3 openUsersPage();
313  3 clickLinkWithLocator("//tbody/tr[td/a='" + login + "']//img[@title='Delete']", false);
314  3 waitForConfirmation();
315  3 assertEquals("The user XWiki." + login + " will be deleted and removed from all groups he belongs to. "
316    + "Are you sure you want to proceed?", getSelenium().getConfirmation());
317    // Wait till the user has been deleted.
318  3 waitForCondition("!selenium.isElementPresent('//tbody/tr[td/a=\"" + login + "\"]')");
319    }
320    }
321   
 
322  2 toggle private void addUserToGroup(String user, String group)
323    {
324  2 openGroupsPage();
325  2 String xpath = "//tbody/tr[td/a='" + group + "']/td[3]/img[@title='Edit']";
326  2 waitForCondition("selenium.isElementPresent(\"" + xpath + "\")");
327  2 getSelenium().click(xpath);
328  2 waitForLightbox("USERS TO ADD");
329  2 setFieldValue("userInput", "XWiki." + user);
330  2 clickLinkWithLocator("addMembers", false);
331   
332  2 String xpathPrefix = "//div[@id='lb-content']/div/div/table/tbody/tr/td/table/tbody/tr";
333  2 String newGroupMemberXPath =
334    xpathPrefix + "/td[contains(@class, 'member')]/a[@href='/xwiki/bin/view/XWiki/" + user + "']";
335    // this xpath expression is fragile, but we have to start as up as the lightbox does, because
336    // the same table with same ids and classes is already displayed in the Preferences page
337    // (that is, the list of existing groups).
338  2 waitForCondition("selenium.isElementPresent(\"" + newGroupMemberXPath + "\")");
339   
340    // Close the group edit lightbox
341  2 clickLinkWithLocator("lb-close");
342  2 open("XWiki", group);
343  2 waitForGroupUsersLiveTable();
344  2 assertTextPresent(user);
345    }
346   
 
347  7 toggle private void waitForLightbox(String lightboxName)
348    {
349  7 waitForBodyContains(lightboxName);
350    }
351   
 
352  3 toggle private void clickGroupsRadioButton()
353    {
354  3 clickLinkWithXPath("//input[@name='uorg' and @value='groups']", false);
355    }
356   
 
357  1 toggle private void openGlobalRightsPage()
358    {
359  1 openAdministrationPage();
360  1 clickLinkWithText("Rights");
361    }
362   
 
363  11 toggle private void openGroupsPage()
364    {
365  11 openAdministrationPage();
366  11 clickLinkWithText("Groups");
367  11 waitForLiveTable("groupstable");
368    }
369   
 
370  7 toggle private void openUsersPage()
371    {
372    // Note: We could have used the following command instead:
373    // open("XWiki", "XWikiUsers", "admin", "editor=users")
374    // However we haven't done it since we also want to verify that clicking on the "Users" tab works.
375  7 openAdministrationPage();
376  7 clickLinkWithText("Users");
377  7 waitForLiveTable("userstable");
378    }
379   
380    /**
381    * @return the number of members in the passed group. Should only be executed when on the Global Rights page.
382    */
 
383  1 toggle private int getGroupMembersCount(String groupname)
384    {
385  1 return Integer.parseInt(getSelenium().getText("//tbody/tr[td/a=\"" + groupname + "\"]/td[2]"));
386    }
387   
388    /**
389    * @param actionToVerify the action that the click is supposed to have done. Valid values are "allow", "deny1" or
390    * "none".
391    */
 
392  3 toggle private void clickViewRightsCheckbox(String groupOrUserName, String actionToVerify)
393    {
394  3 clickRightsCheckbox(groupOrUserName, actionToVerify, 2);
395    }
396   
397    /**
398    * @param actionToVerify the action that the click is supposed to have done. Valid values are "allow", "deny1" or
399    * "none".
400    */
 
401  1 toggle private void clickCommentRightsCheckbox(String groupOrUserName, String actionToVerify)
402    {
403  1 clickRightsCheckbox(groupOrUserName, actionToVerify, 3);
404    }
405   
 
406  4 toggle private void clickRightsCheckbox(String groupOrUserName, String actionToVerify, int positionInTd)
407    {
408  4 String xpath = "//tbody/tr[td/a='" + groupOrUserName + "']/td[" + positionInTd + "]/img";
409  4 clickLinkWithXPath(xpath, false);
410    // Wait till it has been clicked since this can take some time.
411  4 waitForCondition("selenium.isElementPresent(\"" + xpath + "[contains(@src, '" + actionToVerify + ".png')]\")");
412    }
413   
414    /**
415    * Waits for the live table that lists group users to load.
416    */
 
417  4 toggle private void waitForGroupUsersLiveTable()
418    {
419  4 waitForLiveTable("groupusers");
420    }
421    }