Compare commits
No commits in common. "main" and "0.18.2" have entirely different histories.
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"};
|
||||||
|
}
|
||||||
@ -4,21 +4,11 @@ import com.hithomelabs.CFTunnels.Entity.Request;
|
|||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
|
||||||
import org.springframework.data.repository.query.Param;
|
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface RequestRepository extends JpaRepository<Request, UUID> {
|
public interface RequestRepository extends JpaRepository<Request, UUID> {
|
||||||
Page<Request> findByStatus(Request.RequestStatus status, Pageable pageable);
|
Page<Request> findByStatus(Request.RequestStatus status, Pageable pageable);
|
||||||
|
|
||||||
@Query("SELECT r FROM Request r JOIN FETCH r.mapping m JOIN FETCH m.tunnel JOIN FETCH r.createdBy LEFT JOIN FETCH r.acceptedBy")
|
|
||||||
List<Request> findAllWithDetails();
|
|
||||||
|
|
||||||
@Query("SELECT r FROM Request r JOIN FETCH r.mapping m JOIN FETCH m.tunnel JOIN FETCH r.createdBy LEFT JOIN FETCH r.acceptedBy WHERE r.id = :id")
|
|
||||||
Optional<Request> findByIdWithDetails(@Param("id") UUID 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,9 +61,11 @@ public class MappingRequestService {
|
|||||||
return createRequest(mapping, user);
|
return createRequest(mapping, user);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional(readOnly = true)
|
public Page<Request> getAllRequests(Request.RequestStatus status, Pageable pageable) {
|
||||||
public List<Request> getAllRequests() {
|
if (status != null) {
|
||||||
return requestRepository.findAllWithDetails();
|
return requestRepository.findByStatus(status, pageable);
|
||||||
|
}
|
||||||
|
return requestRepository.findAll(pageable);
|
||||||
}
|
}
|
||||||
|
|
||||||
public User mapUser(OidcUser oidcUser){
|
public User mapUser(OidcUser oidcUser){
|
||||||
@ -100,7 +104,7 @@ public class MappingRequestService {
|
|||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Request approveRequest(UUID requestId, User approver) {
|
public Request approveRequest(UUID requestId, User approver) {
|
||||||
Request request = requestRepository.findByIdWithDetails(requestId)
|
Request request = requestRepository.findById(requestId)
|
||||||
.orElseThrow(() -> new NoSuchElementException("Request not found"));
|
.orElseThrow(() -> new NoSuchElementException("Request not found"));
|
||||||
|
|
||||||
if (request.getStatus() != Request.RequestStatus.PENDING) {
|
if (request.getStatus() != Request.RequestStatus.PENDING) {
|
||||||
@ -128,7 +132,7 @@ public class MappingRequestService {
|
|||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Request rejectRequest(UUID requestId, User rejecter) {
|
public Request rejectRequest(UUID requestId, User rejecter) {
|
||||||
Request request = requestRepository.findByIdWithDetails(requestId)
|
Request request = requestRepository.findById(requestId)
|
||||||
.orElseThrow(() -> new NoSuchElementException("Request not found"));
|
.orElseThrow(() -> new NoSuchElementException("Request not found"));
|
||||||
|
|
||||||
if (request.getStatus() != Request.RequestStatus.PENDING) {
|
if (request.getStatus() != Request.RequestStatus.PENDING) {
|
||||||
@ -146,7 +150,7 @@ public class MappingRequestService {
|
|||||||
Tunnel tunnel = mapping.getTunnel();
|
Tunnel tunnel = mapping.getTunnel();
|
||||||
String protocol = mapping.getProtocol().name().toLowerCase();
|
String protocol = mapping.getProtocol().name().toLowerCase();
|
||||||
String service = protocol + "://" + SERVER_IP + ":" + mapping.getPort();
|
String service = protocol + "://" + SERVER_IP + ":" + mapping.getPort();
|
||||||
String hostname = mapping.getSubdomain() + ".hithomelabs.com";
|
String hostname = mapping.getSubdomain() + "." + tunnel.getName() + ".hithomelabs.com";
|
||||||
return new Ingress(service, hostname, null, null);
|
return new Ingress(service, hostname, null, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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=update
|
|
||||||
spring.jpa.show-sql=true
|
|
||||||
spring.datasource.url=jdbc:postgresql://localhost:5432/cftunnel
|
spring.datasource.url=jdbc:postgresql://localhost:5432/cftunnel
|
||||||
|
|||||||
@ -8,5 +8,5 @@ spring.datasource.driver-class-name=org.postgresql.Driver
|
|||||||
|
|
||||||
# JPA Configuration
|
# JPA Configuration
|
||||||
spring.jpa.hibernate.ddl-auto=create-drop
|
spring.jpa.hibernate.ddl-auto=create-drop
|
||||||
spring.jpa.show-sql=false
|
spring.jpa.show-sql=true
|
||||||
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
|
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
|
||||||
@ -7,6 +7,6 @@ spring.datasource.password=${POSTGRES_PASSWORD}
|
|||||||
spring.datasource.driver-class-name=org.postgresql.Driver
|
spring.datasource.driver-class-name=org.postgresql.Driver
|
||||||
|
|
||||||
# JPA Configuration
|
# JPA Configuration
|
||||||
spring.jpa.hibernate.ddl-auto=update
|
spring.jpa.hibernate.ddl-auto=create-drop
|
||||||
spring.jpa.show-sql=true
|
spring.jpa.show-sql=true
|
||||||
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
|
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
|
||||||
|
|||||||
@ -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 {
|
||||||
|
|
||||||
|
|||||||
@ -1,179 +0,0 @@
|
|||||||
package com.hithomelabs.CFTunnels.Repositories;
|
|
||||||
|
|
||||||
import com.hithomelabs.CFTunnels.Entity.Mapping;
|
|
||||||
import com.hithomelabs.CFTunnels.Entity.Protocol;
|
|
||||||
import com.hithomelabs.CFTunnels.Entity.Request;
|
|
||||||
import com.hithomelabs.CFTunnels.Entity.Tunnel;
|
|
||||||
import com.hithomelabs.CFTunnels.Entity.User;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.DisplayName;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
|
||||||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
|
|
||||||
import org.springframework.test.annotation.DirtiesContext;
|
|
||||||
import org.springframework.test.context.TestPropertySource;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
||||||
|
|
||||||
@DataJpaTest
|
|
||||||
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.ANY)
|
|
||||||
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
|
|
||||||
@TestPropertySource(properties = {
|
|
||||||
"spring.jpa.hibernate.ddl-auto=create-drop",
|
|
||||||
"spring.jpa.show-sql=false",
|
|
||||||
"spring.sql.init.mode=never"
|
|
||||||
})
|
|
||||||
class RequestRepositoryTest {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private RequestRepository requestRepository;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private TunnelRepository tunnelRepository;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private MappingRepository mappingRepository;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private UserRepository userRepository;
|
|
||||||
|
|
||||||
private Tunnel tunnel;
|
|
||||||
private User createdByUser;
|
|
||||||
private User acceptedByUser;
|
|
||||||
private Mapping mapping;
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
void setUp() {
|
|
||||||
tunnel = new Tunnel();
|
|
||||||
tunnel.setId(UUID.randomUUID());
|
|
||||||
tunnel.setEnvironment("test");
|
|
||||||
tunnel.setName("test-tunnel");
|
|
||||||
tunnel = tunnelRepository.save(tunnel);
|
|
||||||
|
|
||||||
createdByUser = new User();
|
|
||||||
createdByUser.setEmail("creator@example.com");
|
|
||||||
createdByUser.setName("Creator User");
|
|
||||||
createdByUser = userRepository.save(createdByUser);
|
|
||||||
|
|
||||||
acceptedByUser = new User();
|
|
||||||
acceptedByUser.setEmail("approver@example.com");
|
|
||||||
acceptedByUser.setName("Approver User");
|
|
||||||
acceptedByUser = userRepository.save(acceptedByUser);
|
|
||||||
|
|
||||||
mapping = new Mapping();
|
|
||||||
mapping.setTunnel(tunnel);
|
|
||||||
mapping.setPort(8080);
|
|
||||||
mapping.setProtocol(Protocol.HTTP);
|
|
||||||
mapping.setSubdomain("test-subdomain");
|
|
||||||
mapping = mappingRepository.save(mapping);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("findAllWithDetails should return requests with all relationships loaded")
|
|
||||||
void findAllWithDetails_ShouldReturnRequestsWithAllRelationships() {
|
|
||||||
Request request = new Request();
|
|
||||||
request.setMapping(mapping);
|
|
||||||
request.setCreatedBy(createdByUser);
|
|
||||||
request.setAcceptedBy(acceptedByUser);
|
|
||||||
request.setStatus(Request.RequestStatus.PENDING);
|
|
||||||
requestRepository.save(request);
|
|
||||||
|
|
||||||
List<Request> results = requestRepository.findAllWithDetails();
|
|
||||||
|
|
||||||
assertThat(results).hasSize(1);
|
|
||||||
Request result = results.get(0);
|
|
||||||
assertThat(result.getMapping()).isNotNull();
|
|
||||||
assertThat(result.getMapping().getTunnel()).isNotNull();
|
|
||||||
assertThat(result.getCreatedBy()).isNotNull();
|
|
||||||
assertThat(result.getAcceptedBy()).isNotNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("findByIdWithDetails should return request with all relationships loaded")
|
|
||||||
void findByIdWithDetails_ShouldReturnRequestWithAllRelationships() {
|
|
||||||
Request request = new Request();
|
|
||||||
request.setMapping(mapping);
|
|
||||||
request.setCreatedBy(createdByUser);
|
|
||||||
request.setAcceptedBy(acceptedByUser);
|
|
||||||
request.setStatus(Request.RequestStatus.PENDING);
|
|
||||||
UUID requestId = requestRepository.save(request).getId();
|
|
||||||
|
|
||||||
Optional<Request> result = requestRepository.findByIdWithDetails(requestId);
|
|
||||||
|
|
||||||
assertThat(result).isPresent();
|
|
||||||
assertThat(result.get().getMapping()).isNotNull();
|
|
||||||
assertThat(result.get().getMapping().getTunnel()).isNotNull();
|
|
||||||
assertThat(result.get().getCreatedBy()).isNotNull();
|
|
||||||
assertThat(result.get().getAcceptedBy()).isNotNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("findByIdWithDetails should return empty for non-existent id")
|
|
||||||
void findByIdWithDetails_ShouldReturnEmptyForNonExistentId() {
|
|
||||||
Optional<Request> result = requestRepository.findByIdWithDetails(UUID.randomUUID());
|
|
||||||
|
|
||||||
assertThat(result).isEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("findAllWithDetails should return empty list when no requests exist")
|
|
||||||
void findAllWithDetails_ShouldReturnEmptyListWhenNoRequests() {
|
|
||||||
List<Request> results = requestRepository.findAllWithDetails();
|
|
||||||
|
|
||||||
assertThat(results).isEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("findAllWithDetails should handle multiple requests with different statuses")
|
|
||||||
void findAllWithDetails_ShouldHandleMultipleRequests() {
|
|
||||||
Mapping mapping1 = new Mapping();
|
|
||||||
mapping1.setTunnel(tunnel);
|
|
||||||
mapping1.setPort(8080);
|
|
||||||
mapping1.setProtocol(Protocol.HTTP);
|
|
||||||
mapping1.setSubdomain("pending-subdomain");
|
|
||||||
mapping1 = mappingRepository.save(mapping1);
|
|
||||||
|
|
||||||
Mapping mapping2 = new Mapping();
|
|
||||||
mapping2.setTunnel(tunnel);
|
|
||||||
mapping2.setPort(8081);
|
|
||||||
mapping2.setProtocol(Protocol.HTTP);
|
|
||||||
mapping2.setSubdomain("approved-subdomain");
|
|
||||||
mapping2 = mappingRepository.save(mapping2);
|
|
||||||
|
|
||||||
Mapping mapping3 = new Mapping();
|
|
||||||
mapping3.setTunnel(tunnel);
|
|
||||||
mapping3.setPort(8082);
|
|
||||||
mapping3.setProtocol(Protocol.HTTP);
|
|
||||||
mapping3.setSubdomain("rejected-subdomain");
|
|
||||||
mapping3 = mappingRepository.save(mapping3);
|
|
||||||
|
|
||||||
Request pendingRequest = new Request();
|
|
||||||
pendingRequest.setMapping(mapping1);
|
|
||||||
pendingRequest.setCreatedBy(createdByUser);
|
|
||||||
pendingRequest.setStatus(Request.RequestStatus.PENDING);
|
|
||||||
requestRepository.save(pendingRequest);
|
|
||||||
|
|
||||||
Request approvedRequest = new Request();
|
|
||||||
approvedRequest.setMapping(mapping2);
|
|
||||||
approvedRequest.setCreatedBy(createdByUser);
|
|
||||||
approvedRequest.setAcceptedBy(acceptedByUser);
|
|
||||||
approvedRequest.setStatus(Request.RequestStatus.APPROVED);
|
|
||||||
requestRepository.save(approvedRequest);
|
|
||||||
|
|
||||||
Request rejectedRequest = new Request();
|
|
||||||
rejectedRequest.setMapping(mapping3);
|
|
||||||
rejectedRequest.setCreatedBy(createdByUser);
|
|
||||||
rejectedRequest.setAcceptedBy(acceptedByUser);
|
|
||||||
rejectedRequest.setStatus(Request.RequestStatus.REJECTED);
|
|
||||||
requestRepository.save(rejectedRequest);
|
|
||||||
|
|
||||||
List<Request> results = requestRepository.findAllWithDetails();
|
|
||||||
|
|
||||||
assertThat(results).hasSize(3);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user