Skip to content

Commit

Permalink
PR adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaskabc authored and ledsoft committed Jul 24, 2024
1 parent 1b6f9fb commit 64eaa02
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -47,7 +47,7 @@
@ConditionalOnProperty(prefix = "termit.security", name = "provider", havingValue = "internal", matchIfMissing = true)
@Tag(name = "Admin User Registration", description = "Allows admins to register new users.")
@RestController
@RequestMapping("/users")
@RequestMapping("/admin/users")
public class AdminBasedRegistrationController {

private static final Logger LOG = LoggerFactory.getLogger(AdminBasedRegistrationController.class);
Expand All @@ -67,7 +67,7 @@ public AdminBasedRegistrationController(UserService userService) {
@ApiResponse(responseCode = "409", description = "User data are invalid")
})
@PreAuthorize("hasRole('" + SecurityConstants.ROLE_ADMIN + "')")
@PutMapping(consumes = {MediaType.APPLICATION_JSON_VALUE, JsonLd.MEDIA_TYPE})
@PostMapping(consumes = {MediaType.APPLICATION_JSON_VALUE, JsonLd.MEDIA_TYPE})
public ResponseEntity<Void> createUser(@RequestBody UserAccount user) {
userService.adminCreateUser(user);
LOG.info("User {} successfully registered by {}.", user, userService.getCurrent().getUsername());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public class PasswordChangeController {
@Autowired
public PasswordChangeController(UserService userService) {
this.userService = userService;
LOG.debug("Instantiating password change controller.");
}

@Operation(description = "Requests a password reset for the specified username.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import org.springframework.transaction.annotation.Transactional;

import java.net.URI;
import java.time.Instant;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -368,7 +367,7 @@ public void requestPasswordReset(String username) {
}

private boolean isValid(PasswordChangeRequest request) {
return request.getCreatedAt().plus(securityConfig.getPasswordChangeRequestValidity()).isAfter(Instant.now());
return request.getCreatedAt().plus(securityConfig.getPasswordChangeRequestValidity()).isAfter(Utils.timestamp());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import cz.cvut.kbss.termit.model.UserAccount;
import cz.cvut.kbss.termit.persistence.dao.GenericDao;
import cz.cvut.kbss.termit.persistence.dao.PasswordChangeRequestDao;
import cz.cvut.kbss.termit.util.Utils;
import jakarta.validation.Validator;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.List;
import java.util.UUID;

Expand All @@ -36,7 +36,7 @@ public PasswordChangeRequest create(UserAccount userAccount) {
PasswordChangeRequest request = new PasswordChangeRequest();
request.setUserAccount(userAccount);
request.setToken(UUID.randomUUID().toString());
request.setCreatedAt(Instant.now());
request.setCreatedAt(Utils.timestamp());

passwordChangeRequestDao.persist(request);
postPersist(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
import cz.cvut.kbss.termit.environment.Generator;
import cz.cvut.kbss.termit.model.PasswordChangeRequest;
import cz.cvut.kbss.termit.model.UserAccount;
import cz.cvut.kbss.termit.util.Utils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class PasswordChangeRequestDaoTest extends BaseDaoTestRunner {
Expand Down Expand Up @@ -41,8 +39,8 @@ void findAllByUsernameReturnsAllResults() {
secondPasswordChangeRequest.setToken(ANOTHER_TOKEN);
passwordChangeRequest.setUserAccount(user);
secondPasswordChangeRequest.setUserAccount(user);
passwordChangeRequest.setCreatedAt(Instant.now());
secondPasswordChangeRequest.setCreatedAt(Instant.now());
passwordChangeRequest.setCreatedAt(Utils.timestamp());
secondPasswordChangeRequest.setCreatedAt(Utils.timestamp());
transactional(() -> em.persist(passwordChangeRequest));
transactional(() -> em.persist(secondPasswordChangeRequest));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(AdminBasedRegistrationController.class)
Expand All @@ -69,7 +69,7 @@
@ActiveProfiles("test")
class AdminBasedRegistrationControllerTest extends BaseControllerTestRunner {

private static final String PATH = REST_MAPPING_PATH + "/users";
private static final String PATH = REST_MAPPING_PATH + "/admin/users";

@Autowired
private MockMvc mockMvc;
Expand All @@ -94,7 +94,7 @@ void createUserPersistsUserWhenCalledByAdmin() throws Exception {
when(securityUtils.getCurrentUser()).thenReturn(admin);
userService.persist(admin);
final UserAccount user = Generator.generateUserAccountWithPassword();
mockMvc.perform(put(PATH).content(toJson(user))
mockMvc.perform(post(PATH).content(toJson(user))
.contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isCreated());
verify(userService).adminCreateUser(user);
Expand All @@ -106,7 +106,7 @@ void createUserThrowsForbiddenForNonAdminUser() throws Exception {
Environment.setCurrentUser(admin);
when(securityUtils.getCurrentUser()).thenReturn(admin);
final UserAccount user = Generator.generateUserAccount();
mockMvc.perform(put(PATH).content(toJson(user))
mockMvc.perform(post(PATH).content(toJson(user))
.contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isForbidden());
verify(userService, never()).persist(any());
Expand All @@ -120,7 +120,7 @@ void createUserSendsEmailWhenPasswordIsEmpty() throws Exception {
when(securityUtils.getCurrentUser()).thenReturn(admin);
userService.persist(admin);
final UserAccount user = Generator.generateUserAccount();
mockMvc.perform(put(PATH).content(toJson(user))
mockMvc.perform(post(PATH).content(toJson(user))
.contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(status().isCreated());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import cz.cvut.kbss.termit.service.repository.UserRoleRepositoryService;
import cz.cvut.kbss.termit.service.security.SecurityUtils;
import cz.cvut.kbss.termit.util.Configuration;
import cz.cvut.kbss.termit.util.Utils;
import cz.cvut.kbss.termit.util.Vocabulary;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -51,7 +52,6 @@
import org.mockito.junit.jupiter.MockitoExtension;

import java.net.URI;
import java.time.Instant;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -493,7 +493,7 @@ void changePasswordValidRequestPasswordChanged() {
final UserAccount account = Generator.generateUserAccountWithPassword();
final String originalPassword = account.getPassword();
final PasswordChangeRequest request = new PasswordChangeRequest();
request.setCreatedAt(Instant.now());
request.setCreatedAt(Utils.timestamp());
request.setToken(UUID.randomUUID().toString());
request.setUserAccount(account);
request.setUri(Generator.generateUri());
Expand Down Expand Up @@ -533,7 +533,7 @@ void changePasswordRequestNotFoundExceptionThrown() {
@Test
void changePasswordExpiredRequestExceptionThrown() {
final PasswordChangeRequest request = new PasswordChangeRequest();
request.setCreatedAt(Instant.now().minus(configuration.getSecurity()
request.setCreatedAt(Utils.timestamp().minus(configuration.getSecurity()
.getPasswordChangeRequestValidity())
.minusNanos(1));
request.setUri(Generator.generateUri());
Expand All @@ -555,7 +555,7 @@ void changePasswordExpiredRequestExceptionThrown() {
@Test
void changePasswordValidURINotMatchingTokenExceptionThrown() {
final PasswordChangeRequest request = new PasswordChangeRequest();
request.setCreatedAt(Instant.now());
request.setCreatedAt(Utils.timestamp());
request.setUri(Generator.generateUri());
request.setToken(UUID.randomUUID().toString());

Expand All @@ -578,7 +578,7 @@ void changePasswordUnlocksLockedAccount() {
user.lock();

final PasswordChangeRequest request = new PasswordChangeRequest();
request.setCreatedAt(Instant.now().minusNanos(1));
request.setCreatedAt(Utils.timestamp().minusMillis(1));
request.setUri(Generator.generateUri());
request.setToken(UUID.randomUUID().toString());
request.setUserAccount(user);
Expand Down

0 comments on commit 64eaa02

Please sign in to comment.