diff --git a/api-contract/api.raml b/api-contract/api.raml index 660c8610a..b2d8c5440 100644 --- a/api-contract/api.raml +++ b/api-contract/api.raml @@ -534,6 +534,22 @@ annotationTypes: description: Delete doctor from patient responses: 200 + /autotest/del + delete: + description: Delete autotest users by given prefix + responses: + 200: + body: + application/json: + example: !include samples/user_deletion_result.json + /autotest/del/{email} + delete: + description: Delete autotest user by email + responses: + 200: + body: + application/json: + example: !include samples/user_deletion_result.json # CLOUD ---------------------- /cloud: diff --git a/api-contract/samples/user_deletion_result.json b/api-contract/samples/user_deletion_result.json new file mode 100644 index 000000000..8afb1a290 --- /dev/null +++ b/api-contract/samples/user_deletion_result.json @@ -0,0 +1,9 @@ +{ + "data": [ + { + "count": 1 + } + ], + "errors": [], + "meta": [] +} \ No newline at end of file diff --git a/src/main/kotlin/com/epam/brn/controller/UserDetailController.kt b/src/main/kotlin/com/epam/brn/controller/UserDetailController.kt index ba4dbc47b..bee36703a 100644 --- a/src/main/kotlin/com/epam/brn/controller/UserDetailController.kt +++ b/src/main/kotlin/com/epam/brn/controller/UserDetailController.kt @@ -126,4 +126,18 @@ class UserDetailController( @Operation(summary = "Delete doctor from patient") fun deleteDoctorFromPatient(@PathVariable patientId: Long) = doctorService.deleteDoctorFromPatientAsPatient(patientId) + + @RolesAllowed(BrnRole.ADMIN) + @DeleteMapping("/autotest/del") + @Operation(summary = "Delete all auto test users") + @ResponseStatus(HttpStatus.OK) + fun deleteAutoTestUsers() = ResponseEntity.ok() + .body(BrnResponse(data = userAccountService.deleteAutoTestUsers())) + + @DeleteMapping("/autotest/del/{email}") + @Operation(summary = "Delete auto test user by email") + @Throws(Exception::class) + @ResponseStatus(HttpStatus.OK) + fun deleteAutoTestUserByEmail(@PathVariable("email") email: String) = + ResponseEntity.ok().body(BrnResponse(data = userAccountService.deleteAutoTestUserByEmail(email))) } diff --git a/src/main/kotlin/com/epam/brn/model/StudyHistory.kt b/src/main/kotlin/com/epam/brn/model/StudyHistory.kt index 653db23e9..064246893 100644 --- a/src/main/kotlin/com/epam/brn/model/StudyHistory.kt +++ b/src/main/kotlin/com/epam/brn/model/StudyHistory.kt @@ -25,7 +25,7 @@ data class StudyHistory( val id: Long? = null, @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "user_id") + @JoinColumn(name = "user_id", insertable = true, updatable = false) var userAccount: UserAccount, @ManyToOne(fetch = FetchType.LAZY) diff --git a/src/main/kotlin/com/epam/brn/repo/UserAccountRepository.kt b/src/main/kotlin/com/epam/brn/repo/UserAccountRepository.kt index a77062d28..107e29fca 100644 --- a/src/main/kotlin/com/epam/brn/repo/UserAccountRepository.kt +++ b/src/main/kotlin/com/epam/brn/repo/UserAccountRepository.kt @@ -61,4 +61,10 @@ interface UserAccountRepository : JpaRepository { where u.email = :email""" ) fun updateLastVisitByEmail(email: String, lastVisit: LocalDateTime) + + @Transactional + fun deleteUserAccountsByEmailStartsWith(prefix: String): Long + + @Transactional + fun deleteUserAccountByEmailIs(email: String): Long } diff --git a/src/main/kotlin/com/epam/brn/service/UserAccountService.kt b/src/main/kotlin/com/epam/brn/service/UserAccountService.kt index 4a4ca2627..7aff3749a 100644 --- a/src/main/kotlin/com/epam/brn/service/UserAccountService.kt +++ b/src/main/kotlin/com/epam/brn/service/UserAccountService.kt @@ -1,8 +1,8 @@ package com.epam.brn.service import com.epam.brn.dto.HeadphonesDto -import com.epam.brn.dto.request.UserAccountChangeRequest import com.epam.brn.dto.UserAccountDto +import com.epam.brn.dto.request.UserAccountChangeRequest import com.epam.brn.model.UserAccount import com.google.firebase.auth.UserRecord import org.springframework.data.domain.Pageable @@ -36,4 +36,6 @@ interface UserAccountService { fun getPatientsForDoctor(doctorId: Long): List fun markVisitForCurrentUser() + fun deleteAutoTestUsers(): Long + fun deleteAutoTestUserByEmail(email: String): Long } diff --git a/src/main/kotlin/com/epam/brn/service/impl/UserAccountServiceImpl.kt b/src/main/kotlin/com/epam/brn/service/impl/UserAccountServiceImpl.kt index a06e58998..85d53ebec 100644 --- a/src/main/kotlin/com/epam/brn/service/impl/UserAccountServiceImpl.kt +++ b/src/main/kotlin/com/epam/brn/service/impl/UserAccountServiceImpl.kt @@ -14,6 +14,7 @@ import com.epam.brn.service.RoleService import com.epam.brn.service.TimeService import com.epam.brn.service.UserAccountService import com.google.firebase.auth.UserRecord +import org.springframework.beans.factory.annotation.Value import org.springframework.data.domain.Pageable import org.springframework.security.core.Authentication import org.springframework.security.core.context.SecurityContextHolder @@ -26,8 +27,10 @@ class UserAccountServiceImpl( private val userAccountRepository: UserAccountRepository, private val roleService: RoleService, private val headphonesService: HeadphonesService, - private val timeService: TimeService + private val timeService: TimeService, ) : UserAccountService { + @Value("\${users.delete.prefix}") + private lateinit var prefix: String override fun findUserByEmail(email: String): UserAccountDto = userAccountRepository.findUserAccountByEmail(email) @@ -172,4 +175,11 @@ class UserAccountServiceImpl( private fun getDefaultRoleSet(): MutableSet = setOf(BrnRole.USER).mapTo(mutableSetOf()) { roleService.findByName(it) } + + override fun deleteAutoTestUsers(): Long = userAccountRepository.deleteUserAccountsByEmailStartsWith(prefix) + override fun deleteAutoTestUserByEmail(email: String): Long = + if (email.startsWith(prefix)) + userAccountRepository.deleteUserAccountByEmailIs(email) + else + throw IllegalArgumentException("email = [$email] must start with prefix = [$prefix]") } diff --git a/src/main/kotlin/com/epam/brn/service/load/InitialDataLoader.kt b/src/main/kotlin/com/epam/brn/service/load/InitialDataLoader.kt index 4246cc23c..c14254bb5 100644 --- a/src/main/kotlin/com/epam/brn/service/load/InitialDataLoader.kt +++ b/src/main/kotlin/com/epam/brn/service/load/InitialDataLoader.kt @@ -106,8 +106,7 @@ class InitialDataLoader( val firstUser = createUser("Name1", "default@default.ru", setOf(userRole)) val secondUser = createUser("Name2", "default2@default.ru", setOf(userRole, specialistRole)) val autoTestUser = createUser("autoTestUser", AUTO_TEST_USER_EMAIL, setOf(userRole)) - val autoTestSpecialist = - createUser("autoTestSpecialist", AUTO_TEST_SPECIALIST_EMAIL, setOf(userRole, specialistRole)) + val autoTestSpecialist = createUser("autoTestSpecialist", AUTO_TEST_SPECIALIST_EMAIL, setOf(userRole, specialistRole)) val listOfUsers = listOf(admin, firstUser, secondUser, autoTestUser, autoTestSpecialist) userAccountRepository.saveAll(listOfUsers) } else { diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index b63ea671a..181712d9a 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -152,6 +152,8 @@ github.api.url.base=https://api.github.com github.api.url.path.contributors=/repos/{OWNER}/{REPO}/contributors github.api.url.path.users=/users/{username} +users.delete.prefix=autotest + # Swagger springdoc.swagger-ui.tagsSorter=alpha springdoc.writer-with-order-by-keys=true \ No newline at end of file diff --git a/src/main/resources/db/migration/V220240729_2557.sql b/src/main/resources/db/migration/V220240729_2557.sql new file mode 100644 index 000000000..ce18174ed --- /dev/null +++ b/src/main/resources/db/migration/V220240729_2557.sql @@ -0,0 +1,15 @@ +alter table user_roles + drop constraint fk6teafluo1xt1re7vbgr16w8iy; + +alter table user_roles + add constraint user_roles_to_user_account + foreign key (user_id) references user_account + on delete cascade; + +alter table study_history + drop constraint fk5gkcspvt7bmyc80jv8vfbfpqt; + +alter table study_history + add constraint study_history_to_user_account + foreign key (user_id) references user_account + on delete cascade; \ No newline at end of file diff --git a/src/test/kotlin/com/epam/brn/controller/UserDetailControllerTest.kt b/src/test/kotlin/com/epam/brn/controller/UserDetailControllerTest.kt index 3b9692789..54ee03587 100644 --- a/src/test/kotlin/com/epam/brn/controller/UserDetailControllerTest.kt +++ b/src/test/kotlin/com/epam/brn/controller/UserDetailControllerTest.kt @@ -1,14 +1,14 @@ package com.epam.brn.controller import com.epam.brn.dto.HeadphonesDto +import com.epam.brn.dto.UserAccountDto import com.epam.brn.dto.request.UserAccountChangeRequest import com.epam.brn.dto.request.UserAccountCreateRequest import com.epam.brn.dto.response.BrnResponse -import com.epam.brn.dto.UserAccountDto import com.epam.brn.dto.response.UserWithAnalyticsResponse +import com.epam.brn.enums.BrnGender import com.epam.brn.enums.BrnRole import com.epam.brn.enums.HeadphonesType -import com.epam.brn.enums.BrnGender import com.epam.brn.service.DoctorService import com.epam.brn.service.UserAccountService import com.epam.brn.service.UserAnalyticsService @@ -314,4 +314,35 @@ internal class UserDetailControllerTest { users.statusCodeValue shouldBe HttpStatus.SC_OK (users.body as BrnResponse<*>).data shouldBe listOf(userAccountDto) } + + @Test + fun `deleteAutoTestUsers should return count of deleted users`() { + // GIVEN + val usersCount = 2L + every { userAccountService.deleteAutoTestUsers() } returns usersCount + + // WHEN + val result = userDetailController.deleteAutoTestUsers() + + // THEN + verify { userAccountService.deleteAutoTestUsers() } + result.statusCodeValue shouldBe HttpStatus.SC_OK + (result.body as BrnResponse<*>).data shouldBe usersCount + } + + @Test + fun `deleteAutoTestUserByEmail should return count of deleted users`() { + // GIVEN + val email = "autotest_n@170472339.1784415.com" + val usersCount = 1L + every { userAccountService.deleteAutoTestUserByEmail(email) } returns usersCount + + // WHEN + val result = userDetailController.deleteAutoTestUserByEmail(email) + + // THEN + verify { userAccountService.deleteAutoTestUserByEmail(email) } + result.statusCodeValue shouldBe HttpStatus.SC_OK + (result.body as BrnResponse<*>).data shouldBe usersCount + } } diff --git a/src/test/kotlin/com/epam/brn/integration/StudyHistoryIT.kt b/src/test/kotlin/com/epam/brn/integration/StudyHistoryIT.kt index 9f8be77d7..df0ce7841 100644 --- a/src/test/kotlin/com/epam/brn/integration/StudyHistoryIT.kt +++ b/src/test/kotlin/com/epam/brn/integration/StudyHistoryIT.kt @@ -1,19 +1,22 @@ package com.epam.brn.integration +import com.epam.brn.enums.BrnGender import com.epam.brn.enums.BrnRole import com.epam.brn.model.Exercise import com.epam.brn.model.ExerciseGroup -import com.epam.brn.enums.BrnGender +import com.epam.brn.model.Role import com.epam.brn.model.Series import com.epam.brn.model.StudyHistory import com.epam.brn.model.SubGroup import com.epam.brn.model.UserAccount import com.epam.brn.repo.ExerciseGroupRepository import com.epam.brn.repo.ExerciseRepository +import com.epam.brn.repo.RoleRepository import com.epam.brn.repo.SeriesRepository import com.epam.brn.repo.StudyHistoryRepository import com.epam.brn.repo.SubGroupRepository import com.epam.brn.repo.UserAccountRepository +import com.epam.brn.service.UserAccountService import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Test import org.springframework.beans.factory.annotation.Autowired @@ -54,6 +57,12 @@ class StudyHistoryIT : BaseIT() { @Autowired lateinit var exerciseGroupRepository: ExerciseGroupRepository + @Autowired + lateinit var roleRepository: RoleRepository + + @Autowired + lateinit var userAccountService: UserAccountService + @AfterEach fun deleteAfterTest() { studyHistoryRepository.deleteAll() @@ -62,6 +71,7 @@ class StudyHistoryIT : BaseIT() { seriesRepository.deleteAll() exerciseGroupRepository.deleteAll() userAccountRepository.deleteAll() + roleRepository.deleteAll() } @Test @@ -226,6 +236,110 @@ class StudyHistoryIT : BaseIT() { assertEquals(0, result) } + @Test + fun `test delete study history when delete autotest users`() { + // GIVEN + val roleUser = insertRole(BrnRole.USER) + + val user1 = UserAccount( + fullName = "autotest_n1", + email = "autotest_n@1704819771.8820736.com", + gender = BrnGender.MALE.toString(), + bornYear = 2000, + active = true, + ) + user1.roleSet = mutableSetOf(roleUser) + + val user2 = UserAccount( + fullName = "autotest_n1", + email = "autotest_n@170472339.1784415.com", + gender = BrnGender.MALE.toString(), + bornYear = 2000, + active = true, + ) + user2.roleSet = mutableSetOf(roleUser) + + userAccountRepository.saveAll(listOf(user1, user2)) + + val existingSeries = insertSeries() + val subGroup = insertSubGroup(existingSeries) + val existingExerciseFirst = insertExercise("FirstName", subGroup) + val existingExerciseSecond = insertExercise("SecondName", subGroup) + val now = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS) + val historyFirstExerciseOne = insertStudyHistory(user1, existingExerciseFirst, now) + val historySecondExerciseOne = insertStudyHistory(user2, existingExerciseSecond, now) + + studyHistoryRepository + .saveAll( + listOf( + historyFirstExerciseOne, + historySecondExerciseOne, + ) + ) + + // WHEN + val count = userAccountService.deleteAutoTestUsers() + + val result1 = user1.id?.let { + studyHistoryRepository.findLastByUserAccountIdAndExercises( + it, + listOf(existingExerciseFirst.id!!) + ) + } + + val result2 = user1.id?.let { + studyHistoryRepository.findLastByUserAccountIdAndExercises( + it, + listOf(existingExerciseFirst.id!!) + ) + } + + // THEN + assertEquals(2, count) + assertEquals(0, result1?.size) + assertEquals(0, result2?.size) + } + + @Test + fun `test delete study history when delete single autotest user`() { + // GIVEN + val roleUser = insertRole(BrnRole.USER) + val email = "autotest_n@1704819771.8820736.com" + + val user1 = UserAccount( + fullName = "autotest_n1", + email = email, + gender = BrnGender.MALE.toString(), + bornYear = 2000, + active = true, + ) + user1.roleSet = mutableSetOf(roleUser) + userAccountRepository.save(user1) + + val exerciseFirstName = "FirstName" + val existingSeries = insertSeries() + val subGroup = insertSubGroup(existingSeries) + val existingExerciseFirst = insertExercise(exerciseFirstName, subGroup) + val now = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS) + val historyFirstExerciseTwo = insertStudyHistory(user1, existingExerciseFirst, now) + + studyHistoryRepository.save(historyFirstExerciseTwo) + + // WHEN + val count = userAccountService.deleteAutoTestUserByEmail(email) + + val result1 = user1.id?.let { + studyHistoryRepository.findLastByUserAccountIdAndExercises( + it, + listOf(existingExerciseFirst.id!!) + ) + } + + // THEN + assertEquals(1, count) + assertEquals(0, result1?.size) + } + private fun insertStudyHistory( existingUser: UserAccount, existingExercise: Exercise, @@ -289,4 +403,6 @@ class StudyHistoryIT : BaseIT() { ) ) } + + private fun insertRole(roleName: String): Role = roleRepository.save(Role(name = roleName)) } diff --git a/src/test/kotlin/com/epam/brn/integration/UserDetailsControllerIT.kt b/src/test/kotlin/com/epam/brn/integration/UserDetailsControllerIT.kt index 928f22117..e1f0c9dfc 100644 --- a/src/test/kotlin/com/epam/brn/integration/UserDetailsControllerIT.kt +++ b/src/test/kotlin/com/epam/brn/integration/UserDetailsControllerIT.kt @@ -1,17 +1,17 @@ package com.epam.brn.integration import com.epam.brn.dto.HeadphonesDto +import com.epam.brn.dto.UserAccountDto import com.epam.brn.dto.request.UserAccountChangeRequest import com.epam.brn.dto.response.BrnResponse -import com.epam.brn.dto.UserAccountDto +import com.epam.brn.enums.BrnGender import com.epam.brn.enums.BrnRole import com.epam.brn.enums.HeadphonesType -import com.epam.brn.model.Role -import com.epam.brn.enums.BrnGender import com.epam.brn.model.Headphones +import com.epam.brn.model.Role import com.epam.brn.model.UserAccount -import com.epam.brn.repo.RoleRepository import com.epam.brn.repo.HeadphonesRepository +import com.epam.brn.repo.RoleRepository import com.epam.brn.repo.UserAccountRepository import com.fasterxml.jackson.core.type.TypeReference import com.google.gson.Gson @@ -22,14 +22,15 @@ import org.assertj.core.api.Assertions import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.Test import org.springframework.beans.factory.annotation.Autowired +import org.springframework.beans.factory.annotation.Value import org.springframework.security.test.context.support.WithMockUser import org.springframework.test.web.servlet.ResultActions import org.springframework.test.web.servlet.request.MockMvcRequestBuilders -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status import java.nio.charset.StandardCharsets @@ -48,6 +49,9 @@ class UserDetailsControllerIT : BaseIT() { @Autowired lateinit var roleRepository: RoleRepository + @Value("\${users.delete.prefix}") + private lateinit var prefix: String + internal val email: String = "test@test.test" private val baseUrl = "/users" private val currentUserBaseUrl = "$baseUrl/current" @@ -310,7 +314,6 @@ class UserDetailsControllerIT : BaseIT() { val response = mockMvc.perform( MockMvcRequestBuilders.get(baseUrl) .param("role", BrnRole.ADMIN) - ) .andExpect(status().isOk) .andReturn().response.getContentAsString(StandardCharsets.UTF_8) @@ -323,6 +326,96 @@ class UserDetailsControllerIT : BaseIT() { users.size shouldBe 1 } + @Test + fun `deleteAutoTestUsers should delete all auto test users`() { + // GIVEN + val roleUser = insertRole(BrnRole.USER) + + val user1 = UserAccount( + fullName = "autotest_n1", + email = "autotest_n@1704819771.8820736.com", + gender = BrnGender.MALE.toString(), + bornYear = 2000, + active = true, + ) + user1.roleSet = mutableSetOf(roleUser) + + val user2 = UserAccount( + fullName = "autotest_n1", + email = "autotest_n@170472339.1784415.com", + gender = BrnGender.MALE.toString(), + bornYear = 2000, + active = true, + ) + user2.roleSet = mutableSetOf(roleUser) + + userAccountRepository.save(user1) + userAccountRepository.save(user2) + + // WHEN + val response = mockMvc.perform( + delete("$baseUrl/autotest/del") + .contentType("application/json") + ).andExpect(status().isOk) + .andReturn().response.getContentAsString(StandardCharsets.UTF_8) + + val data = gson.fromJson(response, BrnResponse::class.java).data + + // THEN + data shouldBe 2 + } + + @Test + fun `deleteAutoTestUserByEmail should delete auto test user by email`() { + // GIVEN + val roleUser = insertRole(BrnRole.USER) + val email = "autotest_n@1704819771.8820736.com" + + val user1 = UserAccount( + fullName = "autotest_n1", + email = email, + gender = BrnGender.MALE.toString(), + bornYear = 2000, + active = true, + ) + user1.roleSet = mutableSetOf(roleUser) + userAccountRepository.save(user1) + + // WHEN + val response = mockMvc.perform( + delete("$baseUrl/autotest/del/$email") + .contentType("application/json") + ).andExpect(status().isOk) + .andReturn().response.getContentAsString(StandardCharsets.UTF_8) + + val data = gson.fromJson(response, BrnResponse::class.java).data + + // THEN + data shouldBe 1 + } + + @Test + fun `deleteAutoTestUserByEmail should return 500 when email not autotest`() { + // GIVEN + val roleUser = insertRole(BrnRole.USER) + val email = "abc@xyz.com" + + val user1 = UserAccount( + fullName = "user1", + email = email, + gender = BrnGender.MALE.toString(), + bornYear = 2000, + active = true, + ) + user1.roleSet = mutableSetOf(roleUser) + userAccountRepository.save(user1) + + // WHEN + mockMvc.perform( + delete("$baseUrl/autotest/del/$email") + .contentType("application/json") + ).andExpect(status().isBadRequest) + } @AfterEach fun deleteAfterTest() { userAccountRepository.deleteAll() diff --git a/src/test/kotlin/com/epam/brn/service/UserAccountServiceTest.kt b/src/test/kotlin/com/epam/brn/service/UserAccountServiceTest.kt index 24101b3a0..f7a681fdb 100644 --- a/src/test/kotlin/com/epam/brn/service/UserAccountServiceTest.kt +++ b/src/test/kotlin/com/epam/brn/service/UserAccountServiceTest.kt @@ -1,14 +1,14 @@ package com.epam.brn.service import com.epam.brn.dto.HeadphonesDto -import com.epam.brn.dto.request.UserAccountChangeRequest import com.epam.brn.dto.UserAccountDto +import com.epam.brn.dto.request.UserAccountChangeRequest +import com.epam.brn.enums.BrnGender import com.epam.brn.enums.BrnRole import com.epam.brn.enums.HeadphonesType import com.epam.brn.exception.EntityNotFoundException -import com.epam.brn.model.Role -import com.epam.brn.enums.BrnGender import com.epam.brn.model.Headphones +import com.epam.brn.model.Role import com.epam.brn.model.UserAccount import com.epam.brn.repo.UserAccountRepository import com.epam.brn.service.impl.UserAccountServiceImpl @@ -20,12 +20,13 @@ import io.mockk.every import io.mockk.impl.annotations.InjectMockKs import io.mockk.impl.annotations.MockK import io.mockk.junit5.MockKExtension -import io.mockk.mockkClass import io.mockk.just +import io.mockk.mockkClass import io.mockk.slot import io.mockk.verify import org.apache.commons.lang3.math.NumberUtils import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Assertions.assertThrows import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test @@ -34,6 +35,7 @@ import org.springframework.data.domain.Pageable import org.springframework.security.core.Authentication import org.springframework.security.core.context.SecurityContext import org.springframework.security.core.context.SecurityContextHolder +import org.springframework.test.util.ReflectionTestUtils import java.time.LocalDateTime import java.time.ZoneOffset import java.util.Optional @@ -497,6 +499,51 @@ internal class UserAccountServiceTest { } } + @Test + fun `should delete all auto test users`() { + // GIVEN + val usersCount = 2L + val prefix = "autotest" + ReflectionTestUtils.setField(userAccountService, "prefix", prefix) + every { userAccountService.deleteAutoTestUsers() } returns usersCount + + // WHEN + userAccountService.deleteAutoTestUsers() + + // THEN + verify { userAccountRepository.deleteUserAccountsByEmailStartsWith(prefix) } + } + + @Test + fun `should delete auto test user by email`() { + // GIVEN + val usersCount = 1L + val email = "autotest_n@1704819771.8820736.com" + val prefix = "autotest" + ReflectionTestUtils.setField(userAccountService, "prefix", prefix) + every { userAccountService.deleteAutoTestUserByEmail(email) } returns usersCount + + // WHEN + userAccountService.deleteAutoTestUserByEmail(email) + + // THEN + verify { userAccountRepository.deleteUserAccountByEmailIs(email) } + } + + @Test + fun `should throw IllegalArgumentException when email not starts from prefix`() { + // GIVEN + val usersCount = 1L + val email = "aaa@bbb.com" + val prefix = "autotest" + ReflectionTestUtils.setField(userAccountService, "prefix", prefix) + + // WHEN & THEN + assertThrows(IllegalArgumentException::class.java) { + userAccountService.deleteAutoTestUserByEmail(email) + } + } + @Nested @DisplayName("Doctor related tests") inner class DoctorFunctionality { diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index 8b85fd421..8b4f9b73a 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -130,3 +130,5 @@ github.api.read-timeout=30000 github.api.url.base=https://api.github.com github.api.url.path.contributors=/repos/{OWNER}/{REPO}/contributors github.api.url.path.users=/users/{username} + +users.delete.prefix=autotest \ No newline at end of file