Skip to content

Commit

Permalink
Merge pull request exadel-inc#949 from exadel-inc/EFRS-1341_owner_use…
Browse files Browse the repository at this point in the history
…r_bug

EFRS-1341: Changed registration logic
  • Loading branch information
VolodymyrBushko authored Nov 15, 2022
2 parents 1280973 + dc7cb98 commit fd96df0
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 23 deletions.
14 changes: 11 additions & 3 deletions java/admin/src/main/java/com/exadel/frs/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,21 @@ public void removeExpiredRegistrationTokens() {
userRepository.deleteByEnabledFalseAndRegTimeBefore(seconds);
}

@Transactional
public void confirmRegistration(final String token) {
val user = userRepository.findByRegistrationToken(token)
.orElseThrow(RegistrationTokenExpiredException::new);

user.setEnabled(true);
user.setRegistrationToken(null);
synchronized (this) {
if (!userRepository.isOwnerPresent()) {
user.setGlobalRole(OWNER);
}

userRepository.save(user);
user.setEnabled(true);
user.setRegistrationToken(null);

userRepository.flush();
}
}

private void manageOwnedAppsByUserBeingDeleted(final UserDeleteDto userDeleteDto) {
Expand Down Expand Up @@ -228,6 +235,7 @@ public User updateDemoUser(UserCreateDto userCreateDto) {
user.setAllowStatistics(userCreateDto.isAllowStatistics());

if (isMailServerEnabled) {
user.setGlobalRole(USER);
user.setRegistrationToken(generateRegistrationToken());
sendRegistrationTokenToUser(user);
}
Expand Down
36 changes: 23 additions & 13 deletions java/admin/src/test/java/com/exadel/frs/DbHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,32 @@ public Img insertImg() {
}

public User insertUser(String email) {
var user = User.builder()
.email(email)
.firstName("firstName")
.lastName("lastName")
.password(encoder.encode("1234567890"))
.guid(UUID.randomUUID().toString())
.accountNonExpired(true)
.accountNonLocked(true)
.credentialsNonExpired(true)
.enabled(true)
.allowStatistics(false)
.globalRole(USER)
.build();
var user = createUser(email);
user.setEnabled(true);
return userRepository.saveAndFlush(user);
}

public User insertUnconfirmedUser(String email) {
var user = createUser(email);
user.setRegistrationToken(UUID.randomUUID().toString());
return userRepository.saveAndFlush(user);
}

private User createUser(String email) {
return User.builder()
.email(email)
.firstName("firstName")
.lastName("lastName")
.password(encoder.encode("1234567890"))
.guid(UUID.randomUUID().toString())
.accountNonExpired(true)
.accountNonLocked(true)
.credentialsNonExpired(true)
.allowStatistics(false)
.globalRole(USER)
.build();
}

public ResetPasswordToken insertResetPasswordToken(User user) {
var expiresIn = now(UTC).plus(resetPasswordTokenExpires, MILLIS);
var token = new ResetPasswordToken(expiresIn, user);
Expand Down
11 changes: 5 additions & 6 deletions java/admin/src/test/java/com/exadel/frs/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,21 @@
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

import com.exadel.frs.commonservice.entity.User;
import com.exadel.frs.commonservice.enums.GlobalRole;
import com.exadel.frs.commonservice.enums.Replacer;
import com.exadel.frs.commonservice.exception.EmptyRequiredFieldException;
import com.exadel.frs.commonservice.exception.IllegalReplacerException;
import com.exadel.frs.commonservice.repository.UserRepository;
import com.exadel.frs.dto.ui.UserCreateDto;
import com.exadel.frs.dto.ui.UserDeleteDto;
import com.exadel.frs.dto.ui.UserUpdateDto;
import com.exadel.frs.commonservice.entity.User;
import com.exadel.frs.commonservice.enums.GlobalRole;
import com.exadel.frs.commonservice.enums.Replacer;
import com.exadel.frs.exception.EmailAlreadyRegisteredException;
import com.exadel.frs.commonservice.exception.IllegalReplacerException;
import com.exadel.frs.exception.IncorrectUserPasswordException;
import com.exadel.frs.exception.InvalidEmailException;
import com.exadel.frs.exception.RegistrationTokenExpiredException;
import com.exadel.frs.exception.UserDoesNotExistException;
import com.exadel.frs.helpers.EmailSender;
import com.exadel.frs.commonservice.repository.UserRepository;
import com.exadel.frs.service.AppService;
import com.exadel.frs.service.UserService;
import com.exadel.frs.system.security.AuthorizationManager;
Expand Down
37 changes: 37 additions & 0 deletions java/admin/src/test/java/com/exadel/frs/UserServiceTestIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.exadel.frs;

import com.exadel.frs.commonservice.entity.User;
import com.exadel.frs.commonservice.enums.GlobalRole;
import com.exadel.frs.commonservice.repository.UserRepository;
import com.exadel.frs.dto.ui.UserCreateDto;
import com.exadel.frs.exception.UserDoesNotExistException;
Expand All @@ -36,9 +37,14 @@

import java.util.Optional;
import java.util.UUID;
import org.springframework.transaction.annotation.Transactional;

import static com.exadel.frs.commonservice.enums.GlobalRole.OWNER;
import static com.exadel.frs.commonservice.enums.GlobalRole.USER;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;

@ExtendWith(SpringExtension.class)
Expand All @@ -60,6 +66,9 @@ class UserServiceTestIT extends EmbeddedPostgreSQLTest {
@SpyBean
private UserService userService;

@Autowired
private DbHelper dbHelper;

@Autowired
private UserRepository userRepository;

Expand Down Expand Up @@ -147,6 +156,34 @@ void autocompleteReturnsUsers() {
assertThat(actual).hasSize(2);
}

@Test
@Transactional
void confirmRegistration_ThereAreTwoUnconfirmedUsers_FirstOfThemShouldBecomeAnOwner() {
userRepository.deleteAll();

val user1 = dbHelper.insertUnconfirmedUser(USER_EMAIL);
val user2 = dbHelper.insertUnconfirmedUser(USER_EMAIL_2);

assertFalse(user1.isEnabled());
assertFalse(user2.isEnabled());
assertThat(user1.getRegistrationToken()).isNotNull();
assertThat(user2.getRegistrationToken()).isNotNull();
assertThat(user1.getGlobalRole()).isEqualByComparingTo(USER);
assertThat(user2.getGlobalRole()).isEqualByComparingTo(USER);
assertThat(userRepository.findAll()).containsOnly(user1, user2);

userService.confirmRegistration(user1.getRegistrationToken());
userService.confirmRegistration(user2.getRegistrationToken());

assertTrue(user1.isEnabled());
assertTrue(user2.isEnabled());
assertThat(user1.getRegistrationToken()).isNull();
assertThat(user2.getRegistrationToken()).isNull();
assertThat(user1.getGlobalRole()).isEqualByComparingTo(OWNER);
assertThat(user2.getGlobalRole()).isEqualByComparingTo(USER);
assertThat(userRepository.findAll()).containsOnly(user1, user2);
}

private void createAndEnableUser(final String email) {
val regToken = UUID.randomUUID().toString();
when(userService.generateRegistrationToken()).thenReturn(regToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public enum TableLockName {

MODEL_STATISTIC_LOCK;
MODEL_STATISTIC_LOCK
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public interface UserRepository extends JpaRepository<User, Long> {

User findByGlobalRole(GlobalRole role);

@Query("select count(u) > 0 from User u where u.globalRole = 'O'")
boolean isOwnerPresent();

int deleteByEnabledFalseAndRegTimeBefore(LocalDateTime time);

Optional<User> findByRegistrationToken(String token);
Expand Down

0 comments on commit fd96df0

Please sign in to comment.