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,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.ACLs
{
[TestClass]
public class ACLDetailsTest
{
public TestContext TestContext { get; set; }
private const string AccountId = "023e105f4ecef8ad9ca31a8372d0c353";
private const string AclId = "23ff594956f20c2a721606e94745a8aa";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<ACL> _response;
private List<string> _callbacks;
[TestInitialize]
public void Initialize()
{
_callbacks = [];
_response = new CloudflareResponse<ACL>
{
Success = true,
Messages = [new ResponseInfo(1000, "Message 1")],
Errors = [new ResponseInfo(1000, "Error 1")],
Result = new ACL
{
Id = AclId,
IpRange = "192.0.2.0/24",
Name = "Test ACL"
}
};
}
[TestMethod]
public async Task ShouldGetAclDetails()
{
// Arrange
var client = GetClient();
// Act
var response = await client.ACLDetails(AccountId, AclId, TestContext.CancellationToken);
// Assert
Assert.IsNotNull(response);
Assert.IsTrue(response.Success);
Assert.AreEqual(_response.Result, response.Result);
Assert.HasCount(1, _callbacks);
string requestPath = _callbacks.First();
Assert.AreEqual($"/accounts/{AccountId}/secondary_dns/acls/{AclId}", requestPath);
_clientMock.Verify(m => m.GetAsync<ACL>($"/accounts/{AccountId}/secondary_dns/acls/{AclId}", null, TestContext.CancellationToken), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.GetAsync<ACL>(It.IsAny<string>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
.Callback<string, IQueryParameterFilter, CancellationToken>((requestPath, _, _) => _callbacks.Add(requestPath))
.ReturnsAsync(() => _response);
return _clientMock.Object;
}
}
}

View File

@@ -0,0 +1,166 @@
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.ACLs
{
[TestClass]
public class CreateACLTest
{
public TestContext TestContext { get; set; }
private const string AccountId = "023e105f4ecef8ad9ca31a8372d0c353";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<ACL> _response;
private List<(string RequestPath, InternalDnsZoneTransferAclRequest Request)> _callbacks;
private CreateACLRequest _request;
[TestInitialize]
public void Initialize()
{
_callbacks = [];
_response = new CloudflareResponse<ACL>
{
Success = true,
Messages = [
new ResponseInfo(1000, "Message 1")
],
Errors = [
new ResponseInfo(1000, "Error 1")
],
Result = new ACL
{
Id = "23ff594956f20c2a721606e94745a8aa",
IpRange = "192.0.2.53/28",
Name = "my-acl-1"
}
};
_request = new CreateACLRequest(
accountId: AccountId,
ipRange: "192.0.2.53/28",
name: "my-acl-1"
);
}
[TestMethod]
public async Task ShouldCreateAcl()
{
// Arrange
var client = GetClient();
// Act
var response = await client.CreateACL(_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($"/accounts/{AccountId}/secondary_dns/acls", requestPath);
Assert.IsNotNull(request);
Assert.AreEqual(_request.Name, request.Name);
Assert.AreEqual(_request.IpRange, request.IpRange);
Assert.AreEqual("192.0.2.53", _request.IpRangeBaseAddress.ToString());
Assert.AreEqual(28, _request.IpRangeSubnet);
_clientMock.Verify(m => m.PostAsync<ACL, InternalDnsZoneTransferAclRequest>($"/accounts/{AccountId}/secondary_dns/acls", It.IsAny<InternalDnsZoneTransferAclRequest>(), null, TestContext.CancellationToken), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
[TestMethod]
[DataRow("127.0.0.1/20")]
[DataRow("fd00::/56")]
public async Task ShouldThrowArumentOutOfRangeExceptionForWrongSubnetDefinition(string ipRange)
{
// Arrange
_request.IpRange = ipRange;
var client = GetClient();
// Act & Assert
await Assert.ThrowsExactlyAsync<ArgumentOutOfRangeException>(async () =>
{
await client.CreateACL(_request, TestContext.CancellationToken);
});
}
[TestMethod]
[DataRow(null)]
[DataRow("")]
[DataRow("\t")]
[DataRow(" ")]
public void ShouldThrowArgumentNullExceptionForIpRange(string ipRange)
{
// Arrange
// Act & Assert
Assert.ThrowsExactly<ArgumentNullException>(() =>
{
_ = new CreateACLRequest(AccountId, ipRange, "my-acl-1");
});
}
[TestMethod]
[DataRow("192.0.2.53")]
[DataRow("192.0.2.53/28/28")]
public void ShouldThrowFormatExceptionForInvalidFormat(string ipRange)
{
// Arrange
// Act & Assert
Assert.ThrowsExactly<FormatException>(() =>
{
_ = new CreateACLRequest(AccountId, ipRange, "my-acl-1");
});
}
[TestMethod]
public void ShouldThrowFormatExceptionForInvalidSubnetFormat()
{
// Arrange
// Act & Assert
Assert.ThrowsExactly<FormatException>(() =>
{
_ = new CreateACLRequest(AccountId, "192.0.2.53/a", "my-acl-1");
});
}
[TestMethod]
[DataRow("127.0.0.1/-1")]
[DataRow("127.0.0.1/33")]
[DataRow("fd00::/-1")]
[DataRow("fd00::/129")]
public void ShouldThrowFormatExceptionForInvalidSubnetLength(string ipRange)
{
// Arrange
// Act & Assert
Assert.ThrowsExactly<FormatException>(() =>
{
_ = new CreateACLRequest(AccountId, ipRange, "my-acl-1");
});
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.PostAsync<ACL, InternalDnsZoneTransferAclRequest>(It.IsAny<string>(), It.IsAny<InternalDnsZoneTransferAclRequest>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
.Callback<string, InternalDnsZoneTransferAclRequest, IQueryParameterFilter, CancellationToken>((requestPath, request, _, _) => _callbacks.Add((requestPath, request)))
.ReturnsAsync(() => _response);
return _clientMock.Object;
}
}
}

View File

@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
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.ACLs
{
[TestClass]
public class DeleteACLTest
{
public TestContext TestContext { get; set; }
private const string AccountId = "023e105f4ecef8ad9ca31a8372d0c353";
private const string AclId = "23ff594956f20c2a721606e94745a8aa";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<Identifier> _response;
private List<string> _callbacks;
[TestInitialize]
public void Initialize()
{
_callbacks = new List<string>();
_response = new CloudflareResponse<Identifier>
{
Success = true,
Messages = [new ResponseInfo(1000, "Message 1")],
Errors = [new ResponseInfo(1000, "Error 1")],
Result = new Identifier
{
Id = AclId
}
};
}
[TestMethod]
public async Task ShouldDeleteAcl()
{
// Arrange
var client = GetClient();
// Act
var response = await client.DeleteACL(AccountId, AclId, TestContext.CancellationToken);
// Assert
Assert.IsNotNull(response);
Assert.IsTrue(response.Success);
Assert.AreEqual(_response.Result, response.Result);
Assert.HasCount(1, _callbacks);
string requestPath = _callbacks.First();
Assert.AreEqual($"/accounts/{AccountId}/secondary_dns/acls/{AclId}", requestPath);
_clientMock.Verify(m => m.DeleteAsync<Identifier>($"/accounts/{AccountId}/secondary_dns/acls/{AclId}", 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, _, _) => _callbacks.Add(requestPath))
.ReturnsAsync(() => _response);
return _clientMock.Object;
}
}
}

View File

@@ -0,0 +1,77 @@
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.ACLs
{
[TestClass]
public class ListACLsTest
{
public TestContext TestContext { get; set; }
private const string AccountId = "023e105f4ecef8ad9ca31a8372d0c353";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<IReadOnlyCollection<ACL>> _response;
private List<string> _callbacks;
[TestInitialize]
public void Initialize()
{
_callbacks = new List<string>();
_response = new CloudflareResponse<IReadOnlyCollection<ACL>>
{
Success = true,
Messages = new List<ResponseInfo> { new ResponseInfo(1000, "Message 1") },
Errors = new List<ResponseInfo> { new ResponseInfo(1000, "Error 1") },
Result = new List<ACL>
{
new ACL
{
Id = "23ff594956f20c2a721606e94745a8aa",
IpRange = "192.0.2.0/24",
Name = "Test ACL"
}
}
};
}
[TestMethod]
public async Task ShouldListAcls()
{
// Arrange
var client = GetClient();
// Act
var response = await client.ListACLs(AccountId, TestContext.CancellationToken);
// Assert
Assert.IsNotNull(response);
Assert.IsTrue(response.Success);
Assert.AreEqual(_response.Result, response.Result);
Assert.HasCount(1, _callbacks);
string requestPath = _callbacks.First();
Assert.AreEqual($"/accounts/{AccountId}/secondary_dns/acls", requestPath);
_clientMock.Verify(m => m.GetAsync<IReadOnlyCollection<ACL>>($"/accounts/{AccountId}/secondary_dns/acls", null, TestContext.CancellationToken), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.GetAsync<IReadOnlyCollection<ACL>>(It.IsAny<string>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
.Callback<string, IQueryParameterFilter, CancellationToken>((requestPath, _, _) => _callbacks.Add(requestPath))
.ReturnsAsync(() => _response);
return _clientMock.Object;
}
}
}

View File

@@ -0,0 +1,110 @@
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.ACLs
{
[TestClass]
public class UpdateACLTest
{
public TestContext TestContext { get; set; }
private const string AccountId = "023e105f4ecef8ad9ca31a8372d0c353";
private const string AclId = "23ff594956f20c2a721606e94745a8aa";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<ACL> _response;
private List<(string RequestPath, InternalDnsZoneTransferAclRequest Request)> _callbacks;
private UpdateDnsZoneTransferAclRequest _request;
[TestInitialize]
public void Initialize()
{
_callbacks = [];
_response = new CloudflareResponse<ACL>
{
Success = true,
Messages = [
new ResponseInfo(1000, "Message 1")
],
Errors = [
new ResponseInfo(1000, "Error 1")
],
Result = new ACL
{
Id = AclId,
IpRange = "192.0.2.53/28",
Name = "my-acl-1"
}
};
_request = new UpdateDnsZoneTransferAclRequest(
accountId: AccountId,
aclId: AclId,
ipRange: "192.0.2.53/28",
name: "my-acl-1"
);
}
[TestMethod]
public async Task ShouldUpdateAcl()
{
// Arrange
var client = GetClient();
// Act
var response = await client.UpdateACL(_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($"/accounts/{AccountId}/secondary_dns/acls/{AclId}", requestPath);
Assert.IsNotNull(request);
Assert.AreEqual(_request.Name, request.Name);
Assert.AreEqual(_request.IpRange, request.IpRange);
Assert.AreEqual("192.0.2.53", _request.IpRangeBaseAddress.ToString());
Assert.AreEqual(28, _request.IpRangeSubnet);
_clientMock.Verify(m => m.PutAsync<ACL, InternalDnsZoneTransferAclRequest>($"/accounts/{AccountId}/secondary_dns/acls/{AclId}", It.IsAny<InternalDnsZoneTransferAclRequest>(), TestContext.CancellationToken), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
[TestMethod]
[DataRow("127.0.0.1/20")]
[DataRow("fd00::/56")]
public async Task ShouldThrowArumentOutOfRangeExceptionForWrongSubnetDefinition(string ipRange)
{
// Arrange
_request.IpRange = ipRange;
var client = GetClient();
// Act & Assert
await Assert.ThrowsExactlyAsync<ArgumentOutOfRangeException>(async () =>
{
await client.UpdateACL(_request, TestContext.CancellationToken);
});
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.PutAsync<ACL, InternalDnsZoneTransferAclRequest>(It.IsAny<string>(), It.IsAny<InternalDnsZoneTransferAclRequest>(), It.IsAny<CancellationToken>()))
.Callback<string, InternalDnsZoneTransferAclRequest, 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
{
[TestClass]
public class ForceAXFRTest
{
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 ShouldForceAXFR()
{
// Arrange
var client = GetClient();
// Act
var response = await client.ForceAXFR(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/force_axfr", requestPath);
Assert.IsNull(request);
_clientMock.Verify(m => m.PostAsync<string, object>($"/zones/{ZoneId}/secondary_dns/force_axfr", 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,157 @@
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.Incoming
{
[TestClass]
public class CreateSecondaryZoneConfigurationTest
{
public TestContext TestContext { get; set; }
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
private const string PeerId = "23ff594956f20c2a721606e94745a8aa";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<IncomingZoneConfiguration> _response;
private List<(string RequestPath, InternalSecondaryZoneConfigurationRequest Request)> _callbacks;
private SecondaryZoneConfigurationRequest _request;
[TestInitialize]
public void Initialize()
{
_callbacks = [];
_response = new CloudflareResponse<IncomingZoneConfiguration>
{
Success = true,
Messages = [new ResponseInfo(1000, "Message 1")],
Errors = [new ResponseInfo(1000, "Error 1")],
Result = new IncomingZoneConfiguration
{
Id = "269d8f4853475ca241c4e730be286b20",
Name = "www.example.com",
AutoRefreshSeconds = 86400,
Peers = [PeerId]
}
};
_request = new SecondaryZoneConfigurationRequest(ZoneId, "www.example.com")
{
AutoRefreshSeconds = 86400,
Peers = [PeerId]
};
}
[TestMethod]
public async Task ShouldCreateSecondaryZoneConfiguration()
{
// Arrange
var client = GetClient();
// Act
var response = await client.CreateSecondaryZoneConfiguration(_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/incoming", requestPath);
Assert.IsNotNull(request);
Assert.AreEqual(_request.Name, request.Name);
Assert.AreEqual(_request.AutoRefreshSeconds, request.AutoRefreshSeconds);
CollectionAssert.AreEqual(_request.Peers.ToList(), request.Peers.ToList());
_clientMock.Verify(m => m.PostAsync<IncomingZoneConfiguration, InternalSecondaryZoneConfigurationRequest>(
$"/zones/{ZoneId}/secondary_dns/incoming",
It.IsAny<InternalSecondaryZoneConfigurationRequest>(),
null,
It.IsAny<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.CreateSecondaryZoneConfiguration(_request, TestContext.CancellationToken);
});
}
[TestMethod]
public async Task ShouldThrowArgumentOutOfRangeExceptionForAutoRefreshSeconds()
{
// Arrange
_request.AutoRefreshSeconds = -1;
var client = GetClient();
// Act & Assert
await Assert.ThrowsExactlyAsync<ArgumentOutOfRangeException>(async () =>
{
await client.CreateSecondaryZoneConfiguration(_request, TestContext.CancellationToken);
});
}
[TestMethod]
public async Task ShouldThrowArgumentOutOfRangeExceptionForEmptyPeers()
{
// Arrange
_request.Peers = [];
var client = GetClient();
// Act & Assert
await Assert.ThrowsExactlyAsync<ArgumentOutOfRangeException>(async () =>
{
await client.CreateSecondaryZoneConfiguration(_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.CreateSecondaryZoneConfiguration(_request, TestContext.CancellationToken);
});
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.PostAsync<IncomingZoneConfiguration, InternalSecondaryZoneConfigurationRequest>(
It.IsAny<string>(),
It.IsAny<InternalSecondaryZoneConfigurationRequest>(),
It.IsAny<IQueryParameterFilter>(),
It.IsAny<CancellationToken>()))
.Callback<string, InternalSecondaryZoneConfigurationRequest, IQueryParameterFilter, CancellationToken>((requestPath, request, _, __) => _callbacks.Add((requestPath, request)))
.ReturnsAsync(() => _response);
return _clientMock.Object;
}
}
}

View File

@@ -0,0 +1,71 @@
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.Incoming
{
[TestClass]
public class DeleteSecondaryZoneConfigurationTest
{
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 = new List<(string, IQueryParameterFilter)>();
_response = new CloudflareResponse<Identifier>
{
Success = true,
Messages = new List<ResponseInfo> { new ResponseInfo(1000, "Message 1") },
Errors = new List<ResponseInfo>(),
Result = new Identifier { Id = "269d8f4853475ca241c4e730be286b20" }
};
}
[TestMethod]
public async Task ShouldDeleteSecondaryZoneConfiguration()
{
// Arrange
var client = GetClient();
// Act
var response = await client.DeleteSecondaryZoneConfiguration(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/incoming", requestPath);
Assert.IsNull(queryFilter);
_clientMock.Verify(m => m.DeleteAsync<Identifier>($"/zones/{ZoneId}/secondary_dns/incoming", 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.Incoming
{
[TestClass]
public class SecondaryZoneConfigurationDetailsTest
{
public TestContext TestContext { get; set; }
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
private const string PeerId = "23ff594956f20c2a721606e94745a8aa";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<IncomingZoneConfiguration> _response;
private List<(string RequestPath, IQueryParameterFilter QueryFilter)> _callbacks;
[TestInitialize]
public void Initialize()
{
_callbacks = [];
_response = new CloudflareResponse<IncomingZoneConfiguration>
{
Success = true,
Messages = [new ResponseInfo(1000, "Message 1")],
Errors = [],
Result = new IncomingZoneConfiguration
{
Id = "269d8f4853475ca241c4e730be286b20",
Name = "www.example.com",
AutoRefreshSeconds = 86400,
Peers = [PeerId]
}
};
}
[TestMethod]
public async Task ShouldGetSecondaryZoneConfiguration()
{
// Arrange
var client = GetClient();
// Act
var response = await client.SecondaryZoneConfigurationDetails(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/incoming", requestPath);
Assert.IsNull(queryFilter);
_clientMock.Verify(m => m.GetAsync<IncomingZoneConfiguration>($"/zones/{ZoneId}/secondary_dns/incoming", null, TestContext.CancellationToken), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.GetAsync<IncomingZoneConfiguration>(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,155 @@
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.Incoming
{
[TestClass]
public class UpdateSecondaryZoneConfigurationTest
{
public TestContext TestContext { get; set; }
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
private const string PeerId = "23ff594956f20c2a721606e94745a8aa";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<IncomingZoneConfiguration> _response;
private List<(string RequestPath, InternalSecondaryZoneConfigurationRequest Request)> _callbacks;
private SecondaryZoneConfigurationRequest _request;
[TestInitialize]
public void Initialize()
{
_callbacks = [];
_response = new CloudflareResponse<IncomingZoneConfiguration>
{
Success = true,
Messages = [new ResponseInfo(1000, "Message 1")],
Errors = [new ResponseInfo(1000, "Error 1")],
Result = new IncomingZoneConfiguration
{
Id = "269d8f4853475ca241c4e730be286b20",
Name = "www.example.com",
AutoRefreshSeconds = 86400,
Peers = [PeerId]
}
};
_request = new SecondaryZoneConfigurationRequest(ZoneId, "www.example.com")
{
AutoRefreshSeconds = 86400,
Peers = [PeerId]
};
}
[TestMethod]
public async Task ShouldUpdateSecondaryZoneConfiguration()
{
// Arrange
var client = GetClient();
// Act
var response = await client.UpdateSecondaryZoneConfiguration(_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/incoming", requestPath);
Assert.IsNotNull(request);
Assert.AreEqual(_request.Name, request.Name);
Assert.AreEqual(_request.AutoRefreshSeconds, request.AutoRefreshSeconds);
CollectionAssert.AreEqual(_request.Peers.ToList(), request.Peers.ToList());
_clientMock.Verify(m => m.PutAsync<IncomingZoneConfiguration, InternalSecondaryZoneConfigurationRequest>(
$"/zones/{ZoneId}/secondary_dns/incoming",
It.IsAny<InternalSecondaryZoneConfigurationRequest>(),
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.UpdateSecondaryZoneConfiguration(_request, TestContext.CancellationToken);
});
}
[TestMethod]
public async Task ShouldThrowArgumentOutOfRangeExceptionForAutoRefreshSeconds()
{
// Arrange
_request.AutoRefreshSeconds = -1;
var client = GetClient();
// Act & Assert
await Assert.ThrowsExactlyAsync<ArgumentOutOfRangeException>(async () =>
{
await client.UpdateSecondaryZoneConfiguration(_request, TestContext.CancellationToken);
});
}
[TestMethod]
public async Task ShouldThrowArgumentOutOfRangeExceptionForEmptyPeers()
{
// Arrange
_request.Peers = [];
var client = GetClient();
// Act & Assert
await Assert.ThrowsExactlyAsync<ArgumentOutOfRangeException>(async () =>
{
await client.UpdateSecondaryZoneConfiguration(_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.UpdateSecondaryZoneConfiguration(_request, TestContext.CancellationToken);
});
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.PutAsync<IncomingZoneConfiguration, InternalSecondaryZoneConfigurationRequest>(
It.IsAny<string>(),
It.IsAny<InternalSecondaryZoneConfigurationRequest>(),
It.IsAny<CancellationToken>()))
.Callback<string, InternalSecondaryZoneConfigurationRequest, CancellationToken>((requestPath, request, _) => _callbacks.Add((requestPath, request)))
.ReturnsAsync(() => _response);
return _clientMock.Object;
}
}
}

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;
}
}
}

View File

@@ -0,0 +1,104 @@
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.Peers
{
[TestClass]
public class CreatePeerTest
{
public TestContext TestContext { get; set; }
private const string AccountId = "023e105f4ecef8ad9ca31a8372d0c353";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<Peer> _response;
private List<(string RequestPath, InternalCreatePeerRequest Request)> _callbacks;
private CreatePeerRequest _request;
[TestInitialize]
public void Initialize()
{
_callbacks = new List<(string, InternalCreatePeerRequest)>();
_response = new CloudflareResponse<Peer>
{
Success = true,
Messages = new List<ResponseInfo> { new ResponseInfo(1000, "Message 1") },
Errors = new List<ResponseInfo>(),
Result = new Peer("023e105f4ecef8ad9ca31a8372d0c351", "peer-a")
{
IpAddress = "192.0.2.1",
IXFREnabled = true,
Port = 5353,
TSIGId = "tsig-1"
}
};
_request = new CreatePeerRequest(AccountId, "peer-a");
}
[TestMethod]
public async Task ShouldCreatePeer()
{
// Arrange
var client = GetClient();
// Act
var response = await client.CreatePeer(_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($"/accounts/{AccountId}/secondary_dns/peers", requestPath);
Assert.IsNotNull(req);
Assert.AreEqual(_request.Name, req.Name);
_clientMock.Verify(m => m.PostAsync<Peer, InternalCreatePeerRequest>(
$"/accounts/{AccountId}/secondary_dns/peers",
It.IsAny<InternalCreatePeerRequest>(),
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.CreatePeer(_request, TestContext.CancellationToken);
});
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.PostAsync<Peer, InternalCreatePeerRequest>(It.IsAny<string>(), It.IsAny<InternalCreatePeerRequest>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
.Callback<string, InternalCreatePeerRequest, 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.Peers
{
[TestClass]
public class DeletePeerTest
{
public TestContext TestContext { get; set; }
private const string AccountId = "023e105f4ecef8ad9ca31a8372d0c353";
private const string PeerId = "23ff594956f20c2a721606e94745a8aa";
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 ResponseInfo(1000, "Message 1")],
Errors = [],
Result = new Identifier
{
Id = PeerId
}
};
}
[TestMethod]
public async Task ShouldDeletePeer()
{
// Arrange
var client = GetClient();
// Act
var response = await client.DeletePeer(AccountId, PeerId, TestContext.CancellationToken);
// Assert
Assert.IsNotNull(response);
Assert.IsTrue(response.Success);
Assert.IsNotNull(response.Result);
Assert.AreEqual(PeerId, response.Result.Id);
Assert.HasCount(1, _callbacks);
var (requestPath, queryFilter) = _callbacks.First();
Assert.AreEqual($"/accounts/{AccountId}/secondary_dns/peers/{PeerId}", requestPath);
Assert.IsNull(queryFilter);
_clientMock.Verify(m => m.DeleteAsync<Identifier>($"/accounts/{AccountId}/secondary_dns/peers/{PeerId}", 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,99 @@
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.Peers
{
[TestClass]
public class ListPeersTest
{
public TestContext TestContext { get; set; }
private const string AccountId = "023e105f4ecef8ad9ca31a8372d0c353";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<IReadOnlyCollection<Peer>> _response;
private List<(string RequestPath, IQueryParameterFilter QueryFilter)> _callbacks;
[TestInitialize]
public void Initialize()
{
_callbacks = [];
_response = new CloudflareResponse<IReadOnlyCollection<Peer>>
{
Success = true,
Messages = [new ResponseInfo(1000, "Message 1")],
Errors = [],
Result =
[
new Peer("023e105f4ecef8ad9ca31a8372d0c351", "peer-a")
{
IpAddress = "192.0.2.1",
IXFREnabled = true,
Port = 5353,
TSIGId = "tsig-1"
},
new Peer("023e105f4ecef8ad9ca31a8372d0c352", "peer-b")
{
IpAddress = "2001:db8::1",
IXFREnabled = false,
Port = 53,
TSIGId = null
}
]
};
}
[TestMethod]
public async Task ShouldListPeers()
{
// Arrange
var client = GetClient();
// Act
var response = await client.ListPeers(AccountId, TestContext.CancellationToken);
// Assert
Assert.IsNotNull(response);
Assert.IsTrue(response.Success);
Assert.IsNotNull(response.Result);
Assert.HasCount(2, response.Result);
var peers = response.Result.ToList();
Assert.AreEqual("peer-a", peers[0].Name);
Assert.AreEqual("192.0.2.1", peers[0].IpAddress);
Assert.IsTrue(peers[0].IXFREnabled ?? false);
Assert.AreEqual(5353, peers[0].Port);
Assert.AreEqual("tsig-1", peers[0].TSIGId);
Assert.AreEqual("peer-b", peers[1].Name);
Assert.AreEqual("2001:db8::1", peers[1].IpAddress);
Assert.IsFalse(peers[1].IXFREnabled ?? false);
Assert.AreEqual(53, peers[1].Port);
Assert.IsNull(peers[1].TSIGId);
Assert.HasCount(1, _callbacks);
var (requestPath, queryFilter) = _callbacks.First();
Assert.AreEqual($"/accounts/{AccountId}/secondary_dns/peers", requestPath);
Assert.IsNull(queryFilter);
_clientMock.Verify(m => m.GetAsync<IReadOnlyCollection<Peer>>($"/accounts/{AccountId}/secondary_dns/peers", null, TestContext.CancellationToken), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.GetAsync<IReadOnlyCollection<Peer>>(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,82 @@
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.Peers
{
[TestClass]
public class PeerDetailsTest
{
public TestContext TestContext { get; set; }
private const string AccountId = "023e105f4ecef8ad9ca31a8372d0c353";
private const string PeerId = "23ff594956f20c2a721606e94745a8aa";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<Peer> _response;
private List<(string RequestPath, IQueryParameterFilter QueryFilter)> _callbacks;
[TestInitialize]
public void Initialize()
{
_callbacks = [];
_response = new CloudflareResponse<Peer>
{
Success = true,
Messages = [new ResponseInfo(1000, "Message 1")],
Errors = [],
Result = new Peer(PeerId, "peer-a")
{
IpAddress = "192.0.2.1",
IXFREnabled = true,
Port = 5353,
TSIGId = "tsig-1"
}
};
}
[TestMethod]
public async Task ShouldGetPeerDetails()
{
// Arrange
var client = GetClient();
// Act
var response = await client.PeerDetails(AccountId, PeerId, TestContext.CancellationToken);
// Assert
Assert.IsNotNull(response);
Assert.IsTrue(response.Success);
Assert.IsNotNull(response.Result);
Assert.AreEqual(PeerId, response.Result.Id);
Assert.AreEqual("peer-a", response.Result.Name);
Assert.AreEqual("192.0.2.1", response.Result.IpAddress);
Assert.IsTrue(response.Result.IXFREnabled ?? false);
Assert.AreEqual(5353, response.Result.Port);
Assert.AreEqual("tsig-1", response.Result.TSIGId);
Assert.HasCount(1, _callbacks);
var (requestPath, queryFilter) = _callbacks.First();
Assert.AreEqual($"/accounts/{AccountId}/secondary_dns/peers/{PeerId}", requestPath);
Assert.IsNull(queryFilter);
_clientMock.Verify(m => m.GetAsync<Peer>($"/accounts/{AccountId}/secondary_dns/peers/{PeerId}", null, TestContext.CancellationToken), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.GetAsync<Peer>(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,136 @@
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.Peers
{
[TestClass]
public class UpdatePeerTest
{
public TestContext TestContext { get; set; }
private const string AccountId = "023e105f4ecef8ad9ca31a8372d0c353";
private const string PeerId = "23ff594956f20c2a721606e94745a8aa";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<Peer> _response;
private List<(string RequestPath, InternalUpdatePeerRequest Request)> _callbacks;
private UpdatePeerRequest _request;
[TestInitialize]
public void Initialize()
{
_callbacks = [];
_response = new CloudflareResponse<Peer>
{
Success = true,
Messages = [new ResponseInfo(1000, "Message 1")],
Errors = [],
Result = new Peer(PeerId, "peer-a")
{
IpAddress = "192.0.2.1",
IXFREnabled = true,
Port = 5353,
TSIGId = "tsig-1"
}
};
_request = new UpdatePeerRequest(AccountId, PeerId, "peer-a")
{
IpAddress = "192.0.2.1",
IXFREnable = true,
Port = 5353,
TSIGId = "tsig-1"
};
}
[TestMethod]
public async Task ShouldUpdatePeer()
{
// Arrange
var client = GetClient();
// Act
var response = await client.UpdatePeer(_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($"/accounts/{AccountId}/secondary_dns/peers/{PeerId}", requestPath);
Assert.IsNotNull(req);
Assert.AreEqual(_request.Name, req.Name);
Assert.AreEqual(_request.IpAddress, req.Ip);
Assert.AreEqual(_request.IXFREnable, req.IxfrEnable);
Assert.AreEqual(_request.Port, req.Port);
Assert.AreEqual(_request.TSIGId, req.TSigId);
_clientMock.Verify(m => m.PutAsync<Peer, InternalUpdatePeerRequest>(
$"/accounts/{AccountId}/secondary_dns/peers/{PeerId}",
It.IsAny<InternalUpdatePeerRequest>(),
TestContext.CancellationToken), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
[TestMethod]
[DataRow(null)]
[DataRow("")]
[DataRow(" ")]
public async Task ShouldThrowArgumentNullExceptionForName(string name)
{
_request.Name = name;
var client = GetClient();
await Assert.ThrowsExactlyAsync<ArgumentNullException>(async () =>
{
await client.UpdatePeer(_request, TestContext.CancellationToken);
});
}
[TestMethod]
public async Task ShouldThrowArgumentOutOfRangeExceptionForPortLessThanZero()
{
_request.Port = -1;
var client = GetClient();
await Assert.ThrowsExactlyAsync<ArgumentOutOfRangeException>(async () =>
{
await client.UpdatePeer(_request, TestContext.CancellationToken);
});
}
[TestMethod]
public async Task ShouldThrowArgumentOutOfRangeExceptionForPortGreaterThan65535()
{
_request.Port = 70000;
var client = GetClient();
await Assert.ThrowsExactlyAsync<ArgumentOutOfRangeException>(async () =>
{
await client.UpdatePeer(_request, TestContext.CancellationToken);
});
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.PutAsync<Peer, InternalUpdatePeerRequest>(It.IsAny<string>(), It.IsAny<InternalUpdatePeerRequest>(), It.IsAny<CancellationToken>()))
.Callback<string, InternalUpdatePeerRequest, CancellationToken>((requestPath, request, _) => _callbacks.Add((requestPath, request)))
.ReturnsAsync(() => _response);
return _clientMock.Object;
}
}
}

View File

@@ -0,0 +1,143 @@
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.TSIGs
{
[TestClass]
public class CreateTSIGTest
{
public TestContext TestContext { get; set; }
private const string AccountId = "023e105f4ecef8ad9ca31a8372d0c353";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<TSIG> _response;
private List<(string RequestPath, InternalTSIGRequest Request)> _callbacks;
private CreateTSIGRequest _request;
[TestInitialize]
public void Initialize()
{
_callbacks = [];
_response = new CloudflareResponse<TSIG>
{
Success = true,
Messages = [
new ResponseInfo(1000, "Message 1")
],
Errors = [
new ResponseInfo(1000, "Error 1")
],
Result = new TSIG
{
Id = "tsig-1",
Name = "tsig-key-a",
Secret = "very-secret",
Algorithm = TSigAlgorithm.HMAC_SHA256
}
};
_request = new CreateTSIGRequest(
accountId: AccountId,
name: "tsig-key-a",
secret: "very-secret"
)
{
Algorithm = TSigAlgorithm.HMAC_SHA512
};
}
[TestMethod]
public async Task ShouldCreateTsig()
{
// Arrange
var client = GetClient();
// Act
var response = await client.CreateTSIG(_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($"/accounts/{AccountId}/secondary_dns/tsigs", requestPath);
Assert.IsNotNull(request);
Assert.AreEqual(_request.Name, request.Name);
Assert.AreEqual(_request.Secret, request.Secret);
Assert.AreEqual(_request.Algorithm, request.Algorithm);
_clientMock.Verify(m => m.PostAsync<TSIG, InternalTSIGRequest>($"/accounts/{AccountId}/secondary_dns/tsigs", It.IsAny<InternalTSIGRequest>(), 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.CreateTSIG(_request, TestContext.CancellationToken);
});
}
[TestMethod]
public async Task ShouldThrowArgumentOutOfRangeExceptionForAlgorithm()
{
// Arrange
_request.Algorithm = 0;
var client = GetClient();
// Act & Assert
await Assert.ThrowsExactlyAsync<ArgumentOutOfRangeException>(async () =>
{
await client.CreateTSIG(_request, TestContext.CancellationToken);
});
}
[TestMethod]
[DataRow(null)]
[DataRow("")]
[DataRow(" ")]
public async Task ShouldThrowArgumentNullExceptionForSecret(string secret)
{
// Arrange
_request.Secret = secret;
var client = GetClient();
// Act & Assert
await Assert.ThrowsExactlyAsync<ArgumentNullException>(async () =>
{
await client.CreateTSIG(_request, TestContext.CancellationToken);
});
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.PostAsync<TSIG, InternalTSIGRequest>(It.IsAny<string>(), It.IsAny<InternalTSIGRequest>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
.Callback<string, InternalTSIGRequest, IQueryParameterFilter, CancellationToken>((requestPath, request, _, __) => _callbacks.Add((requestPath, request)))
.ReturnsAsync(() => _response);
return _clientMock.Object;
}
}
}

View File

@@ -0,0 +1,81 @@
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.TSIGs
{
[TestClass]
public class DeleteTSIGTest
{
public TestContext TestContext { get; set; }
private const string AccountId = "023e105f4ecef8ad9ca31a8372d0c353";
private const string TsigId = "023e105f4ecef8ad9ca31a8372d0c351";
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 ResponseInfo(1000, "Message 1")
],
Errors =
[
new ResponseInfo(1000, "Error 1")
],
Result = new Identifier
{
Id = TsigId
}
};
}
[TestMethod]
public async Task ShouldDeleteTSIG()
{
// Arrange
var client = GetClient();
// Act
var response = await client.DeleteTSIG(AccountId, TsigId, 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, queryFilter) = _callbacks.First();
Assert.AreEqual($"/accounts/{AccountId}/secondary_dns/tsigs/{TsigId}", requestPath);
Assert.IsNull(queryFilter);
_clientMock.Verify(m => m.DeleteAsync<Identifier>($"/accounts/{AccountId}/secondary_dns/tsigs/{TsigId}", 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,95 @@
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.TSIGs
{
[TestClass]
public class ListTSIGsTest
{
public TestContext TestContext { get; set; }
private const string AccountId = "023e105f4ecef8ad9ca31a8372d0c353";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<IReadOnlyCollection<TSIG>> _response;
private List<(string RequestPath, IQueryParameterFilter QueryFilter)> _callbacks;
[TestInitialize]
public void Initialize()
{
_callbacks = [];
_response = new CloudflareResponse<IReadOnlyCollection<TSIG>>
{
Success = true,
Messages = [new ResponseInfo(1000, "Message 1")],
Errors = [],
Result =
[
new TSIG
{
Id = "tsig-1",
Name = "tsig-key-a",
Secret = "secretA",
Algorithm = TSigAlgorithm.HMAC_SHA256
},
new TSIG
{
Id = "tsig-2",
Name = "tsig-key-b",
Secret = "secretB",
Algorithm = TSigAlgorithm.HMAC_SHA1
}
]
};
}
[TestMethod]
public async Task ShouldListTSIGs()
{
// Arrange
var client = GetClient();
// Act
var response = await client.ListTSIGs(AccountId, TestContext.CancellationToken);
// Assert
Assert.IsNotNull(response);
Assert.IsTrue(response.Success);
Assert.IsNotNull(response.Result);
Assert.HasCount(2, response.Result);
var tsigs = response.Result.ToList();
Assert.AreEqual("tsig-key-a", tsigs[0].Name);
Assert.AreEqual("secretA", tsigs[0].Secret);
Assert.AreEqual(TSigAlgorithm.HMAC_SHA256, tsigs[0].Algorithm);
Assert.AreEqual("tsig-key-b", tsigs[1].Name);
Assert.AreEqual("secretB", tsigs[1].Secret);
Assert.AreEqual(TSigAlgorithm.HMAC_SHA1, tsigs[1].Algorithm);
Assert.HasCount(1, _callbacks);
var (requestPath, queryFilter) = _callbacks.First();
Assert.AreEqual($"/accounts/{AccountId}/secondary_dns/tsigs", requestPath);
Assert.IsNull(queryFilter);
_clientMock.Verify(m => m.GetAsync<IReadOnlyCollection<TSIG>>($"/accounts/{AccountId}/secondary_dns/tsigs", null, TestContext.CancellationToken), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.GetAsync<IReadOnlyCollection<TSIG>>(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.TSIGs
{
[TestClass]
public class TSIGDetailsTest
{
public TestContext TestContext { get; set; }
private const string AccountId = "023e105f4ecef8ad9ca31a8372d0c353";
private const string TsigId = "023e105f4ecef8ad9ca31a8372d0c351";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<TSIG> _response;
private List<(string RequestPath, IQueryParameterFilter QueryFilter)> _callbacks;
[TestInitialize]
public void Initialize()
{
_callbacks = new List<(string, IQueryParameterFilter)>();
_response = new CloudflareResponse<TSIG>
{
Success = true,
Messages = [new ResponseInfo(1000, "Message 1")],
Errors = [new ResponseInfo(1000, "Error 1")],
Result = new TSIG
{
Id = TsigId,
Name = "tsig-key-a",
Secret = "very-secret",
Algorithm = TSigAlgorithm.HMAC_SHA256
}
};
}
[TestMethod]
public async Task ShouldGetTSIGDetails()
{
// Arrange
var client = GetClient();
// Act
var response = await client.TSIGDetails(AccountId, TsigId, 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, queryFilter) = _callbacks.First();
Assert.AreEqual($"/accounts/{AccountId}/secondary_dns/tsigs/{TsigId}", requestPath);
Assert.IsNull(queryFilter);
_clientMock.Verify(m => m.GetAsync<TSIG>($"/accounts/{AccountId}/secondary_dns/tsigs/{TsigId}", null, TestContext.CancellationToken), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.GetAsync<TSIG>(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,147 @@
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.TSIGs
{
[TestClass]
public class UpdateTSIGTest
{
public TestContext TestContext { get; set; }
private const string AccountId = "023e105f4ecef8ad9ca31a8372d0c353";
private const string TsigId = "023e105f4ecef8ad9ca31a8372d0c351";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<TSIG> _response;
private List<(string RequestPath, InternalTSIGRequest Request)> _callbacks;
private UpdateTSIGRequest _request;
[TestInitialize]
public void Initialize()
{
_callbacks = new List<(string, InternalTSIGRequest)>();
_response = new CloudflareResponse<TSIG>
{
Success = true,
Messages = new[]
{
new ResponseInfo(1000, "Message 1")
},
Errors = new[]
{
new ResponseInfo(1000, "Error 1")
},
Result = new TSIG
{
Id = TsigId,
Name = "tsig-key-a",
Secret = "very-secret",
Algorithm = TSigAlgorithm.HMAC_SHA256
}
};
_request = new UpdateTSIGRequest(
accountId: AccountId,
tsigId: TsigId,
name: "tsig-key-a",
secret: "very-secret"
)
{
Algorithm = TSigAlgorithm.HMAC_SHA512
};
}
[TestMethod]
public async Task ShouldUpdateTsig()
{
// Arrange
var client = GetClient();
// Act
var response = await client.UpdateTSIG(_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($"/accounts/{AccountId}/secondary_dns/tsigs/{TsigId}", requestPath);
Assert.IsNotNull(request);
Assert.AreEqual(_request.Name, request.Name);
Assert.AreEqual(_request.Secret, request.Secret);
Assert.AreEqual(_request.Algorithm, request.Algorithm);
_clientMock.Verify(m => m.PutAsync<TSIG, InternalTSIGRequest>($"/accounts/{AccountId}/secondary_dns/tsigs/{TsigId}", It.IsAny<InternalTSIGRequest>(), 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.UpdateTSIG(_request, TestContext.CancellationToken);
});
}
[TestMethod]
public async Task ShouldThrowArgumentOutOfRangeExceptionForAlgorithm()
{
// Arrange
_request.Algorithm = 0;
var client = GetClient();
// Act & Assert
await Assert.ThrowsExactlyAsync<ArgumentOutOfRangeException>(async () =>
{
await client.UpdateTSIG(_request, TestContext.CancellationToken);
});
}
[TestMethod]
[DataRow(null)]
[DataRow("")]
[DataRow(" ")]
public async Task ShouldThrowArgumentNullExceptionForSecret(string secret)
{
// Arrange
_request.Secret = secret;
var client = GetClient();
// Act & Assert
await Assert.ThrowsExactlyAsync<ArgumentNullException>(async () =>
{
await client.UpdateTSIG(_request, TestContext.CancellationToken);
});
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.PutAsync<TSIG, InternalTSIGRequest>(It.IsAny<string>(), It.IsAny<InternalTSIGRequest>(), It.IsAny<CancellationToken>()))
.Callback<string, InternalTSIGRequest, CancellationToken>((requestPath, request, _) => _callbacks.Add((requestPath, request)))
.ReturnsAsync(() => _response);
return _clientMock.Object;
}
}
}