Skip to content

Commit

Permalink
Merge pull request #548 from it-at-m/547-fehler-wegen-immutable-usern…
Browse files Browse the repository at this point in the history
…ame-bei-klasse-user

547 fehler wegen immutable username bei klasse user - fix mit remove von naturalID
  • Loading branch information
MrSebastian authored Nov 14, 2024
2 parents 2b5b864 + 1b437af commit df844d8
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.val;
import org.hibernate.annotations.NaturalId;

@Entity
@Table(name = "Wlsuser") //user as table name is already in use by h2
Expand All @@ -39,7 +38,6 @@ public static User flatCopyOf(final User user) {
return copy;
}

@NaturalId
@NotNull
@Size(min = 1)
@ToString.Include
Expand Down
8 changes: 3 additions & 5 deletions wls-auth-service/src/main/resources/application-test.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
spring:

flyway:
enabled: true
# Spring JPA
h2.console.enabled: true
jpa:
database: H2
hibernate:
# always drop and create the db should be the best
# configuration for local (development) mode. this
# is also the default, that spring offers by convention.
# but here explicite:
ddl-auto: create-drop
ddl-auto: validate
naming.physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
# Logging for database operation
show-sql: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.transaction.support.TransactionTemplate;

Expand Down Expand Up @@ -129,6 +130,19 @@ void should_returnUnencryptedUsername_when_savingUser() {

Assertions.assertThat(savedUser.getUsername()).isEqualTo(USERNAME_UNENCRYPTED);
}

@Test
void should_throwException_when_userWithUsernameAlreadyExists() {
val userToSave = new User();
userToSave.setUsername(USERNAME_UNENCRYPTED);

transactionTemplate.execute(status -> userRepository.save(userToSave));

val userToSaveWithSameUsername = new User();
userToSaveWithSameUsername.setUsername(USERNAME_UNENCRYPTED);
Assertions.assertThatException().isThrownBy(() -> transactionTemplate.execute(status -> userRepository.save(userToSaveWithSameUsername)))
.isInstanceOf(DataIntegrityViolationException.class);
}
}

@Nested
Expand Down Expand Up @@ -157,6 +171,19 @@ void should_returnUnencryptedUsername_when_savingUser() {

Assertions.assertThat(savedUser).allSatisfy(user -> Assertions.assertThat(user.getUsername()).isEqualTo(USERNAME_UNENCRYPTED));
}

@Test
void should_throwException_when_multipleUsersHaveSameName() {
val user1 = new User();
user1.setUsername(USERNAME_UNENCRYPTED);
val user2 = new User();
user2.setUsername(USERNAME_UNENCRYPTED);

val userToSaveWithSameUsername = new User();
userToSaveWithSameUsername.setUsername(USERNAME_UNENCRYPTED);
Assertions.assertThatException().isThrownBy(() -> transactionTemplate.execute(status -> userRepository.saveAll(List.of(user1, user2))))
.isInstanceOf(DataIntegrityViolationException.class);
}
}

@Nested
Expand Down Expand Up @@ -219,6 +246,42 @@ void should_do_sth() {
}
}

@Nested
class OnSchedule {

@Test
void should_encryptExistingUsers_when_usersExists() {
val wahltagID = "wahltagID";
val userToEncrypt = createUser(USERNAME_UNENCRYPTED, wahltagID);

val savedSavedUnencryptedUser = transactionTemplate.execute(status -> crudUserRepository.save(userToEncrypt));

transactionTemplate.executeWithoutResult(status -> userRepository.onSchedule());

val userAfterEncryption = crudUserRepository.findById(savedSavedUnencryptedUser.getId()).get();

Assertions.assertThat(userAfterEncryption.getUsername()).isEqualTo(USERNAME_ENCRYPTED);
}
}

@Nested
class OnInit {

@Test
void should_encryptExistingUsers_when_usersExists() {
val wahltagID = "wahltagID";
val userToEncrypt = createUser(USERNAME_UNENCRYPTED, wahltagID);

val savedSavedUnencryptedUser = transactionTemplate.execute(status -> crudUserRepository.save(userToEncrypt));

transactionTemplate.executeWithoutResult(status -> userRepository.onInit());

val userAfterEncryption = crudUserRepository.findById(savedSavedUnencryptedUser.getId()).get();

Assertions.assertThat(userAfterEncryption.getUsername()).isEqualTo(USERNAME_ENCRYPTED);
}
}

private User createUser(final String encryptedUsername) {
return createUser(encryptedUsername, null);
}
Expand Down

0 comments on commit df844d8

Please sign in to comment.