Add "Zone" extensions

This commit is contained in:
2025-06-24 15:14:54 +02:00
parent 2fe9ac0657
commit 9aa1dbb02c
18 changed files with 1856 additions and 6 deletions

View File

@@ -1,6 +1,4 @@
using System; using System.Net;
using System.Collections.Generic;
using System.Net;
namespace AMWD.Net.Api.Cloudflare namespace AMWD.Net.Api.Cloudflare
{ {

View File

@@ -0,0 +1,48 @@
using System.Runtime.Serialization;
using Newtonsoft.Json.Converters;
namespace AMWD.Net.Api.Cloudflare.Zones
{
/// <summary>
/// Zone types.
/// <see href="https://github.com/cloudflare/cloudflare-typescript/blob/v4.4.1/src/resources/zones/zones.ts#L223">Source</see>
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
public enum ZoneType
{
/// <summary>
/// Full Setup (most common).
/// </summary>
/// <remarks>
/// Use Cloudflare as your primary DNS provider and manage your DNS records on Cloudflare.
/// </remarks>
[EnumMember(Value = "full")]
Full = 1,
/// <summary>
/// Partial (CNAME) setup.
/// </summary>
/// <remarks>
/// Keep your primary DNS provider and only use Cloudflare's reverse proxy for individual subdomains.
/// </remarks>
[EnumMember(Value = "partial")]
Partial = 2,
/// <summary>
/// Zone transfers.
/// </summary>
/// <remarks>
/// Use Cloudflare and another DNS provider together across your entire zone to increase availability and fault tolerance.
/// <br/>
/// DNS records will be transferred between providers using <see href="https://datatracker.ietf.org/doc/html/rfc5936">AXFR</see> or <see href="https://datatracker.ietf.org/doc/html/rfc1995">IXFR</see>.
/// </remarks>
[EnumMember(Value = "secondary")]
Secondary = 3,
/// <summary>
/// An internal zone.
/// </summary>
[EnumMember(Value = "internal")]
Internal = 4
}
}

View File

@@ -0,0 +1,180 @@
using System.Runtime.Serialization;
using Newtonsoft.Json.Converters;
namespace AMWD.Net.Api.Cloudflare.Zones
{
/// <summary>
/// Filter for listing zones.
/// </summary>
public class ListZonesFilter : IQueryParameterFilter
{
/// <summary>
/// An account ID.
/// </summary>
public string? AccountId { get; set; }
/// <summary>
/// An account Name.
/// </summary>
/// <remarks>
/// Optional filter operators can be provided to extend refine the search:
/// <list type="bullet">
/// <item><description>equal</description> (default)</item>
/// <item><description>not_equal</description></item>
/// <item><description>starts_with</description></item>
/// <item><description>ends_with</description></item>
/// <item><description>contains</description></item>
/// <item><description>starts_with_case_sensitive</description></item>
/// <item><description>ends_with_case_sensitive</description></item>
/// <item><description>contains_case_sensitive</description></item>
/// </list>
/// </remarks>
/// <example>Dev Account</example>
/// <example>contains:Test</example>
public string? AccountName { get; set; }
/// <summary>
/// Direction to order zones.
/// </summary>
public SortDirection? Direction { get; set; }
/// <summary>
/// Whether to match all search requirements or at least one (any).
/// </summary>
public ListZonesMatch? Match { get; set; }
/// <summary>
/// A domain name.
/// </summary>
/// <remarks>
/// Optional filter operators can be provided to extend refine the search:
/// <list type="bullet">
/// <item><description>equal</description> (default)</item>
/// <item><description>not_equal</description></item>
/// <item><description>starts_with</description></item>
/// <item><description>ends_with</description></item>
/// <item><description>contains</description></item>
/// <item><description>starts_with_case_sensitive</description></item>
/// <item><description>ends_with_case_sensitive</description></item>
/// <item><description>contains_case_sensitive</description></item>
/// </list>
/// </remarks>
/// <example>example.com</example>
/// <example>contains:.org</example>
/// <example>ends_with:arpa</example>
/// <example>starts_with:dev</example>
public string? Name { get; set; }
/// <summary>
/// Field to order zones by.
/// </summary>
public ListZonesOrderBy? OrderBy { get; set; }
/// <summary>
/// Page number of paginated results.
/// </summary>
/// <value>1 &lt;= X</value>
public int? Page { get; set; }
/// <summary>
/// Number of zones per page.
/// </summary>
/// <value>5 &lt;= X &lt;= 50</value>
public int? PerPage { get; set; }
/// <summary>
/// A zone status.
/// </summary>
public ZoneStatus? Status { get; set; }
/// <inheritdoc />
public IDictionary<string, string> GetQueryParameters()
{
var dict = new Dictionary<string, string>();
#pragma warning disable CS8602, CS8604 // There will be no null value below.
if (!string.IsNullOrWhiteSpace(AccountId))
dict.Add("account.id", AccountId.Trim());
if (!string.IsNullOrWhiteSpace(AccountName))
dict.Add("account.name", AccountName.Trim());
if (Direction.HasValue && Enum.IsDefined(typeof(SortDirection), Direction.Value))
dict.Add("direction", Direction.Value.GetEnumMemberValue());
if (Match.HasValue && Enum.IsDefined(typeof(ListZonesMatch), Match.Value))
dict.Add("match", Match.Value.GetEnumMemberValue());
if (!string.IsNullOrWhiteSpace(Name))
dict.Add("name", Name);
if (OrderBy.HasValue && Enum.IsDefined(typeof(ListZonesOrderBy), OrderBy.Value))
dict.Add("order", OrderBy.Value.GetEnumMemberValue());
if (Page.HasValue && Page.Value >= 1)
dict.Add("page", Page.Value.ToString());
if (PerPage.HasValue && PerPage.Value >= 5 && PerPage.Value <= 50)
dict.Add("per_page", PerPage.Value.ToString());
if (Status.HasValue && Enum.IsDefined(typeof(ZoneStatus), Status.Value))
dict.Add("status", Status.Value.GetEnumMemberValue());
#pragma warning restore CS8602, CS8604
return dict;
}
}
/// <summary>
/// Match type for listing zones.
/// <see href="https://github.com/cloudflare/cloudflare-typescript/blob/v4.4.1/src/resources/zones/zones.ts#L553">Source</see>
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
public enum ListZonesMatch
{
/// <summary>
/// Match all search requirements.
/// </summary>
[EnumMember(Value = "all")]
All = 1,
/// <summary>
/// Match at least one search requirement.
/// </summary>
[EnumMember(Value = "any")]
Any = 2
}
/// <summary>
/// Field to order zones by.
/// <see href="https://github.com/cloudflare/cloudflare-typescript/blob/v4.4.1/src/resources/zones/zones.ts#L573">Source</see>
/// </summary>
public enum ListZonesOrderBy
{
/// <summary>
/// Order by zone name.
/// </summary>
[EnumMember(Value = "name")]
Name = 1,
/// <summary>
/// Order by zone status.
/// </summary>
[EnumMember(Value = "status")]
Status = 2,
/// <summary>
/// Order by account ID.
/// </summary>
[EnumMember(Value = "account.id")]
AccountId = 3,
/// <summary>
/// Order by account name.
/// </summary>
[EnumMember(Value = "account.name")]
AccountName = 4,
}
}

View File

@@ -0,0 +1,22 @@
namespace AMWD.Net.Api.Cloudflare.Zones.Internals
{
internal class InternalCreateZoneRequest
{
public InternalCreateZoneRequest(string? accountId, string name, ZoneType? type)
{
Account = new Identifier { Id = accountId };
Name = name;
Type = type;
}
[JsonProperty("account")]
public Identifier Account { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("type")]
public ZoneType? Type { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
namespace AMWD.Net.Api.Cloudflare.Zones.Internals
{
internal class InternalEditZoneRequest
{
[JsonProperty("paused")]
public bool? Paused { get; set; }
[JsonProperty("type")]
public ZoneType? Type { get; set; }
[JsonProperty("vanity_name_servers")]
public IReadOnlyCollection<string>? VanityNameServers { get; set; }
}
}

View File

@@ -0,0 +1,408 @@
using System.Runtime.Serialization;
using Newtonsoft.Json.Converters;
namespace AMWD.Net.Api.Cloudflare.Zones
{
/// <summary>
/// A Cloudflare zone.
/// <see href="https://github.com/cloudflare/cloudflare-typescript/blob/v4.4.1/src/resources/zones/zones.ts#L231">Source</see>
/// </summary>
public class Zone
{
/// <summary>
/// Initializes a new instance of the <see cref="Zone"/> class.
/// </summary>
/// <param name="id">Identifier.</param>
/// <param name="name">The domain name.</param>
/// <param name="nameServers">The name servers Cloudflare assigns to the zone.</param>
/// <param name="account">The account the zone belongs to.</param>
/// <param name="meta">Metadata about the zone.</param>
/// <param name="owner">The owner of the zone.</param>
public Zone(string id, string name, IReadOnlyCollection<string> nameServers, ZoneAccount account, ZoneMeta meta, ZoneOwner owner)
{
Id = id;
Account = account;
Meta = meta;
Name = name;
NameServers = nameServers;
Owner = owner;
#pragma warning disable CS0612
Plan = new();
#pragma warning restore CS0612
}
/// <summary>
/// Identifier.
/// </summary>
[JsonProperty("id")]
public string Id { get; set; }
/// <summary>
/// The account the zone belongs to.
/// </summary>
[JsonProperty("account")]
public ZoneAccount Account { get; set; }
/// <summary>
/// The last time proof of ownership was detected and the zone was made active.
/// </summary>
[JsonProperty("activated_on")]
public DateTime? ActivatedOn { get; set; }
/// <summary>
/// When the zone was created.
/// </summary>
[JsonProperty("created_on")]
public DateTime CreatedOn { get; set; }
/// <summary>
/// The interval (in seconds) from when development mode expires (positive integer)
/// or last expired (negative integer) for the domain. If development mode has never
/// been enabled, this value is 0.
/// </summary>
[JsonProperty("development_mode")]
public int DevelopmentMode { get; set; }
/// <summary>
/// Metadata about the zone.
/// </summary>
[JsonProperty("meta")]
public ZoneMeta Meta { get; set; }
/// <summary>
/// When the zone was last modified.
/// </summary>
[JsonProperty("modified_on")]
public DateTime ModifiedOn { get; set; }
/// <summary>
/// The domain name.
/// </summary>
[JsonProperty("name")]
public string Name { get; set; }
/// <summary>
/// The name servers Cloudflare assigns to a zone.
/// </summary>
[JsonProperty("name_servers")]
public IReadOnlyCollection<string> NameServers { get; set; }
/// <summary>
/// DNS host at the time of switching to Cloudflare.
/// </summary>
[JsonProperty("original_dnshost")]
public string? OriginalDnsHost { get; set; }
/// <summary>
/// Original name servers before moving to Cloudflare.
/// </summary>
[JsonProperty("original_name_servers")]
public IReadOnlyCollection<string>? OriginalNameServers { get; set; }
/// <summary>
/// Registrar for the domain at the time of switching to Cloudflare.
/// </summary>
[JsonProperty("original_registrar")]
public string? OriginalRegistrar { get; set; }
/// <summary>
/// The owner of the zone.
/// </summary>
[JsonProperty("owner")]
public ZoneOwner Owner { get; set; }
/// <summary>
/// A Zones subscription information.
/// </summary>
[Obsolete]
[JsonProperty("plan")]
public ZonePlan Plan { get; set; }
/// <summary>
/// Allows the customer to use a custom apex.
/// <em>Tenants Only Configuration</em>.
/// </summary>
[JsonProperty("cname_suffix")]
public string? CnameSuffix { get; set; }
/// <summary>
/// Indicates whether the zone is only using Cloudflare DNS services.
/// </summary>
/// <remarks>
/// A <see langword="true"/> value means the zone will not receive security or performance benefits.
/// </remarks>
[JsonProperty("paused")]
public bool? Paused { get; set; }
/// <summary>
/// Legacy permissions based on legacy user membership information.
/// </summary>
[Obsolete]
[JsonProperty("permissions")]
public IReadOnlyCollection<string>? Permissions { get; set; }
/// <summary>
/// The zone status on Cloudflare.
/// </summary>
[JsonProperty("status")]
public ZoneStatus? Status { get; set; }
/// <summary>
/// The root organizational unit that this zone belongs to (such as a tenant or organization).
/// </summary>
[JsonProperty("tenant")]
public ZoneTenant? Tenant { get; set; }
/// <summary>
/// The immediate parent organizational unit that this zone belongs to (such as under a tenant or sub-organization).
/// </summary>
[JsonProperty("tenant_unit")]
public ZoneTenantUnit? TenantUnit { get; set; }
/// <summary>
/// A full zone implies that DNS is hosted with Cloudflare.
/// A partial zone is typically a partner-hosted zone or a CNAME setup.
/// </summary>
[JsonProperty("type")]
public ZoneType? Type { get; set; }
/// <summary>
/// An array of domains used for custom name servers.
/// This is only available for Business and Enterprise plans.
/// </summary>
[JsonProperty("vanity_name_servers")]
public IReadOnlyCollection<string>? VanityNameServers { get; set; }
/// <summary>
/// Verification key for partial zone setup.
/// </summary>
[JsonProperty("verification_key")]
public string? VerificationKey { get; set; }
}
/// <summary>
/// The account the zone belongs to.
/// <see href="https://github.com/cloudflare/cloudflare-typescript/blob/v4.4.1/src/resources/zones/zones.ts#L359">Source</see>
/// </summary>
public class ZoneAccount
{
/// <summary>
/// Identifier.
/// </summary>
[JsonProperty("id")]
public string? Id { get; set; }
/// <summary>
/// The name of the account.
/// </summary>
[JsonProperty("name")]
public string? Name { get; set; }
}
/// <summary>
/// Metadata about the zone.
/// <see href="https://github.com/cloudflare/cloudflare-typescript/blob/v4.4.1/src/resources/zones/zones.ts#L374">Source</see>
/// </summary>
public class ZoneMeta
{
/// <summary>
/// The zone is only configured for CDN.
/// </summary>
[JsonProperty("cdn_only")]
public bool? CdnOnly { get; set; }
/// <summary>
/// Number of Custom Certificates the zone can have.
/// </summary>
[JsonProperty("custom_certificate_quota")]
public int? CustomCertificateQuota { get; set; }
/// <summary>
/// The zone is only configured for DNS.
/// </summary>
[JsonProperty("dns_only")]
public bool? DnsOnly { get; set; }
/// <summary>
/// The zone is setup with Foundation DNS.
/// </summary>
[JsonProperty("foundation_dns")]
public bool? FoundationDns { get; set; }
/// <summary>
/// Number of Page Rules a zone can have.
/// </summary>
[JsonProperty("page_rule_quota")]
public int? PageRuleQuota { get; set; }
/// <summary>
/// The zone has been flagged for phishing.
/// </summary>
[JsonProperty("phishing_detected")]
public bool? PhishingDetected { get; set; }
/// <summary>
/// Step.
/// </summary>
[JsonProperty("step")]
public int? Step { get; set; }
}
/// <summary>
/// The owner of the zone.
/// <see href="https://github.com/cloudflare/cloudflare-typescript/blob/v4.4.1/src/resources/zones/zones.ts#L411">Source</see>
/// </summary>
public class ZoneOwner
{
/// <summary>
/// Identifier.
/// </summary>
[JsonProperty("id")]
public string? Id { get; set; }
/// <summary>
/// Name of the owner.
/// </summary>
[JsonProperty("name")]
public string? Name { get; set; }
/// <summary>
/// The type of owner.
/// </summary>
[JsonProperty("type")]
public string? Type { get; set; }
}
/// <summary>
/// A Zones subscription information.
/// <see href="https://github.com/cloudflare/cloudflare-typescript/blob/v4.4.1/src/resources/zones/zones.ts#L431">Source</see>
/// </summary>
[Obsolete]
public class ZonePlan
{
/// <summary>
/// Identifier.
/// </summary>
[JsonProperty("id")]
public string? Id { get; set; }
/// <summary>
/// States if the subscription can be activated.
/// </summary>
[JsonProperty("can_subscribe")]
public bool? CanSubscribe { get; set; }
/// <summary>
/// The denomination of the customer.
/// </summary>
[JsonProperty("currency")]
public string? Currency { get; set; }
/// <summary>
/// If this Zone is managed by another company.
/// </summary>
[JsonProperty("externally_managed")]
public bool? ExternallyManaged { get; set; }
/// <summary>
/// How often the customer is billed.
/// </summary>
[JsonProperty("frequency")]
public string? Frequency { get; set; }
/// <summary>
/// States if the subscription active.
/// </summary>
[JsonProperty("is_subscribed")]
public bool? IsSubscribed { get; set; }
/// <summary>
/// If the legacy discount applies to this Zone.
/// </summary>
[JsonProperty("legacy_discount")]
public bool? LegacyDiscount { get; set; }
/// <summary>
/// The legacy name of the plan.
/// </summary>
[JsonProperty("legacy_id")]
public string? LegacyId { get; set; }
/// <summary>
/// Name of the owner.
/// </summary>
[JsonProperty("name")]
public string? Name { get; set; }
/// <summary>
/// How much the customer is paying.
/// </summary>
[JsonProperty("price")]
public decimal? Price { get; set; }
}
/// <summary>
/// Zone status.
/// <see href="https://github.com/cloudflare/cloudflare-typescript/blob/v4.4.1/src/resources/zones/zones.ts#L323">Source</see>
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
public enum ZoneStatus
{
/// <summary>
/// Initializing.
/// </summary>
[EnumMember(Value = "initializing")]
Initializing = 1,
/// <summary>
/// Pending.
/// </summary>
[EnumMember(Value = "pending")]
Pending = 2,
/// <summary>
/// Active.
/// </summary>
[EnumMember(Value = "active")]
Active = 3,
/// <summary>
/// Moved.
/// </summary>
[EnumMember(Value = "moved")]
Moved = 4
}
/// <summary>
/// The root organizational unit that this zone belongs to (such as a tenant or organization).
/// <see href="https://github.com/cloudflare/cloudflare-typescript/blob/v4.4.1/src/resources/zones/zones.ts#L487">Source</see>
/// </summary>
public class ZoneTenant
{
/// <summary>
/// Identifier.
/// </summary>
[JsonProperty("id")]
public string? Id { get; set; }
/// <summary>
/// The name of the Tenant account.
/// </summary>
[JsonProperty("name")]
public string? Name { get; set; }
}
/// <summary>
/// The immediate parent organizational unit that this zone belongs to (such as under a tenant or sub-organization).
/// <see href="https://github.com/cloudflare/cloudflare-typescript/blob/v4.4.1/src/resources/zones/zones.ts#L503">Source</see>
/// </summary>
public class ZoneTenantUnit
{
/// <summary>
/// Identifier.
/// </summary>
[JsonProperty("id")]
public string? Id { get; set; }
}
}

View File

@@ -11,7 +11,13 @@ This package contains the feature set of the _Domain/Zone Management_ section of
- [Update Domain](https://developers.cloudflare.com/api/resources/registrar/subresources/domains/methods/update/) - [Update Domain](https://developers.cloudflare.com/api/resources/registrar/subresources/domains/methods/update/)
### [Zones]
- [Create Zone](https://developers.cloudflare.com/api/resources/zones/methods/create/)
- [Delete Zone](https://developers.cloudflare.com/api/resources/zones/methods/delete/)
- [Edit Zone](https://developers.cloudflare.com/api/resources/zones/methods/edit/)
- [Zone Details](https://developers.cloudflare.com/api/resources/zones/methods/get/)
- [List Zones](https://developers.cloudflare.com/api/resources/zones/methods/list/)

View File

@@ -0,0 +1,33 @@
namespace AMWD.Net.Api.Cloudflare.Zones
{
/// <summary>
/// Represents a request to create a zone.
/// </summary>
public class CreateZoneRequest
{
/// <summary>
/// Initializes a new instance of the <see cref="CreateZoneRequest"/> class.
/// </summary>
/// <param name="name">The domain name.</param>
public CreateZoneRequest(string name)
{
Name = name;
}
/// <summary>
/// The account identifier.
/// </summary>
public string? AccountId { get; set; }
/// <summary>
/// The domain name.
/// </summary>
public string Name { get; set; }
/// <summary>
/// A full zone implies that DNS is hosted with Cloudflare.
/// A partial zone is typically a partner-hosted zone or a CNAME setup.
/// </summary>
public ZoneType? Type { get; set; }
}
}

View File

@@ -0,0 +1,47 @@
namespace AMWD.Net.Api.Cloudflare.Zones
{
/// <summary>
/// Represents a request to edit a zone.
/// </summary>
public class EditZoneRequest
{
/// <summary>
/// Initializes a new instance of the <see cref="EditZoneRequest"/> class.
/// </summary>
/// <param name="zoneId">The zone identifier.</param>
public EditZoneRequest(string zoneId)
{
ZoneId = zoneId;
}
/// <summary>
/// The zone identifier.
/// </summary>
public string ZoneId { get; set; }
/// <summary>
/// Indicates whether the zone is only using Cloudflare DNS services.
/// </summary>
/// <remarks>
/// A <see langword="true"/> value means the zone will not receive security or performance benefits.
/// </remarks>
public bool? Paused { get; set; }
/// <summary>
/// A full zone implies that DNS is hosted with Cloudflare.
/// A partial zone is typically a partner-hosted zone or a CNAME setup.
/// </summary>
/// <remarks>
/// This parameter is only available to Enterprise customers or if it has been explicitly enabled on a zone.
/// </remarks>
public ZoneType? Type { get; set; }
/// <summary>
/// A list of domains used for custom name servers.
/// </summary>
/// <remarks>
/// This is only available for Business and Enterprise plans.
/// </remarks>
public IReadOnlyCollection<string>? VanityNameServers { get; set; }
}
}

View File

@@ -0,0 +1,110 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AMWD.Net.Api.Cloudflare.Zones.Internals;
namespace AMWD.Net.Api.Cloudflare.Zones
{
/// <summary>
/// Extensions for <see href="https://developers.cloudflare.com/api/resources/zones/">Zones</see>.
/// </summary>
public static class ZonesExtensions
{
/// <summary>
/// Create 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<Zone>> CreateZone(this ICloudflareClient client, CreateZoneRequest request, CancellationToken cancellationToken = default)
{
request.AccountId?.ValidateCloudflareId();
request.Name.ValidateLength(253, nameof(request.Name));
if (request.Type.HasValue && !Enum.IsDefined(typeof(ZoneType), request.Type.Value))
throw new ArgumentOutOfRangeException(nameof(request.Type));
var req = new InternalCreateZoneRequest(
request.AccountId,
request.Name,
request.Type
);
return client.PostAsync<Zone, InternalCreateZoneRequest>($"/zones", req, cancellationToken: cancellationToken);
}
/// <summary>
/// Deletes an existing 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<Identifier>> DeleteZone(this ICloudflareClient client, string zoneId, CancellationToken cancellationToken = default)
{
zoneId.ValidateCloudflareId();
return client.DeleteAsync<Identifier>($"/zones/{zoneId}", cancellationToken: cancellationToken);
}
/// <summary>
/// Edits a zone.
/// </summary>
/// <remarks>
/// <strong>Only one zone property can be changed at a time.</strong>
/// </remarks>
/// <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<Zone>> EditZone(this ICloudflareClient client, EditZoneRequest request, CancellationToken cancellationToken = default)
{
request.ZoneId.ValidateCloudflareId();
if (request.Paused.HasValue && (request.Type.HasValue || request.VanityNameServers != null))
throw new CloudflareException("Only one zone property can be changed at a time.");
if (request.Type.HasValue && request.VanityNameServers != null)
throw new CloudflareException("Only one zone property can be changed at a time.");
if (request.Type.HasValue && !Enum.IsDefined(typeof(ZoneType), request.Type.Value))
throw new ArgumentOutOfRangeException(nameof(request.Type));
var req = new InternalEditZoneRequest
{
Paused = request.Paused,
Type = request.Type
};
if (request.VanityNameServers != null)
req.VanityNameServers = request.VanityNameServers.Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
return client.PatchAsync<Zone, InternalEditZoneRequest>($"/zones/{request.ZoneId}", req, cancellationToken: cancellationToken);
}
/// <summary>
/// Zone Details.
/// </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<Zone>> ZoneDetails(this ICloudflareClient client, string zoneId, CancellationToken cancellationToken = default)
{
zoneId.ValidateCloudflareId();
return client.GetAsync<Zone>($"/zones/{zoneId}", cancellationToken: cancellationToken);
}
/// <summary>
/// Lists, searches, sorts, and filters your zones.
/// </summary>
/// <remarks>
/// Listing zones across more than 500 accounts is currently not allowed.
/// </remarks>
/// <param name="client">The <see cref="ICloudflareClient"/> instance.</param>
/// <param name="options">Filter options.</param>
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
public static Task<CloudflareResponse<IReadOnlyCollection<Zone>>> ListZones(this ICloudflareClient client, ListZonesFilter? options = null, CancellationToken cancellationToken = default)
{
return client.GetAsync<IReadOnlyCollection<Zone>>($"/zones", queryFilter: options, cancellationToken: cancellationToken);
}
}
}

View File

@@ -6,7 +6,7 @@ using AMWD.Net.Api.Cloudflare.Zones;
using Moq; using Moq;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
namespace Cloudflare.Zones.Tests.ZoneRegistrar namespace Cloudflare.Zones.Tests.RegistrarExtensions
{ {
[TestClass] [TestClass]
public class GetDomainTest public class GetDomainTest

View File

@@ -5,7 +5,7 @@ using AMWD.Net.Api.Cloudflare;
using AMWD.Net.Api.Cloudflare.Zones; using AMWD.Net.Api.Cloudflare.Zones;
using Moq; using Moq;
namespace Cloudflare.Zones.Tests.ZoneRegistrar namespace Cloudflare.Zones.Tests.RegistrarExtensions
{ {
[TestClass] [TestClass]
public class ListDomainsTest public class ListDomainsTest

View File

@@ -7,7 +7,7 @@ using AMWD.Net.Api.Cloudflare.Zones.Internals;
using Moq; using Moq;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
namespace Cloudflare.Zones.Tests.ZoneRegistrar namespace Cloudflare.Zones.Tests.RegistrarExtensions
{ {
[TestClass] [TestClass]
public class UpdateDomainTest public class UpdateDomainTest

View File

@@ -0,0 +1,153 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AMWD.Net.Api.Cloudflare;
using AMWD.Net.Api.Cloudflare.Zones;
using AMWD.Net.Api.Cloudflare.Zones.Internals;
using Moq;
namespace Cloudflare.Zones.Tests.ZonesExtensions
{
[TestClass]
public class CreateZoneTest
{
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<Zone> _response;
private List<(string RequestPath, InternalCreateZoneRequest Request)> _callbacks;
private CreateZoneRequest _request;
[TestInitialize]
public void Initialize()
{
_callbacks = [];
_response = new CloudflareResponse<Zone>
{
Success = true,
Messages = [
new ResponseInfo(1000, "Message 1")
],
Errors = [
new ResponseInfo(1000, "Error 1")
],
Result = new Zone(
ZoneId,
"example.com",
[
"bob.ns.cloudflare.com",
"lola.ns.cloudflare.com"
],
new ZoneAccount
{
Id = "023e105f4ecef8ad9ca31a8372d0c353",
Name = "Example Account Name"
},
new ZoneMeta
{
CdnOnly = true,
CustomCertificateQuota = 1,
DnsOnly = true,
FoundationDns = true,
PageRuleQuota = 100,
PhishingDetected = false,
Step = 2
},
new ZoneOwner
{
Id = "023e105f4ecef8ad9ca31a8372d0c353",
Name = "Example Org",
Type = "organization"
}
)
{
ActivatedOn = DateTime.Parse("2014-01-02T00:01:00.12345Z"),
CreatedOn = DateTime.Parse("2014-01-01T05:20:00.12345Z"),
DevelopmentMode = 7200,
ModifiedOn = DateTime.Parse("2014-01-01T05:20:00.12345Z"),
Name = "example.com",
OriginalDnsHost = "NameCheap",
OriginalNameServers =
[
"ns1.originaldnshost.com",
"ns2.originaldnshost.com"
],
OriginalRegistrar = "GoDaddy",
Paused = true,
Status = ZoneStatus.Initializing,
Type = ZoneType.Full,
VanityNameServers =
[
"ns1.example.com",
"ns2.example.com"
]
}
};
_request = new CreateZoneRequest("example.com")
{
Type = ZoneType.Full
};
}
[DataTestMethod]
[DataRow(null)]
[DataRow("023e105f4ecef8ad9ca31a8372d0c353")]
public async Task ShouldCreateZone(string accountId)
{
// Arrange
_request.AccountId = accountId;
var client = GetClient();
// Act
var response = await client.CreateZone(_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", callback.RequestPath);
Assert.IsNotNull(callback.Request);
Assert.AreEqual(_request.AccountId, callback.Request.Account.Id);
Assert.AreEqual(_request.Name, callback.Request.Name);
Assert.AreEqual(_request.Type, callback.Request.Type);
_clientMock.Verify(m => m.PostAsync<Zone, InternalCreateZoneRequest>("/zones", It.IsAny<InternalCreateZoneRequest>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public async Task ShouldThrowArgumentOutOfRangeExceptionOnInvalidType()
{
// Arrange
_request.Type = 0;
var client = GetClient();
// Act
await client.CreateZone(_request);
// Assert - ArgumentOutOfRangeException
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.PostAsync<Zone, InternalCreateZoneRequest>(It.IsAny<string>(), It.IsAny<InternalCreateZoneRequest>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
.Callback<string, InternalCreateZoneRequest, IQueryParameterFilter, CancellationToken>((requestPath, request, _, _) => _callbacks.Add((requestPath, request)))
.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.Zones;
using Moq;
namespace Cloudflare.Zones.Tests.ZonesExtensions
{
[TestClass]
public class DeleteZoneTest
{
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 ResponseInfo(1000, "Message 1")
],
Errors = [
new ResponseInfo(1000, "Error 1")
],
Result = new Identifier
{
Id = ZoneId
}
};
}
[TestMethod]
public async Task ShouldDeleteZone()
{
// Arrange
var client = GetClient();
// Act
var response = await client.DeleteZone(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}", callback.RequestPath);
Assert.IsNull(callback.QueryFilter);
_clientMock.Verify(m => m.DeleteAsync<Identifier>($"/zones/{ZoneId}", It.IsAny<IQueryParameterFilter>(), It.IsAny<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,246 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AMWD.Net.Api.Cloudflare;
using AMWD.Net.Api.Cloudflare.Zones;
using AMWD.Net.Api.Cloudflare.Zones.Internals;
using Moq;
namespace Cloudflare.Zones.Tests.ZonesExtensions
{
[TestClass]
public class EditZoneTest
{
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<Zone> _response;
private List<(string RequestPath, InternalEditZoneRequest Request)> _callbacks;
private EditZoneRequest _request;
[TestInitialize]
public void Initialize()
{
_callbacks = [];
_response = new CloudflareResponse<Zone>
{
Success = true,
Messages = [
new ResponseInfo(1000, "Message 1")
],
Errors = [
new ResponseInfo(1000, "Error 1")
],
Result = new Zone(
ZoneId,
"example.com",
[
"bob.ns.cloudflare.com",
"lola.ns.cloudflare.com"
],
new ZoneAccount
{
Id = "023e105f4ecef8ad9ca31a8372d0c353",
Name = "Example Account Name"
},
new ZoneMeta
{
CdnOnly = true,
CustomCertificateQuota = 1,
DnsOnly = true,
FoundationDns = true,
PageRuleQuota = 100,
PhishingDetected = false,
Step = 2
},
new ZoneOwner
{
Id = "023e105f4ecef8ad9ca31a8372d0c353",
Name = "Example Org",
Type = "organization"
}
)
{
ActivatedOn = DateTime.Parse("2014-01-02T00:01:00.12345Z"),
CreatedOn = DateTime.Parse("2014-01-01T05:20:00.12345Z"),
DevelopmentMode = 7200,
ModifiedOn = DateTime.Parse("2014-01-01T05:20:00.12345Z"),
OriginalDnsHost = "NameCheap",
OriginalNameServers =
[
"ns1.originaldnshost.com",
"ns2.originaldnshost.com"
],
OriginalRegistrar = "GoDaddy",
Paused = true,
Status = ZoneStatus.Initializing,
Type = ZoneType.Full,
VanityNameServers =
[
"ns1.example.com",
"ns2.example.com"
]
}
};
_request = new EditZoneRequest(ZoneId)
{
Paused = true,
Type = ZoneType.Full,
VanityNameServers = ["ns1.example.org", "ns2.example.org"]
};
}
[TestMethod]
public async Task ShouldReturnModifiedZoneForPaused()
{
// Arrange
_request.Type = null;
_request.VanityNameServers = null;
var client = GetClient();
// Act
var response = await client.EditZone(_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}", callback.RequestPath);
Assert.IsNotNull(callback.Request);
Assert.IsTrue(callback.Request.Paused);
Assert.IsNull(callback.Request.Type);
Assert.IsNull(callback.Request.VanityNameServers);
_clientMock.Verify(m => m.PatchAsync<Zone, InternalEditZoneRequest>($"/zones/{ZoneId}", It.IsAny<InternalEditZoneRequest>(), It.IsAny<CancellationToken>()), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
[TestMethod]
public async Task ShouldReturnModifiedZoneForType()
{
// Arrange
_request.Paused = null;
_request.VanityNameServers = null;
var client = GetClient();
// Act
var response = await client.EditZone(_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}", callback.RequestPath);
Assert.IsNotNull(callback.Request);
Assert.IsNull(callback.Request.Paused);
Assert.AreEqual(_request.Type.Value, callback.Request.Type.Value);
Assert.IsNull(callback.Request.VanityNameServers);
_clientMock.Verify(m => m.PatchAsync<Zone, InternalEditZoneRequest>($"/zones/{ZoneId}", It.IsAny<InternalEditZoneRequest>(), It.IsAny<CancellationToken>()), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
[TestMethod]
public async Task ShouldReturnModifiedZoneForVanityNameServers()
{
// Arrange
_request.Paused = null;
_request.Type = null;
_request.VanityNameServers = [.. _request.VanityNameServers, ""];
var client = GetClient();
// Act
var response = await client.EditZone(_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}", callback.RequestPath);
Assert.IsNotNull(callback.Request);
Assert.IsNull(callback.Request.Paused);
Assert.IsNull(callback.Request.Type);
Assert.AreEqual(2, callback.Request.VanityNameServers.Count);
Assert.IsTrue(callback.Request.VanityNameServers.Contains("ns1.example.org"));
Assert.IsTrue(callback.Request.VanityNameServers.Contains("ns2.example.org"));
_clientMock.Verify(m => m.PatchAsync<Zone, InternalEditZoneRequest>($"/zones/{ZoneId}", It.IsAny<InternalEditZoneRequest>(), It.IsAny<CancellationToken>()), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
[TestMethod]
[ExpectedException(typeof(CloudflareException))]
public async Task ShouldThrowCloudflareExceptionOnMultiplePropertiesSet1()
{
// Arrange
_request.VanityNameServers = null;
var client = GetClient();
// Act
await client.EditZone(_request);
// Assert - CloudflareException
}
[TestMethod]
[ExpectedException(typeof(CloudflareException))]
public async Task ShouldThrowCloudflareExceptionOnMultiplePropertiesSet2()
{
// Arrange
_request.Paused = null;
var client = GetClient();
// Act
await client.EditZone(_request);
// Assert - CloudflareException
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public async Task ShouldThrowArgumentOutOfRangeExceptionForInvalidType()
{
// Arrange
_request.Paused = null;
_request.Type = 0;
_request.VanityNameServers = null;
var client = GetClient();
// Act
await client.EditZone(_request);
// Assert - ArgumentOutOfRangeException
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.PatchAsync<Zone, InternalEditZoneRequest>(It.IsAny<string>(), It.IsAny<InternalEditZoneRequest>(), It.IsAny<CancellationToken>()))
.Callback<string, InternalEditZoneRequest, CancellationToken>((requestPath, request, _) => _callbacks.Add((requestPath, request)))
.ReturnsAsync(() => _response);
return _clientMock.Object;
}
}
}

View File

@@ -0,0 +1,385 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AMWD.Net.Api.Cloudflare;
using AMWD.Net.Api.Cloudflare.Zones;
using Moq;
namespace Cloudflare.Zones.Tests.ZonesExtensions
{
[TestClass]
public class ListZonesTest
{
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<IReadOnlyCollection<Zone>> _response;
private List<(string RequestPath, IQueryParameterFilter QueryFilter)> _callbacks;
[TestInitialize]
public void Initialize()
{
_callbacks = [];
_response = new CloudflareResponse<IReadOnlyCollection<Zone>>
{
Success = true,
Messages = [
new ResponseInfo(1000, "Message 1")
],
Errors = [
new ResponseInfo(1000, "Error 1")
],
ResultInfo = new PaginationInfo
{
Count = 1,
Page = 1,
PerPage = 20,
TotalCount = 2000
},
Result =
[
new Zone(
ZoneId,
"example.com",
[
"bob.ns.cloudflare.com",
"lola.ns.cloudflare.com"
],
new ZoneAccount
{
Id = "023e105f4ecef8ad9ca31a8372d0c353",
Name = "Example Account Name"
},
new ZoneMeta
{
CdnOnly = true,
CustomCertificateQuota = 1,
DnsOnly = true,
FoundationDns = true,
PageRuleQuota = 100,
PhishingDetected = false,
Step = 2
},
new ZoneOwner
{
Id = "023e105f4ecef8ad9ca31a8372d0c353",
Name = "Example Org",
Type = "organization"
}
)
{
ActivatedOn = DateTime.Parse("2014-01-02T00:01:00.12345Z"),
CreatedOn = DateTime.Parse("2014-01-01T05:20:00.12345Z"),
DevelopmentMode = 7200,
ModifiedOn = DateTime.Parse("2014-01-01T05:20:00.12345Z"),
OriginalDnsHost = "NameCheap",
OriginalNameServers =
[
"ns1.originaldnshost.com",
"ns2.originaldnshost.com"
],
OriginalRegistrar = "GoDaddy",
Paused = true,
Status = ZoneStatus.Initializing,
Type = ZoneType.Full,
VanityNameServers =
[
"ns1.example.com",
"ns2.example.com"
]
}
]
};
}
[TestMethod]
public async Task ShouldReturnListOfZones()
{
// Arrange
var client = GetClient();
// Act
var response = await client.ListZones();
// 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", callback.RequestPath);
Assert.IsNull(callback.QueryFilter);
_clientMock.Verify(m => m.GetAsync<IReadOnlyCollection<Zone>>("/zones", null, It.IsAny<CancellationToken>()), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
[TestMethod]
public async Task ShouldReturnListOfZonesWithFilter()
{
// Arrange
var filter = new ListZonesFilter
{
AccountId = "023e105f4ecef8ad9ca31a8372d0c353"
};
var client = GetClient();
// Act
var response = await client.ListZones(filter);
// 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", callback.RequestPath);
Assert.AreEqual(filter, callback.QueryFilter);
_clientMock.Verify(m => m.GetAsync<IReadOnlyCollection<Zone>>("/zones", filter, It.IsAny<CancellationToken>()), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
[TestMethod]
public void ShouldReturnEmptyParameterList()
{
// Arrange
var filter = new ListZonesFilter();
// Act
var dict = filter.GetQueryParameters();
// Assert
Assert.IsNotNull(dict);
Assert.AreEqual(0, dict.Count);
}
[TestMethod]
public void ShouldReturnFullParameterList()
{
// Arrange
var filter = new ListZonesFilter
{
AccountId = "023e105f4ecef8ad9ca31a8372d0c353",
AccountName = "Example Account Name",
Match = ListZonesMatch.Any,
Name = "example.com",
PerPage = 13,
Page = 5,
OrderBy = ListZonesOrderBy.AccountName,
Direction = SortDirection.Descending,
Status = ZoneStatus.Active
};
// Act
var dict = filter.GetQueryParameters();
// Assert
Assert.IsNotNull(dict);
Assert.AreEqual(9, dict.Count);
Assert.IsTrue(dict.ContainsKey("account.id"));
Assert.IsTrue(dict.ContainsKey("account.name"));
Assert.IsTrue(dict.ContainsKey("direction"));
Assert.IsTrue(dict.ContainsKey("match"));
Assert.IsTrue(dict.ContainsKey("name"));
Assert.IsTrue(dict.ContainsKey("order"));
Assert.IsTrue(dict.ContainsKey("page"));
Assert.IsTrue(dict.ContainsKey("per_page"));
Assert.IsTrue(dict.ContainsKey("status"));
Assert.AreEqual("023e105f4ecef8ad9ca31a8372d0c353", dict["account.id"]);
Assert.AreEqual("Example Account Name", dict["account.name"]);
Assert.AreEqual("desc", dict["direction"]);
Assert.AreEqual("any", dict["match"]);
Assert.AreEqual("example.com", dict["name"]);
Assert.AreEqual("account.name", dict["order"]);
Assert.AreEqual("5", dict["page"]);
Assert.AreEqual("13", dict["per_page"]);
Assert.AreEqual("active", dict["status"]);
}
[DataTestMethod]
[DataRow(null)]
[DataRow("")]
[DataRow(" ")]
public void ShouldNotAddAccountId(string id)
{
// Arrange
var filter = new ListZonesFilter
{
AccountId = id
};
// Act
var dict = filter.GetQueryParameters();
// Assert
Assert.IsNotNull(dict);
Assert.AreEqual(0, dict.Count);
}
[DataTestMethod]
[DataRow(null)]
[DataRow("")]
[DataRow(" ")]
public void ShouldNotAddAccountName(string name)
{
// Arrange
var filter = new ListZonesFilter
{
AccountName = name
};
// Act
var dict = filter.GetQueryParameters();
// Assert
Assert.IsNotNull(dict);
Assert.AreEqual(0, dict.Count);
}
[TestMethod]
public void ShouldNotAddDirection()
{
// Arrange
var filter = new ListZonesFilter
{
Direction = 0
};
// Act
var dict = filter.GetQueryParameters();
// Assert
Assert.IsNotNull(dict);
Assert.AreEqual(0, dict.Count);
}
[TestMethod]
public void ShouldNotAddMatch()
{
// Arrange
var filter = new ListZonesFilter
{
Match = 0
};
// Act
var dict = filter.GetQueryParameters();
// Assert
Assert.IsNotNull(dict);
Assert.AreEqual(0, dict.Count);
}
[DataTestMethod]
[DataRow(null)]
[DataRow("")]
[DataRow(" ")]
public void ShouldNotAddName(string name)
{
// Arrange
var filter = new ListZonesFilter
{
Name = name
};
// Act
var dict = new Dictionary<string, object>();
// Assert
Assert.IsNotNull(dict);
Assert.AreEqual(0, dict.Count);
}
[TestMethod]
public void ShouldNotAddOrder()
{
// Arrange
var filter = new ListZonesFilter
{
OrderBy = 0
};
// Act
var dict = filter.GetQueryParameters();
// Assert
Assert.IsNotNull(dict);
Assert.AreEqual(0, dict.Count);
}
[TestMethod]
public void ShouldNotAddPage()
{
// Arrange
var filter = new ListZonesFilter
{
Page = 0
};
// Act
var dict = filter.GetQueryParameters();
// Assert
Assert.IsNotNull(dict);
Assert.AreEqual(0, dict.Count);
}
[DataTestMethod]
[DataRow(4)]
[DataRow(51)]
public void ShouldNotAddPerPage(int perPage)
{
// Arrange
var filter = new ListZonesFilter
{
PerPage = perPage
};
// Act
var dict = filter.GetQueryParameters();
// Assert
Assert.IsNotNull(dict);
Assert.AreEqual(0, dict.Count);
}
[TestMethod]
public void ShouldNotAddStatus()
{
// Arrange
var filter = new ListZonesFilter
{
Status = 0
};
// Act
var dict = filter.GetQueryParameters();
// Assert
Assert.IsNotNull(dict);
Assert.AreEqual(0, dict.Count);
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.GetAsync<IReadOnlyCollection<Zone>>(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,123 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AMWD.Net.Api.Cloudflare;
using AMWD.Net.Api.Cloudflare.Zones;
using Moq;
namespace Cloudflare.Zones.Tests.ZonesExtensions
{
[TestClass]
public class ZoneDetailsTest
{
private const string ZoneId = "023e105f4ecef8ad9ca31a8372d0c353";
private Mock<ICloudflareClient> _clientMock;
private CloudflareResponse<Zone> _response;
private List<(string RequestPath, IQueryParameterFilter QueryFilter)> _callbacks;
[TestInitialize]
public void Initialize()
{
_callbacks = [];
_response = new CloudflareResponse<Zone>
{
Success = true,
Messages = [
new ResponseInfo(1000, "Message 1")
],
Errors = [
new ResponseInfo(1000, "Error 1")
],
Result = new Zone(
ZoneId,
"example.com",
[
"bob.ns.cloudflare.com",
"lola.ns.cloudflare.com"
],
new ZoneAccount
{
Id = "023e105f4ecef8ad9ca31a8372d0c353",
Name = "Example Account Name"
},
new ZoneMeta
{
CdnOnly = true,
CustomCertificateQuota = 1,
DnsOnly = true,
FoundationDns = true,
PageRuleQuota = 100,
PhishingDetected = false,
Step = 2
},
new ZoneOwner
{
Id = "023e105f4ecef8ad9ca31a8372d0c353",
Name = "Example Org",
Type = "organization"
}
)
{
ActivatedOn = DateTime.Parse("2014-01-02T00:01:00.12345Z"),
CreatedOn = DateTime.Parse("2014-01-01T05:20:00.12345Z"),
DevelopmentMode = 7200,
ModifiedOn = DateTime.Parse("2014-01-01T05:20:00.12345Z"),
OriginalDnsHost = "NameCheap",
OriginalNameServers =
[
"ns1.originaldnshost.com",
"ns2.originaldnshost.com"
],
OriginalRegistrar = "GoDaddy",
Paused = true,
Status = ZoneStatus.Initializing,
Type = ZoneType.Full,
VanityNameServers =
[
"ns1.example.com",
"ns2.example.com"
]
}
};
}
[TestMethod]
public async Task ShouldReturnZoneDetails()
{
// Arrange
var client = GetClient();
// Act
var response = await client.ZoneDetails(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}", callback.RequestPath);
Assert.IsNull(callback.QueryFilter);
_clientMock.Verify(m => m.GetAsync<Zone>($"/zones/{ZoneId}", null, It.IsAny<CancellationToken>()), Times.Once);
_clientMock.VerifyNoOtherCalls();
}
private ICloudflareClient GetClient()
{
_clientMock = new Mock<ICloudflareClient>();
_clientMock
.Setup(m => m.GetAsync<Zone>(It.IsAny<string>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
.Callback<string, IQueryParameterFilter, CancellationToken>((requestPath, queryFilter, _) => _callbacks.Add((requestPath, queryFilter)))
.ReturnsAsync(() => _response);
return _clientMock.Object;
}
}
}