ISSUE-44: Hithomelabs/HomeLabDocker#44 adding more tests

This commit is contained in:
hitanshu310 2025-11-16 18:59:06 +05:30 committed by hitanshu
parent f36d807731
commit 49837b3109
2 changed files with 74 additions and 45 deletions

View File

@ -1,8 +1,17 @@
package com.hithomelabs.CFTunnels.Models; package com.hithomelabs.CFTunnels.Models;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class Ingress { public class Ingress {
private String service; private String service;
@ -10,40 +19,8 @@ public class Ingress {
private Map<String, Object> originRequest; private Map<String, Object> originRequest;
private String path; private String path;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public static boolean deleteByHostName(List<Ingress> ingressList, String toBeDeleted){ public static boolean deleteByHostName(List<Ingress> ingressList, String toBeDeleted){
return ingressList.removeIf(ingress -> ingress.getHostname() != null && ingress.getHostname().equals(toBeDeleted)); return ingressList.removeIf(ingress -> ingress.getHostname() != null && ingress.getHostname().equals(toBeDeleted));
} }
public String getService() {
return service;
}
public void setService(String service) {
this.service = service;
}
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
public Map<String, Object> getOriginRequest() {
return originRequest;
}
public void setOriginRequest(Map<String, Object> originRequest) {
this.originRequest = originRequest;
}
} }

View File

@ -1,25 +1,25 @@
package com.hithomelabs.CFTunnels.Integration; package com.hithomelabs.CFTunnels.Integration;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hithomelabs.CFTunnels.Config.CloudflareConfig; import com.hithomelabs.CFTunnels.Config.CloudflareConfig;
import com.hithomelabs.CFTunnels.Headers.AuthKeyEmailHeader; import com.hithomelabs.CFTunnels.Headers.AuthKeyEmailHeader;
import com.hithomelabs.CFTunnels.Models.Config;
import com.hithomelabs.CFTunnels.Models.Ingress;
import com.hithomelabs.CFTunnels.Models.TunnelResponse;
import com.hithomelabs.CFTunnels.Services.CloudflareAPIService;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ActiveProfiles;
import org.springframework.web.client.RestTemplate;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("integration") @ActiveProfiles("integration")
@ -27,7 +27,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
public class CoudflareApiIntegrationTest { public class CoudflareApiIntegrationTest {
@Autowired @Autowired
TestRestTemplate restTemplate; RestTemplate restTemplate;
@Autowired @Autowired
AuthKeyEmailHeader authKeyEmailHeader; AuthKeyEmailHeader authKeyEmailHeader;
@ -35,21 +35,73 @@ public class CoudflareApiIntegrationTest {
@Autowired @Autowired
CloudflareConfig cloudflareConfig; CloudflareConfig cloudflareConfig;
private static ObjectMapper mapper = new ObjectMapper(); @Autowired
CloudflareAPIService cloudflareAPIService;
private static final String DEV_TUNNEL_ID = "50df9101-f625-4618-b7c5-100338a57124";
@Test @Test
@DisplayName("Calls cloudflare cfd tunnels API and checks that dev tunnel should be a part of the response") @DisplayName("Calls cloudflare cfd tunnels API and checks that dev tunnel should be a part of the response")
public void getTunnelsTest(){ public void testGetTunnelsTest() {
ResponseEntity<Map> response = cloudflareAPIService.getCloudflareTunnels();
// * * Resource URL to hit get request at
String url = "https://api.cloudflare.com/client/v4/accounts/" + cloudflareConfig.getAccountId() + "/cfd_tunnel";
HttpEntity<String> httpEntity = new HttpEntity<>("", authKeyEmailHeader.getHttpHeaders());
ResponseEntity<Map> response = restTemplate.exchange(url, HttpMethod.GET, httpEntity, Map.class);
assertEquals(HttpStatus.OK, response.getStatusCode()); assertEquals(HttpStatus.OK, response.getStatusCode());
List<Map<String, Object>> tunnelList = (List<Map<String, Object>>) response.getBody().get("result"); List<Map<String, Object>> tunnelList = (List<Map<String, Object>>) response.getBody().get("result");
boolean hasName = tunnelList.stream() boolean hasName = tunnelList.stream()
.anyMatch(tunnel -> "devtunnel".equals(tunnel.get("name"))); .anyMatch(tunnel -> "devtunnel".equals(tunnel.get("name")));
assertTrue(hasName); assertTrue(hasName);
} }
@Test
@DisplayName("Calls cloudflare API to get mappings for devtunnel tunnel")
public void testTunnelConfigurations() {
ResponseEntity<TunnelResponse> responseEntity = cloudflareAPIService.getCloudflareTunnelConfigurations(DEV_TUNNEL_ID, restTemplate, TunnelResponse.class);
// * * Check if status code is 200
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
// * * Checking if mapping for devdocker exists
TunnelResponse tunnelResponse = responseEntity.getBody();
boolean hasMatch = tunnelResponse.getResult().getConfig().getIngress().stream()
.anyMatch(ingress -> "devdocker.hithomelabs.com".equals(ingress.getHostname()));
assertTrue(hasMatch);
}
@Test
@DisplayName("Inserts and deletes a mapping using Cloudflare API")
public void testAddAndDeleteMapping() {
ResponseEntity<TunnelResponse> beforeMapping = cloudflareAPIService.getCloudflareTunnelConfigurations(DEV_TUNNEL_ID, restTemplate, TunnelResponse.class);
assertEquals(HttpStatus.OK, beforeMapping.getStatusCode());
Ingress ingress = new Ingress();
ingress.setHostname("random.hithomelabs.com");
ingress.setService("http://192.168.0.100:3457");
Config beforeInsertConfig = beforeMapping.getBody().getResult().getConfig();
List<Ingress> beforeInsert = beforeInsertConfig.getIngress();
beforeInsert.add(beforeInsert.size() - 1, ingress);
ResponseEntity<TunnelResponse> afterInsert = cloudflareAPIService.putCloudflareTunnelConfigurations(DEV_TUNNEL_ID, restTemplate, TunnelResponse.class, beforeInsertConfig);
assertEquals(HttpStatus.OK, afterInsert.getStatusCode());
Config afterInsertConfig = afterInsert.getBody().getResult().getConfig();
List<Ingress> ingressList = afterInsertConfig.getIngress();
boolean hasIngress = ingressList.get(ingressList.size() - 2 ).getHostname().equals("random.hithomelabs.com");
assertTrue(hasIngress);
Boolean deleteSuccess = Ingress.deleteByHostName(ingressList, ingress.getHostname());
assertTrue(deleteSuccess);
ResponseEntity<TunnelResponse> afterDelete = cloudflareAPIService.putCloudflareTunnelConfigurations(DEV_TUNNEL_ID, restTemplate, TunnelResponse.class, afterInsertConfig);
assertEquals(HttpStatus.OK, afterDelete.getStatusCode());
assertFalse(afterDelete.getBody().getResult().getConfig().getIngress().stream().anyMatch(anyIngress -> "random.hithomelabs.com".equals(anyIngress.getHostname())));
}
} }