diff --git a/wls-auth-service/src/main/java/de/muenchen/oss/wahllokalsystem/authservice/domain/User.java b/wls-auth-service/src/main/java/de/muenchen/oss/wahllokalsystem/authservice/domain/User.java index 4b347990c..35a31201e 100644 --- a/wls-auth-service/src/main/java/de/muenchen/oss/wahllokalsystem/authservice/domain/User.java +++ b/wls-auth-service/src/main/java/de/muenchen/oss/wahllokalsystem/authservice/domain/User.java @@ -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 @@ -39,7 +38,6 @@ public static User flatCopyOf(final User user) { return copy; } - @NaturalId @NotNull @Size(min = 1) @ToString.Include diff --git a/wls-auth-service/src/main/resources/application-test.yml b/wls-auth-service/src/main/resources/application-test.yml index 562b25f44..791dd3351 100644 --- a/wls-auth-service/src/main/resources/application-test.yml +++ b/wls-auth-service/src/main/resources/application-test.yml @@ -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 diff --git a/wls-auth-service/src/test/java/de/muenchen/oss/wahllokalsystem/authservice/domain/UserRepositoryImplIntegrationTest.java b/wls-auth-service/src/test/java/de/muenchen/oss/wahllokalsystem/authservice/domain/UserRepositoryImplIntegrationTest.java index 0e02d8e00..e55ac7c13 100644 --- a/wls-auth-service/src/test/java/de/muenchen/oss/wahllokalsystem/authservice/domain/UserRepositoryImplIntegrationTest.java +++ b/wls-auth-service/src/test/java/de/muenchen/oss/wahllokalsystem/authservice/domain/UserRepositoryImplIntegrationTest.java @@ -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; @@ -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 @@ -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 @@ -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); }