Changing API design and making tests compatible #106
@ -11,6 +11,8 @@ import com.hithomelabs.CFTunnels.Models.Ingress;
|
|||||||
import com.hithomelabs.CFTunnels.Models.TunnelResponse;
|
import com.hithomelabs.CFTunnels.Models.TunnelResponse;
|
||||||
import com.hithomelabs.CFTunnels.Services.CloudflareAPIService;
|
import com.hithomelabs.CFTunnels.Services.CloudflareAPIService;
|
||||||
import com.hithomelabs.CFTunnels.Services.MappingRequestService;
|
import com.hithomelabs.CFTunnels.Services.MappingRequestService;
|
||||||
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
|
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.web.servlet.error.ErrorController;
|
import org.springframework.boot.web.servlet.error.ErrorController;
|
||||||
import org.springframework.http.*;
|
import org.springframework.http.*;
|
||||||
@ -64,6 +66,7 @@ public class TunnelController implements ErrorController {
|
|||||||
|
|
||||||
@PreAuthorize("hasAnyRole('USER')")
|
@PreAuthorize("hasAnyRole('USER')")
|
||||||
@GetMapping("/tunnels")
|
@GetMapping("/tunnels")
|
||||||
|
@Operation( security = { @SecurityRequirement(name = "oidcAuth") } )
|
||||||
public ResponseEntity<Map<String,Object>> getTunnels(){
|
public ResponseEntity<Map<String,Object>> getTunnels(){
|
||||||
|
|
||||||
ResponseEntity<Map> responseEntity = cloudflareAPIService.getCloudflareTunnels();
|
ResponseEntity<Map> responseEntity = cloudflareAPIService.getCloudflareTunnels();
|
||||||
@ -75,7 +78,7 @@ public class TunnelController implements ErrorController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PreAuthorize("hasAnyRole('DEVELOPER')")
|
@PreAuthorize("hasAnyRole('DEVELOPER')")
|
||||||
@GetMapping("/tunnel/{tunnelId}")
|
@GetMapping("/tunnel/{tunnelId}/mappings")
|
||||||
public ResponseEntity<Map<String,Object>> getTunnelConfigurations(@PathVariable String tunnelId) {
|
public ResponseEntity<Map<String,Object>> getTunnelConfigurations(@PathVariable String tunnelId) {
|
||||||
|
|
||||||
ResponseEntity<Map> responseEntity = cloudflareAPIService.getCloudflareTunnelConfigurations(tunnelId, restTemplate, Map.class);
|
ResponseEntity<Map> responseEntity = cloudflareAPIService.getCloudflareTunnelConfigurations(tunnelId, restTemplate, Map.class);
|
||||||
@ -88,7 +91,7 @@ public class TunnelController implements ErrorController {
|
|||||||
|
|
||||||
// 50df9101-f625-4618-b7c5-100338a57124
|
// 50df9101-f625-4618-b7c5-100338a57124
|
||||||
@PreAuthorize("hasAnyRole('ADMIN')")
|
@PreAuthorize("hasAnyRole('ADMIN')")
|
||||||
@PutMapping("/tunnel/{tunnelId}/add")
|
@PostMapping("/tunnel/{tunnelId}/mappings")
|
||||||
public ResponseEntity<Map<String, Object>> addTunnelconfiguration(@PathVariable String tunnelId, @RequestBody Ingress ingress) throws JsonProcessingException {
|
public ResponseEntity<Map<String, Object>> addTunnelconfiguration(@PathVariable String tunnelId, @RequestBody Ingress ingress) throws JsonProcessingException {
|
||||||
|
|
||||||
ResponseEntity<TunnelResponse> responseEntity = cloudflareAPIService.getCloudflareTunnelConfigurations(tunnelId, restTemplateConfig.restTemplate(), TunnelResponse.class);
|
ResponseEntity<TunnelResponse> responseEntity = cloudflareAPIService.getCloudflareTunnelConfigurations(tunnelId, restTemplateConfig.restTemplate(), TunnelResponse.class);
|
||||||
@ -110,7 +113,7 @@ public class TunnelController implements ErrorController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PreAuthorize("hasAnyRole('DEVELOPER')")
|
@PreAuthorize("hasAnyRole('DEVELOPER')")
|
||||||
@PutMapping("/tunnel/{tunnelId}/delete")
|
@DeleteMapping("/tunnel/{tunnelId}/mappings")
|
||||||
public ResponseEntity<Map<String, Object>> deleteTunnelConfiguration(@PathVariable String tunnelId, @RequestBody Ingress ingress) throws JsonProcessingException {
|
public ResponseEntity<Map<String, Object>> deleteTunnelConfiguration(@PathVariable String tunnelId, @RequestBody Ingress ingress) throws JsonProcessingException {
|
||||||
|
|
||||||
ResponseEntity<TunnelResponse> responseEntity = cloudflareAPIService.getCloudflareTunnelConfigurations(tunnelId, restTemplateConfig.restTemplate(), TunnelResponse.class);
|
ResponseEntity<TunnelResponse> responseEntity = cloudflareAPIService.getCloudflareTunnelConfigurations(tunnelId, restTemplateConfig.restTemplate(), TunnelResponse.class);
|
||||||
@ -139,7 +142,7 @@ public class TunnelController implements ErrorController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PreAuthorize("hasAnyRole('DEVELOPER')")
|
@PreAuthorize("hasAnyRole('DEVELOPER')")
|
||||||
@PutMapping("/tunnel/{tunnelId}/request")
|
@PostMapping("/tunnel/{tunnelId}/requests")
|
||||||
public ResponseEntity<Request> createTunnelMappingRequest(@PathVariable String tunnelId, @AuthenticationPrincipal OidcUser oidcUser, @RequestBody Ingress ingess){
|
public ResponseEntity<Request> createTunnelMappingRequest(@PathVariable String tunnelId, @AuthenticationPrincipal OidcUser oidcUser, @RequestBody Ingress ingess){
|
||||||
Request request = mappingRequestService.createMappingRequest(tunnelId, ingess, oidcUser);
|
Request request = mappingRequestService.createMappingRequest(tunnelId, ingess, oidcUser);
|
||||||
if(request.getId() != null)
|
if(request.getId() != null)
|
||||||
|
|||||||
@ -36,8 +36,7 @@ import static org.mockito.ArgumentMatchers.eq;
|
|||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
|
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
|
||||||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.oauth2Login;
|
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.oauth2Login;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
|
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||||
import static org.hamcrest.Matchers.not;
|
import static org.hamcrest.Matchers.not;
|
||||||
@ -162,7 +161,7 @@ class TunnelControllerTest {
|
|||||||
|
|
||||||
when(cloudflareAPIService.getCloudflareTunnelConfigurations(eq("sampleTunnelId"), any(RestTemplate.class), eq(Map.class))).thenReturn(mockResponse);
|
when(cloudflareAPIService.getCloudflareTunnelConfigurations(eq("sampleTunnelId"), any(RestTemplate.class), eq(Map.class))).thenReturn(mockResponse);
|
||||||
|
|
||||||
mockMvc.perform(get("/cloudflare/tunnel/{tunnelId}", "sampleTunnelId")
|
mockMvc.perform(get("/cloudflare/tunnel/{tunnelId}/mappings", "sampleTunnelId")
|
||||||
.with(oauth2Login().oauth2User(buildOidcUser("username", Groups.HOMELAB_DEVELOPER))))
|
.with(oauth2Login().oauth2User(buildOidcUser("username", Groups.HOMELAB_DEVELOPER))))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
|
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
|
||||||
@ -184,7 +183,7 @@ class TunnelControllerTest {
|
|||||||
ResponseEntity<TunnelResponse> expectedHttpTunnelResponse = new ResponseEntity<>(expectedTunnelConfig, HttpStatus.OK);
|
ResponseEntity<TunnelResponse> expectedHttpTunnelResponse = new ResponseEntity<>(expectedTunnelConfig, HttpStatus.OK);
|
||||||
when(cloudflareAPIService.putCloudflareTunnelConfigurations(eq("sampleTunnelId"), any(RestTemplate.class), eq(TunnelResponse.class), any(Config.class))).thenReturn(expectedHttpTunnelResponse);
|
when(cloudflareAPIService.putCloudflareTunnelConfigurations(eq("sampleTunnelId"), any(RestTemplate.class), eq(TunnelResponse.class), any(Config.class))).thenReturn(expectedHttpTunnelResponse);
|
||||||
|
|
||||||
mockMvc.perform(put("/cloudflare/tunnel/{tunnelId}/add", "sampleTunnelId")
|
mockMvc.perform(post("/cloudflare/tunnel/{tunnelId}/mappings", "sampleTunnelId")
|
||||||
.with(oauth2Login().oauth2User(buildOidcUser("admin", Groups.SYSTEM_ADMIN)))
|
.with(oauth2Login().oauth2User(buildOidcUser("admin", Groups.SYSTEM_ADMIN)))
|
||||||
.with(csrf())
|
.with(csrf())
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
@ -209,7 +208,7 @@ class TunnelControllerTest {
|
|||||||
ResponseEntity<TunnelResponse> expectedHttpTunnelResponse = new ResponseEntity<>(expectedTunnelConfig, HttpStatus.OK);
|
ResponseEntity<TunnelResponse> expectedHttpTunnelResponse = new ResponseEntity<>(expectedTunnelConfig, HttpStatus.OK);
|
||||||
when(cloudflareAPIService.putCloudflareTunnelConfigurations(eq("sampleTunnelId"), any(RestTemplate.class), eq(TunnelResponse.class), any(Config.class))).thenReturn(expectedHttpTunnelResponse);
|
when(cloudflareAPIService.putCloudflareTunnelConfigurations(eq("sampleTunnelId"), any(RestTemplate.class), eq(TunnelResponse.class), any(Config.class))).thenReturn(expectedHttpTunnelResponse);
|
||||||
|
|
||||||
mockMvc.perform(put("/cloudflare/tunnel/{tunnelId}/delete", "sampleTunnelId")
|
mockMvc.perform(delete("/cloudflare/tunnel/{tunnelId}/mappings", "sampleTunnelId")
|
||||||
.with(oauth2Login().oauth2User(buildOidcUser("admin", Groups.SYSTEM_ADMIN)))
|
.with(oauth2Login().oauth2User(buildOidcUser("admin", Groups.SYSTEM_ADMIN)))
|
||||||
.with(csrf())
|
.with(csrf())
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user