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

File LoginTest.java

 
testLoginLogoutAsAdmin: expected:<[Administrator]> but was:<[]>
 

Code metrics

6
69
8
1
216
146
11
0.16
8.62
8
1.38

Classes

Class Line # Actions
LoginTest 39 69 0% 11 13
0.843373584.3%
 

Contributing tests

This file is covered by 7 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.ui;
21   
22    import org.junit.Assert;
23    import org.junit.Before;
24    import org.junit.Test;
25    import org.xwiki.administration.test.po.GlobalRightsAdministrationSectionPage;
26    import org.xwiki.test.ui.browser.IgnoreBrowser;
27    import org.xwiki.test.ui.browser.IgnoreBrowsers;
28    import org.xwiki.test.ui.po.LoginPage;
29    import org.xwiki.test.ui.po.ResubmissionPage;
30    import org.xwiki.test.ui.po.ViewPage;
31    import org.xwiki.test.ui.po.editor.WikiEditPage;
32   
33    /**
34    * Test the Login feature.
35    *
36    * @version $Id: ba32ed9b9b48ba065728e128b8cf7d1769334e5e $
37    * @since 2.3M1
38    */
 
39    public class LoginTest extends AbstractTest
40    {
41    private ViewPage vp;
42   
43    private String nonExistentPageURL;
44   
 
45  7 toggle @Before
46    public void setUp()
47    {
48    // Force log out (we're using the fast way since this is not part of what we want to test)
49  7 getUtil().forceGuestUser();
50   
51    // Go to any page in view mode. We choose to go to a nonexisting page so that it loads as fast as possible
52    // Note: since the page doesn't exist, we need to disable the space redirect feature so that we end up on the
53    // terminal page and not on WebHome in the space.
54  7 getUtil().gotoPage("NonExistentSpace", "NonExistentPage", "view", "spaceRedirect=false");
55  7 this.vp = new ViewPage();
56  7 this.nonExistentPageURL = getDriver().getCurrentUrl();
57    }
58   
 
59  0 toggle @Test
60    @IgnoreBrowsers({
61    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason="See http://jira.xwiki.org/browse/XE-1146"),
62    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason="See http://jira.xwiki.org/browse/XE-1177")
63    })
64    public void testLoginLogoutAsAdmin()
65    {
66  0 LoginPage loginPage = this.vp.login();
67  0 loginPage.loginAsAdmin();
68   
69    // Verify that after logging in we're redirected to the page on which the login button was clicked, i.e. the
70    // non existent page here.
71  0 Assert.assertEquals(this.nonExistentPageURL, getDriver().getCurrentUrl());
72   
73  0 Assert.assertTrue(this.vp.isAuthenticated());
74  0 Test failure here Assert.assertEquals("Administrator", this.vp.getCurrentUser());
75   
76    // Test Logout and verify we stay on the same page
77  0 this.vp.logout();
78  0 Assert.assertFalse(this.vp.isAuthenticated());
79  0 Assert.assertEquals(this.nonExistentPageURL, getDriver().getCurrentUrl());
80    }
81   
 
82  1 toggle @Test
83    @IgnoreBrowsers({
84    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason="See http://jira.xwiki.org/browse/XE-1146"),
85    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason="See http://jira.xwiki.org/browse/XE-1177")
86    })
87    public void testLoginWithInvalidCredentials()
88    {
89  1 LoginPage loginPage = this.vp.login();
90  1 loginPage.loginAs("Admin", "wrong password");
91  1 Assert.assertTrue(loginPage.hasInvalidCredentialsErrorMessage());
92    }
93   
 
94  1 toggle @Test
95    @IgnoreBrowsers({
96    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason="See http://jira.xwiki.org/browse/XE-1146"),
97    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason="See http://jira.xwiki.org/browse/XE-1177")
98    })
99    public void testLoginWithInvalidUsername()
100    {
101  1 LoginPage loginPage = this.vp.login();
102  1 loginPage.loginAs("non existent user", "admin");
103  1 Assert.assertTrue(loginPage.hasInvalidCredentialsErrorMessage());
104    }
105   
106    /**
107    * Verify that the initial URL is not lost after logging in when the session has expired.
108    * See XWIKI-5317.
109    */
 
110  1 toggle @Test
111    public void testRedirectBackAfterLogin()
112    {
113  1 try {
114    // Test setup: disallow view right for unauthenticated users. We need to be logged as admin in order to
115    // do that. Since this is not what we are testing use the fast way to log in
116  1 GlobalRightsAdministrationSectionPage grasp = new GlobalRightsAdministrationSectionPage();
117  1 getDriver().get(getUtil().getURLToLoginAsAdminAndGotoPage(grasp.getURL()));
118  1 getUtil().recacheSecretToken();
119  1 getUtil().setDefaultCredentials(TestUtils.ADMIN_CREDENTIALS);
120  1 grasp.forceAuthenticatedView();
121   
122    // Go to a page, log out and expire session by removing cookies, log in again and verify that the user is
123    // redirected to the initial page.
124  1 ViewPage page = getUtil().gotoPage("SomeSpace", "SomePage");
125  1 page.logout();
126    // Since view is disallowed for unauthenticated users, at this point we see a log in page.
127  1 LoginPage loginPage = new LoginPage();
128    // Remove all cookie to simulate a session expiry
129  1 getDriver().manage().deleteAllCookies();
130  1 loginPage.loginAsAdmin();
131   
132    // We use startsWith since the URL contains a jsessionid and a srid.
133  1 Assert.assertTrue(getDriver().getCurrentUrl().startsWith(getUtil().getURL("SomeSpace", "SomePage")));
134    } finally {
135  1 GlobalRightsAdministrationSectionPage grasp = GlobalRightsAdministrationSectionPage.gotoPage();
136  1 if (!grasp.isAuthenticated()) {
137  0 getDriver().get(getUtil().getURLToLoginAsAdminAndGotoPage(grasp.getURL()));
138    }
139  1 grasp.unforceAuthenticatedView();
140    }
141    }
142   
 
143  1 toggle @Test
144    @IgnoreBrowsers({
145    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason="See http://jira.xwiki.org/browse/XE-1146"),
146    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason="See http://jira.xwiki.org/browse/XE-1177")
147    })
148    public void testRedirectPreservesPOSTParameters()
149    {
150  1 String test = "Test string " + System.currentTimeMillis();
151  1 final String space = "Main";
152  1 final String page = "POSTTest";
153  1 LoginPage loginPage = this.vp.login();
154  1 loginPage.loginAsAdmin();
155    // start editing a page
156  1 WikiEditPage editPage = WikiEditPage.gotoPage(space, page);
157  1 editPage.setTitle(test);
158  1 editPage.setContent(test);
159    // emulate expired session: delete the cookies
160  1 getDriver().manage().deleteAllCookies();
161    // try to save
162  1 editPage.clickSaveAndView();
163    // we should have been redirected to login
164  1 String wantUrl = getUtil().getURL("XWiki", "XWikiLogin", "login");
165  1 if (wantUrl.indexOf('?') > 0) {
166    // strip parameters
167  1 wantUrl = wantUrl.substring(0, wantUrl.indexOf('?'));
168    }
169  1 Assert.assertTrue(getDriver().getCurrentUrl().startsWith(wantUrl));
170  1 loginPage.loginAsAdmin();
171    // we should have been redirected back to view, and the page should have been saved
172  1 Assert.assertTrue(getDriver().getCurrentUrl().startsWith(getUtil().getURL(space, page)));
173  1 editPage = WikiEditPage.gotoPage(space, page);
174  1 Assert.assertEquals(test, editPage.getTitle());
175  1 Assert.assertEquals(test, editPage.getContent());
176    }
177   
 
178  1 toggle @Test
179    @IgnoreBrowsers({
180    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason="See http://jira.xwiki.org/browse/XE-1146"),
181    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason="See http://jira.xwiki.org/browse/XE-1177")
182    })
183    public void testCorrectUrlIsAccessedAfterLogin()
184    {
185    // We will choose the Scheduler.WebHome page to make our testing
186    // since it can't be viewed without being logged in
187  1 getUtil().gotoPage("Scheduler", "WebHome");
188  1 LoginPage loginPage = new LoginPage();
189  1 Assert.assertFalse(loginPage.isAuthenticated());
190  1 loginPage.loginAsAdmin();
191    // We should be redirected back to Scheduler.WebHome
192  1 Assert.assertTrue(getDriver().getCurrentUrl().contains("/xwiki/bin/view/Scheduler/WebHome"));
193  1 Assert.assertTrue(getDriver().getTitle().contains("Job Scheduler"));
194    }
195   
 
196  1 toggle @Test
197    @IgnoreBrowsers({
198    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason="See http://jira.xwiki.org/browse/XE-1146"),
199    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason="See http://jira.xwiki.org/browse/XE-1177")
200    })
201    public void testDataIsPreservedAfterLogin()
202    {
203  1 getUtil().gotoPage("Test", "TestData", "save", "content=this+should+not+be+saved");
204  1 getUtil().gotoPage("Test", "TestData", "save", "content=this+should+be+saved+instead&parent=Main.WebHome");
205  1 LoginPage loginPage = new LoginPage();
206  1 loginPage.loginAsAdmin();
207    // we switched to another user, CSRF protection (if enabled) will ask for confirmation
208  1 ResubmissionPage resubmissionPage = new ResubmissionPage();
209  1 if (resubmissionPage.isOnResubmissionPage()) {
210  1 resubmissionPage.resubmit();
211    }
212  1 Assert.assertTrue(getDriver().getCurrentUrl().contains("/xwiki/bin/view/Test/TestData"));
213  1 ViewPage viewPage = new ViewPage();
214  1 Assert.assertEquals("this should be saved instead", viewPage.getContent());
215    }
216    }