Skip to content
This repository has been archived by the owner on Dec 15, 2024. It is now read-only.

feat: add SpringMockUserExtension to fake users #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rbraeunlich
Copy link

Compatible with Spring 2 and Spring security 5.

@rbraeunlich
Copy link
Author

Hi everyone, I finally managed a working extension for the Spring mock user.
Nevertheless, I think this will need a lot of discussion 😅 In order to register the mock user at the right moment I had to introduce a new hook interface that executes before Spring does its magic. Additionally, the change introduces a hard dependency to Spring security 5 and Reactor 3 dot something. Initially, I had it working with Spring Boot 3 and when backporting it I could see some breaking changes.
Maybe it would be better to separate the whole thing into a submodule. I'm open to suggestions.

Compatible with Spring 2 and Spring security 5.
@rbraeunlich rbraeunlich force-pushed the feat/spring-mock-user branch from 80347a0 to 184322b Compare November 30, 2024 10:31
@SpringBootTest(classes = [SpringTestApplication::class])
@AutoConfigureWebTestClient
class SpringMockUserExtensionIntegrationTest(
@Autowired private val webTestClient: WebTestClient,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@Autowired private val webTestClient: WebTestClient,
private val webTestClient: WebTestClient,

Just a small FYI. The Kotest SpringAutowireConstructorExtension actually doesn't care for @Autowired annotations, so they can be omitted :)

Comment on lines +35 to +42
TestCase(
descriptor = SpringMockUserExtensionTest::class.toDescriptor().append("aaa"),
name = TestName("name"),
spec = this@SpringMockUserExtensionTest,
test = {},
source = sourceRef(),
type = TestType.Test
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could perhaps be extracted, seems to be used in every test w/o any relevant differences?

Comment on lines +24 to +36
init {
describe("ADMIN") {
extensions(SpringMockUserExtension(authorities = listOf("ADMIN")))
it("should provide mock authentication") {
webTestClient
.get()
.uri("/secure")
.exchange()
.expectStatus()
.isOk
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great to see an integration test 💯

Comment on lines +17 to +20
private val username: String = "user",
private val password: String = "password",
private val roles: List<String> = listOf("USER"),
private val authorities: List<String> = listOf()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these defaults used by Spring's own @WithMockUser as well?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants