-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrationtest get oidc username from endpoint
RISDEV-6102
- Loading branch information
1 parent
7bc679b
commit bac2348
Showing
1 changed file
with
33 additions
and
9 deletions.
There are no files selected for viewing
42 changes: 33 additions & 9 deletions
42
...ervice/ris/norms/integration/adapter/input/restapi/UserInfoControllerIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,51 @@ | ||
package de.bund.digitalservice.ris.norms.integration.adapter.input.restapi; | ||
|
||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import de.bund.digitalservice.ris.norms.integration.BaseIntegrationTest; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; | ||
import org.springframework.security.oauth2.core.user.DefaultOAuth2User; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | ||
|
||
class UserInfoControllerIntegrationTest extends BaseIntegrationTest { | ||
|
||
@Autowired | ||
MockMvc mvc; | ||
private MockMvc mockMvc; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
OAuth2User oAuth2User = new DefaultOAuth2User( | ||
Collections.singletonList(() -> "ROLE_USER"), | ||
Map.of("name", "testuser"), | ||
"name" | ||
); | ||
SecurityContextHolder | ||
.getContext() | ||
.setAuthentication( | ||
new OAuth2AuthenticationToken( | ||
oAuth2User, | ||
Collections.singletonList(() -> "ROLE_USER"), | ||
"client-id" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void getUserInfo() throws Exception { | ||
mvc | ||
.perform(get("/api/v1/me").with(user("user")).accept(MediaType.APPLICATION_JSON)) | ||
// then | ||
void getUserInfo_ShouldReturnUserInfo() throws Exception { | ||
mockMvc | ||
.perform(get("/api/v1/me")) | ||
.andDo(print()) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("name").value("user")); | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.name").value("testuser")); | ||
} | ||
} |