Compare commits
No commits in common. "3b43039a295add8ea89a8c4c24f28f91a31b9f73" and "7e3882febf201121ef7c756ecd87d3b4362f7514" have entirely different histories.
3b43039a29
...
7e3882febf
11
CFTunnels/Get tunnels.bru
Normal file
11
CFTunnels/Get tunnels.bru
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
meta {
|
||||||
|
name: Get tunnels
|
||||||
|
type: http
|
||||||
|
seq: 4
|
||||||
|
}
|
||||||
|
|
||||||
|
get {
|
||||||
|
url: {{base_url}}/cloudflare/tunnels
|
||||||
|
body: none
|
||||||
|
auth: none
|
||||||
|
}
|
||||||
11
CFTunnels/Tunnel.bru
Normal file
11
CFTunnels/Tunnel.bru
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
meta {
|
||||||
|
name: Tunnel
|
||||||
|
type: http
|
||||||
|
seq: 5
|
||||||
|
}
|
||||||
|
|
||||||
|
get {
|
||||||
|
url: {{base_url}}/cloudflare/tunnel/{{tunnel_id}}
|
||||||
|
body: none
|
||||||
|
auth: none
|
||||||
|
}
|
||||||
19
CFTunnels/Write ingress.bru
Normal file
19
CFTunnels/Write ingress.bru
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
meta {
|
||||||
|
name: Write ingress
|
||||||
|
type: http
|
||||||
|
seq: 2
|
||||||
|
}
|
||||||
|
|
||||||
|
put {
|
||||||
|
url: {{base_url}}/cloudflare/tunnel/{{tunnel_id}}/add
|
||||||
|
body: json
|
||||||
|
auth: none
|
||||||
|
}
|
||||||
|
|
||||||
|
body:json {
|
||||||
|
{
|
||||||
|
"service": "http://192.168.0.100:3457",
|
||||||
|
"hostname": "random.hithomelabs.com",
|
||||||
|
"originRequest": {}
|
||||||
|
}
|
||||||
|
}
|
||||||
9
CFTunnels/bruno.json
Normal file
9
CFTunnels/bruno.json
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"version": "1",
|
||||||
|
"name": "CFTunnels",
|
||||||
|
"type": "collection",
|
||||||
|
"ignore": [
|
||||||
|
"node_modules",
|
||||||
|
".git"
|
||||||
|
]
|
||||||
|
}
|
||||||
19
CFTunnels/delete mapping.bru
Normal file
19
CFTunnels/delete mapping.bru
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
meta {
|
||||||
|
name: delete mapping
|
||||||
|
type: http
|
||||||
|
seq: 3
|
||||||
|
}
|
||||||
|
|
||||||
|
put {
|
||||||
|
url: {{base_url}}/cloudflare/tunnel/{{tunnel_id}}/delete
|
||||||
|
body: json
|
||||||
|
auth: none
|
||||||
|
}
|
||||||
|
|
||||||
|
body:json {
|
||||||
|
{
|
||||||
|
"service": "http://192.168.0.100:6000",
|
||||||
|
"hostname": "random.hithomelabs.com",
|
||||||
|
"originRequest": {}
|
||||||
|
}
|
||||||
|
}
|
||||||
4
CFTunnels/environments/CFTunnels Local.bru
Normal file
4
CFTunnels/environments/CFTunnels Local.bru
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
vars {
|
||||||
|
tunnel_id: 50df9101-f625-4618-b7c5-100338a57124
|
||||||
|
base_url: http://localhost:8080
|
||||||
|
}
|
||||||
4
CFTunnels/environments/CFTunnels.bru
Normal file
4
CFTunnels/environments/CFTunnels.bru
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
vars {
|
||||||
|
tunnel_id: 50df9101-f625-4618-b7c5-100338a57124
|
||||||
|
base_url: https://testcf.hithomelabs.com
|
||||||
|
}
|
||||||
@ -10,6 +10,7 @@ import com.hithomelabs.CFTunnels.Entity.User;
|
|||||||
import com.hithomelabs.CFTunnels.Headers.AuthKeyEmailHeader;
|
import com.hithomelabs.CFTunnels.Headers.AuthKeyEmailHeader;
|
||||||
import com.hithomelabs.CFTunnels.Models.Config;
|
import com.hithomelabs.CFTunnels.Models.Config;
|
||||||
import com.hithomelabs.CFTunnels.Models.Ingress;
|
import com.hithomelabs.CFTunnels.Models.Ingress;
|
||||||
|
import com.hithomelabs.CFTunnels.Models.PaginationRequest;
|
||||||
import com.hithomelabs.CFTunnels.Models.TunnelResponse;
|
import com.hithomelabs.CFTunnels.Models.TunnelResponse;
|
||||||
import com.hithomelabs.CFTunnels.Models.TunnelsResponse;
|
import com.hithomelabs.CFTunnels.Models.TunnelsResponse;
|
||||||
import com.hithomelabs.CFTunnels.Repositories.UserRepository;
|
import com.hithomelabs.CFTunnels.Repositories.UserRepository;
|
||||||
@ -21,7 +22,10 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.web.servlet.error.ErrorController;
|
import org.springframework.boot.web.servlet.error.ErrorController;
|
||||||
import org.springframework.dao.DataAccessException;
|
import org.springframework.dao.DataAccessException;
|
||||||
import org.springframework.http.*;
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.http.*;
|
import org.springframework.http.*;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
import org.springframework.security.core.GrantedAuthority;
|
import org.springframework.security.core.GrantedAuthority;
|
||||||
@ -108,12 +112,21 @@ public class TunnelController implements ErrorController {
|
|||||||
|
|
||||||
@PreAuthorize("hasAnyRole('USER')")
|
@PreAuthorize("hasAnyRole('USER')")
|
||||||
@GetMapping("/requests")
|
@GetMapping("/requests")
|
||||||
public ResponseEntity<Map<String,Object>> getAllRequests() {
|
public ResponseEntity<Map<String,Object>> getAllRequests(
|
||||||
|
@RequestParam(required = false) Request.RequestStatus status,
|
||||||
|
@ModelAttribute PaginationRequest paginationRequest) {
|
||||||
try {
|
try {
|
||||||
List<Request> requests = mappingRequestService.getAllRequests();
|
Sort sort = paginationRequest.getSort() != null && paginationRequest.getSort().length > 0
|
||||||
|
? Sort.by(paginationRequest.getSort())
|
||||||
|
: Sort.by("id");
|
||||||
|
Pageable pageable = PageRequest.of(paginationRequest.getPage(), paginationRequest.getSize(), sort);
|
||||||
|
Page<Request> requests = mappingRequestService.getAllRequests(status, pageable);
|
||||||
Map<String, Object> jsonResponse = new HashMap<>();
|
Map<String, Object> jsonResponse = new HashMap<>();
|
||||||
jsonResponse.put("status", "success");
|
jsonResponse.put("status", "success");
|
||||||
jsonResponse.put("data", requests);
|
jsonResponse.put("data", requests.getContent());
|
||||||
|
jsonResponse.put("currentPage", requests.getNumber());
|
||||||
|
jsonResponse.put("totalItems", requests.getTotalElements());
|
||||||
|
jsonResponse.put("totalPages", requests.getTotalPages());
|
||||||
return ResponseEntity.ok(jsonResponse);
|
return ResponseEntity.ok(jsonResponse);
|
||||||
} catch (DataAccessException e) {
|
} catch (DataAccessException e) {
|
||||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
|
||||||
|
|||||||
@ -0,0 +1,14 @@
|
|||||||
|
package com.hithomelabs.CFTunnels.Models;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class PaginationRequest {
|
||||||
|
private int page = 0;
|
||||||
|
private int size = 10;
|
||||||
|
private String[] sort = {"id"};
|
||||||
|
}
|
||||||
@ -11,6 +11,8 @@ import com.hithomelabs.CFTunnels.Repositories.RequestRepository;
|
|||||||
import com.hithomelabs.CFTunnels.Repositories.TunnelRepository;
|
import com.hithomelabs.CFTunnels.Repositories.TunnelRepository;
|
||||||
import com.hithomelabs.CFTunnels.Repositories.UserRepository;
|
import com.hithomelabs.CFTunnels.Repositories.UserRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.oauth2.core.oidc.user.OidcUser;
|
import org.springframework.security.oauth2.core.oidc.user.OidcUser;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@ -59,8 +61,11 @@ public class MappingRequestService {
|
|||||||
return createRequest(mapping, user);
|
return createRequest(mapping, user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Request> getAllRequests() {
|
public Page<Request> getAllRequests(Request.RequestStatus status, Pageable pageable) {
|
||||||
return requestRepository.findAll();
|
if (status != null) {
|
||||||
|
return requestRepository.findByStatus(status, pageable);
|
||||||
|
}
|
||||||
|
return requestRepository.findAll(pageable);
|
||||||
}
|
}
|
||||||
|
|
||||||
public User mapUser(OidcUser oidcUser){
|
public User mapUser(OidcUser oidcUser){
|
||||||
|
|||||||
@ -7,6 +7,4 @@ management.endpoint.health.show-details=always
|
|||||||
logging.level.org.hibernate.SQL=DEBUG
|
logging.level.org.hibernate.SQL=DEBUG
|
||||||
debug=true
|
debug=true
|
||||||
|
|
||||||
spring.jpa.hibernate.ddl-auto=create-drop
|
|
||||||
spring.jpa.show-sql=true
|
|
||||||
spring.datasource.url=jdbc:postgresql://localhost:5432/cftunnel
|
spring.datasource.url=jdbc:postgresql://localhost:5432/cftunnel
|
||||||
|
|||||||
@ -5,7 +5,6 @@ import com.hithomelabs.CFTunnels.Config.AuthoritiesToGroupMapping;
|
|||||||
import com.hithomelabs.CFTunnels.Config.CloudflareConfig;
|
import com.hithomelabs.CFTunnels.Config.CloudflareConfig;
|
||||||
import com.hithomelabs.CFTunnels.Config.RestTemplateConfig;
|
import com.hithomelabs.CFTunnels.Config.RestTemplateConfig;
|
||||||
import com.hithomelabs.CFTunnels.Headers.AuthKeyEmailHeader;
|
import com.hithomelabs.CFTunnels.Headers.AuthKeyEmailHeader;
|
||||||
import com.hithomelabs.CFTunnels.Entity.Request;
|
|
||||||
import com.hithomelabs.CFTunnels.Entity.Tunnel;
|
import com.hithomelabs.CFTunnels.Entity.Tunnel;
|
||||||
import com.hithomelabs.CFTunnels.Models.Authorities;
|
import com.hithomelabs.CFTunnels.Models.Authorities;
|
||||||
import com.hithomelabs.CFTunnels.Models.Config;
|
import com.hithomelabs.CFTunnels.Models.Config;
|
||||||
@ -205,22 +204,44 @@ class TunnelControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("should return list of requests")
|
@DisplayName("should return list of requests with pagination")
|
||||||
void getAllRequests_Success() throws Exception {
|
void getAllRequests_Success() throws Exception {
|
||||||
List<com.hithomelabs.CFTunnels.Entity.Request> requests = Arrays.asList(
|
List<com.hithomelabs.CFTunnels.Entity.Request> requests = Arrays.asList(
|
||||||
createTestRequest(UUID.randomUUID(), com.hithomelabs.CFTunnels.Entity.Request.RequestStatus.PENDING),
|
createTestRequest(UUID.randomUUID(), com.hithomelabs.CFTunnels.Entity.Request.RequestStatus.PENDING),
|
||||||
createTestRequest(UUID.randomUUID(), com.hithomelabs.CFTunnels.Entity.Request.RequestStatus.APPROVED)
|
createTestRequest(UUID.randomUUID(), com.hithomelabs.CFTunnels.Entity.Request.RequestStatus.APPROVED)
|
||||||
);
|
);
|
||||||
|
Page<com.hithomelabs.CFTunnels.Entity.Request> page = new PageImpl<>(requests, PageRequest.of(0, 10), 2);
|
||||||
|
|
||||||
when(mappingRequestService.getAllRequests()).thenReturn(requests);
|
when(mappingRequestService.getAllRequests(any(), any(PageRequest.class))).thenReturn(page);
|
||||||
|
|
||||||
mockMvc.perform(get("/cloudflare/requests")
|
mockMvc.perform(get("/cloudflare/requests")
|
||||||
.with(oauth2Login().oauth2User(buildOidcUser("username", Groups.GITEA_USER))))
|
.with(oauth2Login().oauth2User(buildOidcUser("username", Groups.GITEA_USER)))
|
||||||
|
.param("page", "0")
|
||||||
|
.param("size", "10"))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
|
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON))
|
||||||
.andExpect(jsonPath("$.status").value("success"))
|
.andExpect(jsonPath("$.status").value("success"))
|
||||||
.andExpect(jsonPath("$.data").isArray())
|
.andExpect(jsonPath("$.data").isArray())
|
||||||
.andExpect(jsonPath("$.data.length()").value(2));
|
.andExpect(jsonPath("$.totalItems").value(2))
|
||||||
|
.andExpect(jsonPath("$.totalPages").value(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("should filter requests by status")
|
||||||
|
void getAllRequests_WithStatusFilter() throws Exception {
|
||||||
|
List<com.hithomelabs.CFTunnels.Entity.Request> requests = List.of(
|
||||||
|
createTestRequest(UUID.randomUUID(), com.hithomelabs.CFTunnels.Entity.Request.RequestStatus.PENDING)
|
||||||
|
);
|
||||||
|
Page<com.hithomelabs.CFTunnels.Entity.Request> page = new PageImpl<>(requests, PageRequest.of(0, 10), 1);
|
||||||
|
|
||||||
|
when(mappingRequestService.getAllRequests(eq(com.hithomelabs.CFTunnels.Entity.Request.RequestStatus.PENDING), any(PageRequest.class))).thenReturn(page);
|
||||||
|
|
||||||
|
mockMvc.perform(get("/cloudflare/requests")
|
||||||
|
.with(oauth2Login().oauth2User(buildOidcUser("username", Groups.GITEA_USER)))
|
||||||
|
.param("status", "PENDING"))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$.status").value("success"))
|
||||||
|
.andExpect(jsonPath("$.data[0].status").value("PENDING"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -243,6 +264,13 @@ class TunnelControllerTest {
|
|||||||
.andExpect(jsonPath("$.status").value("PENDING"));
|
.andExpect(jsonPath("$.status").value("PENDING"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private com.hithomelabs.CFTunnels.Entity.Request createTestRequest(UUID id, com.hithomelabs.CFTunnels.Entity.Request.RequestStatus status) {
|
||||||
|
com.hithomelabs.CFTunnels.Entity.Request request = new com.hithomelabs.CFTunnels.Entity.Request();
|
||||||
|
request.setId(id);
|
||||||
|
request.setStatus(status);
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("should approve mapping request successfully")
|
@DisplayName("should approve mapping request successfully")
|
||||||
void approveMappingRequest_Success() throws Exception {
|
void approveMappingRequest_Success() throws Exception {
|
||||||
@ -409,13 +437,6 @@ class TunnelControllerTest {
|
|||||||
.andExpect(jsonPath("$.data.result.config.ingress[*].hostname", hasItem("random.hithomelabs.com")));
|
.andExpect(jsonPath("$.data.result.config.ingress[*].hostname", hasItem("random.hithomelabs.com")));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Request createTestRequest(UUID id, Request.RequestStatus status) {
|
|
||||||
Request request = new Request();
|
|
||||||
request.setId(id);
|
|
||||||
request.setStatus(status);
|
|
||||||
return request;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void deleteTunnelConfiguration() throws Exception {
|
void deleteTunnelConfiguration() throws Exception {
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user