test: cover AI code review changes in CfClientTest (#2)
Build and Analyse / build-and-analyse (push) Successful in 30s
Build and Analyse / build-and-analyse (push) Successful in 30s
- live tests for API error details (status code, Cloudflare errors), 404 on deleting an unknown record, unknown zones with and without emptyResultThrowsException, closing the client, multi-level subdomains and the zone apex, server- and client-side type filter, record attributes (priority, comment, SRV data) and Instant timestamps - testPaging fetches all pages with a small page size, therefore CfDnsClient got a package-private constructor with a custom page size - testDns checks that recordUpdate doesn't modify the given record - SRV records: the priority is part of the data, fixed javadoc, README and changelog accordingly refs #2 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
20fabf9667
commit
c3a5c5f3e5
6 files changed
+229
-17
No files matched your search
@@ -1,8 +1,11 @@
|
||||
package codes.thischwa.cf;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
@@ -13,6 +16,7 @@ import codes.thischwa.cf.model.PagingRequest;
|
||||
import codes.thischwa.cf.model.RecordEntity;
|
||||
import codes.thischwa.cf.model.RecordType;
|
||||
import codes.thischwa.cf.model.ZoneEntity;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
@@ -36,6 +40,9 @@ public class CfClientTest {
|
||||
|
||||
private final CfDnsClient client = new CfDnsClientBuilder().withEmptyResultThrowsException(true).withApiTokenAuth(API_TOKEN).build();
|
||||
|
||||
private static final String IP_PREFIX = "130.0.0.";
|
||||
private static final String UPDATED_IP_PREFIX = "130.1.0.";
|
||||
|
||||
@BeforeAll
|
||||
static void checkEnv() {
|
||||
assumeTrue(API_TOKEN != null && !API_TOKEN.isBlank(), "API_TOKEN not set; skipping client tests");
|
||||
@@ -207,9 +214,14 @@ public class CfClientTest {
|
||||
assertFalse(fluentList.isEmpty());
|
||||
assertTrue(fluentList.stream().anyMatch(re -> re.getId().equals(createdRe1.getId())));
|
||||
|
||||
// update AAAA record
|
||||
// update AAAA record, the given record (incl. its timestamps) must not be modified
|
||||
createdRe2.setContent("2a0a:4cc0:c0:2e4::2");
|
||||
client.recordUpdate(z, createdRe2);
|
||||
Instant createdOn = createdRe2.getCreatedOn();
|
||||
RecordEntity updatedRe2 = client.recordUpdate(z, createdRe2);
|
||||
assertEquals(createdOn, createdRe2.getCreatedOn());
|
||||
assertNotNull(createdRe2.getModifiedOn());
|
||||
assertEquals("2a0a:4cc0:c0:2e4::2", updatedRe2.getContent());
|
||||
assertEquals(z.getId(), updatedRe2.getZoneId());
|
||||
aaaaRecords = client.recordListBySubdomain(z, randomSld, RecordType.AAAA);
|
||||
assertEquals(1, aaaaRecords.size());
|
||||
r = aaaaRecords.get(0);
|
||||
@@ -246,10 +258,6 @@ public class CfClientTest {
|
||||
assertTrue(exception.getMessage().contains("Must be one of:"));
|
||||
}
|
||||
|
||||
private static final String IP_PREFIX = "130.0.0.";
|
||||
private static final String UPDATED_IP_PREFIX = "130.1.0.";
|
||||
|
||||
|
||||
@Test
|
||||
void testBatch() throws Exception {
|
||||
// starting point: already existing zone 'mein-d-ns.de'
|
||||
@@ -518,6 +526,15 @@ public class CfClientTest {
|
||||
Set<String> allRecordIds = allRecords.stream().map(RecordEntity::getId).collect(Collectors.toSet());
|
||||
assertEquals(createdRecords.size(), allRecordIds.size());
|
||||
assertTrue(allRecordIds.containsAll(generatedRecordIds));
|
||||
|
||||
// Without PagingRequest all pages must be fetched, also if the page size is small
|
||||
try (CfDnsClient smallPageClient = new CfDnsClient(true, CfDnsClientBuilder.DEFAULT_BASEURL,
|
||||
new CfDnsClientBuilder.ApiTokenAuth(API_TOKEN), 5)) {
|
||||
assertEquals(allRecordIds, smallPageClient.recordList(zone).stream()
|
||||
.map(RecordEntity::getId).collect(Collectors.toSet()));
|
||||
assertEquals(createdRecords.size(), smallPageClient.recordListBySubdomain(zone, pagingSld).size());
|
||||
assertEquals(createdRecords.size(), smallPageClient.recordListByType(zone, RecordType.A).size());
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
client.recordDeleteTypeIfExists(zone, pagingSld, RecordType.A);
|
||||
@@ -525,4 +542,186 @@ public class CfClientTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testApiErrorDetails() throws Exception {
|
||||
ZoneEntity zone = client.zoneGet(ZONE_STR);
|
||||
String sub = "invalid-" + System.currentTimeMillis();
|
||||
try {
|
||||
// an A record with an invalid IP is rejected by Cloudflare
|
||||
CloudflareApiException e = assertThrows(CloudflareApiException.class,
|
||||
() -> client.recordCreateSubdomain(zone, sub, TTL, RecordType.A, "not-an-ip"));
|
||||
assertEquals(CloudflareApiException.class, e.getClass());
|
||||
assertEquals(400, e.getStatusCode());
|
||||
assertFalse(e.getErrors().isEmpty());
|
||||
assertTrue(e.getErrors().get(0).getCode() > 0);
|
||||
// the Cloudflare error message is part of the exception message, not wrapped
|
||||
assertTrue(e.getMessage().contains(e.getErrors().get(0).getMessage()), e.getMessage());
|
||||
} finally {
|
||||
try {
|
||||
client.recordDeleteTypeIfExists(zone, sub, RecordType.A);
|
||||
} catch (Exception e) { /* ignore */ }
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRecordDeleteUnknownId() throws Exception {
|
||||
ZoneEntity zone = client.zoneGet(ZONE_STR);
|
||||
CloudflareApiException e = assertThrows(CloudflareApiException.class,
|
||||
() -> client.recordDelete(zone, "0123456789abcdef0123456789abcdef"));
|
||||
assertInstanceOf(CloudflareNotFoundException.class, e);
|
||||
assertEquals(404, e.getStatusCode());
|
||||
assertFalse(e.getErrors().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testZoneNotFound() throws Exception {
|
||||
String unknownZone = "not-existing-" + System.currentTimeMillis() + ".de";
|
||||
// emptyResultThrowsException = true
|
||||
assertThrows(CloudflareNotFoundException.class, () -> client.zoneGet(unknownZone));
|
||||
assertThrows(CloudflareNotFoundException.class, () -> client.zone(unknownZone));
|
||||
|
||||
// emptyResultThrowsException = false
|
||||
try (CfDnsClient lenientClient = new CfDnsClientBuilder().withApiTokenAuth(API_TOKEN).build()) {
|
||||
assertNull(lenientClient.zoneGet(unknownZone));
|
||||
assertThrows(CloudflareNotFoundException.class, () -> lenientClient.zone(unknownZone));
|
||||
assertNotNull(lenientClient.zoneGet(ZONE_STR));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testClientClose() throws Exception {
|
||||
CfDnsClient closeableClient = new CfDnsClientBuilder().withApiTokenAuth(API_TOKEN).build();
|
||||
// multiple requests with the same client
|
||||
assertNotNull(closeableClient.zoneGet(ZONE_STR));
|
||||
assertFalse(closeableClient.zoneList().isEmpty());
|
||||
assertDoesNotThrow(closeableClient::close);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMultiLevelSubdomainAndApex() throws Exception {
|
||||
ZoneEntity zone = client.zoneGet(ZONE_STR);
|
||||
String multiLevel = "a.b-" + System.currentTimeMillis();
|
||||
String apexContent = "cf-client-test-" + System.currentTimeMillis();
|
||||
List<RecordEntity> created = new ArrayList<>();
|
||||
try {
|
||||
// multi-level subdomain
|
||||
RecordEntity rec = client.recordCreateSubdomain(zone, multiLevel, TTL, RecordType.A, "130.0.0.10");
|
||||
created.add(rec);
|
||||
assertEquals(multiLevel + "." + ZONE_STR, rec.getName());
|
||||
assertEquals(multiLevel, rec.getSubdomain());
|
||||
List<RecordEntity> fetched = client.recordListBySubdomain(zone, multiLevel, RecordType.A);
|
||||
assertEquals(1, fetched.size());
|
||||
assertEquals(multiLevel, fetched.get(0).getSubdomain());
|
||||
assertEquals(ZONE_STR, fetched.get(0).getZoneName());
|
||||
|
||||
// batch with a multi-level name must not cut the name
|
||||
String batchName = "c.d-" + System.currentTimeMillis() + "." + ZONE_STR;
|
||||
BatchEntry batchResult = client.recordBatch(zone,
|
||||
List.of(RecordEntity.build(batchName, RecordType.A, TTL, "130.0.0.11")), null, null, null);
|
||||
RecordEntity batched = batchResult.getPosts().get(0);
|
||||
created.add(batched);
|
||||
assertEquals(batchName, batched.getName());
|
||||
|
||||
// zone apex (TXT record with unique content, existing apex records stay untouched)
|
||||
RecordEntity apex = client.recordCreateSubdomain(zone, CfDnsClient.APEX, TTL, RecordType.TXT,
|
||||
"\"" + apexContent + "\"");
|
||||
created.add(apex);
|
||||
assertEquals(ZONE_STR, apex.getName());
|
||||
assertEquals(CfDnsClient.APEX, apex.getSubdomain());
|
||||
assertTrue(client.recordListBySubdomain(zone, CfDnsClient.APEX, RecordType.TXT).stream()
|
||||
.anyMatch(r -> r.getId().equals(apex.getId()) && CfDnsClient.APEX.equals(r.getSubdomain())));
|
||||
} finally {
|
||||
deleteQuietly(zone, created);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testTypeFilter() throws Exception {
|
||||
ZoneEntity zone = client.zoneGet(ZONE_STR);
|
||||
String sub = "types-" + System.currentTimeMillis();
|
||||
List<RecordEntity> created = new ArrayList<>();
|
||||
try {
|
||||
created.add(client.recordCreateSubdomain(zone, sub, TTL, RecordType.A, "130.0.0.20"));
|
||||
created.add(client.recordCreateSubdomain(zone, sub, TTL, RecordType.AAAA, "2001:db8::20"));
|
||||
created.add(client.recordCreateSubdomain(zone, sub, TTL, RecordType.TXT, "\"type filter\""));
|
||||
|
||||
// one type: filtered by Cloudflare
|
||||
List<RecordEntity> txt = client.recordListBySubdomain(zone, sub, RecordType.TXT);
|
||||
assertEquals(1, txt.size());
|
||||
assertEquals(RecordType.TXT.getType(), txt.get(0).getType());
|
||||
assertTrue(client.recordListByType(zone, RecordType.TXT).stream()
|
||||
.allMatch(r -> RecordType.TXT.getType().equals(r.getType())));
|
||||
|
||||
// multiple types: filtered by the client
|
||||
List<RecordEntity> ip = client.recordListBySubdomain(zone, sub, RecordType.A, RecordType.AAAA);
|
||||
assertEquals(Set.of("A", "AAAA"), ip.stream().map(RecordEntity::getType).collect(Collectors.toSet()));
|
||||
|
||||
// no type: all records
|
||||
assertEquals(3, client.recordListBySubdomain(zone, sub).size());
|
||||
} finally {
|
||||
deleteQuietly(zone, created);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRecordAttributes() throws Exception {
|
||||
ZoneEntity zone = client.zoneGet(ZONE_STR);
|
||||
String sub = "attr-" + System.currentTimeMillis();
|
||||
List<RecordEntity> created = new ArrayList<>();
|
||||
Instant start = Instant.now().minusSeconds(60);
|
||||
try {
|
||||
// MX record with priority and comment
|
||||
RecordEntity mx = RecordEntity.build(sub + "." + ZONE_STR, RecordType.MX, TTL, "mail." + ZONE_STR);
|
||||
mx.setPriority(10);
|
||||
mx.setComment("cf client test");
|
||||
RecordEntity createdMx = client.recordCreate(zone, mx);
|
||||
created.add(createdMx);
|
||||
assertEquals(10, createdMx.getPriority());
|
||||
assertEquals("cf client test", createdMx.getComment());
|
||||
assertNull(mx.getId(), "the given record must not be modified");
|
||||
// timestamps are Instants (UTC)
|
||||
assertTrue(createdMx.getCreatedOn().isAfter(start), createdMx.getCreatedOn().toString());
|
||||
assertTrue(createdMx.getCreatedOn().isBefore(Instant.now().plusSeconds(60)));
|
||||
|
||||
RecordEntity fetchedMx = client.recordListBySubdomain(zone, sub, RecordType.MX).get(0);
|
||||
assertEquals(10, fetchedMx.getPriority());
|
||||
assertEquals("cf client test", fetchedMx.getComment());
|
||||
|
||||
// partial update keeps the attributes which aren't sent
|
||||
RecordEntity updatedMx = client.recordUpdate(zone, RecordEntity.build(fetchedMx.getId(), "mx2." + ZONE_STR));
|
||||
assertEquals("mx2." + ZONE_STR, updatedMx.getContent());
|
||||
assertEquals(10, updatedMx.getPriority());
|
||||
assertEquals("cf client test", updatedMx.getComment());
|
||||
|
||||
// batch PATCH changes the priority
|
||||
fetchedMx = client.recordListBySubdomain(zone, sub, RecordType.MX).get(0);
|
||||
fetchedMx.setPriority(20);
|
||||
client.recordBatch(zone, null, null, List.of(fetchedMx), null);
|
||||
assertEquals(20, client.recordListBySubdomain(zone, sub, RecordType.MX).get(0).getPriority());
|
||||
|
||||
// SRV record with structured data, for SRV records the priority is part of the data
|
||||
String srvName = "_sip._tcp." + sub + "." + ZONE_STR;
|
||||
RecordEntity srv = RecordEntity.build(srvName, RecordType.SRV, TTL, null);
|
||||
srv.setData(Map.of("priority", 10, "weight", 5, "port", 5060, "target", "sip." + ZONE_STR));
|
||||
RecordEntity createdSrv = client.recordCreate(zone, srv);
|
||||
created.add(createdSrv);
|
||||
assertEquals(srvName, createdSrv.getName());
|
||||
assertEquals(10, ((Number) createdSrv.getData().get("priority")).intValue());
|
||||
assertEquals(5060, ((Number) createdSrv.getData().get("port")).intValue());
|
||||
assertEquals(5, ((Number) createdSrv.getData().get("weight")).intValue());
|
||||
assertEquals("sip." + ZONE_STR, createdSrv.getData().get("target"));
|
||||
} finally {
|
||||
deleteQuietly(zone, created);
|
||||
}
|
||||
}
|
||||
|
||||
private void deleteQuietly(ZoneEntity zone, List<RecordEntity> records) {
|
||||
for (RecordEntity rec : records) {
|
||||
try {
|
||||
client.recordDelete(zone, rec.getId());
|
||||
} catch (Exception e) {
|
||||
log.warn("Couldn't delete test record {}: {}", rec.getName(), e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -706,7 +706,7 @@ class CfDnsClientMockTest {
|
||||
RecordEntity srv = RecordEntity.build("_sip._tcp.example.com", RecordType.SRV, 300, null);
|
||||
srv.setId("srv1");
|
||||
srv.setPriority(10);
|
||||
srv.setData(Map.of("weight", 5, "port", 5060, "target", "sip.example.com"));
|
||||
srv.setData(Map.of("priority", 10, "weight", 5, "port", 5060, "target", "sip.example.com"));
|
||||
RecordEntity proxied = RecordEntity.build("www.example.com", RecordType.A, 1, "1.2.3.4");
|
||||
proxied.setId("www1");
|
||||
proxied.setProxied(true);
|
||||
|
||||
Reference in new issue
Block a user