From 71161a4da121f9ade16d0376db80252ffd6b8bf1 Mon Sep 17 00:00:00 2001 From: hitanshu310 Date: Wed, 28 Jan 2026 00:21:13 +0530 Subject: [PATCH] Changing API design and making tests compatible --- .../CFTunnels/Controllers/TunnelController.java | 11 +++++++---- .../CFTunnels/Controllers/TunnelControllerTest.java | 9 ++++----- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/hithomelabs/CFTunnels/Controllers/TunnelController.java b/src/main/java/com/hithomelabs/CFTunnels/Controllers/TunnelController.java index 53fb4f9..d8343b9 100644 --- a/src/main/java/com/hithomelabs/CFTunnels/Controllers/TunnelController.java +++ b/src/main/java/com/hithomelabs/CFTunnels/Controllers/TunnelController.java @@ -11,6 +11,8 @@ import com.hithomelabs.CFTunnels.Models.Ingress; import com.hithomelabs.CFTunnels.Models.TunnelResponse; import com.hithomelabs.CFTunnels.Services.CloudflareAPIService; 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.boot.web.servlet.error.ErrorController; import org.springframework.http.*; @@ -64,6 +66,7 @@ public class TunnelController implements ErrorController { @PreAuthorize("hasAnyRole('USER')") @GetMapping("/tunnels") + @Operation( security = { @SecurityRequirement(name = "oidcAuth") } ) public ResponseEntity> getTunnels(){ ResponseEntity responseEntity = cloudflareAPIService.getCloudflareTunnels(); @@ -75,7 +78,7 @@ public class TunnelController implements ErrorController { } @PreAuthorize("hasAnyRole('DEVELOPER')") - @GetMapping("/tunnel/{tunnelId}") + @GetMapping("/tunnel/{tunnelId}/mappings") public ResponseEntity> getTunnelConfigurations(@PathVariable String tunnelId) { ResponseEntity responseEntity = cloudflareAPIService.getCloudflareTunnelConfigurations(tunnelId, restTemplate, Map.class); @@ -88,7 +91,7 @@ public class TunnelController implements ErrorController { // 50df9101-f625-4618-b7c5-100338a57124 @PreAuthorize("hasAnyRole('ADMIN')") - @PutMapping("/tunnel/{tunnelId}/add") + @PostMapping("/tunnel/{tunnelId}/mappings") public ResponseEntity> addTunnelconfiguration(@PathVariable String tunnelId, @RequestBody Ingress ingress) throws JsonProcessingException { ResponseEntity responseEntity = cloudflareAPIService.getCloudflareTunnelConfigurations(tunnelId, restTemplateConfig.restTemplate(), TunnelResponse.class); @@ -110,7 +113,7 @@ public class TunnelController implements ErrorController { } @PreAuthorize("hasAnyRole('DEVELOPER')") - @PutMapping("/tunnel/{tunnelId}/delete") + @DeleteMapping("/tunnel/{tunnelId}/mappings") public ResponseEntity> deleteTunnelConfiguration(@PathVariable String tunnelId, @RequestBody Ingress ingress) throws JsonProcessingException { ResponseEntity responseEntity = cloudflareAPIService.getCloudflareTunnelConfigurations(tunnelId, restTemplateConfig.restTemplate(), TunnelResponse.class); @@ -139,7 +142,7 @@ public class TunnelController implements ErrorController { } @PreAuthorize("hasAnyRole('DEVELOPER')") - @PutMapping("/tunnel/{tunnelId}/request") + @PostMapping("/tunnel/{tunnelId}/requests") public ResponseEntity createTunnelMappingRequest(@PathVariable String tunnelId, @AuthenticationPrincipal OidcUser oidcUser, @RequestBody Ingress ingess){ Request request = mappingRequestService.createMappingRequest(tunnelId, ingess, oidcUser); if(request.getId() != null) diff --git a/src/test/java/com/hithomelabs/CFTunnels/Controllers/TunnelControllerTest.java b/src/test/java/com/hithomelabs/CFTunnels/Controllers/TunnelControllerTest.java index ff7d303..69fbbea 100644 --- a/src/test/java/com/hithomelabs/CFTunnels/Controllers/TunnelControllerTest.java +++ b/src/test/java/com/hithomelabs/CFTunnels/Controllers/TunnelControllerTest.java @@ -36,8 +36,7 @@ import static org.mockito.ArgumentMatchers.eq; 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.oauth2Login; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; 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); - mockMvc.perform(get("/cloudflare/tunnel/{tunnelId}", "sampleTunnelId") + mockMvc.perform(get("/cloudflare/tunnel/{tunnelId}/mappings", "sampleTunnelId") .with(oauth2Login().oauth2User(buildOidcUser("username", Groups.HOMELAB_DEVELOPER)))) .andExpect(status().isOk()) .andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON)) @@ -184,7 +183,7 @@ class TunnelControllerTest { ResponseEntity expectedHttpTunnelResponse = new ResponseEntity<>(expectedTunnelConfig, HttpStatus.OK); 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(csrf()) .contentType(MediaType.APPLICATION_JSON) @@ -209,7 +208,7 @@ class TunnelControllerTest { ResponseEntity expectedHttpTunnelResponse = new ResponseEntity<>(expectedTunnelConfig, HttpStatus.OK); 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(csrf()) .contentType(MediaType.APPLICATION_JSON) -- 2.45.2