feat: добавление доменной записи
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
76
src/test/java/ru/di9/ihc/AddDomainRecordTest.java
Normal file
76
src/test/java/ru/di9/ihc/AddDomainRecordTest.java
Normal file
@@ -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": "<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());
|
||||
|
||||
assertDoesNotThrow(() -> client.addDomainRecord(optDomain.get(), "_some-test", RecordType.TXT, "SOME-TEST-02", null));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user