From 8aba361d1894661b110d2acab6f3aa3cca32f6ff Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Mon, 24 Jul 2023 14:59:08 +0300 Subject: [PATCH 1/4] Fix tests after #2344 ### What's done: - avoid changing Spring's ObjectMapper It's part of #2336 --- .../saveourtool/save/gateway/service/BackendService.kt | 10 +++++----- .../backend/controllers/internal/UsersController.kt | 10 +++++----- .../save/backend/storage/BackendInternalFileStorage.kt | 2 +- .../backend/controller/TestExecutionControllerTest.kt | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/api-gateway/src/main/kotlin/com/saveourtool/save/gateway/service/BackendService.kt b/api-gateway/src/main/kotlin/com/saveourtool/save/gateway/service/BackendService.kt index 9f60e95552..71bf0188ec 100644 --- a/api-gateway/src/main/kotlin/com/saveourtool/save/gateway/service/BackendService.kt +++ b/api-gateway/src/main/kotlin/com/saveourtool/save/gateway/service/BackendService.kt @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.ObjectMapper import org.springframework.http.MediaType import org.springframework.security.core.userdetails.User as SpringUser import org.springframework.security.core.userdetails.UserDetails +import org.springframework.security.jackson2.CoreJackson2Module import org.springframework.security.jackson2.SecurityJackson2Modules import org.springframework.stereotype.Service import org.springframework.web.reactive.function.client.WebClient @@ -20,12 +21,11 @@ import reactor.core.publisher.Mono @Service class BackendService( configurationProperties: ConfigurationProperties, - objectMapper: ObjectMapper, ) { - private val springUserDetailsReader = objectMapper - .also { - it.registerModules(SecurityJackson2Modules.getModules(javaClass.classLoader)) - } + private val springUserDetailsReader = ObjectMapper() + .findAndRegisterModules() + .registerModule(CoreJackson2Module()) + .registerModules(SecurityJackson2Modules.getModules(javaClass.classLoader)) .readerFor(SpringUser::class.java) private val webClient = WebClient.create(configurationProperties.backend.url) diff --git a/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/internal/UsersController.kt b/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/internal/UsersController.kt index aeebff20ff..2776a4e7a2 100644 --- a/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/internal/UsersController.kt +++ b/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/internal/UsersController.kt @@ -8,6 +8,7 @@ import com.saveourtool.save.utils.StringResponse import com.fasterxml.jackson.databind.ObjectMapper import org.slf4j.LoggerFactory import org.springframework.http.ResponseEntity +import org.springframework.security.jackson2.CoreJackson2Module import org.springframework.security.core.userdetails.User as SpringUser import org.springframework.security.jackson2.SecurityJackson2Modules import org.springframework.transaction.annotation.Transactional @@ -26,13 +27,12 @@ import reactor.core.publisher.Mono class UsersController( private val userService: UserDetailsService, private val originalLoginRepository: OriginalLoginRepository, - objectMapper: ObjectMapper, ) { private val logger = LoggerFactory.getLogger(javaClass) - private val springUserDetailsWriter = objectMapper - .also { - it.registerModules(SecurityJackson2Modules.getModules(javaClass.classLoader)) - } + private val springUserDetailsWriter = ObjectMapper() + .findAndRegisterModules() + .registerModule(CoreJackson2Module()) + .registerModules(SecurityJackson2Modules.getModules(javaClass.classLoader)) .writerFor(SpringUser::class.java) /** diff --git a/save-backend/src/main/kotlin/com/saveourtool/save/backend/storage/BackendInternalFileStorage.kt b/save-backend/src/main/kotlin/com/saveourtool/save/backend/storage/BackendInternalFileStorage.kt index d5c69b8361..ebffc380d2 100644 --- a/save-backend/src/main/kotlin/com/saveourtool/save/backend/storage/BackendInternalFileStorage.kt +++ b/save-backend/src/main/kotlin/com/saveourtool/save/backend/storage/BackendInternalFileStorage.kt @@ -22,6 +22,6 @@ class BackendInternalFileStorage( s3Operations, ) { override suspend fun doInitAdditionally(underlying: DefaultStorageCoroutines) { - underlying.downloadSaveCliFromGithub() +// underlying.downloadSaveCliFromGithub() } } diff --git a/save-backend/src/test/kotlin/com/saveourtool/save/backend/controller/TestExecutionControllerTest.kt b/save-backend/src/test/kotlin/com/saveourtool/save/backend/controller/TestExecutionControllerTest.kt index 4b986cf814..f2bdba53ed 100644 --- a/save-backend/src/test/kotlin/com/saveourtool/save/backend/controller/TestExecutionControllerTest.kt +++ b/save-backend/src/test/kotlin/com/saveourtool/save/backend/controller/TestExecutionControllerTest.kt @@ -42,7 +42,7 @@ import reactor.kotlin.core.publisher.toMono import java.time.Instant @SpringBootTest(classes = [SaveApplication::class]) -@AutoConfigureWebTestClient +@AutoConfigureWebTestClient(timeout = "5d") @ExtendWith(InfraExtension::class) @MockBeans( MockBean(ProjectController::class), @@ -163,7 +163,7 @@ class TestExecutionControllerTest { webClient.post() .uri("/internal/saveTestResult") .contentType(MediaType.APPLICATION_JSON) - .body(BodyInserters.fromValue(listOf(testExecutionDtoFirst, testExecutionDtoSecond))) + .body(BodyInserters.fromValue(listOf(testExecutionDtoFirst, testExecutionDtoSecond).toMutableList())) .exchange() .expectBody() .isEqualTo("Saved") From ace469bf8ea018a98b75ce3550a94a9fd2034251 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Mon, 24 Jul 2023 15:00:15 +0300 Subject: [PATCH 2/4] reverted test changes --- .../save/backend/storage/BackendInternalFileStorage.kt | 2 +- .../save/backend/controller/TestExecutionControllerTest.kt | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/save-backend/src/main/kotlin/com/saveourtool/save/backend/storage/BackendInternalFileStorage.kt b/save-backend/src/main/kotlin/com/saveourtool/save/backend/storage/BackendInternalFileStorage.kt index ebffc380d2..d5c69b8361 100644 --- a/save-backend/src/main/kotlin/com/saveourtool/save/backend/storage/BackendInternalFileStorage.kt +++ b/save-backend/src/main/kotlin/com/saveourtool/save/backend/storage/BackendInternalFileStorage.kt @@ -22,6 +22,6 @@ class BackendInternalFileStorage( s3Operations, ) { override suspend fun doInitAdditionally(underlying: DefaultStorageCoroutines) { -// underlying.downloadSaveCliFromGithub() + underlying.downloadSaveCliFromGithub() } } diff --git a/save-backend/src/test/kotlin/com/saveourtool/save/backend/controller/TestExecutionControllerTest.kt b/save-backend/src/test/kotlin/com/saveourtool/save/backend/controller/TestExecutionControllerTest.kt index f2bdba53ed..4b986cf814 100644 --- a/save-backend/src/test/kotlin/com/saveourtool/save/backend/controller/TestExecutionControllerTest.kt +++ b/save-backend/src/test/kotlin/com/saveourtool/save/backend/controller/TestExecutionControllerTest.kt @@ -42,7 +42,7 @@ import reactor.kotlin.core.publisher.toMono import java.time.Instant @SpringBootTest(classes = [SaveApplication::class]) -@AutoConfigureWebTestClient(timeout = "5d") +@AutoConfigureWebTestClient @ExtendWith(InfraExtension::class) @MockBeans( MockBean(ProjectController::class), @@ -163,7 +163,7 @@ class TestExecutionControllerTest { webClient.post() .uri("/internal/saveTestResult") .contentType(MediaType.APPLICATION_JSON) - .body(BodyInserters.fromValue(listOf(testExecutionDtoFirst, testExecutionDtoSecond).toMutableList())) + .body(BodyInserters.fromValue(listOf(testExecutionDtoFirst, testExecutionDtoSecond))) .exchange() .expectBody() .isEqualTo("Saved") From fb2452e8e42f2e8e1e2ae2d2ab4b3b3f7e8a5395 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Mon, 24 Jul 2023 15:02:19 +0300 Subject: [PATCH 3/4] diktatFix --- .../save/backend/controllers/internal/UsersController.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/internal/UsersController.kt b/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/internal/UsersController.kt index 2776a4e7a2..eb0c6ff583 100644 --- a/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/internal/UsersController.kt +++ b/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/internal/UsersController.kt @@ -8,8 +8,8 @@ import com.saveourtool.save.utils.StringResponse import com.fasterxml.jackson.databind.ObjectMapper import org.slf4j.LoggerFactory import org.springframework.http.ResponseEntity -import org.springframework.security.jackson2.CoreJackson2Module import org.springframework.security.core.userdetails.User as SpringUser +import org.springframework.security.jackson2.CoreJackson2Module import org.springframework.security.jackson2.SecurityJackson2Modules import org.springframework.transaction.annotation.Transactional import org.springframework.web.bind.annotation.GetMapping From b38cbcb3a6446e258608182c66c60ddc405e1026 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Mon, 24 Jul 2023 16:13:53 +0300 Subject: [PATCH 4/4] removed old tests --- .../backend/controller/UsersControllerTest.kt | 31 +++---------------- .../backend/security/BasicSecurityTest.kt | 13 ++------ 2 files changed, 7 insertions(+), 37 deletions(-) diff --git a/save-backend/src/test/kotlin/com/saveourtool/save/backend/controller/UsersControllerTest.kt b/save-backend/src/test/kotlin/com/saveourtool/save/backend/controller/UsersControllerTest.kt index 180beafb58..93bc200eb3 100644 --- a/save-backend/src/test/kotlin/com/saveourtool/save/backend/controller/UsersControllerTest.kt +++ b/save-backend/src/test/kotlin/com/saveourtool/save/backend/controller/UsersControllerTest.kt @@ -2,11 +2,7 @@ package com.saveourtool.save.backend.controller import com.saveourtool.save.backend.SaveApplication import com.saveourtool.save.backend.utils.InfraExtension -import com.saveourtool.save.domain.Role -import com.saveourtool.save.entities.OriginalLogin -import com.saveourtool.save.entities.User import com.saveourtool.save.info.UserInfo -import com.saveourtool.save.info.UserStatus import com.saveourtool.save.v1 import org.junit.jupiter.api.Assertions import org.junit.jupiter.api.Test @@ -14,8 +10,6 @@ import org.junit.jupiter.api.extension.ExtendWith import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient import org.springframework.boot.test.context.SpringBootTest -import org.springframework.boot.test.mock.mockito.MockBean -import org.springframework.boot.test.mock.mockito.MockBeans import org.springframework.http.MediaType import org.springframework.security.test.context.support.WithMockUser import org.springframework.test.web.reactive.server.WebTestClient @@ -30,40 +24,23 @@ class UsersControllerTest { @Test @WithMockUser - fun `serialize and deserialize user`() { - val user = User( - "admin2", - null, - Role.VIEWER.asSpringSecurityRole(), - "basic2", - null, - status = UserStatus.CREATED, - ).apply { id = 99 } - - val originalLogin = OriginalLogin( - "admin", - user, - "basic" - ).apply { id = 4 } - user.apply { originalLogins = listOf(originalLogin) } - + fun `new user`() { webTestClient.post() - .uri("/internal/users/new") + .uri("/internal/users/new/basic/admin") .contentType(MediaType.APPLICATION_JSON) - .bodyValue(user) .exchange() .expectStatus() .isOk webTestClient.get() - .uri("/api/$v1/users/admin2") + .uri("/api/$v1/users/admin") .exchange() .expectStatus() .isOk .expectBody() .consumeWith { requireNotNull(it.responseBody) - Assertions.assertEquals("admin2", it.responseBody!!.name) + Assertions.assertEquals("admin", it.responseBody!!.name) } } } diff --git a/save-backend/src/test/kotlin/com/saveourtool/save/backend/security/BasicSecurityTest.kt b/save-backend/src/test/kotlin/com/saveourtool/save/backend/security/BasicSecurityTest.kt index 2fadf967e1..deb46e4b9c 100644 --- a/save-backend/src/test/kotlin/com/saveourtool/save/backend/security/BasicSecurityTest.kt +++ b/save-backend/src/test/kotlin/com/saveourtool/save/backend/security/BasicSecurityTest.kt @@ -52,22 +52,15 @@ class BasicSecurityTest { @Test fun `should allow access for registered user`() { - val authentication = tryAuthenticate("basic:user") + val authentication = tryAuthenticate("user") Assertions.assertTrue(authentication.isAuthenticated) } @Test - fun `should forbid requests if user has the same name but different source`() { + fun `should forbid requests if user not valid`() { Assertions.assertThrows(BadCredentialsException::class.java) { - tryAuthenticate("github:user") - } - } - - @Test - fun `should forbid requests if user has the same name but no source`() { - Assertions.assertThrows(BadCredentialsException::class.java) { - tryAuthenticate(":user") + tryAuthenticate("not_existed") } }