Merge branch 'w2p-97298_issue-3281_self-register-issue-7.2' into w2p-97298_issue-3281_self-register-issue-main

# Conflicts:
#	dspace-server-webapp/src/test/java/org/dspace/app/rest/RegistrationRestRepositoryIT.java
This commit is contained in:
Marie Verdonck
2023-01-25 16:30:23 +01:00
2 changed files with 35 additions and 11 deletions

View File

@@ -53,7 +53,7 @@ public class RegistrationRestRepository extends DSpaceRestRepository<Registratio
private static Logger log = LogManager.getLogger(RegistrationRestRepository.class); private static Logger log = LogManager.getLogger(RegistrationRestRepository.class);
public static final String TYPE_QUERY_PARAM = "type"; public static final String TYPE_QUERY_PARAM = "accountRequestType";
public static final String TYPE_REGISTER = "register"; public static final String TYPE_REGISTER = "register";
public static final String TYPE_FORGOT = "forgot"; public static final String TYPE_FORGOT = "forgot";
@@ -114,8 +114,9 @@ public class RegistrationRestRepository extends DSpaceRestRepository<Registratio
if (StringUtils.isBlank(registrationRest.getEmail())) { if (StringUtils.isBlank(registrationRest.getEmail())) {
throw new UnprocessableEntityException("The email cannot be omitted from the Registration endpoint"); throw new UnprocessableEntityException("The email cannot be omitted from the Registration endpoint");
} }
String type = request.getParameter(TYPE_QUERY_PARAM); String accountType = request.getParameter(TYPE_QUERY_PARAM);
if (!type.equalsIgnoreCase(TYPE_FORGOT) && !type.equalsIgnoreCase(TYPE_REGISTER)) { if (StringUtils.isBlank(accountType) ||
(!accountType.equalsIgnoreCase(TYPE_FORGOT) && !accountType.equalsIgnoreCase(TYPE_REGISTER))) {
throw new IllegalArgumentException(String.format("Needs query param '%s' with value %s or %s indicating " + throw new IllegalArgumentException(String.format("Needs query param '%s' with value %s or %s indicating " +
"what kind of registration request it is", TYPE_QUERY_PARAM, TYPE_FORGOT, TYPE_REGISTER)); "what kind of registration request it is", TYPE_QUERY_PARAM, TYPE_FORGOT, TYPE_REGISTER));
} }
@@ -125,7 +126,7 @@ public class RegistrationRestRepository extends DSpaceRestRepository<Registratio
} catch (SQLException e) { } catch (SQLException e) {
log.error("Something went wrong retrieving EPerson for email: " + registrationRest.getEmail(), e); log.error("Something went wrong retrieving EPerson for email: " + registrationRest.getEmail(), e);
} }
if (eperson != null && type.equalsIgnoreCase(TYPE_FORGOT)) { if (eperson != null && accountType.equalsIgnoreCase(TYPE_FORGOT)) {
try { try {
if (!AuthorizeUtil.authorizeUpdatePassword(context, eperson.getEmail())) { if (!AuthorizeUtil.authorizeUpdatePassword(context, eperson.getEmail())) {
throw new DSpaceBadRequestException("Password cannot be updated for the given EPerson with email: " throw new DSpaceBadRequestException("Password cannot be updated for the given EPerson with email: "
@@ -136,7 +137,7 @@ public class RegistrationRestRepository extends DSpaceRestRepository<Registratio
log.error("Something went wrong with sending forgot password info email: " log.error("Something went wrong with sending forgot password info email: "
+ registrationRest.getEmail(), e); + registrationRest.getEmail(), e);
} }
} else if (type.equalsIgnoreCase(TYPE_REGISTER)) { } else if (accountType.equalsIgnoreCase(TYPE_REGISTER)) {
try { try {
String email = registrationRest.getEmail(); String email = registrationRest.getEmail();
if (!AuthorizeUtil.authorizeNewAccountRegistration(context, request)) { if (!AuthorizeUtil.authorizeNewAccountRegistration(context, request)) {
@@ -144,8 +145,9 @@ public class RegistrationRestRepository extends DSpaceRestRepository<Registratio
"Registration is disabled, you are not authorized to create a new Authorization"); "Registration is disabled, you are not authorized to create a new Authorization");
} }
if (!authenticationService.canSelfRegister(context, request, email)) { if (!authenticationService.canSelfRegister(context, request, email)) {
throw new DSpaceBadRequestException(String.format("Registration is not allowed with email address" + throw new UnprocessableEntityException(
" %s", email)); String.format("Registration is not allowed with email address" +
" %s", email));
} }
accountService.sendRegistrationInfo(context, email); accountService.sendRegistrationInfo(context, email);
} catch (SQLException | IOException | MessagingException | AuthorizeException e) { } catch (SQLException | IOException | MessagingException | AuthorizeException e) {

View File

@@ -22,7 +22,6 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@@ -203,7 +202,7 @@ public class RegistrationRestRepositoryIT extends AbstractControllerIntegrationT
@Test @Test
public void testRegisterDomainNotRegistered() throws Exception { public void testRegisterDomainNotRegistered() throws Exception {
List<RegistrationData> registrationDataList = new ArrayList<>(); List<RegistrationData> registrationDataList;
try { try {
configurationService.setProperty("authentication-password.domain.valid", "test.com"); configurationService.setProperty("authentication-password.domain.valid", "test.com");
RegistrationRest registrationRest = new RegistrationRest(); RegistrationRest registrationRest = new RegistrationRest();
@@ -215,7 +214,7 @@ public class RegistrationRestRepositoryIT extends AbstractControllerIntegrationT
.param(TYPE_QUERY_PARAM, TYPE_REGISTER) .param(TYPE_QUERY_PARAM, TYPE_REGISTER)
.content(mapper.writeValueAsBytes(registrationRest)) .content(mapper.writeValueAsBytes(registrationRest))
.contentType(contentType)) .contentType(contentType))
.andExpect(status().isBadRequest()); .andExpect(status().isUnprocessableEntity());
} finally { } finally {
registrationDataList = registrationDataDAO.findAll(context, RegistrationData.class); registrationDataList = registrationDataDAO.findAll(context, RegistrationData.class);
Iterator<RegistrationData> iterator = registrationDataList.iterator(); Iterator<RegistrationData> iterator = registrationDataList.iterator();
@@ -246,7 +245,7 @@ public class RegistrationRestRepositoryIT extends AbstractControllerIntegrationT
.param(TYPE_QUERY_PARAM, TYPE_REGISTER) .param(TYPE_QUERY_PARAM, TYPE_REGISTER)
.content(mapper.writeValueAsBytes(registrationRest)) .content(mapper.writeValueAsBytes(registrationRest))
.contentType(contentType)) .contentType(contentType))
.andExpect(status().isBadRequest()); .andExpect(status().isUnprocessableEntity());
registrationDataList = registrationDataDAO.findAll(context, RegistrationData.class); registrationDataList = registrationDataDAO.findAll(context, RegistrationData.class);
assertEquals(0, registrationDataList.size()); assertEquals(0, registrationDataList.size());
} finally { } finally {
@@ -420,4 +419,27 @@ public class RegistrationRestRepositoryIT extends AbstractControllerIntegrationT
captchaService.init(); captchaService.init();
} }
@Test
public void accountEndpoint_WithoutAccountTypeParam() throws Exception {
ObjectMapper mapper = new ObjectMapper();
RegistrationRest registrationRest = new RegistrationRest();
registrationRest.setEmail(eperson.getEmail());
getClient().perform(post("/api/eperson/registrations")
.content(mapper.writeValueAsBytes(registrationRest))
.contentType(contentType))
.andExpect(status().isBadRequest());
}
@Test
public void accountEndpoint_WrongAccountTypeParam() throws Exception {
ObjectMapper mapper = new ObjectMapper();
RegistrationRest registrationRest = new RegistrationRest();
registrationRest.setEmail(eperson.getEmail());
getClient().perform(post("/api/eperson/registrations")
.param(TYPE_QUERY_PARAM, "nonValidValue")
.content(mapper.writeValueAsBytes(registrationRest))
.contentType(contentType))
.andExpect(status().isBadRequest());
}
} }