Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests after #2344 #2368

Merged
merged 4 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
import org.slf4j.LoggerFactory
import org.springframework.http.ResponseEntity
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
Expand All @@ -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)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@ 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
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
Expand All @@ -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<UserInfo>()
.consumeWith {
requireNotNull(it.responseBody)
Assertions.assertEquals("admin2", it.responseBody!!.name)
Assertions.assertEquals("admin", it.responseBody!!.name)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down
Loading