From cc1d4929f975d8d3e900b29339059a74aac3b233 Mon Sep 17 00:00:00 2001 From: MrSebastian <13592751+MrSebastian@users.noreply.github.com> Date: Thu, 14 Nov 2024 17:25:17 +0100 Subject: [PATCH 1/5] create tests to verify bug --- .../src/main/resources/application-test.yml | 8 ++- .../UserRepositoryImplIntegrationTest.java | 50 +++++++++++++++++++ 2 files changed, 53 insertions(+), 5 deletions(-) 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..4456fed9d 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_whenUserWithUsernameAlreadyExists() { + 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 @@ -219,6 +233,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.onSchedule()); + + val userAfterEncryption = crudUserRepository.findById(savedSavedUnencryptedUser.getId()).get(); + + Assertions.assertThat(userAfterEncryption.getUsername()).isEqualTo(USERNAME_ENCRYPTED); + } + } + private User createUser(final String encryptedUsername) { return createUser(encryptedUsername, null); } From 415457c592affbdbff20895336d9e874d9535101 Mon Sep 17 00:00:00 2001 From: MrSebastian <13592751+MrSebastian@users.noreply.github.com> Date: Thu, 14 Nov 2024 17:26:01 +0100 Subject: [PATCH 2/5] remove naturalID cause the property must be changeable during startup --- .../muenchen/oss/wahllokalsystem/authservice/domain/User.java | 2 -- 1 file changed, 2 deletions(-) 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 From 90ea298646df754a30a17642437e1d1ccb0db9f6 Mon Sep 17 00:00:00 2001 From: MrSebastian <13592751+MrSebastian@users.noreply.github.com> Date: Thu, 14 Nov 2024 17:36:28 +0100 Subject: [PATCH 3/5] create test for saveAll to verify uniques username --- .../domain/UserRepositoryImplIntegrationTest.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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 4456fed9d..166c815b9 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 @@ -171,6 +171,19 @@ void should_returnUnencryptedUsername_when_savingUser() { Assertions.assertThat(savedUser).allSatisfy(user -> Assertions.assertThat(user.getUsername()).isEqualTo(USERNAME_UNENCRYPTED)); } + + @Test + void should_throwException_whenMultipleUsersHaveSameName() { + 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 From 398c72b7b3dc7cbd4efa9c1131b20db3a797a5ac Mon Sep 17 00:00:00 2001 From: MrSebastian <13592751+MrSebastian@users.noreply.github.com> Date: Thu, 14 Nov 2024 17:43:43 +0100 Subject: [PATCH 4/5] fix typo in test case name --- .../authservice/domain/UserRepositoryImplIntegrationTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 166c815b9..fadb305a9 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 @@ -132,7 +132,7 @@ void should_returnUnencryptedUsername_when_savingUser() { } @Test - void should_throwException_whenUserWithUsernameAlreadyExists() { + void should_throwException_when_userWithUsernameAlreadyExists() { val userToSave = new User(); userToSave.setUsername(USERNAME_UNENCRYPTED); @@ -173,7 +173,7 @@ void should_returnUnencryptedUsername_when_savingUser() { } @Test - void should_throwException_whenMultipleUsersHaveSameName() { + void should_throwException_when_multipleUsersHaveSameName() { val user1 = new User(); user1.setUsername(USERNAME_UNENCRYPTED); val user2 = new User(); From 1b437af6014772e3eec147b85b6308582c8ff6e1 Mon Sep 17 00:00:00 2001 From: MrSebastian <13592751+MrSebastian@users.noreply.github.com> Date: Thu, 14 Nov 2024 17:49:36 +0100 Subject: [PATCH 5/5] fix testmethod --- .../authservice/domain/UserRepositoryImplIntegrationTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 fadb305a9..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 @@ -274,7 +274,7 @@ void should_encryptExistingUsers_when_usersExists() { val savedSavedUnencryptedUser = transactionTemplate.execute(status -> crudUserRepository.save(userToEncrypt)); - transactionTemplate.executeWithoutResult(status -> userRepository.onSchedule()); + transactionTemplate.executeWithoutResult(status -> userRepository.onInit()); val userAfterEncryption = crudUserRepository.findById(savedSavedUnencryptedUser.getId()).get();