Add 'DNS Account Settings > Internal Views'
This commit is contained in:
@@ -10,8 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
- `Cloudflare` with basic functionality to communicate with Cloudflare's API
|
- `Cloudflare` with the core functionality to communicate with Cloudflare's API endpoint
|
||||||
- `Cloudflare.Zones` extending the core package with specific methods to manage Cloudflare's DNS zones
|
- `Cloudflare.Dns` extending the core package with specific methods to manage Cloudflare's DNS settings
|
||||||
|
- `Cloudflare.Zones` extending the core package with specific methods to manage Cloudflare's Domain/Zone Management
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -86,5 +86,104 @@ namespace AMWD.Net.Api.Cloudflare.Dns
|
|||||||
|
|
||||||
return client.GetAsync<DnsAccountSettings>($"/accounts/{accountId}/dns_settings", null, cancellationToken);
|
return client.GetAsync<DnsAccountSettings>($"/accounts/{accountId}/dns_settings", null, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#region Views
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create Internal DNS View for an account.
|
||||||
|
/// </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<InternalDnsView>> CreateInternalDnsView(this ICloudflareClient client, CreateInternalDnsViewRequest request, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
request.AccountId.ValidateCloudflareId();
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(request.Name))
|
||||||
|
throw new ArgumentNullException(nameof(request.Name));
|
||||||
|
|
||||||
|
if (request.Name.Length > 255)
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(request.Name), request.Name, "The Name length must be between 1 and 255 characters.");
|
||||||
|
|
||||||
|
var req = new InternalModifyInternalDnsViewRequest
|
||||||
|
{
|
||||||
|
Name = request.Name,
|
||||||
|
Zones = request.ZoneIds
|
||||||
|
};
|
||||||
|
|
||||||
|
return client.PostAsync<InternalDnsView, InternalModifyInternalDnsViewRequest>($"/accounts/{request.AccountId}/dns_settings/views", req, null, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Delete an existing Internal DNS View.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="client">The <see cref="ICloudflareClient"/> instance.</param>
|
||||||
|
/// <param name="accountId">The account identifier.</param>
|
||||||
|
/// <param name="viewId">The view identifier.</param>
|
||||||
|
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
|
||||||
|
public static Task<CloudflareResponse<Identifier>> DeleteInternalDnsView(this ICloudflareClient client, string accountId, string viewId, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
accountId.ValidateCloudflareId();
|
||||||
|
viewId.ValidateCloudflareId();
|
||||||
|
|
||||||
|
return client.DeleteAsync<Identifier>($"/accounts/{accountId}/dns_settings/views/{viewId}", null, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Update an existing Internal DNS View.
|
||||||
|
/// </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<InternalDnsView>> UpdateInternalDnsView(this ICloudflareClient client, UpdateInternalDnsViewRequest request, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
request.AccountId.ValidateCloudflareId();
|
||||||
|
request.ViewId.ValidateCloudflareId();
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(request.Name))
|
||||||
|
throw new ArgumentNullException(nameof(request.Name));
|
||||||
|
|
||||||
|
if (request.Name.Length > 255)
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(request.Name), request.Name, "The Name length must be between 1 and 255 characters.");
|
||||||
|
|
||||||
|
var req = new InternalModifyInternalDnsViewRequest
|
||||||
|
{
|
||||||
|
Name = request.Name,
|
||||||
|
Zones = request.ZoneIds
|
||||||
|
};
|
||||||
|
|
||||||
|
return client.PatchAsync<InternalDnsView, InternalModifyInternalDnsViewRequest>($"/accounts/{request.AccountId}/dns_settings/views/{request.ViewId}", req, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get DNS Internal View.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="client">The <see cref="ICloudflareClient"/> instance.</param>
|
||||||
|
/// <param name="accountId">The account identifier.</param>
|
||||||
|
/// <param name="viewId">The view identifier.</param>
|
||||||
|
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
|
||||||
|
public static Task<CloudflareResponse<InternalDnsView>> InternalDnsViewDetails(this ICloudflareClient client, string accountId, string viewId, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
accountId.ValidateCloudflareId();
|
||||||
|
viewId.ValidateCloudflareId();
|
||||||
|
|
||||||
|
return client.GetAsync<InternalDnsView>($"/accounts/{accountId}/dns_settings/views/{viewId}", null, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// List DNS Internal Views for an Account.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="client">The <see cref="ICloudflareClient"/> instance.</param>
|
||||||
|
/// <param name="accountId">The account identifier.</param>
|
||||||
|
/// <param name="options">Filter options (optional).</param>
|
||||||
|
/// <param name="cancellationToken">A cancellation token used to propagate notification that this operation should be canceled.</param>
|
||||||
|
public static Task<CloudflareResponse<IReadOnlyCollection<InternalDnsView>>> ListInternalDnsViews(this ICloudflareClient client, string accountId, ListInternalDnsViewsFilter? options = null, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
accountId.ValidateCloudflareId();
|
||||||
|
|
||||||
|
return client.GetAsync<IReadOnlyCollection<InternalDnsView>>($"/accounts/{accountId}/dns_settings/views", options, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion Views
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,153 @@
|
|||||||
|
using System.Runtime.Serialization;
|
||||||
|
using Newtonsoft.Json.Converters;
|
||||||
|
|
||||||
|
namespace AMWD.Net.Api.Cloudflare.Dns
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Filter for listing internal DNS views.
|
||||||
|
/// </summary>
|
||||||
|
public class ListInternalDnsViewsFilter : IQueryParameterFilter
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Direction to order DNS views in.
|
||||||
|
/// </summary>
|
||||||
|
public SortDirection? Direction { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Whether to match all search requirements or at least one (any).
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// <para>
|
||||||
|
/// If set to <see cref="FilterMatchType.All"/>, acts like a logical AND between filters.
|
||||||
|
/// <br/>
|
||||||
|
/// If set to <see cref="FilterMatchType.Any"/>, acts like a logical OR instead.
|
||||||
|
/// </para>
|
||||||
|
/// </remarks>
|
||||||
|
public FilterMatchType? Match { get; set; }
|
||||||
|
|
||||||
|
#region Name
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Substring of the DNS record Name.
|
||||||
|
/// Name filters are case-insensitive.
|
||||||
|
/// </summary>
|
||||||
|
public string? NameContains { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Suffix of the DNS record Name.
|
||||||
|
/// Name filters are case-insensitive.
|
||||||
|
/// </summary>
|
||||||
|
public string? NameEndsWith { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Exact value of the DNS record Name.
|
||||||
|
/// Name filters are case-insensitive.
|
||||||
|
/// </summary>
|
||||||
|
public string? NameExact { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Prefix of the DNS record Name.
|
||||||
|
/// Name filters are case-insensitive.
|
||||||
|
/// </summary>
|
||||||
|
public string? NameStartsWith { get; set; }
|
||||||
|
|
||||||
|
#endregion Name
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Field to order DNS views by.
|
||||||
|
/// </summary>
|
||||||
|
public InternalDnsViewsOrderBy? OrderBy { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Page number of paginated results.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>1 <= X</value>
|
||||||
|
public int? Page { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Number of DNS records per page.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>1 <= X <= 5,000,000</value>
|
||||||
|
public int? PerPage { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A zone ID that exists in the zones list for the view.
|
||||||
|
/// </summary>
|
||||||
|
public string? ZoneId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A zone name that exists in the zones list for the view.
|
||||||
|
/// </summary>
|
||||||
|
public string? ZoneName { get; set; }
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public IReadOnlyDictionary<string, string> GetQueryParameters()
|
||||||
|
{
|
||||||
|
var dict = new Dictionary<string, string>();
|
||||||
|
|
||||||
|
#pragma warning disable CS8602, CS8604 // There will be no null value below.
|
||||||
|
|
||||||
|
if (Direction.HasValue && Enum.IsDefined(typeof(SortDirection), Direction.Value))
|
||||||
|
dict.Add("direction", Direction.Value.GetEnumMemberValue());
|
||||||
|
|
||||||
|
if (Match.HasValue && Enum.IsDefined(typeof(FilterMatchType), Match.Value))
|
||||||
|
dict.Add("match", Match.Value.GetEnumMemberValue());
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(NameContains))
|
||||||
|
dict.Add("name.contains", NameContains.Trim());
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(NameEndsWith))
|
||||||
|
dict.Add("name.endswith", NameEndsWith.Trim());
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(NameExact))
|
||||||
|
dict.Add("name.exact", NameExact.Trim());
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(NameStartsWith))
|
||||||
|
dict.Add("name.startswith", NameStartsWith.Trim());
|
||||||
|
|
||||||
|
if (OrderBy.HasValue && Enum.IsDefined(typeof(InternalDnsViewsOrderBy), 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 >= 1 && PerPage.Value <= 5_000_000)
|
||||||
|
dict.Add("per_page", PerPage.Value.ToString());
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(ZoneId))
|
||||||
|
dict.Add("zone_id", ZoneId.Trim());
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(ZoneName))
|
||||||
|
dict.Add("zone_name", ZoneName.Trim());
|
||||||
|
|
||||||
|
#pragma warning restore CS8602, CS8604
|
||||||
|
|
||||||
|
return dict;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Possible fields to order internal DNS views by.
|
||||||
|
/// </summary>
|
||||||
|
[JsonConverter(typeof(StringEnumConverter))]
|
||||||
|
public enum InternalDnsViewsOrderBy
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Order by name.
|
||||||
|
/// </summary>
|
||||||
|
[EnumMember(Value = "name")]
|
||||||
|
Name = 1,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Order by creation date.
|
||||||
|
/// </summary>
|
||||||
|
[EnumMember(Value = "created_on")]
|
||||||
|
CreatedOn = 2,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Order by last modified date.
|
||||||
|
/// </summary>
|
||||||
|
[EnumMember(Value = "modified_on")]
|
||||||
|
ModifiedOn = 3
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
namespace AMWD.Net.Api.Cloudflare.Dns.Internals
|
||||||
|
{
|
||||||
|
internal class InternalModifyInternalDnsViewRequest
|
||||||
|
{
|
||||||
|
[JsonProperty("name")]
|
||||||
|
public string? Name { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("zones")]
|
||||||
|
public IReadOnlyCollection<string>? Zones { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
49
src/Extensions/Cloudflare.Dns/Models/InternalDnsView.cs
Normal file
49
src/Extensions/Cloudflare.Dns/Models/InternalDnsView.cs
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
namespace AMWD.Net.Api.Cloudflare.Dns
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A Cloudflare internal DNS view.
|
||||||
|
/// </summary>
|
||||||
|
public class InternalDnsView
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="InternalDnsView"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">The identifier.</param>
|
||||||
|
/// <param name="name">The name of the view.</param>
|
||||||
|
public InternalDnsView(string id, string name)
|
||||||
|
{
|
||||||
|
Id = id;
|
||||||
|
Name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The identifier.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("id")]
|
||||||
|
public string Id { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// When the view was created.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("created_time")]
|
||||||
|
public DateTime? CreatedTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// When the view was last modified.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("modified_time")]
|
||||||
|
public DateTime? ModifiedTime { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The name of the view.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("name")]
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The list of zones linked to this view.
|
||||||
|
/// </summary>
|
||||||
|
[JsonProperty("zones")]
|
||||||
|
public IReadOnlyCollection<string> ZoneIds { get; set; } = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -33,6 +33,11 @@ This package contains the feature set of the _DNS_ section of the Cloudflare API
|
|||||||
|
|
||||||
- [Update DNS Settings](https://developers.cloudflare.com/api/resources/dns/subresources/settings/subresources/account/methods/edit/)
|
- [Update DNS Settings](https://developers.cloudflare.com/api/resources/dns/subresources/settings/subresources/account/methods/edit/)
|
||||||
- [Show DNS Settings](https://developers.cloudflare.com/api/resources/dns/subresources/settings/subresources/account/methods/get/)
|
- [Show DNS Settings](https://developers.cloudflare.com/api/resources/dns/subresources/settings/subresources/account/methods/get/)
|
||||||
|
- [Create Internal DNS View](https://developers.cloudflare.com/api/resources/dns/subresources/settings/subresources/account/subresources/views/methods/create/)
|
||||||
|
- [Delete Internal DNS View](https://developers.cloudflare.com/api/resources/dns/subresources/settings/subresources/account/subresources/views/methods/delete/)
|
||||||
|
- [Update Internal DNS View](https://developers.cloudflare.com/api/resources/dns/subresources/settings/subresources/account/subresources/views/methods/edit/)
|
||||||
|
- [DNS Internal View Details](https://developers.cloudflare.com/api/resources/dns/subresources/settings/subresources/account/subresources/views/methods/get/)
|
||||||
|
- [List Internal DNS Views](https://developers.cloudflare.com/api/resources/dns/subresources/settings/subresources/account/subresources/views/methods/list/)
|
||||||
|
|
||||||
|
|
||||||
##### [Zone]
|
##### [Zone]
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
namespace AMWD.Net.Api.Cloudflare.Dns
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a request to create an internal DNS view.
|
||||||
|
/// </summary>
|
||||||
|
public class CreateInternalDnsViewRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="CreateInternalDnsViewRequest"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="accountId">The account identifier.</param>
|
||||||
|
/// <param name="name">The name of the view.</param>
|
||||||
|
public CreateInternalDnsViewRequest(string accountId, string name)
|
||||||
|
{
|
||||||
|
AccountId = accountId;
|
||||||
|
Name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The account identifier.
|
||||||
|
/// </summary>
|
||||||
|
public string AccountId { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The name of the view.
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The list of zones linked to this view.
|
||||||
|
/// </summary>
|
||||||
|
public IReadOnlyCollection<string> ZoneIds { get; set; } = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
namespace AMWD.Net.Api.Cloudflare.Dns
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a request to update an internal DNS view.
|
||||||
|
/// </summary>
|
||||||
|
public class UpdateInternalDnsViewRequest : CreateInternalDnsViewRequest
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="UpdateInternalDnsViewRequest"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="accountId">The account identifier.</param>
|
||||||
|
/// <param name="viewId">The view identifier.</param>
|
||||||
|
/// <param name="name">The name of the view.</param>
|
||||||
|
public UpdateInternalDnsViewRequest(string accountId, string viewId, string name)
|
||||||
|
: base(accountId, name)
|
||||||
|
{
|
||||||
|
ViewId = viewId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The view identifier.
|
||||||
|
/// </summary>
|
||||||
|
public string ViewId { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -83,8 +83,8 @@ namespace Cloudflare.Dns.Tests.DnsAccountSettingsExtensions
|
|||||||
|
|
||||||
Assert.IsNull(callback.QueryFilter);
|
Assert.IsNull(callback.QueryFilter);
|
||||||
|
|
||||||
_clientMock?.Verify(m => m.GetAsync<DnsAccountSettings>($"/accounts/{AccountId}/dns_settings", null, It.IsAny<CancellationToken>()), Times.Once);
|
_clientMock.Verify(m => m.GetAsync<DnsAccountSettings>($"/accounts/{AccountId}/dns_settings", null, It.IsAny<CancellationToken>()), Times.Once);
|
||||||
_clientMock?.VerifyNoOtherCalls();
|
_clientMock.VerifyNoOtherCalls();
|
||||||
}
|
}
|
||||||
|
|
||||||
private ICloudflareClient GetClient()
|
private ICloudflareClient GetClient()
|
||||||
|
|||||||
@@ -0,0 +1,115 @@
|
|||||||
|
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.DnsAccountSettingsExtensions.Views
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class CreateInternalDnsViewTest
|
||||||
|
{
|
||||||
|
private const string AccountId = "023e105f4ecef8ad9ca31a8372d0c353";
|
||||||
|
private const string ViewId = "023e105f4ecef8ad9ca31a8372d0c354";
|
||||||
|
private const string ViewName = "InternalView";
|
||||||
|
|
||||||
|
private Mock<ICloudflareClient> _clientMock;
|
||||||
|
private CloudflareResponse<InternalDnsView> _response;
|
||||||
|
private List<(string RequestPath, InternalModifyInternalDnsViewRequest Request)> _callbacks;
|
||||||
|
private CreateInternalDnsViewRequest _request;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Initialize()
|
||||||
|
{
|
||||||
|
_callbacks = [];
|
||||||
|
|
||||||
|
_response = new CloudflareResponse<InternalDnsView>
|
||||||
|
{
|
||||||
|
Success = true,
|
||||||
|
Messages = [
|
||||||
|
new ResponseInfo(1000, "Message 1")
|
||||||
|
],
|
||||||
|
Errors = [
|
||||||
|
new ResponseInfo(1000, "Error 1")
|
||||||
|
],
|
||||||
|
Result = new InternalDnsView(ViewId, ViewName)
|
||||||
|
};
|
||||||
|
|
||||||
|
_request = new CreateInternalDnsViewRequest(AccountId, ViewName)
|
||||||
|
{
|
||||||
|
ZoneIds = ["zone1", "zone2"]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldCreateInternalDnsView()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.CreateInternalDnsView(_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($"/accounts/{AccountId}/dns_settings/views", callback.RequestPath);
|
||||||
|
Assert.IsNotNull(callback.Request);
|
||||||
|
|
||||||
|
Assert.AreEqual(ViewName, callback.Request.Name);
|
||||||
|
CollectionAssert.AreEqual(_request.ZoneIds.ToList(), callback.Request.Zones.ToList());
|
||||||
|
|
||||||
|
_clientMock.Verify(m => m.PostAsync<InternalDnsView, InternalModifyInternalDnsViewRequest>($"/accounts/{AccountId}/dns_settings/views", It.IsAny<InternalModifyInternalDnsViewRequest>(), null, It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
_clientMock.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
|
[DataTestMethod]
|
||||||
|
[DataRow(null)]
|
||||||
|
[DataRow("")]
|
||||||
|
[DataRow(" ")]
|
||||||
|
[ExpectedException(typeof(ArgumentNullException))]
|
||||||
|
public async Task ShouldThrowArgumentNullExceptionWhenNameIsNull(string name)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_request.Name = name;
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.CreateInternalDnsView(_request);
|
||||||
|
|
||||||
|
// Assert - ArgumentNullException
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||||
|
public async Task ShouldThrowArgumentOutOfRangeExceptionWhenNameTooLong()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_request.Name = new string('a', 256);
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.CreateInternalDnsView(_request);
|
||||||
|
|
||||||
|
// Assert - ArgumentOutOfRangeException
|
||||||
|
}
|
||||||
|
|
||||||
|
private ICloudflareClient GetClient()
|
||||||
|
{
|
||||||
|
_clientMock = new Mock<ICloudflareClient>();
|
||||||
|
_clientMock
|
||||||
|
.Setup(m => m.PostAsync<InternalDnsView, InternalModifyInternalDnsViewRequest>(It.IsAny<string>(), It.IsAny<InternalModifyInternalDnsViewRequest>(), It.IsAny<IQueryParameterFilter>(), It.IsAny<CancellationToken>()))
|
||||||
|
.Callback<string, InternalModifyInternalDnsViewRequest, IQueryParameterFilter, CancellationToken>((requestPath, request, _, __) => _callbacks.Add((requestPath, request)))
|
||||||
|
.ReturnsAsync(() => _response);
|
||||||
|
|
||||||
|
return _clientMock.Object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
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.DnsAccountSettingsExtensions.Views
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class DeleteInternalDnsViewTest
|
||||||
|
{
|
||||||
|
private const string AccountId = "023e105f4ecef8ad9ca31a8372d0c353";
|
||||||
|
private const string ViewId = "023e105f4ecef8ad9ca31a8372d0c354";
|
||||||
|
|
||||||
|
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 = ViewId
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldDeleteInternalDnsView()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.DeleteInternalDnsView(AccountId, ViewId);
|
||||||
|
|
||||||
|
// 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($"/accounts/{AccountId}/dns_settings/views/{ViewId}", callback.RequestPath);
|
||||||
|
Assert.IsNull(callback.QueryFilter);
|
||||||
|
|
||||||
|
_clientMock.Verify(m => m.DeleteAsync<Identifier>($"/accounts/{AccountId}/dns_settings/views/{ViewId}", null, 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
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.DnsAccountSettingsExtensions.Views
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class InternalDnsViewDetailsTest
|
||||||
|
{
|
||||||
|
private const string AccountId = "023e105f4ecef8ad9ca31a8372d0c353";
|
||||||
|
private const string ViewId = "023e105f4ecef8ad9ca31a8372d0c354";
|
||||||
|
private const string ViewName = "InternalView";
|
||||||
|
|
||||||
|
private Mock<ICloudflareClient> _clientMock;
|
||||||
|
private CloudflareResponse<InternalDnsView> _response;
|
||||||
|
private List<(string RequestPath, IQueryParameterFilter QueryFilter)> _callbacks;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Initialize()
|
||||||
|
{
|
||||||
|
_callbacks = [];
|
||||||
|
|
||||||
|
_response = new CloudflareResponse<InternalDnsView>
|
||||||
|
{
|
||||||
|
Success = true,
|
||||||
|
Messages = [
|
||||||
|
new ResponseInfo(1000, "Message 1")
|
||||||
|
],
|
||||||
|
Errors = [
|
||||||
|
new ResponseInfo(1000, "Error 1")
|
||||||
|
],
|
||||||
|
Result = new InternalDnsView(ViewId, ViewName)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldGetInternalDnsViewDetails()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.InternalDnsViewDetails(AccountId, ViewId);
|
||||||
|
|
||||||
|
// 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($"/accounts/{AccountId}/dns_settings/views/{ViewId}", callback.RequestPath);
|
||||||
|
Assert.IsNull(callback.QueryFilter);
|
||||||
|
|
||||||
|
_clientMock.Verify(m => m.GetAsync<InternalDnsView>(
|
||||||
|
$"/accounts/{AccountId}/dns_settings/views/{ViewId}",
|
||||||
|
null,
|
||||||
|
It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
_clientMock.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
|
private ICloudflareClient GetClient()
|
||||||
|
{
|
||||||
|
_clientMock = new Mock<ICloudflareClient>();
|
||||||
|
_clientMock
|
||||||
|
.Setup(m => m.GetAsync<InternalDnsView>(
|
||||||
|
It.IsAny<string>(),
|
||||||
|
It.IsAny<IQueryParameterFilter>(),
|
||||||
|
It.IsAny<CancellationToken>()))
|
||||||
|
.Callback<string, IQueryParameterFilter, CancellationToken>((requestPath, queryFilter, _) => _callbacks.Add((requestPath, queryFilter)))
|
||||||
|
.ReturnsAsync(() => _response);
|
||||||
|
|
||||||
|
return _clientMock.Object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,354 @@
|
|||||||
|
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.DnsAccountSettingsExtensions.Views
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class ListInternalDnsViewsTest
|
||||||
|
{
|
||||||
|
private const string AccountId = "023e105f4ecef8ad9ca31a8372d0c353";
|
||||||
|
|
||||||
|
private Mock<ICloudflareClient> _clientMock;
|
||||||
|
private CloudflareResponse<IReadOnlyCollection<InternalDnsView>> _response;
|
||||||
|
private List<(string RequestPath, IQueryParameterFilter QueryFilter)> _callbacks;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Initialize()
|
||||||
|
{
|
||||||
|
_callbacks = [];
|
||||||
|
|
||||||
|
_response = new CloudflareResponse<IReadOnlyCollection<InternalDnsView>>
|
||||||
|
{
|
||||||
|
Success = true,
|
||||||
|
Messages = [
|
||||||
|
new ResponseInfo(1000, "Message 1")
|
||||||
|
],
|
||||||
|
Errors = [
|
||||||
|
new ResponseInfo(1000, "Error 1")
|
||||||
|
],
|
||||||
|
Result =
|
||||||
|
[
|
||||||
|
new InternalDnsView("023e105f4ecef8ad9ca31a8372d0c354", "View1"),
|
||||||
|
new InternalDnsView("023e105f4ecef8ad9ca31a8372d0c355", "View2")
|
||||||
|
]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldListInternalDnsViews()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.ListInternalDnsViews(AccountId);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(response);
|
||||||
|
Assert.IsTrue(response.Success);
|
||||||
|
Assert.IsNotNull(response.Result);
|
||||||
|
Assert.AreEqual(2, response.Result.Count);
|
||||||
|
|
||||||
|
Assert.AreEqual(1, _callbacks.Count);
|
||||||
|
|
||||||
|
var callback = _callbacks.First();
|
||||||
|
Assert.AreEqual($"/accounts/{AccountId}/dns_settings/views", callback.RequestPath);
|
||||||
|
Assert.IsNull(callback.QueryFilter);
|
||||||
|
|
||||||
|
_clientMock.Verify(m => m.GetAsync<IReadOnlyCollection<InternalDnsView>>(
|
||||||
|
$"/accounts/{AccountId}/dns_settings/views",
|
||||||
|
null,
|
||||||
|
It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
_clientMock.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldListInternalDnsViewsWithFilter()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var client = GetClient();
|
||||||
|
var filter = new ListInternalDnsViewsFilter
|
||||||
|
{
|
||||||
|
NameContains = "View"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.ListInternalDnsViews(AccountId, filter);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(response);
|
||||||
|
Assert.IsTrue(response.Success);
|
||||||
|
Assert.IsNotNull(response.Result);
|
||||||
|
|
||||||
|
Assert.AreEqual(1, _callbacks.Count);
|
||||||
|
|
||||||
|
var callback = _callbacks.First();
|
||||||
|
Assert.AreEqual($"/accounts/{AccountId}/dns_settings/views", callback.RequestPath);
|
||||||
|
Assert.AreEqual(filter, callback.QueryFilter);
|
||||||
|
|
||||||
|
_clientMock.Verify(m => m.GetAsync<IReadOnlyCollection<InternalDnsView>>(
|
||||||
|
$"/accounts/{AccountId}/dns_settings/views",
|
||||||
|
filter,
|
||||||
|
It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
_clientMock.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
|
#region QueryFilter
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldReturnEmptyParameterList()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new ListInternalDnsViewsFilter();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(0, dict.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public void ShouldReturnFullParameterList()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new ListInternalDnsViewsFilter
|
||||||
|
{
|
||||||
|
Direction = SortDirection.Descending,
|
||||||
|
Match = FilterMatchType.All,
|
||||||
|
NameContains = "view",
|
||||||
|
NameEndsWith = "end",
|
||||||
|
NameExact = "exactView",
|
||||||
|
NameStartsWith = "start",
|
||||||
|
OrderBy = InternalDnsViewsOrderBy.ModifiedOn,
|
||||||
|
Page = 2,
|
||||||
|
PerPage = 100,
|
||||||
|
ZoneId = "zone123",
|
||||||
|
ZoneName = "zone.example.com",
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(11, dict.Count);
|
||||||
|
|
||||||
|
Assert.AreEqual("desc", dict["direction"]);
|
||||||
|
Assert.AreEqual("all", dict["match"]);
|
||||||
|
Assert.AreEqual("view", dict["name.contains"]);
|
||||||
|
Assert.AreEqual("end", dict["name.endswith"]);
|
||||||
|
Assert.AreEqual("exactView", dict["name.exact"]);
|
||||||
|
Assert.AreEqual("start", dict["name.startswith"]);
|
||||||
|
Assert.AreEqual("modified_on", dict["order"]);
|
||||||
|
Assert.AreEqual("2", dict["page"]);
|
||||||
|
Assert.AreEqual("100", dict["per_page"]);
|
||||||
|
Assert.AreEqual("zone123", dict["zone_id"]);
|
||||||
|
Assert.AreEqual("zone.example.com", dict["zone_name"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DataTestMethod]
|
||||||
|
[DataRow(null)]
|
||||||
|
[DataRow("")]
|
||||||
|
[DataRow(" ")]
|
||||||
|
public void ShouldNotAddNameContains(string str)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new ListInternalDnsViewsFilter { NameContains = str };
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(0, dict.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DataTestMethod]
|
||||||
|
[DataRow(null)]
|
||||||
|
[DataRow("")]
|
||||||
|
[DataRow(" ")]
|
||||||
|
public void ShouldNotAddNameEndsWith(string str)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new ListInternalDnsViewsFilter { NameEndsWith = str };
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(0, dict.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DataTestMethod]
|
||||||
|
[DataRow(null)]
|
||||||
|
[DataRow("")]
|
||||||
|
[DataRow(" ")]
|
||||||
|
public void ShouldNotAddNameExact(string str)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new ListInternalDnsViewsFilter { NameExact = str };
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(0, dict.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DataTestMethod]
|
||||||
|
[DataRow(null)]
|
||||||
|
[DataRow("")]
|
||||||
|
[DataRow(" ")]
|
||||||
|
public void ShouldNotAddNameStartsWith(string str)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new ListInternalDnsViewsFilter { NameStartsWith = str };
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(0, dict.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DataTestMethod]
|
||||||
|
[DataRow(null)]
|
||||||
|
[DataRow((SortDirection)0)]
|
||||||
|
public void ShouldNotAddDirection(SortDirection? direction)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new ListInternalDnsViewsFilter { Direction = direction };
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(0, dict.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DataTestMethod]
|
||||||
|
[DataRow(null)]
|
||||||
|
[DataRow((FilterMatchType)0)]
|
||||||
|
public void ShouldNotAddMatch(FilterMatchType? match)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new ListInternalDnsViewsFilter { Match = match };
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(0, dict.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DataTestMethod]
|
||||||
|
[DataRow(null)]
|
||||||
|
[DataRow((InternalDnsViewsOrderBy)0)]
|
||||||
|
public void ShouldNotAddOrder(InternalDnsViewsOrderBy? order)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new ListInternalDnsViewsFilter { OrderBy = order };
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(0, dict.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DataTestMethod]
|
||||||
|
[DataRow(null)]
|
||||||
|
[DataRow(0)]
|
||||||
|
public void ShouldNotAddPage(int? page)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new ListInternalDnsViewsFilter { Page = page };
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(0, dict.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DataTestMethod]
|
||||||
|
[DataRow(null)]
|
||||||
|
[DataRow(0)]
|
||||||
|
[DataRow(5_000_001)]
|
||||||
|
public void ShouldNotAddPerPage(int? perPage)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new ListInternalDnsViewsFilter { PerPage = perPage };
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(0, dict.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DataTestMethod]
|
||||||
|
[DataRow(null)]
|
||||||
|
[DataRow("")]
|
||||||
|
[DataRow(" ")]
|
||||||
|
public void ShouldNotAddZoneId(string str)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new ListInternalDnsViewsFilter { ZoneId = str };
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(0, dict.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[DataTestMethod]
|
||||||
|
[DataRow(null)]
|
||||||
|
[DataRow("")]
|
||||||
|
[DataRow(" ")]
|
||||||
|
public void ShouldNotAddZoneName(string str)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var filter = new ListInternalDnsViewsFilter { ZoneName = str };
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var dict = filter.GetQueryParameters();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(dict);
|
||||||
|
Assert.AreEqual(0, dict.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion QueryFilter
|
||||||
|
|
||||||
|
private ICloudflareClient GetClient()
|
||||||
|
{
|
||||||
|
_clientMock = new Mock<ICloudflareClient>();
|
||||||
|
_clientMock
|
||||||
|
.Setup(m => m.GetAsync<IReadOnlyCollection<InternalDnsView>>(
|
||||||
|
It.IsAny<string>(),
|
||||||
|
It.IsAny<IQueryParameterFilter>(),
|
||||||
|
It.IsAny<CancellationToken>()))
|
||||||
|
.Callback<string, IQueryParameterFilter, CancellationToken>((requestPath, queryFilter, _) => _callbacks.Add((requestPath, queryFilter)))
|
||||||
|
.ReturnsAsync(() => _response);
|
||||||
|
|
||||||
|
return _clientMock.Object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,121 @@
|
|||||||
|
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.DnsAccountSettingsExtensions.Views
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class UpdateInternalDnsViewTest
|
||||||
|
{
|
||||||
|
private const string AccountId = "023e105f4ecef8ad9ca31a8372d0c353";
|
||||||
|
private const string ViewId = "023e105f4ecef8ad9ca31a8372d0c354";
|
||||||
|
private const string ViewName = "InternalView";
|
||||||
|
|
||||||
|
private Mock<ICloudflareClient> _clientMock;
|
||||||
|
private CloudflareResponse<InternalDnsView> _response;
|
||||||
|
private List<(string RequestPath, InternalModifyInternalDnsViewRequest Request)> _callbacks;
|
||||||
|
private UpdateInternalDnsViewRequest _request;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Initialize()
|
||||||
|
{
|
||||||
|
_callbacks = [];
|
||||||
|
|
||||||
|
_response = new CloudflareResponse<InternalDnsView>
|
||||||
|
{
|
||||||
|
Success = true,
|
||||||
|
Messages = [
|
||||||
|
new ResponseInfo(1000, "Message 1")
|
||||||
|
],
|
||||||
|
Errors = [
|
||||||
|
new ResponseInfo(1000, "Error 1")
|
||||||
|
],
|
||||||
|
Result = new InternalDnsView(ViewId, ViewName)
|
||||||
|
};
|
||||||
|
|
||||||
|
_request = new UpdateInternalDnsViewRequest(AccountId, ViewId, ViewName)
|
||||||
|
{
|
||||||
|
ZoneIds = ["zone1", "zone2"]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ShouldUpdateInternalDnsView()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.UpdateInternalDnsView(_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($"/accounts/{AccountId}/dns_settings/views/{ViewId}", callback.RequestPath);
|
||||||
|
Assert.IsNotNull(callback.Request);
|
||||||
|
|
||||||
|
Assert.AreEqual(ViewName, callback.Request.Name);
|
||||||
|
CollectionAssert.AreEqual(_request.ZoneIds.ToList(), callback.Request.Zones.ToList());
|
||||||
|
|
||||||
|
_clientMock.Verify(m => m.PatchAsync<InternalDnsView, InternalModifyInternalDnsViewRequest>(
|
||||||
|
$"/accounts/{AccountId}/dns_settings/views/{ViewId}",
|
||||||
|
It.IsAny<InternalModifyInternalDnsViewRequest>(),
|
||||||
|
It.IsAny<CancellationToken>()), Times.Once);
|
||||||
|
_clientMock.VerifyNoOtherCalls();
|
||||||
|
}
|
||||||
|
|
||||||
|
[DataTestMethod]
|
||||||
|
[DataRow(null)]
|
||||||
|
[DataRow("")]
|
||||||
|
[DataRow(" ")]
|
||||||
|
[ExpectedException(typeof(ArgumentNullException))]
|
||||||
|
public async Task ShouldThrowArgumentNullExceptionWhenNameIsNull(string name)
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_request.Name = name;
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.UpdateInternalDnsView(_request);
|
||||||
|
|
||||||
|
// Assert - ArgumentNullException
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
[ExpectedException(typeof(ArgumentOutOfRangeException))]
|
||||||
|
public async Task ShouldThrowArgumentOutOfRangeExceptionWhenNameTooLong()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
_request.Name = new string('a', 256);
|
||||||
|
var client = GetClient();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var response = await client.UpdateInternalDnsView(_request);
|
||||||
|
|
||||||
|
// Assert - ArgumentOutOfRangeException
|
||||||
|
}
|
||||||
|
|
||||||
|
private ICloudflareClient GetClient()
|
||||||
|
{
|
||||||
|
_clientMock = new Mock<ICloudflareClient>();
|
||||||
|
_clientMock
|
||||||
|
.Setup(m => m.PatchAsync<InternalDnsView, InternalModifyInternalDnsViewRequest>(
|
||||||
|
It.IsAny<string>(),
|
||||||
|
It.IsAny<InternalModifyInternalDnsViewRequest>(),
|
||||||
|
It.IsAny<CancellationToken>()))
|
||||||
|
.Callback<string, InternalModifyInternalDnsViewRequest, CancellationToken>((requestPath, request, _) => _callbacks.Add((requestPath, request)))
|
||||||
|
.ReturnsAsync(() => _response);
|
||||||
|
|
||||||
|
return _clientMock.Object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user