diff --git a/src/main/java/ru/di9/ihc/IhcClient.java b/src/main/java/ru/di9/ihc/IhcClient.java index 0270631..9f0015b 100644 --- a/src/main/java/ru/di9/ihc/IhcClient.java +++ b/src/main/java/ru/di9/ihc/IhcClient.java @@ -196,4 +196,31 @@ public class IhcClient { throw new RuntimeException(e); } } + + public void removeDomainRecord(Domain domain, int domainRecordId) { + if (!isAuth) { + throw new RuntimeException("IS NOT AUTH"); + } + + var httpPost = new HttpPost(URI.create("%s/dnsZone/deleteRecord".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("recordId", String.valueOf(domainRecordId)), + new BasicNameValuePair("id", String.valueOf(domain.id())) + ))); + + try { + httpClient.execute(httpPost, resp -> { + if (resp.getCode() != 200) { + throw new RuntimeException(resp.toString()); + } + return null; + }); + } catch (IOException e) { + throw new RuntimeException(e); + } + } } diff --git a/src/test/java/ru/di9/ihc/RemoveDomainRecordTest.java b/src/test/java/ru/di9/ihc/RemoveDomainRecordTest.java new file mode 100644 index 0000000..41c10f6 --- /dev/null +++ b/src/test/java/ru/di9/ihc/RemoveDomainRecordTest.java @@ -0,0 +1,80 @@ +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 RemoveDomainRecordTest { + 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/deleteRecord") + .willReturn(WireMock.okJson(""" + { + "alert": { + "type": "success", + "message": "Операция выполнена успешно" + }, + "data": { + "success": true + } + }"""))); + + var client = new IhcClient("http://localhost:%d".formatted(port)); + boolean auth = client.auth("user1", "passwd1"); + assertTrue(auth); + + List domains = client.getDomains(); + Optional optDomain = domains.stream().filter(it -> it.name().equals("example-2.ru")).findFirst(); + assertTrue(optDomain.isPresent()); + + List records = client.getDomainRecords(optDomain.get()); + Optional optRecord = records.stream().filter(it -> it.getName().equals("_acme-challenge")).findFirst(); + assertTrue(optRecord.isPresent()); + + assertDoesNotThrow(() -> client.removeDomainRecord(optDomain.get(), optRecord.get().getId())); + } +}