From 36b4f9459f7e76d9da04ea5df781b9fc43803521 Mon Sep 17 00:00:00 2001 From: Voomra Date: Thu, 24 Jul 2025 03:20:29 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B4=D0=BE=D0=BC=D0=B5=D0=BD=D0=BD?= =?UTF-8?q?=D0=BE=D0=B9=20=D0=B7=D0=B0=D0=BF=D0=B8=D1=81=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ru/di9/ihc/IhcClient.java | 29 +++++++ .../java/ru/di9/ihc/AddDomainRecordTest.java | 76 +++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 src/test/java/ru/di9/ihc/AddDomainRecordTest.java diff --git a/src/main/java/ru/di9/ihc/IhcClient.java b/src/main/java/ru/di9/ihc/IhcClient.java index 639e406..0270631 100644 --- a/src/main/java/ru/di9/ihc/IhcClient.java +++ b/src/main/java/ru/di9/ihc/IhcClient.java @@ -167,4 +167,33 @@ public class IhcClient { throw new RuntimeException(e); } } + + public void addDomainRecord(Domain domain, String name, RecordType type, String content, Integer priority) { + if (!isAuth) { + throw new RuntimeException("IS NOT AUTH"); + } + + var httpPost = new HttpPost(URI.create("%s/dnsZone/createRecord".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", name), + new BasicNameValuePair("type", type.name()), + new BasicNameValuePair("content", content), + 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/AddDomainRecordTest.java b/src/test/java/ru/di9/ihc/AddDomainRecordTest.java new file mode 100644 index 0000000..2afbb28 --- /dev/null +++ b/src/test/java/ru/di9/ihc/AddDomainRecordTest.java @@ -0,0 +1,76 @@ +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 AddDomainRecordTest { + 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/createRecord") + .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()); + + assertDoesNotThrow(() -> client.addDomainRecord(optDomain.get(), "_some-test", RecordType.TXT, "SOME-TEST-02", null)); + } +}