Added ZoneTransfer implementation

This commit is contained in:
2025-10-28 21:10:24 +01:00
parent 6eed338ea8
commit 591b2f8899
51 changed files with 4176 additions and 0 deletions

View File

@@ -0,0 +1,140 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AMWD.Net.Api.Cloudflare;
using AMWD.Net.Api.Cloudflare.Dns;
using AMWD.Net.Api.Cloudflare.Dns.Internals;
using Moq;
namespace Cloudflare.Dns.Tests.DnsZoneTransfersExtensions.Outgoing
{
[TestClass]
public class CreatePrimaryZoneConfigurationTest
{
public TestContext TestContext { get; set; }
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
private const string PeerId = "23ff594956f20c2a721606e94745a8aa";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<OutgoingZoneConfiguration> _response;
private List<(string RequestPath, InternalPrimaryZoneConfigurationRequest Request)> _callbacks;
private PrimaryZoneConfigurationRequest _request;
[TestInitialize]
public void Initialize()
{
_callbacks = [];
_response = new CloudflareResponse<OutgoingZoneConfiguration>
{
Success = true,
Messages = [new ResponseInfo(1000, "Message 1")],
Errors = [new ResponseInfo(1000, "Error 1")],
Result = new OutgoingZoneConfiguration
{
Id = "269d8f4853475ca241c4e730be286b20",
Name = "www.example.com",
Peers = [PeerId]
}
};
_request = new PrimaryZoneConfigurationRequest(ZoneId, "www.example.com")
{
Peers = [PeerId]
};
}
[TestMethod]
public async Task ShouldCreatePrimaryZoneConfiguration()
{
// Arrange
var client = GetClient();
// Act
var response = await client.CreatePrimaryZoneConfiguration(_request, TestContext.CancellationToken);
// Assert
Assert.IsNotNull(response);
Assert.IsTrue(response.Success);
Assert.AreEqual(_response.Result, response.Result);
Assert.HasCount(1, _callbacks);
var (requestPath, request) = _callbacks.First();
Assert.AreEqual($"/zones/{ZoneId}/secondary_dns/outgoing", requestPath);
Assert.IsNotNull(request);
Assert.AreEqual(_request.Name, request.Name);
CollectionAssert.AreEqual(_request.Peers.ToList(), request.Peers.ToList());
_clientMock.Verify(m => m.PostAsync<OutgoingZoneConfiguration, InternalPrimaryZoneConfigurationRequest>(
$"/zones/{ZoneId}/secondary_dns/outgoing",
It.IsAny<InternalPrimaryZoneConfigurationRequest>(),
null,
TestContext.CancellationToken), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
[TestMethod]
[DataRow(null)]
[DataRow("")]
[DataRow(" ")]
public async Task ShouldThrowArgumentNullExceptionForName(string name)
{
// Arrange
_request.Name = name;
var client = GetClient();
// Act & Assert
await Assert.ThrowsExactlyAsync<ArgumentNullException>(async () =>
{
await client.CreatePrimaryZoneConfiguration(_request, TestContext.CancellationToken);
});
}
[TestMethod]
public async Task ShouldThrowArgumentOutOfRangeExceptionForEmptyPeers()
{
// Arrange
_request.Peers = [];
var client = GetClient();
// Act & Assert
await Assert.ThrowsExactlyAsync<ArgumentOutOfRangeException>(async () =>
{
await client.CreatePrimaryZoneConfiguration(_request, TestContext.CancellationToken);
});
}
[TestMethod]
public async Task ShouldThrowArgumentExceptionForInvalidPeerId()
{
// Arrange
_request.Peers = ["invalid-peer-id"]; // invalid format
var client = GetClient();
// Act & Assert
await Assert.ThrowsExactlyAsync<ArgumentException>(async () =>
{
await client.CreatePrimaryZoneConfiguration(_request, TestContext.CancellationToken);
});
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.PostAsync<OutgoingZoneConfiguration, InternalPrimaryZoneConfigurationRequest>(
It.IsAny<string>(),
It.IsAny<InternalPrimaryZoneConfigurationRequest>(),
It.IsAny<IQueryParameterFilter>(),
It.IsAny<CancellationToken>()))
.Callback<string, InternalPrimaryZoneConfigurationRequest, IQueryParameterFilter, CancellationToken>((requestPath, request, _, __) => _callbacks.Add((requestPath, request)))
.ReturnsAsync(() => _response);
return _clientMock.Object;
}
}
}

View File

@@ -0,0 +1,74 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AMWD.Net.Api.Cloudflare;
using AMWD.Net.Api.Cloudflare.Dns;
using Moq;
namespace Cloudflare.Dns.Tests.DnsZoneTransfersExtensions.Outgoing
{
[TestClass]
public class DeletePrimaryZoneConfigurationTest
{
public TestContext TestContext { get; set; }
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<Identifier> _response;
private List<(string RequestPath, IQueryParameterFilter QueryFilter)> _callbacks;
[TestInitialize]
public void Initialize()
{
_callbacks = [];
_response = new CloudflareResponse<Identifier>
{
Success = true,
Messages = new List<ResponseInfo> { new(1000, "Message 1") },
Errors = [],
Result = new Identifier
{
Id = "269d8f4853475ca241c4e730be286b20"
}
};
}
[TestMethod]
public async Task ShouldDeletePrimaryZoneConfiguration()
{
// Arrange
var client = GetClient();
// Act
var response = await client.DeletePrimaryZoneConfiguration(ZoneId, TestContext.CancellationToken);
// Assert
Assert.IsNotNull(response);
Assert.IsTrue(response.Success);
Assert.IsNotNull(response.Result);
Assert.AreEqual("269d8f4853475ca241c4e730be286b20", response.Result.Id);
Assert.HasCount(1, _callbacks);
var (requestPath, queryFilter) = _callbacks.First();
Assert.AreEqual($"/zones/{ZoneId}/secondary_dns/outgoing", requestPath);
Assert.IsNull(queryFilter);
_clientMock.Verify(m => m.DeleteAsync<Identifier>($"/zones/{ZoneId}/secondary_dns/outgoing", null, TestContext.CancellationToken), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.DeleteAsync<Identifier>(It.IsAny<string>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
.Callback<string, IQueryParameterFilter, CancellationToken>((requestPath, queryFilter, _) => _callbacks.Add((requestPath, queryFilter)))
.ReturnsAsync(() => _response);
return _clientMock.Object;
}
}
}

View File

@@ -0,0 +1,78 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AMWD.Net.Api.Cloudflare;
using AMWD.Net.Api.Cloudflare.Dns;
using Moq;
namespace Cloudflare.Dns.Tests.DnsZoneTransfersExtensions.Outgoing
{
[TestClass]
public class DisableOutgoingZoneTransfersTest
{
public TestContext TestContext { get; set; }
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<string> _response;
private List<(string RequestPath, object Request)> _callbacks;
[TestInitialize]
public void Initialize()
{
_callbacks = [];
_response = new CloudflareResponse<string>
{
Success = true,
Messages = [
new ResponseInfo(1000, "Message 1")
],
Errors = [
new ResponseInfo(1000, "Error 1")
],
Result = "Disabled"
};
}
[TestMethod]
public async Task ShouldDisableOutgoingZoneTransfers()
{
// Arrange
var client = GetClient();
// Act
var response = await client.DisableOutgoingZoneTransfers(ZoneId, TestContext.CancellationToken);
// Assert
Assert.IsNotNull(response);
Assert.IsTrue(response.Success);
Assert.AreEqual(_response.Result, response.Result);
Assert.HasCount(1, _callbacks);
var (requestPath, request) = _callbacks.First();
Assert.AreEqual($"/zones/{ZoneId}/secondary_dns/outgoing/disable", requestPath);
Assert.IsNull(request);
_clientMock.Verify(m => m.PostAsync<string, object>(
$"/zones/{ZoneId}/secondary_dns/outgoing/disable",
null,
null,
TestContext.CancellationToken), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.PostAsync<string, object>(It.IsAny<string>(), It.IsAny<object>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
.Callback<string, object, IQueryParameterFilter, CancellationToken>((requestPath, request, _, _) => _callbacks.Add((requestPath, request)))
.ReturnsAsync(() => _response);
return _clientMock.Object;
}
}
}

View File

@@ -0,0 +1,78 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AMWD.Net.Api.Cloudflare;
using AMWD.Net.Api.Cloudflare.Dns;
using Moq;
namespace Cloudflare.Dns.Tests.DnsZoneTransfersExtensions.Outgoing
{
[TestClass]
public class EnableOutgoingZoneTransfersTest
{
public TestContext TestContext { get; set; }
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<string> _response;
private List<(string RequestPath, object Request)> _callbacks;
[TestInitialize]
public void Initialize()
{
_callbacks = [];
_response = new CloudflareResponse<string>
{
Success = true,
Messages = [
new ResponseInfo(1000, "Message 1")
],
Errors = [
new ResponseInfo(1000, "Error 1")
],
Result = "Enabled"
};
}
[TestMethod]
public async Task ShouldDisableOutgoingZoneTransfers()
{
// Arrange
var client = GetClient();
// Act
var response = await client.EnableOutgoingZoneTransfers(ZoneId, TestContext.CancellationToken);
// Assert
Assert.IsNotNull(response);
Assert.IsTrue(response.Success);
Assert.AreEqual(_response.Result, response.Result);
Assert.HasCount(1, _callbacks);
var (requestPath, request) = _callbacks.First();
Assert.AreEqual($"/zones/{ZoneId}/secondary_dns/outgoing/enable", requestPath);
Assert.IsNull(request);
_clientMock.Verify(m => m.PostAsync<string, object>(
$"/zones/{ZoneId}/secondary_dns/outgoing/enable",
null,
null,
TestContext.CancellationToken), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.PostAsync<string, object>(It.IsAny<string>(), It.IsAny<object>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
.Callback<string, object, IQueryParameterFilter, CancellationToken>((requestPath, request, _, _) => _callbacks.Add((requestPath, request)))
.ReturnsAsync(() => _response);
return _clientMock.Object;
}
}
}

View File

@@ -0,0 +1,75 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AMWD.Net.Api.Cloudflare;
using AMWD.Net.Api.Cloudflare.Dns;
using Moq;
namespace Cloudflare.Dns.Tests.DnsZoneTransfersExtensions.Outgoing
{
[TestClass]
public class ForceDNSNotifyTest
{
public TestContext TestContext { get; set; }
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<string> _response;
private List<(string RequestPath, object Request)> _callbacks;
[TestInitialize]
public void Initialize()
{
_callbacks = [];
_response = new CloudflareResponse<string>
{
Success = true,
Messages = [new ResponseInfo(1000, "Message 1")],
Errors = [new ResponseInfo(1000, "Error 1")],
Result = "OK"
};
}
[TestMethod]
public async Task ShouldForceDNSNotify()
{
// Arrange
var client = GetClient();
// Act
var response = await client.ForceDNSNotify(ZoneId, TestContext.CancellationToken);
// Assert
Assert.IsNotNull(response);
Assert.IsTrue(response.Success);
Assert.AreEqual(_response.Result, response.Result);
Assert.HasCount(1, _callbacks);
var (requestPath, request) = _callbacks.First();
Assert.AreEqual($"/zones/{ZoneId}/secondary_dns/outgoing/force_notify", requestPath);
Assert.IsNull(request);
_clientMock.Verify(m => m.PostAsync<string, object>(
$"/zones/{ZoneId}/secondary_dns/outgoing/force_notify",
null,
null,
TestContext.CancellationToken), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.PostAsync<string, object>(It.IsAny<string>(), It.IsAny<object>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
.Callback<string, object, IQueryParameterFilter, CancellationToken>((requestPath, request, _, _) => _callbacks.Add((requestPath, request)))
.ReturnsAsync(() => _response);
return _clientMock.Object;
}
}
}

View File

@@ -0,0 +1,70 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AMWD.Net.Api.Cloudflare;
using AMWD.Net.Api.Cloudflare.Dns;
using Moq;
namespace Cloudflare.Dns.Tests.DnsZoneTransfersExtensions.Outgoing
{
[TestClass]
public class GetOutgoingZoneTransferStatusTest
{
public TestContext TestContext { get; set; }
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<string> _response;
private List<(string RequestPath, IQueryParameterFilter QueryFilter)> _callbacks;
[TestInitialize]
public void Initialize()
{
_callbacks = [];
_response = new CloudflareResponse<string>
{
Success = true,
Messages = [new ResponseInfo(1000, "Message 1")],
Errors = [],
Result = "Enabled"
};
}
[TestMethod]
public async Task ShouldGetOutgoingZoneTransferStatus()
{
// Arrange
var client = GetClient();
// Act
var response = await client.GetOutgoingZoneTransferStatus(ZoneId, TestContext.CancellationToken);
// Assert
Assert.IsNotNull(response);
Assert.IsTrue(response.Success);
Assert.AreEqual(_response.Result, response.Result);
Assert.HasCount(1, _callbacks);
var (requestPath, queryFilter) = _callbacks.First();
Assert.AreEqual($"/zones/{ZoneId}/secondary_dns/outgoing/status", requestPath);
Assert.IsNull(queryFilter);
_clientMock.Verify(m => m.GetAsync<string>($"/zones/{ZoneId}/secondary_dns/outgoing/status", null, TestContext.CancellationToken), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.GetAsync<string>(It.IsAny<string>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
.Callback<string, IQueryParameterFilter, CancellationToken>((requestPath, queryFilter, _) => _callbacks.Add((requestPath, queryFilter)))
.ReturnsAsync(() => _response);
return _clientMock.Object;
}
}
}

View File

@@ -0,0 +1,78 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AMWD.Net.Api.Cloudflare;
using AMWD.Net.Api.Cloudflare.Dns;
using Moq;
namespace Cloudflare.Dns.Tests.DnsZoneTransfersExtensions.Outgoing
{
[TestClass]
public class PrimaryZoneConfigurationDetailsTest
{
public TestContext TestContext { get; set; }
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
private const string PeerId = "23ff594956f20c2a721606e94745a8aa";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<OutgoingZoneConfiguration> _response;
private List<(string RequestPath, IQueryParameterFilter QueryFilter)> _callbacks;
[TestInitialize]
public void Initialize()
{
_callbacks = [];
_response = new CloudflareResponse<OutgoingZoneConfiguration>
{
Success = true,
Messages = [new ResponseInfo(1000, "Message 1")],
Errors = [],
Result = new OutgoingZoneConfiguration
{
Id = "269d8f4853475ca241c4e730be286b20",
Name = "www.example.com",
Peers = [PeerId],
SoaSerial = 12345
}
};
}
[TestMethod]
public async Task ShouldGetPrimaryZoneConfiguration()
{
// Arrange
var client = GetClient();
// Act
var response = await client.PrimaryZoneConfigurationDetails(ZoneId, TestContext.CancellationToken);
// Assert
Assert.IsNotNull(response);
Assert.IsTrue(response.Success);
Assert.IsNotNull(response.Result);
Assert.AreEqual("269d8f4853475ca241c4e730be286b20", response.Result.Id);
Assert.HasCount(1, _callbacks);
var (requestPath, queryFilter) = _callbacks.First();
Assert.AreEqual($"/zones/{ZoneId}/secondary_dns/outgoing", requestPath);
Assert.IsNull(queryFilter);
_clientMock.Verify(m => m.GetAsync<OutgoingZoneConfiguration>($"/zones/{ZoneId}/secondary_dns/outgoing", null, TestContext.CancellationToken), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.GetAsync<OutgoingZoneConfiguration>(It.IsAny<string>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
.Callback<string, IQueryParameterFilter, CancellationToken>((requestPath, queryFilter, _) => _callbacks.Add((requestPath, queryFilter)))
.ReturnsAsync(() => _response);
return _clientMock.Object;
}
}
}

View File

@@ -0,0 +1,126 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AMWD.Net.Api.Cloudflare;
using AMWD.Net.Api.Cloudflare.Dns;
using AMWD.Net.Api.Cloudflare.Dns.Internals;
using Moq;
namespace Cloudflare.Dns.Tests.DnsZoneTransfersExtensions.Outgoing
{
[TestClass]
public class UpdatePrimaryZoneConfigurationTest
{
public TestContext TestContext { get; set; }
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
private const string PeerId = "23ff594956f20c2a721606e94745a8aa";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<OutgoingZoneConfiguration> _response;
private List<(string RequestPath, InternalPrimaryZoneConfigurationRequest Request)> _callbacks;
private PrimaryZoneConfigurationRequest _request;
[TestInitialize]
public void Initialize()
{
_callbacks = [];
_response = new CloudflareResponse<OutgoingZoneConfiguration>
{
Success = true,
Messages = [new ResponseInfo(1000, "Message 1")],
Errors = [],
Result = new OutgoingZoneConfiguration
{
Id = "269d8f4853475ca241c4e730be286b20",
Name = "www.example.com",
Peers = [PeerId],
SoaSerial = 12345
}
};
_request = new PrimaryZoneConfigurationRequest(ZoneId, "www.example.com")
{
Peers = [PeerId]
};
}
[TestMethod]
public async Task ShouldUpdatePrimaryZoneConfiguration()
{
// Arrange
var client = GetClient();
// Act
var response = await client.UpdatePrimaryZoneConfiguration(_request, TestContext.CancellationToken);
// Assert
Assert.IsNotNull(response);
Assert.IsTrue(response.Success);
Assert.IsNotNull(response.Result);
Assert.AreEqual(_response.Result, response.Result);
Assert.HasCount(1, _callbacks);
var (requestPath, req) = _callbacks.First();
Assert.AreEqual($"/zones/{ZoneId}/secondary_dns/outgoing", requestPath);
Assert.IsNotNull(req);
Assert.AreEqual(_request.Name, req.Name);
CollectionAssert.AreEqual(_request.Peers.ToList(), req.Peers.ToList());
_clientMock.Verify(m => m.PutAsync<OutgoingZoneConfiguration, InternalPrimaryZoneConfigurationRequest>(
$"/zones/{ZoneId}/secondary_dns/outgoing",
It.IsAny<InternalPrimaryZoneConfigurationRequest>(),
TestContext.CancellationToken), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
[TestMethod]
[DataRow(null)]
[DataRow("")]
[DataRow(" ")]
public async Task ShouldThrowArgumentNullExceptionForName(string name)
{
// Arrange
_request.Name = name;
var client = GetClient();
// Act & Assert
await Assert.ThrowsExactlyAsync<ArgumentNullException>(async () =>
{
await client.UpdatePrimaryZoneConfiguration(_request, TestContext.CancellationToken);
});
}
[TestMethod]
public async Task ShouldThrowArgumentOutOfRangeExceptionForEmptyPeers()
{
// Arrange
_request.Peers = [];
var client = GetClient();
// Act & Assert
await Assert.ThrowsExactlyAsync<ArgumentOutOfRangeException>(async () =>
{
await client.UpdatePrimaryZoneConfiguration(_request, TestContext.CancellationToken);
});
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.PutAsync<OutgoingZoneConfiguration, InternalPrimaryZoneConfigurationRequest>(
It.IsAny<string>(),
It.IsAny<InternalPrimaryZoneConfigurationRequest>(),
It.IsAny<CancellationToken>()))
.Callback<string, InternalPrimaryZoneConfigurationRequest, CancellationToken>((requestPath, request, _) => _callbacks.Add((requestPath, request)))
.ReturnsAsync(() => _response);
return _clientMock.Object;
}
}
}