Skip to content

Commit

Permalink
Integrationtest get oidc username from endpoint
Browse files Browse the repository at this point in the history
RISDEV-6102
  • Loading branch information
SebastianRossa committed Jan 13, 2025
1 parent 7bc679b commit bac2348
Showing 1 changed file with 33 additions and 9 deletions.
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"));
}
}

0 comments on commit bac2348

Please sign in to comment.