Add 'DNS Zone Settings' implementation

This commit is contained in:
2025-07-29 11:06:07 +02:00
parent 50b8efbb46
commit e49e975388
11 changed files with 882 additions and 0 deletions

View File

@@ -0,0 +1,86 @@
using System.Threading;
using System.Threading.Tasks;
using AMWD.Net.Api.Cloudflare.Dns.Internals;
namespace AMWD.Net.Api.Cloudflare.Dns
{
/// <summary>
/// Extensions for <see href="https://developers.cloudflare.com/api/resources/dns/subresources/settings/subresources/zone/">DNS Zone Settings</see>.
/// </summary>
public static class DnsZoneSettingsExtensions
{
/// <summary>
/// Update DNS settings for a zone.
/// </summary>
/// <param name="client">The <see cref="ICloudflareClient"/> instance.</param>
/// <param name="request">The request.</param>
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
public static Task<CloudflareResponse<DnsZoneSettings>> UpdateDnsZoneSettings(this ICloudflareClient client, UpdateDnsZoneSettingsRequest request, CancellationToken cancellationToken = default)
{
request.ZoneId.ValidateCloudflareId();
if (request.ZoneMode.HasValue && !Enum.IsDefined(typeof(DnsZoneMode), request.ZoneMode))
throw new ArgumentOutOfRangeException(nameof(request.ZoneMode), request.ZoneMode, "Value must be one of the ZoneMode enum values.");
if (request.Nameservers != null && !Enum.IsDefined(typeof(DnsZoneNameserversType), request.Nameservers.Type))
throw new ArgumentOutOfRangeException($"{nameof(request.Nameservers)}.{nameof(request.Nameservers.Type)}", request.Nameservers.Type, "Value must be one of the NameserverType enum values.");
if (request.NameserverTtl.HasValue && (request.NameserverTtl < 30 || 86400 < request.NameserverTtl))
throw new ArgumentOutOfRangeException(nameof(request.NameserverTtl), request.NameserverTtl, "Value must be between 30 and 86400.");
if (request.SOA != null)
{
string paramNameBase = $"{nameof(request.SOA)}";
if (request.SOA.Expire < 86400 || 2419200 < request.SOA.Expire)
throw new ArgumentOutOfRangeException($"{paramNameBase}.{nameof(request.SOA.Expire)}", request.SOA.Expire, "Value must be between 86400 and 2419200.");
if (request.SOA.MinimumTtl < 60 || 86400 < request.SOA.MinimumTtl)
throw new ArgumentOutOfRangeException($"{paramNameBase}.{nameof(request.SOA.MinimumTtl)}", request.SOA.MinimumTtl, "Value must be between 60 and 86400.");
if (string.IsNullOrWhiteSpace(request.SOA.PrimaryNameserver))
throw new ArgumentNullException($"{paramNameBase}.{nameof(request.SOA.PrimaryNameserver)}");
if (request.SOA.Refresh < 600 || 86400 < request.SOA.Refresh)
throw new ArgumentOutOfRangeException($"{paramNameBase}.{nameof(request.SOA.Refresh)}", request.SOA.Refresh, "Value must be between 600 and 86400.");
if (request.SOA.Retry < 600 || 86400 < request.SOA.Retry)
throw new ArgumentOutOfRangeException($"{paramNameBase}.{nameof(request.SOA.Retry)}", request.SOA.Retry, "Value must be between 600 and 86400.");
if (request.SOA.TimeToLive < 300 || 86400 < request.SOA.TimeToLive)
throw new ArgumentOutOfRangeException($"{paramNameBase}.{nameof(request.SOA.TimeToLive)}", request.SOA.TimeToLive, "Value must be between 300 and 86400.");
if (string.IsNullOrWhiteSpace(request.SOA.ZoneAdministrator))
throw new ArgumentNullException($"{paramNameBase}.{nameof(request.SOA.ZoneAdministrator)}");
}
var req = new InternalUpdateDnsZoneSettingsRequest
{
FlattenAllCnames = request.FlattenAllCnames,
FoundationDns = request.FoundationDns,
InternalDns = request.InternalDns,
MultiProvider = request.MultiProvider,
Nameservers = request.Nameservers,
NameserverTtl = request.NameserverTtl,
SecondaryOverrides = request.SecondaryOverrides,
SOA = request.SOA,
ZoneMode = request.ZoneMode
};
return client.PatchAsync<DnsZoneSettings, InternalUpdateDnsZoneSettingsRequest>($"/zones/{request.ZoneId}/dns_settings", req, cancellationToken);
}
/// <summary>
/// Show DNS settings for a zone.
/// </summary>
/// <param name="client">The <see cref="ICloudflareClient"/> instance.</param>
/// <param name="zoneId">The zone identifier.</param>
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
public static Task<CloudflareResponse<DnsZoneSettings>> ShowDnsZoneSettings(this ICloudflareClient client, string zoneId, CancellationToken cancellationToken = default)
{
zoneId.ValidateCloudflareId();
return client.GetAsync<DnsZoneSettings>($"/zones/{zoneId}/dns_settings", null, cancellationToken);
}
}
}

View File

@@ -0,0 +1,31 @@
using System.Runtime.Serialization;
using Newtonsoft.Json.Converters;
namespace AMWD.Net.Api.Cloudflare.Dns
{
/// <summary>
/// The mode of a DNS zone.
/// <see href="https://github.com/cloudflare/cloudflare-typescript/blob/v4.4.1/src/resources/dns/settings/zone.ts#L94">Source</see>
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
public enum DnsZoneMode
{
/// <summary>
/// The standard mode.
/// </summary>
[EnumMember(Value = "standard")]
Standard = 1,
/// <summary>
/// The CDN-only mode.
/// </summary>
[EnumMember(Value = "cdn_only")]
CdnOnly = 2,
/// <summary>
/// The DNS-only mode.
/// </summary>
[EnumMember(Value = "dns_only")]
DnsOnly = 3
}
}

View File

@@ -0,0 +1,32 @@
namespace AMWD.Net.Api.Cloudflare.Dns.Internals
{
internal class InternalUpdateDnsZoneSettingsRequest
{
[JsonProperty("flatten_all_cnames")]
public bool? FlattenAllCnames { get; set; }
[JsonProperty("foundation_dns")]
public bool? FoundationDns { get; set; }
[JsonProperty("internal_dns")]
public DnsZoneInternalDns? InternalDns { get; set; }
[JsonProperty("multi_provider")]
public bool? MultiProvider { get; set; }
[JsonProperty("nameservers")]
public DnsZoneNameservers? Nameservers { get; set; }
[JsonProperty("ns_ttl")]
public int? NameserverTtl { get; set; }
[JsonProperty("secondary_overrides")]
public bool? SecondaryOverrides { get; set; }
[JsonProperty("soa")]
public DnsZoneSoa? SOA { get; set; }
[JsonProperty("zone_mode")]
public DnsZoneMode? ZoneMode { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
namespace AMWD.Net.Api.Cloudflare.Dns
{
/// <summary>
/// Settings for this internal zone.
/// </summary>
public class DnsZoneInternalDns
{
/// <summary>
/// The ID of the zone to fallback to.
/// </summary>
[JsonProperty("reference_zone_id")]
public string? ReferenceZoneId { get; set; }
}
}

View File

@@ -0,0 +1,64 @@
using System.Runtime.Serialization;
using Newtonsoft.Json.Converters;
namespace AMWD.Net.Api.Cloudflare.Dns
{
/// <summary>
/// Settings determining the nameservers through which the zone should be available.
/// </summary>
public class DnsZoneNameservers
{
/// <summary>
/// Initializes a new instance of the <see cref="DnsZoneNameservers"/> class.
/// </summary>
/// <param name="type">Nameserver type.</param>
public DnsZoneNameservers(DnsZoneNameserversType type)
{
Type = type;
}
/// <summary>
/// Nameserver type.
/// </summary>
[JsonProperty("type")]
public DnsZoneNameserversType Type { get; set; }
/// <summary>
/// Configured nameserver set to be used for this zone.
/// </summary>
[JsonProperty("ns_set")]
public int? NameserverSet { get; set; }
}
/// <summary>
/// The type of a DNS zone nameserver.
/// <see href="https://github.com/cloudflare/cloudflare-typescript/blob/v4.4.1/src/resources/dns/settings/zone.ts#L115">Source</see>
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
public enum DnsZoneNameserversType
{
/// <summary>
/// The Cloudflare standard nameservers.
/// </summary>
[EnumMember(Value = "cloudflare.standard")]
Standard = 1,
/// <summary>
/// The account specific nameservers.
/// </summary>
[EnumMember(Value = "custom.account")]
Account = 2,
/// <summary>
/// The tenant specific nameservers.
/// </summary>
[EnumMember(Value = "custom.tenant")]
Tenant = 3,
/// <summary>
/// The zone specific nameservers.
/// </summary>
[EnumMember(Value = "custom.zone")]
Zone = 4
}
}

View File

@@ -0,0 +1,66 @@
namespace AMWD.Net.Api.Cloudflare.Dns
{
/// <summary>
/// The response for a DNS zone edit.
/// </summary>
public class DnsZoneSettings
{
/// <summary>
/// Whether to flatten all CNAME records in the zone. Note that, due to DNS
/// limitations, a CNAME record at the zone apex will always be flattened.
/// </summary>
[JsonProperty("flatten_all_cnames")]
public bool? FlattenAllCnames { get; set; }
/// <summary>
/// Whether to enable Foundation DNS Advanced Nameservers on the zone.
/// </summary>
[JsonProperty("foundation_dns")]
public bool? FoundationDns { get; set; }
/// <summary>
/// Settings for this internal zone.
/// </summary>
[JsonProperty("internal_dns")]
public DnsZoneInternalDns? InternalDns { get; set; }
/// <summary>
/// Whether to enable multi-provider DNS, which causes Cloudflare to activate the
/// zone even when non-Cloudflare NS records exist, and to respect NS records at the
/// zone apex during outbound zone transfers.
/// </summary>
[JsonProperty("multi_provider")]
public bool? MultiProvider { get; set; }
/// <summary>
/// Settings determining the nameservers through which the zone should be available.
/// </summary>
[JsonProperty("nameservers")]
public DnsZoneNameservers? Nameservers { get; set; }
/// <summary>
/// The time to live (TTL) of the zone's nameserver (NS) records.
/// </summary>
[JsonProperty("ns_ttl")]
public int? NameserverTtl { get; set; }
/// <summary>
/// Allows a Secondary DNS zone to use (proxied) override records and CNAME
/// flattening at the zone apex.
/// </summary>
[JsonProperty("secondary_overrides")]
public bool? SecondaryOverrides { get; set; }
/// <summary>
/// Components of the zone's SOA record.
/// </summary>
[JsonProperty("soa")]
public DnsZoneSoa? SOA { get; set; }
/// <summary>
/// Whether the zone mode is a regular or CDN/DNS only zone.
/// </summary>
[JsonProperty("zone_mode")]
public DnsZoneMode? ZoneMode { get; set; }
}
}

View File

@@ -0,0 +1,75 @@
namespace AMWD.Net.Api.Cloudflare.Dns
{
/// <summary>
/// Components of the zone's SOA record.
/// </summary>
public class DnsZoneSoa
{
/// <summary>
/// Initializes a new instance of the <see cref="DnsZoneSoa"/> class.
/// </summary>
/// <param name="expire">Time in seconds of being unable to query the primary server after which secondary servers should stop serving the zone.</param>
/// <param name="minttl">The time to live (TTL) for negative caching of records within the zone.</param>
/// <param name="mname">The primary nameserver for the zone.</param>
/// <param name="refresh">Time in seconds after which secondary servers should re-check the SOA record to see if the zone has been updated.</param>
/// <param name="retry">The time to live (TTL) for negative caching of records within the zone.</param>
/// <param name="rname">The email address of the zone administrator.</param>
/// <param name="ttl">The time to live (TTL) of the SOA record itself.</param>
public DnsZoneSoa(int expire, int minttl, string mname, int refresh, int retry, string rname, int ttl)
{
Expire = expire;
MinimumTtl = minttl;
PrimaryNameserver = mname;
Refresh = refresh;
Retry = retry;
ZoneAdministrator = rname;
TimeToLive = ttl;
}
/// <summary>
/// Time in seconds of being unable to query the primary server after which
/// secondary servers should stop serving the zone.
/// </summary>
[JsonProperty("expire")]
public int Expire { get; set; }
/// <summary>
/// The time to live (TTL) for negative caching of records within the zone.
/// </summary>
[JsonProperty("min_ttl")]
public int MinimumTtl { get; set; }
/// <summary>
/// The primary nameserver, which may be used for outbound zone transfers.
/// </summary>
[JsonProperty("mname")]
public string PrimaryNameserver { get; set; }
/// <summary>
/// Time in seconds after which secondary servers should re-check the SOA record to
/// see if the zone has been updated.
/// </summary>
[JsonProperty("refresh")]
public int Refresh { get; set; }
/// <summary>
/// Time in seconds after which secondary servers should retry queries after the
/// primary server was unresponsive.
/// </summary>
[JsonProperty("retry")]
public int Retry { get; set; }
/// <summary>
/// The email address of the zone administrator, with the first label representing
/// the local part of the email address.
/// </summary>
[JsonProperty("rname")]
public string ZoneAdministrator { get; set; }
/// <summary>
/// The time to live (TTL) of the SOA record itself.
/// </summary>
[JsonProperty("ttl")]
public int TimeToLive { get; set; }
}
}

View File

@@ -27,6 +27,14 @@ This package contains the feature set of the _DNS_ section of the Cloudflare API
- [Overwrite DNS Record](https://developers.cloudflare.com/api/resources/dns/subresources/records/methods/update/)
#### [Settings]
##### [Zone]
- [Update DNS Settings](https://developers.cloudflare.com/api/resources/dns/subresources/settings/subresources/zone/methods/edit/)
- [Show DNS Settings](https://developers.cloudflare.com/api/resources/dns/subresources/settings/subresources/zone/methods/get/)
---
Published under MIT License (see [choose a license])
@@ -39,3 +47,6 @@ Published under MIT License (see [choose a license])
[DNS]: https://developers.cloudflare.com/api/resources/dns/
[Records]: https://developers.cloudflare.com/api/resources/dns/subresources/records/
[Settings]: https://developers.cloudflare.com/api/resources/dns/subresources/settings/
[Zone]: https://developers.cloudflare.com/api/resources/dns/subresources/settings/subresources/zone/

View File

@@ -0,0 +1,71 @@
namespace AMWD.Net.Api.Cloudflare.Dns
{
/// <summary>
/// Represents a request to update DNS zone settings.
/// </summary>
public class UpdateDnsZoneSettingsRequest
{
/// <summary>
/// Initializes a new instance of the <see cref="UpdateDnsZoneSettingsRequest"/> class.
/// </summary>
/// <param name="zoneId">The zone identifier.</param>
public UpdateDnsZoneSettingsRequest(string zoneId)
{
ZoneId = zoneId;
}
/// <summary>
/// The zone identifier.
/// </summary>
public string ZoneId { get; set; }
/// <summary>
/// Whether to flatten all CNAME records in the zone. Note that, due to
/// DNS limitations, a CNAME record at the zone apex will always be flattened.
/// </summary>
public bool? FlattenAllCnames { get; set; }
/// <summary>
/// Whether to enable Foundation DNS Advanced Nameservers on the zone.
/// </summary>
public bool? FoundationDns { get; set; }
/// <summary>
/// Settings for this internal zone.
/// </summary>
public DnsZoneInternalDns? InternalDns { get; set; }
/// <summary>
/// Whether to enable multi-provider DNS, which causes Cloudflare to
/// activate the zone even when non-Cloudflare NS records exist, and to respect NS
/// records at the zone apex during outbound zone transfers.
/// </summary>
public bool? MultiProvider { get; set; }
/// <summary>
/// Settings determining the nameservers through which the zone should be available.
/// </summary>
public DnsZoneNameservers? Nameservers { get; set; }
/// <summary>
/// The time to live (TTL) of the zone's nameserver (NS) records.
/// </summary>
public int? NameserverTtl { get; set; }
/// <summary>
/// Allows a Secondary DNS zone to use (proxied) override records and CNAME
/// flattening at the zone apex.
/// </summary>
public bool? SecondaryOverrides { get; set; }
/// <summary>
/// Components of the zone's SOA record.
/// </summary>
public DnsZoneSoa? SOA { get; set; }
/// <summary>
/// Whether the zone mode is a regular or CDN/DNS only zone.
/// </summary>
public DnsZoneMode? ZoneMode { get; set; }
}
}

View File

@@ -0,0 +1,97 @@
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.DnsZoneSettingsExtensions
{
[TestClass]
public class ShowDnsZoneSettingsTest
{
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<DnsZoneSettings> _response;
private List<(string RequestPath, IQueryParameterFilter QueryFilter)> _callbacks;
[TestInitialize]
public void Initialize()
{
_callbacks = [];
_response = new CloudflareResponse<DnsZoneSettings>
{
Success = true,
Messages = [
new ResponseInfo(1000, "Message 1")
],
Errors = [
new ResponseInfo(1000, "Error 1")
],
Result = new DnsZoneSettings
{
FlattenAllCnames = true,
FoundationDns = false,
InternalDns = new DnsZoneInternalDns
{
ReferenceZoneId = ZoneId
},
MultiProvider = false,
Nameservers = new DnsZoneNameservers(
type: DnsZoneNameserversType.Zone
),
NameserverTtl = 86400,
SecondaryOverrides = false,
SOA = new DnsZoneSoa(
expire: 604800,
minttl: 1800,
mname: "bob.ns.example.com",
refresh: 10000,
retry: 2400,
rname: "admin.example.com",
ttl: 3600
),
ZoneMode = DnsZoneMode.DnsOnly
}
};
}
[TestMethod]
public async Task ShouldGetDnsSettings()
{
// Arrange
var client = GetClient();
// Act
var response = await client.ShowDnsZoneSettings(ZoneId);
// Assert
Assert.IsNotNull(response);
Assert.IsTrue(response.Success);
Assert.AreEqual(_response.Result, response.Result);
Assert.AreEqual(1, _callbacks.Count);
var callback = _callbacks.First();
Assert.AreEqual($"/zones/{ZoneId}/dns_settings", callback.RequestPath);
Assert.IsNull(callback.QueryFilter);
_clientMock?.Verify(m => m.GetAsync<DnsZoneSettings>($"/zones/{ZoneId}/dns_settings", null, It.IsAny<CancellationToken>()), Times.Once);
_clientMock?.VerifyNoOtherCalls();
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.GetAsync<DnsZoneSettings>(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,335 @@
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.DnsZoneSettingsExtensions
{
[TestClass]
public class UpdateDnsZoneSettingsTest
{
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<DnsZoneSettings> _response;
private List<(string RequestPath, InternalUpdateDnsZoneSettingsRequest Request)> _callbacks;
private UpdateDnsZoneSettingsRequest _request;
[TestInitialize]
public void Initialize()
{
_callbacks = [];
_response = new CloudflareResponse<DnsZoneSettings>
{
Success = true,
Messages = [
new ResponseInfo(1000, "Message 1")
],
Errors = [
new ResponseInfo(1000, "Error 1")
],
Result = new DnsZoneSettings
{
FlattenAllCnames = true,
FoundationDns = false,
InternalDns = new DnsZoneInternalDns
{
ReferenceZoneId = ZoneId
},
MultiProvider = false,
Nameservers = new DnsZoneNameservers(
type: DnsZoneNameserversType.Zone
),
NameserverTtl = 86400,
SecondaryOverrides = false,
SOA = new DnsZoneSoa(
expire: 604800,
minttl: 1800,
mname: "bob.ns.example.com",
refresh: 10000,
retry: 2400,
rname: "admin.example.com",
ttl: 3600
),
ZoneMode = DnsZoneMode.DnsOnly
}
};
_request = new UpdateDnsZoneSettingsRequest(ZoneId)
{
FlattenAllCnames = true,
FoundationDns = false,
InternalDns = new DnsZoneInternalDns
{
ReferenceZoneId = ZoneId
},
MultiProvider = false,
Nameservers = new DnsZoneNameservers(
type: DnsZoneNameserversType.Standard
),
NameserverTtl = 86400,
SecondaryOverrides = false,
SOA = new DnsZoneSoa(
expire: 604800,
minttl: 1800,
mname: "ns1.example.org",
refresh: 28800,
retry: 3600,
rname: "admin.example.org",
ttl: 43200
),
ZoneMode = DnsZoneMode.Standard
};
}
[TestMethod]
public async Task ShouldUpdateDnsSettingsFull()
{
// Arrange
var client = GetClient();
// Act
var response = await client.UpdateDnsZoneSettings(_request);
// Assert
Assert.IsNotNull(response);
Assert.IsTrue(response.Success);
Assert.AreEqual(_response.Result, response.Result);
Assert.AreEqual(1, _callbacks.Count);
var callback = _callbacks.First();
Assert.AreEqual($"/zones/{ZoneId}/dns_settings", callback.RequestPath);
Assert.IsNotNull(callback.Request);
Assert.IsTrue(callback.Request.FlattenAllCnames);
Assert.IsFalse(callback.Request.FoundationDns);
Assert.IsNotNull(callback.Request.InternalDns);
Assert.AreEqual(ZoneId, callback.Request.InternalDns.ReferenceZoneId);
Assert.IsFalse(callback.Request.MultiProvider);
Assert.IsNotNull(callback.Request.Nameservers);
Assert.AreEqual(DnsZoneNameserversType.Standard, callback.Request.Nameservers.Type);
Assert.AreEqual(86400, callback.Request.NameserverTtl);
Assert.IsFalse(callback.Request.SecondaryOverrides);
Assert.IsNotNull(callback.Request.SOA);
Assert.AreEqual(604800, callback.Request.SOA.Expire);
Assert.AreEqual(1800, callback.Request.SOA.MinimumTtl);
Assert.AreEqual("ns1.example.org", callback.Request.SOA.PrimaryNameserver);
Assert.AreEqual(28800, callback.Request.SOA.Refresh);
Assert.AreEqual(3600, callback.Request.SOA.Retry);
Assert.AreEqual(43200, callback.Request.SOA.TimeToLive);
Assert.AreEqual("admin.example.org", callback.Request.SOA.ZoneAdministrator);
Assert.AreEqual(DnsZoneMode.Standard, callback.Request.ZoneMode);
_clientMock.Verify(m => m.PatchAsync<DnsZoneSettings, InternalUpdateDnsZoneSettingsRequest>($"/zones/{ZoneId}/dns_settings", It.IsAny<InternalUpdateDnsZoneSettingsRequest>(), It.IsAny<CancellationToken>()), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
[TestMethod]
public async Task ShouldUpdateDnsSettingsNone()
{
// Arrange
var request = new UpdateDnsZoneSettingsRequest(ZoneId);
var client = GetClient();
// Act
var response = await client.UpdateDnsZoneSettings(request);
// Assert
Assert.IsNotNull(response);
Assert.IsTrue(response.Success);
Assert.AreEqual(_response.Result, response.Result);
Assert.AreEqual(1, _callbacks.Count);
var callback = _callbacks.First();
Assert.AreEqual($"/zones/{ZoneId}/dns_settings", callback.RequestPath);
Assert.IsNotNull(callback.Request);
Assert.IsNull(callback.Request.FlattenAllCnames);
Assert.IsNull(callback.Request.FoundationDns);
Assert.IsNull(callback.Request.MultiProvider);
Assert.IsNull(callback.Request.Nameservers);
Assert.IsNull(callback.Request.NameserverTtl);
Assert.IsNull(callback.Request.SecondaryOverrides);
Assert.IsNull(callback.Request.SOA);
Assert.IsNull(callback.Request.ZoneMode);
_clientMock.Verify(m => m.PatchAsync<DnsZoneSettings, InternalUpdateDnsZoneSettingsRequest>($"/zones/{ZoneId}/dns_settings", It.IsAny<InternalUpdateDnsZoneSettingsRequest>(), It.IsAny<CancellationToken>()), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public async Task ShouldThrowArgumentOutOfRangeExceptionForInvalidMode()
{
// Arrange
_request.ZoneMode = 0;
var client = GetClient();
// Act
await client.UpdateDnsZoneSettings(_request);
// Assert - ArgumentOutOfRangeException
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public async Task ShouldThrowArgumentOutOfRangeExceptionForInvalidNameserverType()
{
// Arrange
_request.Nameservers.Type = 0;
var client = GetClient();
// Act
await client.UpdateDnsZoneSettings(_request);
// Assert - ArgumentOutOfRangeException
}
[DataTestMethod]
[DataRow(29)]
[DataRow(86401)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public async Task ShouldThrowArgumentOutOfRangeExceptionForInvalidNameserverTtl(int ttl)
{
// Arrange
_request.NameserverTtl = ttl;
var client = GetClient();
// Act
await client.UpdateDnsZoneSettings(_request);
// Assert - ArgumentOutOfRangeException
}
[DataTestMethod]
[DataRow(86399)]
[DataRow(2419201)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public async Task ShouldThrowArgumentOutOfRangeExceptionForInvalidSoaExpire(int ttl)
{
// Arrange
_request.SOA.Expire = ttl;
var client = GetClient();
// Act
await client.UpdateDnsZoneSettings(_request);
// Assert - ArgumentOutOfRangeException
}
[DataTestMethod]
[DataRow(59)]
[DataRow(86401)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public async Task ShouldThrowArgumentOutOfRangeExceptionForInvalidSoaMinimumTtl(int ttl)
{
// Arrange
_request.SOA.MinimumTtl = ttl;
var client = GetClient();
// Act
await client.UpdateDnsZoneSettings(_request);
// Assert - ArgumentOutOfRangeException
}
[DataTestMethod]
[DataRow(null)]
[DataRow("")]
[DataRow(" ")]
[ExpectedException(typeof(ArgumentNullException))]
public async Task ShouldThrowArgumentNullExceptionForMissingSoaNameserver(string nameserver)
{
// Arrange
_request.SOA.PrimaryNameserver = nameserver;
var client = GetClient();
// Act
await client.UpdateDnsZoneSettings(_request);
// Assert - ArgumentNullException
}
[DataTestMethod]
[DataRow(599)]
[DataRow(86401)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public async Task ShouldThrowArgumentOutOfRangeExceptionForInvalidSoaRefresh(int ttl)
{
// Arrange
_request.SOA.Refresh = ttl;
var client = GetClient();
// Act
await client.UpdateDnsZoneSettings(_request);
// Assert - ArgumentOutOfRangeException
}
[DataTestMethod]
[DataRow(599)]
[DataRow(86401)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public async Task ShouldThrowArgumentOutOfRangeExceptionForInvalidSoaRetry(int ttl)
{
// Arrange
_request.SOA.Retry = ttl;
var client = GetClient();
// Act
await client.UpdateDnsZoneSettings(_request);
// Assert - ArgumentOutOfRangeException
}
[DataTestMethod]
[DataRow(299)]
[DataRow(86401)]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public async Task ShouldThrowArgumentOutOfRangeExceptionForInvalidSoaTtl(int ttl)
{
// Arrange
_request.SOA.TimeToLive = ttl;
var client = GetClient();
// Act
await client.UpdateDnsZoneSettings(_request);
// Assert - ArgumentOutOfRangeException
}
[DataTestMethod]
[DataRow(null)]
[DataRow("")]
[DataRow(" ")]
[ExpectedException(typeof(ArgumentNullException))]
public async Task ShouldThrowArgumentNullExceptionForMissingSoaAdministrator(string admin)
{
// Arrange
_request.SOA.ZoneAdministrator = admin;
var client = GetClient();
// Act
await client.UpdateDnsZoneSettings(_request);
// Assert - ArgumentNullException
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.PatchAsync<DnsZoneSettings, InternalUpdateDnsZoneSettingsRequest>(It.IsAny<string>(), It.IsAny<InternalUpdateDnsZoneSettingsRequest>(), It.IsAny<CancellationToken>()))
.Callback<string, InternalUpdateDnsZoneSettingsRequest, CancellationToken>((requestPath, request, _) => _callbacks.Add((requestPath, request)))
.ReturnsAsync(() => _response);
return _clientMock.Object;
}
}
}