KEYCLOAK-1823

Annoying behaviour of validations in user registration form
This commit is contained in:
Stian Thorgersen 2015-10-13 07:28:01 +02:00
parent c987d57b9e
commit 85a886da18
2 changed files with 59 additions and 35 deletions

View file

@ -59,7 +59,6 @@ public class RegistrationProfile implements FormAction, FormActionFactory {
if (Validation.isBlank(email)) { if (Validation.isBlank(email)) {
errors.add(new FormMessage(RegistrationPage.FIELD_EMAIL, Messages.MISSING_EMAIL)); errors.add(new FormMessage(RegistrationPage.FIELD_EMAIL, Messages.MISSING_EMAIL));
} else if (!Validation.isEmailValid(email)) { } else if (!Validation.isEmailValid(email)) {
formData.remove(Validation.FIELD_EMAIL);
context.getEvent().detail(Details.EMAIL, email); context.getEvent().detail(Details.EMAIL, email);
errors.add(new FormMessage(RegistrationPage.FIELD_EMAIL, Messages.INVALID_EMAIL)); errors.add(new FormMessage(RegistrationPage.FIELD_EMAIL, Messages.INVALID_EMAIL));
} }

View file

@ -21,10 +21,7 @@
*/ */
package org.keycloak.testsuite.forms; package org.keycloak.testsuite.forms;
import org.junit.Assert; import org.junit.*;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.keycloak.events.Details; import org.keycloak.events.Details;
import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSession;
import org.keycloak.models.PasswordPolicy; import org.keycloak.models.PasswordPolicy;
@ -42,6 +39,8 @@ import org.keycloak.testsuite.rule.WebResource;
import org.keycloak.testsuite.rule.WebRule; import org.keycloak.testsuite.rule.WebRule;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebDriver;
import static org.junit.Assert.assertEquals;
/** /**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a> * @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/ */
@ -80,15 +79,15 @@ public class RegisterTest {
registerPage.register("firstName", "lastName", "registerExistingUser@email", "test-user@localhost", "password", "password"); registerPage.register("firstName", "lastName", "registerExistingUser@email", "test-user@localhost", "password", "password");
registerPage.assertCurrent(); registerPage.assertCurrent();
Assert.assertEquals("Username already exists.", registerPage.getError()); assertEquals("Username already exists.", registerPage.getError());
// assert form keeps form fields on error // assert form keeps form fields on error
Assert.assertEquals("firstName", registerPage.getFirstName()); assertEquals("firstName", registerPage.getFirstName());
Assert.assertEquals("lastName", registerPage.getLastName()); assertEquals("lastName", registerPage.getLastName());
Assert.assertEquals("", registerPage.getEmail()); assertEquals("", registerPage.getEmail());
Assert.assertEquals("", registerPage.getUsername()); assertEquals("", registerPage.getUsername());
Assert.assertEquals("", registerPage.getPassword()); assertEquals("", registerPage.getPassword());
Assert.assertEquals("", registerPage.getPasswordConfirm()); assertEquals("", registerPage.getPasswordConfirm());
events.expectRegister("test-user@localhost", "registerExistingUser@email") events.expectRegister("test-user@localhost", "registerExistingUser@email")
.removeDetail(Details.EMAIL) .removeDetail(Details.EMAIL)
@ -104,15 +103,15 @@ public class RegisterTest {
registerPage.register("firstName", "lastName", "registerUserInvalidPasswordConfirm@email", "registerUserInvalidPasswordConfirm", "password", "invalid"); registerPage.register("firstName", "lastName", "registerUserInvalidPasswordConfirm@email", "registerUserInvalidPasswordConfirm", "password", "invalid");
registerPage.assertCurrent(); registerPage.assertCurrent();
Assert.assertEquals("Password confirmation doesn't match.", registerPage.getError()); assertEquals("Password confirmation doesn't match.", registerPage.getError());
// assert form keeps form fields on error // assert form keeps form fields on error
Assert.assertEquals("firstName", registerPage.getFirstName()); assertEquals("firstName", registerPage.getFirstName());
Assert.assertEquals("lastName", registerPage.getLastName()); assertEquals("lastName", registerPage.getLastName());
Assert.assertEquals("registerUserInvalidPasswordConfirm@email", registerPage.getEmail()); assertEquals("registerUserInvalidPasswordConfirm@email", registerPage.getEmail());
Assert.assertEquals("registerUserInvalidPasswordConfirm", registerPage.getUsername()); assertEquals("registerUserInvalidPasswordConfirm", registerPage.getUsername());
Assert.assertEquals("", registerPage.getPassword()); assertEquals("", registerPage.getPassword());
Assert.assertEquals("", registerPage.getPasswordConfirm()); assertEquals("", registerPage.getPasswordConfirm());
events.expectRegister("registerUserInvalidPasswordConfirm", "registerUserInvalidPasswordConfirm@email") events.expectRegister("registerUserInvalidPasswordConfirm", "registerUserInvalidPasswordConfirm@email")
.removeDetail(Details.USERNAME) .removeDetail(Details.USERNAME)
@ -129,7 +128,7 @@ public class RegisterTest {
registerPage.register("firstName", "lastName", "registerUserMissingPassword@email", "registerUserMissingPassword", null, null); registerPage.register("firstName", "lastName", "registerUserMissingPassword@email", "registerUserMissingPassword", null, null);
registerPage.assertCurrent(); registerPage.assertCurrent();
Assert.assertEquals("Please specify password.", registerPage.getError()); assertEquals("Please specify password.", registerPage.getError());
events.expectRegister("registerUserMissingPassword", "registerUserMissingPassword@email") events.expectRegister("registerUserMissingPassword", "registerUserMissingPassword@email")
.removeDetail(Details.USERNAME) .removeDetail(Details.USERNAME)
@ -154,7 +153,7 @@ public class RegisterTest {
registerPage.register("firstName", "lastName", "registerPasswordPolicy@email", "registerPasswordPolicy", "pass", "pass"); registerPage.register("firstName", "lastName", "registerPasswordPolicy@email", "registerPasswordPolicy", "pass", "pass");
registerPage.assertCurrent(); registerPage.assertCurrent();
Assert.assertEquals("Invalid password: minimum length 8.", registerPage.getError()); assertEquals("Invalid password: minimum length 8.", registerPage.getError());
events.expectRegister("registerPasswordPolicy", "registerPasswordPolicy@email") events.expectRegister("registerPasswordPolicy", "registerPasswordPolicy@email")
.removeDetail(Details.USERNAME) .removeDetail(Details.USERNAME)
@ -162,7 +161,7 @@ public class RegisterTest {
.user((String) null).error("invalid_registration").assertEvent(); .user((String) null).error("invalid_registration").assertEvent();
registerPage.register("firstName", "lastName", "registerPasswordPolicy@email", "registerPasswordPolicy", "password", "password"); registerPage.register("firstName", "lastName", "registerPasswordPolicy@email", "registerPasswordPolicy", "password", "password");
Assert.assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType()); assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType());
String userId = events.expectRegister("registerPasswordPolicy", "registerPasswordPolicy@email").assertEvent().getUserId(); String userId = events.expectRegister("registerPasswordPolicy", "registerPasswordPolicy@email").assertEvent().getUserId();
@ -186,7 +185,7 @@ public class RegisterTest {
registerPage.register("firstName", "lastName", "registerUserMissingUsername@email", null, "password", "password"); registerPage.register("firstName", "lastName", "registerUserMissingUsername@email", null, "password", "password");
registerPage.assertCurrent(); registerPage.assertCurrent();
Assert.assertEquals("Please specify username.", registerPage.getError()); assertEquals("Please specify username.", registerPage.getError());
events.expectRegister(null, "registerUserMissingUsername@email") events.expectRegister(null, "registerUserMissingUsername@email")
.removeDetail(Details.USERNAME) .removeDetail(Details.USERNAME)
@ -195,21 +194,47 @@ public class RegisterTest {
} }
@Test @Test
public void registerUserMissingOrInvalidEmail() { @Ignore
public void registerUserMissingUsernameAndInvalidEmail() {
loginPage.open();
loginPage.clickRegister();
registerPage.assertCurrent();
registerPage.register("firstName", "lastName", "registerUserInvalidEmail", null, "password", "password");
registerPage.assertCurrent();
assertEquals("Please specify username.", registerPage.getError());
events.expectRegister(null, "registerUserMissingUsername@email")
.removeDetail(Details.USERNAME)
.removeDetail(Details.EMAIL)
.error("invalid_registration").assertEvent();
}
@Test
public void registerUserMissingEmail() {
loginPage.open(); loginPage.open();
loginPage.clickRegister(); loginPage.clickRegister();
registerPage.assertCurrent(); registerPage.assertCurrent();
registerPage.register("firstName", "lastName", null, "registerUserMissingEmail", "password", "password"); registerPage.register("firstName", "lastName", null, "registerUserMissingEmail", "password", "password");
registerPage.assertCurrent(); registerPage.assertCurrent();
Assert.assertEquals("Please specify email.", registerPage.getError()); assertEquals("Please specify email.", registerPage.getError());
events.expectRegister("registerUserMissingEmail", null) events.expectRegister("registerUserMissingEmail", null)
.removeDetail("email") .removeDetail("email")
.error("invalid_registration").assertEvent(); .error("invalid_registration").assertEvent();
}
@Test
public void registerUserInvalidEmail() {
loginPage.open();
loginPage.clickRegister();
registerPage.assertCurrent();
registerPage.register("firstName", "lastName", "registerUserInvalidEmailemail", "registerUserInvalidEmail", "password", "password"); registerPage.register("firstName", "lastName", "registerUserInvalidEmailemail", "registerUserInvalidEmail", "password", "password");
registerPage.assertCurrent(); registerPage.assertCurrent();
Assert.assertEquals("Invalid email address.", registerPage.getError()); assertEquals("registerUserInvalidEmailemail", registerPage.getEmail());
assertEquals("Invalid email address.", registerPage.getError());
events.expectRegister("registerUserInvalidEmail", "registerUserInvalidEmailemail") events.expectRegister("registerUserInvalidEmail", "registerUserInvalidEmailemail")
.error("invalid_registration").assertEvent(); .error("invalid_registration").assertEvent();
} }
@ -222,7 +247,7 @@ public class RegisterTest {
registerPage.register("firstName", "lastName", "registerUserSuccess@email", "registerUserSuccess", "password", "password"); registerPage.register("firstName", "lastName", "registerUserSuccess@email", "registerUserSuccess", "password", "password");
Assert.assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType()); assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType());
String userId = events.expectRegister("registerUserSuccess", "registerUserSuccess@email").assertEvent().getUserId(); String userId = events.expectRegister("registerUserSuccess", "registerUserSuccess@email").assertEvent().getUserId();
events.expectLogin().detail("username", "registerusersuccess").user(userId).assertEvent(); events.expectLogin().detail("username", "registerusersuccess").user(userId).assertEvent();
@ -233,10 +258,10 @@ public class RegisterTest {
// test that timestamp is current with 10s tollerance // test that timestamp is current with 10s tollerance
Assert.assertTrue((System.currentTimeMillis() - user.getCreatedTimestamp()) < 10000); Assert.assertTrue((System.currentTimeMillis() - user.getCreatedTimestamp()) < 10000);
// test user info is set from form // test user info is set from form
Assert.assertEquals("registerusersuccess", user.getUsername()); assertEquals("registerusersuccess", user.getUsername());
Assert.assertEquals("registerusersuccess@email", user.getEmail()); assertEquals("registerusersuccess@email", user.getEmail());
Assert.assertEquals("firstName", user.getFirstName()); assertEquals("firstName", user.getFirstName());
Assert.assertEquals("lastName", user.getLastName()); assertEquals("lastName", user.getLastName());
} }
protected UserModel getUser(String userId) { protected UserModel getUser(String userId) {
@ -261,7 +286,7 @@ public class RegisterTest {
registerPage.registerWithEmailAsUsername("firstName", "lastName", "test-user@localhost", "password", "password"); registerPage.registerWithEmailAsUsername("firstName", "lastName", "test-user@localhost", "password", "password");
registerPage.assertCurrent(); registerPage.assertCurrent();
Assert.assertEquals("Username already exists.", registerPage.getError()); assertEquals("Username already exists.", registerPage.getError());
events.expectRegister("test-user@localhost", "test-user@localhost").user((String) null).error("username_in_use").assertEvent(); events.expectRegister("test-user@localhost", "test-user@localhost").user((String) null).error("username_in_use").assertEvent();
} finally { } finally {
@ -280,12 +305,12 @@ public class RegisterTest {
registerPage.registerWithEmailAsUsername("firstName", "lastName", null, "password", "password"); registerPage.registerWithEmailAsUsername("firstName", "lastName", null, "password", "password");
registerPage.assertCurrent(); registerPage.assertCurrent();
Assert.assertEquals("Please specify email.", registerPage.getError()); assertEquals("Please specify email.", registerPage.getError());
events.expectRegister(null, null).removeDetail("username").removeDetail("email").error("invalid_registration").assertEvent(); events.expectRegister(null, null).removeDetail("username").removeDetail("email").error("invalid_registration").assertEvent();
registerPage.registerWithEmailAsUsername("firstName", "lastName", "registerUserInvalidEmailemail", "password", "password"); registerPage.registerWithEmailAsUsername("firstName", "lastName", "registerUserInvalidEmailemail", "password", "password");
registerPage.assertCurrent(); registerPage.assertCurrent();
Assert.assertEquals("Invalid email address.", registerPage.getError()); assertEquals("Invalid email address.", registerPage.getError());
events.expectRegister("registerUserInvalidEmailemail", "registerUserInvalidEmailemail").error("invalid_registration").assertEvent(); events.expectRegister("registerUserInvalidEmailemail", "registerUserInvalidEmailemail").error("invalid_registration").assertEvent();
} finally { } finally {
configureRelamRegistrationEmailAsUsername(false); configureRelamRegistrationEmailAsUsername(false);
@ -303,7 +328,7 @@ public class RegisterTest {
registerPage.registerWithEmailAsUsername("firstName", "lastName", "registerUserSuccessE@email", "password", "password"); registerPage.registerWithEmailAsUsername("firstName", "lastName", "registerUserSuccessE@email", "password", "password");
Assert.assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType()); assertEquals(RequestType.AUTH_RESPONSE, appPage.getRequestType());
String userId = events.expectRegister("registerUserSuccessE@email", "registerUserSuccessE@email").assertEvent().getUserId(); String userId = events.expectRegister("registerUserSuccessE@email", "registerUserSuccessE@email").assertEvent().getUserId();
events.expectLogin().detail("username", "registerusersuccesse@email").user(userId).assertEvent(); events.expectLogin().detail("username", "registerusersuccesse@email").user(userId).assertEvent();