feat: редактирование доменной записи

This commit is contained in:
2025-07-23 17:16:56 +03:00
parent 009d5fff10
commit 5069ea72ef
3 changed files with 117 additions and 6 deletions

View File

@@ -34,7 +34,7 @@ public class IhcClient {
} }
public boolean auth(String login, String password) { public boolean auth(String login, String password) {
HttpPost httpPost = new HttpPost(URI.create("%s/j_spring_security_check?ajax=true".formatted(baseUrl))); var httpPost = new HttpPost(URI.create("%s/j_spring_security_check?ajax=true".formatted(baseUrl)));
httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
httpPost.setHeader("Referer", "%s/login/auth".formatted(baseUrl)); httpPost.setHeader("Referer", "%s/login/auth".formatted(baseUrl));
@@ -97,7 +97,7 @@ public class IhcClient {
} }
} }
public List<DomainRecord> getDomainRecords(int domainId) { public List<DomainRecord> getDomainRecords(Domain domain) {
if (!isAuth) { if (!isAuth) {
throw new RuntimeException("IS NOT AUTH"); throw new RuntimeException("IS NOT AUTH");
} }
@@ -105,9 +105,9 @@ public class IhcClient {
HttpPost httpPost = new HttpPost(URI.create("%s/dnsZone/records".formatted(baseUrl))); HttpPost httpPost = new HttpPost(URI.create("%s/dnsZone/records".formatted(baseUrl)));
httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
httpPost.setHeader("Referer", "%s/dnsZone/index/%d".formatted(baseUrl, domainId)); httpPost.setHeader("Referer", "%s/dnsZone/index/%d".formatted(baseUrl, domain.id()));
httpPost.setEntity(new UrlEncodedFormEntity(List.of( httpPost.setEntity(new UrlEncodedFormEntity(List.of(
new BasicNameValuePair("id", String.valueOf(domainId)) new BasicNameValuePair("id", String.valueOf(domain.id()))
))); )));
try { try {
@@ -137,4 +137,34 @@ public class IhcClient {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
public void updateDomainRecord(Domain domain, DomainRecord domainRecord) {
if (!isAuth) {
throw new RuntimeException("IS NOT AUTH");
}
var httpPost = new HttpPost(URI.create("%s/dnsZone/updateRecord".formatted(baseUrl)));
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
httpPost.setHeader("Referer", "%s/dnsZone/index/%d".formatted(baseUrl, domain.id()));
httpPost.setHeader("X-Requested-With", "XMLHttpRequest");
httpPost.setEntity(new UrlEncodedFormEntity(List.of(
new BasicNameValuePair("name", domainRecord.getName()),
new BasicNameValuePair("content", domainRecord.getContent()),
new BasicNameValuePair("id", String.valueOf(domain.id())),
new BasicNameValuePair("recordId", String.valueOf(domainRecord.getId())),
new BasicNameValuePair("type", domainRecord.getType().name())
)));
try {
httpClient.execute(httpPost, resp -> {
if (resp.getCode() != 200) {
throw new RuntimeException(resp.toString());
}
return null;
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}
} }

View File

@@ -51,8 +51,8 @@ class GetDomainRecordsTest {
var ihc = new IhcClient("http://localhost:%d".formatted(port)); var ihc = new IhcClient("http://localhost:%d".formatted(port));
ihc.auth("user1", "passwd1"); ihc.auth("user1", "passwd1");
int domainId = ihc.getDomains().getFirst().id(); Domain domain = ihc.getDomains().getFirst();
List<DomainRecord> domainRecords = ihc.getDomainRecords(domainId); List<DomainRecord> domainRecords = ihc.getDomainRecords(domain);
assertEquals(7, domainRecords.size()); assertEquals(7, domainRecords.size());

View File

@@ -0,0 +1,81 @@
package ru.di9.ihc;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.RandomUtils;
import org.junit.jupiter.api.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Optional;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertTrue;
class UpdateDomainRecordTest {
static int port;
static WireMockServer wireMockServer;
@BeforeAll
static void beforeAll() {
port = RandomUtils.nextInt(9000, 9999);
wireMockServer = new WireMockServer(port);
wireMockServer.start();
}
@AfterAll
static void afterAll() {
wireMockServer.stop();
}
@BeforeEach
void before() throws IOException {
wireMockServer.stubFor(post("/j_spring_security_check?ajax=true")
.willReturn(WireMock.okJson("""
{"redirect":{"url":"/"},"alert":{"type":"none","message":""}}""")));
wireMockServer.stubFor(get("/dnsZone/list")
.willReturn(WireMock.ok(IOUtils.resourceToString("/ihc-dns.html", StandardCharsets.UTF_8))
.withHeader("Content-Type", "text/html")));
wireMockServer.stubFor(post("/dnsZone/records")
.willReturn(WireMock.okJson(IOUtils.resourceToString("/ihc-domain.json", StandardCharsets.UTF_8))));
}
@AfterEach
void after() {
wireMockServer.resetAll();
}
@Test
void test() {
wireMockServer.stubFor(post("/dnsZone/updateRecord")
.willReturn(WireMock.okJson("""
{
"alert": {
"type": "success",
"message": "<span class='alert__msg'>Операция выполнена успешно</span>"
},
"data": {
"success": true
}
}""")));
var client = new IhcClient("http://localhost:%d".formatted(port));
boolean auth = client.auth("user1", "passwd1");
assertTrue(auth);
List<Domain> domains = client.getDomains();
Optional<Domain> optDomain = domains.stream().filter(it -> it.name().equals("example-2.ru")).findFirst();
assertTrue(optDomain.isPresent());
List<DomainRecord> records = client.getDomainRecords(optDomain.get());
Optional<DomainRecord> optRecord = records.stream().filter(it -> it.getName().equals("_acme-challenge")).findFirst();
assertTrue(optRecord.isPresent());
optRecord.get().setContent("SOME-TEST-01");
assertDoesNotThrow(() -> client.updateDomainRecord(optDomain.get(), optRecord.get()));
}
}