diff --git a/samples/javaconfig/helloworld/spring-security-samples-javaconfig-helloworld.gradle b/samples/javaconfig/helloworld/spring-security-samples-javaconfig-helloworld.gradle index aa3713e3f9b..5cf9363a537 100644 --- a/samples/javaconfig/helloworld/spring-security-samples-javaconfig-helloworld.gradle +++ b/samples/javaconfig/helloworld/spring-security-samples-javaconfig-helloworld.gradle @@ -1,3 +1,19 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + apply plugin: 'io.spring.convention.spring-sample-war' dependencies { @@ -9,5 +25,5 @@ dependencies { providedCompile 'javax.servlet:javax.servlet-api' providedCompile 'javax.servlet.jsp:javax.servlet.jsp-api' - integrationTestCompile gebDependencies + integrationTestCompile seleniumDependencies } diff --git a/samples/javaconfig/helloworld/src/integration-test/groovy/org/springframework/security/samples/HelloWorldJcTests.groovy b/samples/javaconfig/helloworld/src/integration-test/groovy/org/springframework/security/samples/HelloWorldJcTests.groovy deleted file mode 100644 index b8f33ef7083..00000000000 --- a/samples/javaconfig/helloworld/src/integration-test/groovy/org/springframework/security/samples/HelloWorldJcTests.groovy +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2002-2011 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.security.samples - -import geb.spock.* -import spock.lang.Shared -import spock.lang.Stepwise -import org.springframework.security.samples.pages.* - -/** - * Tests the CAS sample application using service tickets. - * - * @author Rob Winch - */ -@Stepwise -class HelloWorldJcTests extends GebReportingSpec { - def 'access home page with unauthenticated user sends to login page'() { - when: 'Unauthenticated user accesses the Home Page' - via HomePage - then: 'The login page is displayed' - at LoginPage - } - - def 'authenticated user is sent to original page'() { - when: 'user authenticates' - login() - then: 'The home page is displayed' - at HomePage - and: 'The username is displayed' - message == 'Hello user' - } - - def 'authenticated user logs out'() { - when: 'user logs out' - logout() - then: 'the login page is displayed' - at LoginPage - when: 'Unauthenticated user accesses the Home Page' - via HomePage - then: 'The login page is displayed' - at LoginPage - } -} diff --git a/samples/javaconfig/helloworld/src/integration-test/groovy/org/springframework/security/samples/pages/HomePage.groovy b/samples/javaconfig/helloworld/src/integration-test/groovy/org/springframework/security/samples/pages/HomePage.groovy deleted file mode 100644 index 0e7320a9c40..00000000000 --- a/samples/javaconfig/helloworld/src/integration-test/groovy/org/springframework/security/samples/pages/HomePage.groovy +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2002-2011 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.security.samples.pages; - -import geb.* - -/** - * The home page - * - * @author Rob Winch - */ -class HomePage extends Page { - static url = '' - static at = { assert driver.title == 'Hello Security'; true} - static content = { - message { $('p').text() } - logout { $('input', type: 'submit').click() } - } -} diff --git a/samples/javaconfig/helloworld/src/integration-test/java/org/springframework/security/samples/HelloWorldJcTests.java b/samples/javaconfig/helloworld/src/integration-test/java/org/springframework/security/samples/HelloWorldJcTests.java new file mode 100644 index 00000000000..5f4ebec2101 --- /dev/null +++ b/samples/javaconfig/helloworld/src/integration-test/java/org/springframework/security/samples/HelloWorldJcTests.java @@ -0,0 +1,77 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.security.samples; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.htmlunit.HtmlUnitDriver; +import org.springframework.security.samples.pages.HomePage; +import org.springframework.security.samples.pages.LoginPage; + +/** + * @author Michael Simons + */ +public class HelloWorldJcTests { + + private WebDriver driver; + + private int port; + + @Before + public void setup() { + this.port = Integer.parseInt(System.getProperty("app.httpPort")); + this.driver = new HtmlUnitDriver(); + } + + @After + public void tearDown() { + this.driver.quit(); + } + + @Test + public void accessHomePageWithUnauthenticatedUserSendsToLoginPage() { + final LoginPage loginPage = HomePage.to(this.driver, this.port); + loginPage.assertAt(); + } + + @Test + public void authenticatedUserIsSentToOriginalPage() { + final HomePage homePage = HomePage.to(this.driver, this.port) + .loginForm() + .username("user") + .password("password") + .submit(); + homePage + .assertAt() + .andTheUserNameIsDisplayed(); + } + + @Test + public void authenticatedUserLogsOut() { + LoginPage loginPage = HomePage.to(this.driver, this.port) + .loginForm() + .username("user") + .password("password") + .submit() + .logout(); + loginPage.assertAt(); + + loginPage = HomePage.to(this.driver, this.port); + loginPage.assertAt(); + } +} diff --git a/samples/javaconfig/helloworld/src/integration-test/java/org/springframework/security/samples/pages/HomePage.java b/samples/javaconfig/helloworld/src/integration-test/java/org/springframework/security/samples/pages/HomePage.java new file mode 100644 index 00000000000..76d033b4361 --- /dev/null +++ b/samples/javaconfig/helloworld/src/integration-test/java/org/springframework/security/samples/pages/HomePage.java @@ -0,0 +1,65 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.security.samples.pages; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Michael Simons + */ +public class HomePage { + private final WebDriver webDriver; + + @FindBy(css = "p") + private WebElement message; + + @FindBy(css = "input[type=submit]") + private WebElement logoutButton; + + public static LoginPage to(WebDriver driver, int port) { + driver.get("http://localhost:" + port +"/"); + return PageFactory.initElements(driver, LoginPage.class); + } + + public HomePage(WebDriver webDriver) { + this.webDriver = webDriver; + } + + public Content assertAt() { + assertThat(this.webDriver.getTitle()).isEqualTo("Hello Security"); + return PageFactory.initElements(this.webDriver, Content.class); + } + + public LoginPage logout() { + this.logoutButton.submit(); + return PageFactory.initElements(this.webDriver, LoginPage.class); + } + + public static class Content { + @FindBy(css = "p") + private WebElement message; + + public Content andTheUserNameIsDisplayed() { + assertThat(message.getText()).isEqualTo("Hello user"); + return this; + } + } +} diff --git a/samples/javaconfig/helloworld/src/integration-test/java/org/springframework/security/samples/pages/LoginPage.java b/samples/javaconfig/helloworld/src/integration-test/java/org/springframework/security/samples/pages/LoginPage.java new file mode 100644 index 00000000000..0a118128d2a --- /dev/null +++ b/samples/javaconfig/helloworld/src/integration-test/java/org/springframework/security/samples/pages/LoginPage.java @@ -0,0 +1,74 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.security.samples.pages; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Michael Simons + */ +public class LoginPage { + + private final WebDriver webDriver; + + private final LoginForm loginForm; + + public LoginPage(WebDriver webDriver) { + this.webDriver = webDriver; + this.loginForm = PageFactory.initElements(this.webDriver, LoginForm.class); + } + + public LoginPage assertAt() { + assertThat(this.webDriver.getTitle()).isEqualTo("Login Page"); + return this; + } + + public LoginForm loginForm() { + return this.loginForm; + } + + public static class LoginForm { + private WebDriver webDriver; + private WebElement username; + private WebElement password; + @FindBy(css = "input[type=submit]") + private WebElement submit; + + public LoginForm(WebDriver webDriver) { + this.webDriver = webDriver; + } + + public LoginForm username(String username) { + this.username.sendKeys(username); + return this; + } + + public LoginForm password(String password) { + this.password.sendKeys(password); + return this; + } + + public HomePage submit() { + this.submit.click(); + return PageFactory.initElements(this.webDriver, HomePage.class); + } + } +} diff --git a/samples/javaconfig/ldap/spring-security-samples-javaconfig-ldap.gradle b/samples/javaconfig/ldap/spring-security-samples-javaconfig-ldap.gradle index 36f3f79048f..0b229a330f0 100644 --- a/samples/javaconfig/ldap/spring-security-samples-javaconfig-ldap.gradle +++ b/samples/javaconfig/ldap/spring-security-samples-javaconfig-ldap.gradle @@ -1,3 +1,19 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + apply plugin: 'io.spring.convention.spring-sample-war' dependencies { @@ -9,7 +25,6 @@ dependencies { compile 'javax.servlet.jsp.jstl:javax.servlet.jsp.jstl-api' compile 'javax.validation:validation-api' compile 'org.hibernate:hibernate-validator' - compile 'org.springframework:spring-jdbc' compile 'org.springframework:spring-webmvc' compile apachedsDependencies compile slf4jDependencies @@ -19,5 +34,5 @@ dependencies { runtime 'opensymphony:sitemesh' - integrationTestCompile gebDependencies + integrationTestCompile seleniumDependencies } diff --git a/samples/javaconfig/ldap/src/integration-test/groovy/org/springframework/security/samples/LdapJcTests.groovy b/samples/javaconfig/ldap/src/integration-test/groovy/org/springframework/security/samples/LdapJcTests.groovy deleted file mode 100644 index 54e97832ccd..00000000000 --- a/samples/javaconfig/ldap/src/integration-test/groovy/org/springframework/security/samples/LdapJcTests.groovy +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2002-2011 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.security.samples - -import geb.spock.* -import spock.lang.Shared -import spock.lang.Stepwise -import org.springframework.security.samples.pages.* - -/** - * Tests the CAS sample application using service tickets. - * - * @author Rob Winch - */ -@Stepwise -class LdapJcTests extends GebReportingSpec { - def 'access home page with unauthenticated user sends to login page'() { - when: 'Unauthenticated user accesses the Home Page' - via HomePage - then: 'The login page is displayed' - at LoginPage - } - - def 'authenticated user is sent to original page'() { - when: 'user authenticates' - login() - then: 'The home page is displayed' - at HomePage - and: 'The username is displayed' - user == 'user' - } - - def 'authenticated user logs out'() { - when: 'user logs out' - logout() - then: 'the login page is displayed' - at LoginPage - when: 'Unauthenticated user accesses the Home Page' - via HomePage - then: 'The login page is displayed' - at LoginPage - } -} diff --git a/samples/javaconfig/ldap/src/integration-test/groovy/org/springframework/security/samples/pages/HomePage.groovy b/samples/javaconfig/ldap/src/integration-test/groovy/org/springframework/security/samples/pages/HomePage.groovy deleted file mode 100644 index 97af93f3838..00000000000 --- a/samples/javaconfig/ldap/src/integration-test/groovy/org/springframework/security/samples/pages/HomePage.groovy +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2002-2011 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.security.samples.pages; - -import geb.* - -/** - * The home page - * - * @author Rob Winch - */ -class HomePage extends Page { - static url = '' - static at = { assert driver.title == 'SecureMail: View All'; true} - static content = { - user { $('p.navbar-text').text() } - logout { $('input', type: 'submit').click() } - } -} diff --git a/samples/javaconfig/ldap/src/integration-test/groovy/org/springframework/security/samples/pages/LoginPage.groovy b/samples/javaconfig/ldap/src/integration-test/groovy/org/springframework/security/samples/pages/LoginPage.groovy deleted file mode 100644 index 89a4b92cc73..00000000000 --- a/samples/javaconfig/ldap/src/integration-test/groovy/org/springframework/security/samples/pages/LoginPage.groovy +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2002-2011 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.security.samples.pages; - -import geb.* - -/** - * The login page. - * - * @author Rob Winch - */ -class LoginPage extends Page { - static url = 'login' - static at = { assert driver.title == 'Login Page'; true} - static content = { - login(required:false) { user='user', password='password' -> - loginForm.username = user - loginForm.password = password - submit.click() - } - loginForm { $('form') } - submit { $('input', type: 'submit') } - } -} diff --git a/samples/javaconfig/ldap/src/integration-test/java/org/springframework/security/samples/LdapJcTests.java b/samples/javaconfig/ldap/src/integration-test/java/org/springframework/security/samples/LdapJcTests.java new file mode 100644 index 00000000000..1f02f9aa02f --- /dev/null +++ b/samples/javaconfig/ldap/src/integration-test/java/org/springframework/security/samples/LdapJcTests.java @@ -0,0 +1,81 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.security.samples; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.htmlunit.HtmlUnitDriver; +import org.springframework.security.samples.pages.HomePage; +import org.springframework.security.samples.pages.LoginPage; + +/** + * @author Michael Simons + */ +public class LdapJcTests { + private WebDriver driver; + + private int port; + + @Before + public void setup() { + this.port = Integer.parseInt(System.getProperty("app.httpPort")); + this.driver = new HtmlUnitDriver(); + } + + @After + public void tearDown() { + this.driver.quit(); + } + + @Test + public void accessHomePageWithUnauthenticatedUserSendsToLoginPage() { + final LoginPage loginPage = HomePage.to(this.driver, this.port); + loginPage.assertAt(); + } + + @Test + public void authenticatedUserIsSentToOriginalPage() { + final String userName = "user"; + // @formatter:off + final HomePage homePage = HomePage.to(this.driver, this.port) + .loginForm() + .username(userName) + .password("password") + .submit(); + // @formatter:on + homePage + .assertAt() + .andTheUserNameDisplayedIs(userName); + } + + @Test + public void authenticatedUserLogsOut() { + // @formatter:off + LoginPage loginPage = HomePage.to(this.driver, this.port) + .loginForm() + .username("user") + .password("password") + .submit() + .logout(); + // @formatter:on + loginPage.assertAt(); + + loginPage = HomePage.to(this.driver, this.port); + loginPage.assertAt(); + } +} diff --git a/samples/javaconfig/ldap/src/integration-test/java/org/springframework/security/samples/pages/HomePage.java b/samples/javaconfig/ldap/src/integration-test/java/org/springframework/security/samples/pages/HomePage.java new file mode 100644 index 00000000000..a3237051a41 --- /dev/null +++ b/samples/javaconfig/ldap/src/integration-test/java/org/springframework/security/samples/pages/HomePage.java @@ -0,0 +1,62 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.security.samples.pages; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Michael Simons + */ +public class HomePage { + private final WebDriver webDriver; + + @FindBy(css = "input[type=submit]") + private WebElement logoutButton; + + public static LoginPage to(WebDriver driver, int port) { + driver.get("http://localhost:" + port +"/"); + return PageFactory.initElements(driver, LoginPage.class); + } + + public HomePage(WebDriver webDriver) { + this.webDriver = webDriver; + } + + public Content assertAt() { + assertThat(this.webDriver.getTitle()).isEqualTo("SecureMail: View All"); + return PageFactory.initElements(this.webDriver, Content.class); + } + + public LoginPage logout() { + this.logoutButton.submit(); + return PageFactory.initElements(this.webDriver, LoginPage.class); + } + + public static class Content { + @FindBy(css = "p.navbar-text") + private WebElement message; + + public Content andTheUserNameDisplayedIs(final String userName) { + assertThat(message.getText()).isEqualTo(userName); + return this; + } + } +} diff --git a/samples/javaconfig/ldap/src/integration-test/java/org/springframework/security/samples/pages/LoginPage.java b/samples/javaconfig/ldap/src/integration-test/java/org/springframework/security/samples/pages/LoginPage.java new file mode 100644 index 00000000000..0a118128d2a --- /dev/null +++ b/samples/javaconfig/ldap/src/integration-test/java/org/springframework/security/samples/pages/LoginPage.java @@ -0,0 +1,74 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.security.samples.pages; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Michael Simons + */ +public class LoginPage { + + private final WebDriver webDriver; + + private final LoginForm loginForm; + + public LoginPage(WebDriver webDriver) { + this.webDriver = webDriver; + this.loginForm = PageFactory.initElements(this.webDriver, LoginForm.class); + } + + public LoginPage assertAt() { + assertThat(this.webDriver.getTitle()).isEqualTo("Login Page"); + return this; + } + + public LoginForm loginForm() { + return this.loginForm; + } + + public static class LoginForm { + private WebDriver webDriver; + private WebElement username; + private WebElement password; + @FindBy(css = "input[type=submit]") + private WebElement submit; + + public LoginForm(WebDriver webDriver) { + this.webDriver = webDriver; + } + + public LoginForm username(String username) { + this.username.sendKeys(username); + return this; + } + + public LoginForm password(String password) { + this.password.sendKeys(password); + return this; + } + + public HomePage submit() { + this.submit.click(); + return PageFactory.initElements(this.webDriver, HomePage.class); + } + } +} diff --git a/samples/xml/contacts/spring-security-samples-xml-contacts.gradle b/samples/xml/contacts/spring-security-samples-xml-contacts.gradle index 3c0f7f557fa..652a4b69381 100644 --- a/samples/xml/contacts/spring-security-samples-xml-contacts.gradle +++ b/samples/xml/contacts/spring-security-samples-xml-contacts.gradle @@ -1,3 +1,18 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ apply plugin: 'io.spring.convention.spring-sample-war' dependencies { @@ -24,5 +39,5 @@ dependencies { runtime 'org.slf4j:jcl-over-slf4j' runtime 'org.springframework:spring-context-support' - integrationTestCompile gebDependencies + integrationTestCompile seleniumDependencies } diff --git a/samples/xml/contacts/src/integration-test/groovy/org/springframework/security/samples/ContactsTests.groovy b/samples/xml/contacts/src/integration-test/groovy/org/springframework/security/samples/ContactsTests.groovy deleted file mode 100644 index 33092c64edf..00000000000 --- a/samples/xml/contacts/src/integration-test/groovy/org/springframework/security/samples/ContactsTests.groovy +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright 2002-2011 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.security.samples - -import geb.spock.* -import spock.lang.Stepwise -import org.springframework.security.samples.pages.* - -/** - * Tests the CAS sample application using service tickets. - * - * @author Rob Winch - */ -@Stepwise -class ContactsTests extends GebReportingSpec { - def 'access home page with unauthenticated user success'() { - when: 'Unauthenticated user accesses the Home Page' - to HomePage - then: 'The page is displayed' - at HomePage - } - - def 'access manage page with unauthenticated user sends to login page'() { - when: 'Unauthenticated user accesses the Manage Page' - manage.click(LoginPage) - then: 'The login page is displayed' - at LoginPage - } - - def 'authenticated user is sent to original page'() { - when: 'user authenticates' - login() - then: 'The manage page is displayed' - at ContactsPage - } - - def 'add contact link works'() { - when: 'user clicks add link' - addContact.click(AddPage) - then: 'The add page is displayed' - at AddPage - } - - def 'add contact'() { - when: 'add a contact' - addContact - then: 'The add page is displayed' - at ContactsPage - and: 'The new contact is displayed' - contacts.find { it.email == 'rob@example.com' }?.name == 'Rob Winch' - } - - def 'delete contact'() { - when: 'delete a contact' - contacts.find { it.email == 'rob@example.com' }.delete() - then: 'Delete confirmation displayed' - at DeleteConfirmPage - when: 'View Manage Page' - manage.click() - then: 'New contact has been removed' - !contacts.find { it.email == 'rob@example.com' } - } - - def 'authenticated user logs out'() { - when: 'user logs out' - logout.click() - then: 'the default logout success page is displayed' - at HomePage - when: 'Unauthenticated user accesses the Manage Page' - via ContactsPage - then: 'The login page is displayed' - at LoginPage - } -} diff --git a/samples/xml/contacts/src/integration-test/groovy/org/springframework/security/samples/pages/AddPage.groovy b/samples/xml/contacts/src/integration-test/groovy/org/springframework/security/samples/pages/AddPage.groovy deleted file mode 100644 index 2988d4ac421..00000000000 --- a/samples/xml/contacts/src/integration-test/groovy/org/springframework/security/samples/pages/AddPage.groovy +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2002-2011 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.security.samples.pages - -import geb.Page - -/** - * The login page. - * - * @author Rob Winch - */ -class AddPage extends Page { - static url = 'add' - static at = { assert driver.title == 'Add New Contact'; true} - static content = { - addContact(required:false) { name = 'Rob Winch', email = 'rob@example.com'-> - addForm.name = name - addForm.email = email - submit.click() - } - addForm { $('form') } - submit { $('input', type: 'submit') } - } -} diff --git a/samples/xml/contacts/src/integration-test/groovy/org/springframework/security/samples/pages/ContactsPage.groovy b/samples/xml/contacts/src/integration-test/groovy/org/springframework/security/samples/pages/ContactsPage.groovy deleted file mode 100644 index 607bd99073a..00000000000 --- a/samples/xml/contacts/src/integration-test/groovy/org/springframework/security/samples/pages/ContactsPage.groovy +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2002-2011 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.security.samples.pages - -import geb.* - -/** - * The home page - * - * @author Rob Winch - */ -class ContactsPage extends Page { - static url = 'secure/' - static at = { assert driver.title == 'Your Contacts'; true} - static content = { - addContact(to: AddPage) { $('a', text: 'Add') } - contacts { moduleList Contact, $("table tr").tail() } - logout { $("input[type=submit]", value: "Logoff") } - } -} - -class Contact extends Module { - static content = { - cell { $("td", it) } - id { cell(0).text().toInteger() } - name { cell(1).text() } - email { cell(2).text() } - delete { cell(3).$('a').click() } - adminPermission { cell(4).$('a') } - } -} diff --git a/samples/xml/contacts/src/integration-test/groovy/org/springframework/security/samples/pages/DeleteConfirmPage.groovy b/samples/xml/contacts/src/integration-test/groovy/org/springframework/security/samples/pages/DeleteConfirmPage.groovy deleted file mode 100644 index ded6ea964ae..00000000000 --- a/samples/xml/contacts/src/integration-test/groovy/org/springframework/security/samples/pages/DeleteConfirmPage.groovy +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2002-2011 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.security.samples.pages - -import geb.Page - -/** - * The home page - * - * @author Rob Winch - */ -class DeleteConfirmPage extends Page { - static at = { assert driver.title == 'Deletion completed'; true} - static content = { - manage(to: ContactsPage) { $('a', text: 'Manage') } - } -} diff --git a/samples/xml/contacts/src/integration-test/groovy/org/springframework/security/samples/pages/HomePage.groovy b/samples/xml/contacts/src/integration-test/groovy/org/springframework/security/samples/pages/HomePage.groovy deleted file mode 100644 index cf26ec895ba..00000000000 --- a/samples/xml/contacts/src/integration-test/groovy/org/springframework/security/samples/pages/HomePage.groovy +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2002-2011 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.security.samples.pages; - -import geb.* - -/** - * The home page - * - * @author Rob Winch - */ -class HomePage extends Page { - static url = '' - static at = { assert driver.title == 'Contacts Security Demo'; true} - static content = { - manage(to: [ContactsPage,LoginPage]) { $('a', text: 'Manage') } - debug { $('a', text: 'Debug').click() } - frames { $('a', text: 'Frames').click() } - } -} diff --git a/samples/xml/contacts/src/integration-test/groovy/org/springframework/security/samples/pages/LoginPage.groovy b/samples/xml/contacts/src/integration-test/groovy/org/springframework/security/samples/pages/LoginPage.groovy deleted file mode 100644 index 609ccce10a4..00000000000 --- a/samples/xml/contacts/src/integration-test/groovy/org/springframework/security/samples/pages/LoginPage.groovy +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2002-2011 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.security.samples.pages; - -import geb.* - -/** - * The login page. - * - * @author Rob Winch - */ -class LoginPage extends Page { - static url = 'login' - static at = { assert driver.title == 'Login'; true} - static content = { - login(required:false) { user='rod', password='koala' -> - loginForm.username = user - loginForm.password = password - submit.click() - } - loginForm { $('form') } - submit { $('input', type: 'submit') } - } -} diff --git a/samples/xml/contacts/src/integration-test/java/org/springframework/security/samples/ContactsTests.java b/samples/xml/contacts/src/integration-test/java/org/springframework/security/samples/ContactsTests.java new file mode 100644 index 00000000000..ecc1e4ab987 --- /dev/null +++ b/samples/xml/contacts/src/integration-test/java/org/springframework/security/samples/ContactsTests.java @@ -0,0 +1,92 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.security.samples; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.htmlunit.HtmlUnitDriver; +import org.springframework.security.samples.pages.ContactsPage; +import org.springframework.security.samples.pages.HomePage; + +/** + * @author Michael Simons + */ +public class ContactsTests { + private WebDriver driver; + + private int port; + + @Before + public void setup() { + this.port = Integer.parseInt(System.getProperty("app.httpPort")); + this.driver = new HtmlUnitDriver(); + } + + @After + public void tearDown() { + this.driver.quit(); + } + + @Test + public void accessHomePageWithUnauthenticatedUserSuccess() { + final HomePage homePage = HomePage.to(this.driver, this.port); + homePage.assertAt(); + } + + @Test + public void authenticatedUserCanAddContacts() { + final String name = "Rob Winch"; + final String email = "rob@example.com"; + + // @formatter:off + ContactsPage.accessManagePageWithUnauthenticatedUser(this.driver, this.port) + .sendsToLoginPage() + .username("rod") + .password("koala") + .submit() + .isAtContactsPage() + .addContact() + .name(name) + .email(email) + .submit() + .andHasContact(name, email) + .delete() + .andConfirmDeletion() + .isAtContactsPage() + .andConctactHasBeenRemoved(name, email); + // @formatter:on + } + + + @Test + public void authenticatedUserLogsOut() { + // @formatter:off + final HomePage homePage = ContactsPage.accessManagePageWithUnauthenticatedUser(this.driver, this.port) + .sendsToLoginPage() + .username("rod") + .password("koala") + .submit() + .isAtContactsPage() + .logout(); + // @formatter:on + homePage.assertAt(); + + ContactsPage.accessManagePageWithUnauthenticatedUser(this.driver, this.port) + .sendsToLoginPage(); + } +} diff --git a/samples/xml/contacts/src/integration-test/java/org/springframework/security/samples/pages/AddPage.java b/samples/xml/contacts/src/integration-test/java/org/springframework/security/samples/pages/AddPage.java new file mode 100644 index 00000000000..1ef9239857c --- /dev/null +++ b/samples/xml/contacts/src/integration-test/java/org/springframework/security/samples/pages/AddPage.java @@ -0,0 +1,70 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.security.samples.pages; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Michael Simons + */ +public class AddPage { + + private final WebDriver webDriver; + + private final AddForm addForm; + + public AddPage(WebDriver webDriver) { + this.webDriver = webDriver; + this.addForm = PageFactory.initElements(this.webDriver, AddForm.class); + } + + AddForm addForm() { + assertThat(this.webDriver.getTitle()).isEqualTo("Add New Contact"); + return this.addForm; + } + + public static class AddForm { + private WebDriver webDriver; + private WebElement name; + private WebElement email; + @FindBy(css = "input[type=submit]") + private WebElement submit; + + public AddForm(WebDriver webDriver) { + this.webDriver = webDriver; + } + + public AddForm name(String name) { + this.name.sendKeys(name); + return this; + } + + public AddForm email(String email) { + this.email.sendKeys(email); + return this; + } + + public ContactsPage submit() { + this.submit.click(); + return PageFactory.initElements(this.webDriver, ContactsPage.class); + } + } +} diff --git a/samples/xml/contacts/src/integration-test/java/org/springframework/security/samples/pages/ContactsPage.java b/samples/xml/contacts/src/integration-test/java/org/springframework/security/samples/pages/ContactsPage.java new file mode 100644 index 00000000000..0821e52bf12 --- /dev/null +++ b/samples/xml/contacts/src/integration-test/java/org/springframework/security/samples/pages/ContactsPage.java @@ -0,0 +1,130 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.security.samples.pages; + +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; +import org.springframework.security.samples.pages.AddPage.AddForm; + +import java.util.List; +import java.util.function.Predicate; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * The contacts / manage page. + * + * @author Michael Simons + */ +public class ContactsPage { + public static LoginPage accessManagePageWithUnauthenticatedUser(WebDriver driver, int port) { + driver.get("http://localhost:" + port +"/secure/"); + return PageFactory.initElements(driver, LoginPage.class); + } + + private final WebDriver webDriver; + + @FindBy(linkText = "Add") + private WebElement a; + + @FindBy(css = "table tr") + private List contacts; + + @FindBy(xpath = "//input[@type='submit' and @value='Logoff']") + private WebElement logout; + + public ContactsPage(WebDriver webDriver) { + this.webDriver = webDriver; + } + + public ContactsPage isAtContactsPage() { + assertThat(this.webDriver.getTitle()).isEqualTo("Your Contacts"); + return this; + } + + public AddForm addContact() { + a.click(); + final AddPage addPage = PageFactory.initElements(this.webDriver, AddPage.class); + return addPage.addForm(); + } + + Predicate byEmail(final String val) { + return e -> e.findElements(By.xpath("td[position()=3 and normalize-space()='" + val + "']")).size() == 1; + } + + Predicate byName(final String val) { + return e -> e.findElements(By.xpath("td[position()=2 and normalize-space()='" + val + "']")).size() == 1; + } + + public DeleteContactLink andHasContact(final String name, final String email) { + return this.contacts.stream() + .filter(byEmail(email).and(byName(name))) + .map(e -> e.findElement(By.cssSelector("td:nth-child(4) > a"))) + .findFirst() + .map(e -> new DeleteContactLink(webDriver, e)) + .get(); + } + + public ContactsPage andConctactHasBeenRemoved(final String name, final String email) { + assertThat(this.contacts.stream() + .filter(byEmail(email).and(byName(name))) + .findAny()).isEmpty(); + return this; + } + + public HomePage logout() { + this.logout.click(); + return PageFactory.initElements(this.webDriver, HomePage.class); + } + + public static class DeleteContactLink { + + private final WebDriver webDriver; + + private final WebElement a; + + public DeleteContactLink(WebDriver webDriver, WebElement a) { + this.webDriver = webDriver; + this.a = a; + } + + public DeleteConfirmationPage delete() { + this.a.click(); + return PageFactory.initElements(this.webDriver, DeleteConfirmationPage.class); + } + } + + public static class DeleteConfirmationPage { + private final WebDriver webDriver; + + @FindBy(linkText = "Manage") + private WebElement a; + + + public DeleteConfirmationPage(WebDriver webDriver) { + this.webDriver = webDriver; + } + + public ContactsPage andConfirmDeletion() { + assertThat(this.webDriver.getTitle()).isEqualTo("Deletion completed"); + this.a.click(); + return PageFactory.initElements(this.webDriver, ContactsPage.class); + } + } +} diff --git a/samples/xml/contacts/src/integration-test/java/org/springframework/security/samples/pages/HomePage.java b/samples/xml/contacts/src/integration-test/java/org/springframework/security/samples/pages/HomePage.java new file mode 100644 index 00000000000..be0a3628a98 --- /dev/null +++ b/samples/xml/contacts/src/integration-test/java/org/springframework/security/samples/pages/HomePage.java @@ -0,0 +1,69 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.security.samples.pages; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; +import org.springframework.security.samples.pages.LoginPage; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * The home page. + * + * @author Michael Simons + */ +public class HomePage { + + public static HomePage to(WebDriver driver, int port) { + driver.get("http://localhost:" + port +"/"); + return PageFactory.initElements(driver, HomePage.class); + } + + private final WebDriver webDriver; + + @FindBy(css = "p") + private WebElement message; + + @FindBy(css = "input[type=submit]") + private WebElement logoutButton; + + public HomePage(WebDriver webDriver) { + this.webDriver = webDriver; + } + + public Content assertAt() { + assertThat(this.webDriver.getTitle()).isEqualTo("Contacts Security Demo"); + return PageFactory.initElements(this.webDriver, Content.class); + } + + public LoginPage logout() { + this.logoutButton.submit(); + return PageFactory.initElements(this.webDriver, LoginPage.class); + } + + public static class Content { + @FindBy(css = "p") + private WebElement message; + + public Content andTheUserNameIsDisplayed() { + assertThat(message.getText()).isEqualTo("Hello user"); + return this; + } + } +} diff --git a/samples/xml/contacts/src/integration-test/java/org/springframework/security/samples/pages/LoginPage.java b/samples/xml/contacts/src/integration-test/java/org/springframework/security/samples/pages/LoginPage.java new file mode 100644 index 00000000000..75ad20dc4c5 --- /dev/null +++ b/samples/xml/contacts/src/integration-test/java/org/springframework/security/samples/pages/LoginPage.java @@ -0,0 +1,72 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.security.samples.pages; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * The login page. + * + * @author Michael Simons + */ +public class LoginPage { + + private final WebDriver webDriver; + + private final LoginForm loginForm; + + public LoginPage(WebDriver webDriver) { + this.webDriver = webDriver; + this.loginForm = PageFactory.initElements(this.webDriver, LoginForm.class); + } + + public LoginForm sendsToLoginPage() { + assertThat(this.webDriver.getTitle()).isEqualTo("Login"); + return this.loginForm; + } + + public static class LoginForm { + private WebDriver webDriver; + private WebElement username; + private WebElement password; + @FindBy(css = "input[type=submit]") + private WebElement submit; + + public LoginForm(WebDriver webDriver) { + this.webDriver = webDriver; + } + + public LoginForm username(String username) { + this.username.sendKeys(username); + return this; + } + + public LoginForm password(String password) { + this.password.sendKeys(password); + return this; + } + + public ContactsPage submit() { + this.submit.click(); + return PageFactory.initElements(this.webDriver, ContactsPage.class); + } + } +} diff --git a/samples/xml/insecure/spring-security-samples-xml-insecure.gradle b/samples/xml/insecure/spring-security-samples-xml-insecure.gradle index 97106699751..6e9e855f1a5 100644 --- a/samples/xml/insecure/spring-security-samples-xml-insecure.gradle +++ b/samples/xml/insecure/spring-security-samples-xml-insecure.gradle @@ -1,3 +1,19 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + apply plugin: 'io.spring.convention.spring-sample-war' dependencies { @@ -7,5 +23,5 @@ dependencies { providedCompile 'javax.servlet:javax.servlet-api' providedCompile 'javax.servlet.jsp:javax.servlet.jsp-api' - integrationTestCompile gebDependencies + integrationTestCompile seleniumDependencies } diff --git a/samples/xml/insecure/src/integration-test/groovy/org/springframework/security/samples/HelloInsecureTests.groovy b/samples/xml/insecure/src/integration-test/groovy/org/springframework/security/samples/HelloInsecureTests.groovy deleted file mode 100644 index a7d0d3101d6..00000000000 --- a/samples/xml/insecure/src/integration-test/groovy/org/springframework/security/samples/HelloInsecureTests.groovy +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2002-2011 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.security.samples - -import geb.spock.* -import spock.lang.Stepwise -import org.springframework.security.samples.pages.* - -/** - * - * @author Rob Winch - */ -@Stepwise -class HelloInsecureTests extends GebReportingSpec { - def 'The HomePage is accessible'() { - when: 'Unauthenticated user accesses the Home Page' - to HomePage - then: 'The HomePage is displayed' - at HomePage - and: 'We can see the message' - message == 'We would like to secure this page' - } -} diff --git a/samples/xml/insecure/src/integration-test/groovy/org/springframework/security/samples/pages/HomePage.groovy b/samples/xml/insecure/src/integration-test/groovy/org/springframework/security/samples/pages/HomePage.groovy deleted file mode 100644 index afdb5eb7d62..00000000000 --- a/samples/xml/insecure/src/integration-test/groovy/org/springframework/security/samples/pages/HomePage.groovy +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2002-2011 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.security.samples.pages; - -import geb.* - -/** - * The home page - * - * @author Rob Winch - */ -class HomePage extends Page { - static url = '' - static at = { assert driver.title == 'Hello World'; true} - static content = { - message { $('p').text() } - } -} diff --git a/samples/xml/insecure/src/integration-test/java/org/springframework/security/samples/HelloInsecureTests.java b/samples/xml/insecure/src/integration-test/java/org/springframework/security/samples/HelloInsecureTests.java new file mode 100644 index 00000000000..715e2b7711b --- /dev/null +++ b/samples/xml/insecure/src/integration-test/java/org/springframework/security/samples/HelloInsecureTests.java @@ -0,0 +1,51 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.security.samples; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.htmlunit.HtmlUnitDriver; +import org.springframework.security.samples.pages.HomePage; + +/** + * @author Michael Simons + */ +public class HelloInsecureTests { + + private WebDriver driver; + + private int port; + + @Before + public void setup() { + this.port = Integer.parseInt(System.getProperty("app.httpPort")); + this.driver = new HtmlUnitDriver(); + } + + @After + public void tearDown() { + this.driver.quit(); + } + + @Test + public void theHomePageIsAccessible() { + HomePage.to(this.driver, this.port) + .assertAt() + .andWeCanSeeTheMessage(); + } +} diff --git a/samples/xml/insecure/src/integration-test/java/org/springframework/security/samples/pages/HomePage.java b/samples/xml/insecure/src/integration-test/java/org/springframework/security/samples/pages/HomePage.java new file mode 100644 index 00000000000..b8d11bdf935 --- /dev/null +++ b/samples/xml/insecure/src/integration-test/java/org/springframework/security/samples/pages/HomePage.java @@ -0,0 +1,54 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.security.samples.pages; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Michael Simons + */ +public class HomePage { + private final WebDriver webDriver; + + public HomePage(WebDriver webDriver) { + this.webDriver = webDriver; + } + + public static HomePage to(WebDriver driver, int port) { + driver.get("http://localhost:" + port +"/"); + return PageFactory.initElements(driver, HomePage.class); + } + + public Content assertAt() { + assertThat(this.webDriver.getTitle()).isEqualTo("Hello World"); + return PageFactory.initElements(this.webDriver, Content.class); + } + + public static class Content { + @FindBy(css = "p") + private WebElement message; + + public Content andWeCanSeeTheMessage() { + assertThat(message.getText()).isEqualTo("We would like to secure this page"); + return this; + } + } +} diff --git a/samples/xml/ldap/spring-security-samples-xml-ldap.gradle b/samples/xml/ldap/spring-security-samples-xml-ldap.gradle index 5fc778106e4..ef5de17fdcf 100644 --- a/samples/xml/ldap/spring-security-samples-xml-ldap.gradle +++ b/samples/xml/ldap/spring-security-samples-xml-ldap.gradle @@ -1,3 +1,18 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ apply plugin: 'io.spring.convention.spring-sample-war' dependencies { @@ -12,5 +27,5 @@ dependencies { providedCompile 'javax.servlet:javax.servlet-api' - integrationTestCompile gebDependencies + integrationTestCompile seleniumDependencies } diff --git a/samples/xml/ldap/src/integration-test/groovy/org/springframework/security/samples/LdapXmlTests.groovy b/samples/xml/ldap/src/integration-test/groovy/org/springframework/security/samples/LdapXmlTests.groovy deleted file mode 100644 index 8cf2fec7603..00000000000 --- a/samples/xml/ldap/src/integration-test/groovy/org/springframework/security/samples/LdapXmlTests.groovy +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2002-2011 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.security.samples - -import geb.spock.* -import spock.lang.Stepwise -import org.springframework.security.samples.pages.* - -/** - * Tests the CAS sample application using service tickets. - * - * @author Rob Winch - */ -@Stepwise -class LdapXmlTests extends GebReportingSpec { - def 'access home page with unauthenticated user success'() { - when: 'Unauthenticated user accesses the Home Page' - to HomePage - then: 'The page is displayed' - at HomePage - } - - def 'access manage page with unauthenticated user sends to login page'() { - when: 'Unauthenticated user accesses the Manage Page' - secure.click(LoginPage) - then: 'The login page is displayed' - at LoginPage - } - - def 'authenticated user is sent to original page'() { - when: 'user authenticates' - login() - then: 'The secure page is displayed' - at SecurePage - } - - def 'authenticated user logs out'() { - when: 'user logs out' - logout.click() - then: 'the default logout success page is displayed' - at LogoutPage - when: 'Unauthenticated user accesses the Manage Page' - via SecurePage - then: 'The login page is displayed' - at LoginPage - } -} diff --git a/samples/xml/ldap/src/integration-test/groovy/org/springframework/security/samples/pages/HomePage.groovy b/samples/xml/ldap/src/integration-test/groovy/org/springframework/security/samples/pages/HomePage.groovy deleted file mode 100644 index 9505292b28e..00000000000 --- a/samples/xml/ldap/src/integration-test/groovy/org/springframework/security/samples/pages/HomePage.groovy +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2002-2011 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.security.samples.pages; - -import geb.* - -/** - * The home page - * - * @author Rob Winch - */ -class HomePage extends Page { - static url = '' - static at = { assert driver.title == 'Home Page'; true} - static content = { - secure(to: [SecurePage,LoginPage]) { $('a', text: 'Secure page') } - } -} diff --git a/samples/xml/ldap/src/integration-test/groovy/org/springframework/security/samples/pages/LoginPage.groovy b/samples/xml/ldap/src/integration-test/groovy/org/springframework/security/samples/pages/LoginPage.groovy deleted file mode 100644 index 8e56afcb024..00000000000 --- a/samples/xml/ldap/src/integration-test/groovy/org/springframework/security/samples/pages/LoginPage.groovy +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2002-2011 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.security.samples.pages; - -import geb.* - -/** - * The login page. - * - * @author Rob Winch - */ -class LoginPage extends Page { - static url = 'login' - static at = { assert driver.title == 'Login Page'; true} - static content = { - login(required:false) { user='rod', password='koala' -> - loginForm.username = user - loginForm.password = password - submit.click() - } - loginForm { $('form') } - submit { $('input', type: 'submit') } - } -} diff --git a/samples/xml/ldap/src/integration-test/groovy/org/springframework/security/samples/pages/LogoutPage.groovy b/samples/xml/ldap/src/integration-test/groovy/org/springframework/security/samples/pages/LogoutPage.groovy deleted file mode 100644 index 4b14b4a625a..00000000000 --- a/samples/xml/ldap/src/integration-test/groovy/org/springframework/security/samples/pages/LogoutPage.groovy +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2002-2011 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.security.samples.pages -/** - * The login page. - * - * @author Rob Winch - */ -class LogoutPage extends LoginPage { - static url = 'login' - static at = { assert driver.title == 'Login Page' && $('p').text() == 'You have been logged out'; true} -} diff --git a/samples/xml/ldap/src/integration-test/groovy/org/springframework/security/samples/pages/SecurePage.groovy b/samples/xml/ldap/src/integration-test/groovy/org/springframework/security/samples/pages/SecurePage.groovy deleted file mode 100644 index 5ba6cafab03..00000000000 --- a/samples/xml/ldap/src/integration-test/groovy/org/springframework/security/samples/pages/SecurePage.groovy +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2002-2011 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.security.samples.pages - -import geb.* - -/** - * The home page - * - * @author Rob Winch - */ -class SecurePage extends Page { - static url = 'secure/' - static at = { assert driver.title == 'Secure Page'; true} - static content = { - logout { $("input[type=submit]", value: "Logoff") } - } -} diff --git a/samples/xml/ldap/src/integration-test/java/org/springframework/security/samples/LdapXmlTests.java b/samples/xml/ldap/src/integration-test/java/org/springframework/security/samples/LdapXmlTests.java new file mode 100644 index 00000000000..6fd18d29161 --- /dev/null +++ b/samples/xml/ldap/src/integration-test/java/org/springframework/security/samples/LdapXmlTests.java @@ -0,0 +1,83 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.security.samples; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.htmlunit.HtmlUnitDriver; +import org.springframework.security.samples.pages.HomePage; +import org.springframework.security.samples.pages.LoginPage; +import org.springframework.security.samples.pages.LogoutPage; +import org.springframework.security.samples.pages.SecurePage; + +/** + * @author Michael Simons + */ +public class LdapXmlTests { + private WebDriver driver; + + private int port; + + @Before + public void setup() { + this.port = Integer.parseInt(System.getProperty("app.httpPort")); + this.driver = new HtmlUnitDriver(); + } + + @After + public void tearDown() { + this.driver.quit(); + } + + @Test + public void accessHomepageWithUnauthenticatedUserSuccess() { + final HomePage homePage = HomePage.to(this.driver, this.port); + homePage.assertAt(); + } + + @Test + public void accessManagePageWithUnauthenticatedUserSendsToLoginPage() { + final LoginPage loginPage = SecurePage.to(this.driver, this.port); + loginPage.assertAt(); + } + + @Test + public void authenticatedUserIsSentToOriginalPage() { + final SecurePage securePage = SecurePage.to(this.driver, this.port) + .loginForm() + .username("rod") + .password("koala") + .submit(); + securePage + .assertAt(); + } + + @Test + public void authenticatedUserLogsOut() { + final LogoutPage logoutPage = SecurePage.to(this.driver, this.port) + .loginForm() + .username("rod") + .password("koala") + .submit() + .logout(); + logoutPage.assertAt(); + + final LoginPage loginPage = SecurePage.to(this.driver, this.port); + loginPage.assertAt(); + } +} diff --git a/samples/xml/ldap/src/integration-test/java/org/springframework/security/samples/pages/HomePage.java b/samples/xml/ldap/src/integration-test/java/org/springframework/security/samples/pages/HomePage.java new file mode 100644 index 00000000000..c26f50c8889 --- /dev/null +++ b/samples/xml/ldap/src/integration-test/java/org/springframework/security/samples/pages/HomePage.java @@ -0,0 +1,68 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.security.samples.pages; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * The home page. + * + * @author Michael Simons + */ +public class HomePage { + + public static HomePage to(WebDriver driver, int port) { + driver.get("http://localhost:" + port +"/"); + return PageFactory.initElements(driver, HomePage.class); + } + + private final WebDriver webDriver; + + @FindBy(css = "p") + private WebElement message; + + @FindBy(css = "input[type=submit]") + private WebElement logoutButton; + + public HomePage(WebDriver webDriver) { + this.webDriver = webDriver; + } + + public Content assertAt() { + assertThat(this.webDriver.getTitle()).isEqualTo("Home Page"); + return PageFactory.initElements(this.webDriver, Content.class); + } + + public LoginPage logout() { + this.logoutButton.submit(); + return PageFactory.initElements(this.webDriver, LoginPage.class); + } + + public static class Content { + @FindBy(css = "p") + private WebElement message; + + public Content andTheUserNameIsDisplayed() { + assertThat(message.getText()).isEqualTo("Hello user"); + return this; + } + } +} diff --git a/samples/xml/ldap/src/integration-test/java/org/springframework/security/samples/pages/LoginPage.java b/samples/xml/ldap/src/integration-test/java/org/springframework/security/samples/pages/LoginPage.java new file mode 100644 index 00000000000..6a13e93eca8 --- /dev/null +++ b/samples/xml/ldap/src/integration-test/java/org/springframework/security/samples/pages/LoginPage.java @@ -0,0 +1,76 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.security.samples.pages; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * The login page. + * + * @author Michael Simons + */ +public class LoginPage { + + private final WebDriver webDriver; + + private final LoginForm loginForm; + + public LoginPage(WebDriver webDriver) { + this.webDriver = webDriver; + this.loginForm = PageFactory.initElements(this.webDriver, LoginForm.class); + } + + public LoginPage assertAt() { + assertThat(this.webDriver.getTitle()).isEqualTo("Login Page"); + return this; + } + + public LoginForm loginForm() { + return this.loginForm; + } + + public static class LoginForm { + private WebDriver webDriver; + private WebElement username; + private WebElement password; + @FindBy(css = "input[type=submit]") + private WebElement submit; + + public LoginForm(WebDriver webDriver) { + this.webDriver = webDriver; + } + + public LoginForm username(String username) { + this.username.sendKeys(username); + return this; + } + + public LoginForm password(String password) { + this.password.sendKeys(password); + return this; + } + + public SecurePage submit() { + this.submit.click(); + return PageFactory.initElements(this.webDriver, SecurePage.class); + } + } +} diff --git a/samples/javaconfig/helloworld/src/integration-test/groovy/org/springframework/security/samples/pages/LoginPage.groovy b/samples/xml/ldap/src/integration-test/java/org/springframework/security/samples/pages/LogoutPage.java similarity index 50% rename from samples/javaconfig/helloworld/src/integration-test/groovy/org/springframework/security/samples/pages/LoginPage.groovy rename to samples/xml/ldap/src/integration-test/java/org/springframework/security/samples/pages/LogoutPage.java index f67f9547f25..abaf6ccdb3f 100644 --- a/samples/javaconfig/helloworld/src/integration-test/groovy/org/springframework/security/samples/pages/LoginPage.groovy +++ b/samples/xml/ldap/src/integration-test/java/org/springframework/security/samples/pages/LogoutPage.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,23 +15,30 @@ */ package org.springframework.security.samples.pages; -import geb.* +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; + +import static org.assertj.core.api.Assertions.assertThat; /** - * The login page. + * Logout Page is the same as login page with an additional message. * - * @author Rob Winch + * @author Michael Simons */ -class LoginPage extends Page { - static url = 'login' - static at = { assert driver.title == 'Login Page'; true} - static content = { - login(required:false) { user='user', password='password' -> - loginForm.username = user - loginForm.password = password - submit.click() - } - loginForm { $('form') } - submit { $('input', type: 'submit') } +public class LogoutPage extends LoginPage { + @FindBy(css = "p") + private WebElement p; + + public LogoutPage(WebDriver webDriver) { + super(webDriver); + } + + @Override + public LogoutPage assertAt() { + super.assertAt(); + + assertThat(p.getText()).isEqualTo("You have been logged out"); + return this; } } diff --git a/samples/xml/ldap/src/integration-test/java/org/springframework/security/samples/pages/SecurePage.java b/samples/xml/ldap/src/integration-test/java/org/springframework/security/samples/pages/SecurePage.java new file mode 100644 index 00000000000..7fd90a16a94 --- /dev/null +++ b/samples/xml/ldap/src/integration-test/java/org/springframework/security/samples/pages/SecurePage.java @@ -0,0 +1,55 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.security.samples.pages; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * A secure page. + * + * @author Michael Simons + */ +public class SecurePage { + + public static LoginPage to(WebDriver driver, int port) { + driver.get("http://localhost:" + port + "/secure"); + return PageFactory.initElements(driver, LoginPage.class); + }; + + private final WebDriver webDriver; + + @FindBy(css = "input[type=submit]") + private WebElement submit; + + public SecurePage(WebDriver webDriver) { + this.webDriver = webDriver; + } + + public SecurePage assertAt() { + assertThat(this.webDriver.getTitle()).isEqualTo("Secure Page"); + return this; + } + + public LogoutPage logout() { + this.submit.click(); + return PageFactory.initElements(this.webDriver, LogoutPage.class); + } +}