Skip to content

Commit

Permalink
Cleanup of passing in API base uri
Browse files Browse the repository at this point in the history
Cleanup of interfaces and adding xmldoc

Enforcing public on all properties on entities

Adding PATCH user using UpdateUserPayload
  • Loading branch information
Kencdk committed May 13, 2020
1 parent 211c66c commit 8e60e6b
Show file tree
Hide file tree
Showing 44 changed files with 465 additions and 369 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
namespace Kenc.Cloudflare.Core.IntegrationTests
{
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Kenc.Cloudflare.Core.Clients;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
Expand All @@ -15,8 +19,8 @@ public async Task GetDomain()
var domainId = TestContextSetting("domainId");
var domainName = TestContextSetting("domainName");

var client = CreateClient();
var domain = await client.Zones.ListAsync(domainName, Clients.Enums.ZoneStatus.Active);
CloudflareClient client = CreateClient();
IList<Entities.Zone> domain = await client.Zones.ListAsync(domainName, Clients.Enums.ZoneStatus.Active);
Assert.IsNotNull(domain);
Assert.AreEqual(domainId, domain[0].Id);
}
Expand All @@ -26,31 +30,36 @@ public async Task ListTxtRecords()
{
var domainId = TestContextSetting("domainId");

var client = CreateClient();
var dnsRecords = await client.Zones.DNSSettings.ListAsync(domainId, Clients.Enums.DNSRecordType.TXT);
CloudflareClient client = CreateClient();
Entities.EntityList<Entities.DNSRecord> dnsRecords = await client.Zones.DNSSettings.ListAsync(domainId, Clients.Enums.DNSRecordType.TXT);
Assert.IsNotNull(dnsRecords);
Assert.AreNotEqual(0, dnsRecords.Count);
}

[TestMethod]
public async Task CreateTextRecord()
{
var recordIdentifier = $"_intTest{System.DateTime.UtcNow.ToString("yyyymmddhhMM")}";
var recordIdentifier = $"_intTest{System.DateTime.UtcNow:yyyymmddhhMM}";
var domainId = TestContextSetting("domainId");

var client = CreateClient();
var record = await client.Zones.DNSSettings.CreateRecordAsync(domainId, recordIdentifier, Clients.Enums.DNSRecordType.TXT, recordIdentifier);
CloudflareClient client = CreateClient();
Entities.DNSRecord record = await client.Zones.DNSSettings.CreateRecordAsync(domainId, recordIdentifier, Clients.Enums.DNSRecordType.TXT, recordIdentifier);
Assert.IsNotNull(record);
Assert.AreEqual(recordIdentifier, record.Content);
}

private Clients.CloudflareClient CreateClient()
private CloudflareClient CreateClient()
{
var apiKey = TestContextSetting("cloudflareapikey");
var username = TestContextSetting("cloudflareusername");

var restFactory = new Clients.CloudflareRestClientFactory(Clients.CloudflareClient.V4Endpoint);
return new Clients.CloudflareClient(restFactory, username, apiKey, Clients.CloudflareClient.V4Endpoint);
var serviceCollection = new ServiceCollection();
serviceCollection.AddHttpClient();
var services = serviceCollection.BuildServiceProvider();
var httpClientFactory = services.GetRequiredService<IHttpClientFactory>();

var restClientFactory = new CloudflareRestClientFactory(httpClientFactory);
return new CloudflareClient(restClientFactory, username, apiKey, CloudflareAPIEndpoint.V4Endpoint);
}

private string TestContextSetting(string name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task CloudflareRestClient_ShouldUseHttpClientFactoryToCreateClientF
.Returns(httpClient);

var cloudflareRestClient = new CloudflareRestClient(httpClientFactory.Object, "username", "apikey");
var user = await cloudflareRestClient.GetAsync<User>(new System.Uri(CloudflareClient.V4Endpoint, "user"));
var user = await cloudflareRestClient.GetAsync<User>(new System.Uri(CloudflareAPIEndpoint.V4Endpoint, "user"));

// assert
httpClientFactory.Verify(x => x.CreateClient(It.IsAny<string>()), Times.Once, $"{nameof(CloudflareRestClient)} should have called httpClientFactory.CreateClient())");
Expand All @@ -58,7 +58,7 @@ public async Task CloudflareRestClient_ShouldThrowCloudflareException()
.Returns(httpClient);

var cloudflareRestClient = new CloudflareRestClient(httpClientFactory.Object, "username", "apikey");
var user = await cloudflareRestClient.GetAsync<User>(new System.Uri(CloudflareClient.V4Endpoint, "user"));
var user = await cloudflareRestClient.GetAsync<User>(new System.Uri(CloudflareAPIEndpoint.V4Endpoint, "user"));
}

[DataTestMethod]
Expand All @@ -84,10 +84,10 @@ public async Task CloudflareRestClient_ShouldThrowCloudflareExceptionRegardlessO
.Returns(httpClient);

var cloudflareRestClient = new CloudflareRestClient(httpClientFactory.Object, "username", "apikey");
var user = await cloudflareRestClient.GetAsync<User>(new System.Uri(CloudflareClient.V4Endpoint, "user"));
var user = await cloudflareRestClient.GetAsync<User>(new System.Uri(CloudflareAPIEndpoint.V4Endpoint, "user"));
}

private HttpResponseMessage CreateResponseMessage<TResponse>(HttpStatusCode statuscode, CloudflareResult<TResponse> response)
private HttpResponseMessage CreateResponseMessage<TResponse>(HttpStatusCode statuscode, CloudflareResult<TResponse> response) where TResponse : class, ICloudflareEntity
{
return new HttpResponseMessage()
{
Expand Down
12 changes: 0 additions & 12 deletions src/Libraries/Kenc.Cloudflare.Core.Tests/HttpClientFactory.cs

This file was deleted.

51 changes: 25 additions & 26 deletions src/Libraries/Kenc.Cloudflare.Core.Tests/ZoneClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task ZoneClient_GetCallsRestClient()
restClient.Setup(x => x.GetAsync<Zone>(It.IsAny<Uri>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(zone);

var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var zoneClient = new ZoneClient(restClient.Object, CloudflareAPIEndpoint.V4Endpoint);
var result = await zoneClient.GetAsync(zoneIdentifier);

// assert
Expand All @@ -46,7 +46,7 @@ public async Task ZoneClient_GetDoesntSwallowExceptions()
new CloudflareAPIError("1049", "<domain> is not a registered domain")
}));

var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var zoneClient = new ZoneClient(restClient.Object, CloudflareAPIEndpoint.V4Endpoint);
_ = await zoneClient.GetAsync(zoneIdentifier);
}

Expand All @@ -57,7 +57,7 @@ public async Task ZoneClient_GetDoesntSwallowExceptions()
public async Task ZoneClient_GetThrowsArgumentExceptionForInvalidIdentifierInputs(string identifier)
{
var restClient = new Mock<IRestClient>();
var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var zoneClient = new ZoneClient(restClient.Object, CloudflareAPIEndpoint.V4Endpoint);
_ = await zoneClient.GetAsync(identifier);
}

Expand All @@ -69,7 +69,7 @@ public async Task ZoneClient_ListCallsRestClient()
restClient.Setup(x => x.GetAsync<EntityList<Zone>>(It.IsAny<Uri>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(zone);

var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var zoneClient = new ZoneClient(restClient.Object, CloudflareAPIEndpoint.V4Endpoint);
var result = await zoneClient.ListAsync();

// assert
Expand All @@ -86,7 +86,7 @@ public async Task ZoneClient_ListPassesAppropriateParameters(string name, ZoneSt
restClient.Setup(x => x.GetAsync<EntityList<Zone>>(It.IsAny<Uri>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(zone);

var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var zoneClient = new ZoneClient(restClient.Object, CloudflareAPIEndpoint.V4Endpoint);
var result = await zoneClient.ListAsync(name, status, page, perPage, order, direction, match);

// assert
Expand All @@ -112,7 +112,7 @@ public async Task ZoneClient_ListDoesntSwallowExceptions()
new CloudflareAPIError("1049", "<domain> is not a registered domain")
}));

var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var zoneClient = new ZoneClient(restClient.Object, CloudflareAPIEndpoint.V4Endpoint);
_ = await zoneClient.ListAsync();
}

Expand All @@ -124,7 +124,7 @@ public async Task ZoneClient_CreateCallsRestClient()
restClient.Setup(x => x.PostAsync<CreateZonePayload, Zone>(It.IsAny<Uri>(), It.IsAny<CreateZonePayload>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(zone);

var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var zoneClient = new ZoneClient(restClient.Object, CloudflareAPIEndpoint.V4Endpoint);

var account = new Account
{
Expand All @@ -148,7 +148,7 @@ public async Task ZoneClient_CreateDoesntSwallowExceptions()
new CloudflareAPIError("1049", "<domain> is not a registered domain")
}));

var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var zoneClient = new ZoneClient(restClient.Object, CloudflareAPIEndpoint.V4Endpoint);

var account = new Account
{
Expand All @@ -169,7 +169,7 @@ public async Task ZoneClient_CreateDoesntSwallowExceptions()
public async Task ZoneClient_CreateThrowsArgumentExceptionForInvalidIdentifierInputs(string name, string accountId, string accountName)
{
var restClient = new Mock<IRestClient>();
var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var zoneClient = new ZoneClient(restClient.Object, CloudflareAPIEndpoint.V4Endpoint);

var account = new Account
{
Expand All @@ -187,7 +187,7 @@ public async Task ZoneClient_DeleteCallsRestClient()
restClient.Setup(x => x.DeleteAsync<IdResult>(It.IsAny<Uri>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(idResult);

var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var zoneClient = new ZoneClient(restClient.Object, CloudflareAPIEndpoint.V4Endpoint);

var identifier = "1235678";
var result = await zoneClient.DeleteAsync(identifier);
Expand All @@ -207,7 +207,7 @@ public async Task ZoneClient_DeleteDoesntSwallowExceptions()
new CloudflareAPIError("1001", "Invalid zone identifier")
}));

var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var zoneClient = new ZoneClient(restClient.Object, CloudflareAPIEndpoint.V4Endpoint);

var identifier = "1235678";
_ = await zoneClient.DeleteAsync(identifier);
Expand All @@ -220,7 +220,7 @@ public async Task ZoneClient_DeleteDoesntSwallowExceptions()
public async Task ZoneClient_DeleteThrowsArgumentExceptionForInvalidIdentifierInputs(string identifier)
{
var restClient = new Mock<IRestClient>();
var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var zoneClient = new ZoneClient(restClient.Object, CloudflareAPIEndpoint.V4Endpoint);
_ = await zoneClient.DeleteAsync(identifier);
}

Expand All @@ -229,18 +229,17 @@ public async Task ZoneClient_InitiateZoneActivationCheckCallsRestClient()
{
var idResult = new IdResult();
var restClient = new Mock<IRestClient>();
restClient.Setup(x => x.PutAsync<object, IdResult>(It.IsAny<Uri>(), null, It.IsAny<CancellationToken>()))
restClient.Setup(x => x.PutAsync<IdResult>(It.IsAny<Uri>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(idResult);

var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var zoneClient = new ZoneClient(restClient.Object, CloudflareAPIEndpoint.V4Endpoint);
var result = await zoneClient.InitiateZoneActivationCheckAsync(zoneIdentifier);

// assert
Assert.AreSame(idResult, result, "The returned zone object should have been passed through");
restClient.Verify(x =>
x.PutAsync<object, IdResult>(
x.PutAsync<IdResult>(
It.Is<Uri>(y => y.PathAndQuery == $"/client/v4/zones/{zoneIdentifier}/activation_check"),
null,
It.IsAny<CancellationToken>()),
Times.Once,
"Put should have been called on the REST client.");
Expand All @@ -251,12 +250,12 @@ public async Task ZoneClient_InitiateZoneActivationCheckCallsRestClient()
public async Task ZoneClient_InitiateZoneActivationCheckDoesntSwallowExceptions()
{
var restClient = new Mock<IRestClient>();
restClient.Setup(x => x.PutAsync<object, IdResult>(It.IsAny<Uri>(), null, It.IsAny<CancellationToken>()))
restClient.Setup(x => x.PutAsync<IdResult>(It.IsAny<Uri>(), It.IsAny<CancellationToken>()))
.ThrowsAsync(new CloudflareException(new List<CloudflareAPIError> {
new CloudflareAPIError("1049", "<domain> is not a registered domain")
}));

var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var zoneClient = new ZoneClient(restClient.Object, CloudflareAPIEndpoint.V4Endpoint);
_ = await zoneClient.InitiateZoneActivationCheckAsync(zoneIdentifier);
}

Expand All @@ -267,7 +266,7 @@ public async Task ZoneClient_InitiateZoneActivationCheckDoesntSwallowExceptions(
public async Task ZoneClient_InitiateZoneActivationCheckThrowsArgumentExceptionForInvalidIdentifierInputs(string identifier)
{
var restClient = new Mock<IRestClient>();
var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var zoneClient = new ZoneClient(restClient.Object, CloudflareAPIEndpoint.V4Endpoint);
_ = await zoneClient.InitiateZoneActivationCheckAsync(identifier);
}

Expand All @@ -283,7 +282,7 @@ public async Task ZoneClient_PurgeAllFilesCallsRestClient(bool purgeAll)
It.IsAny<Uri>(), It.IsAny<PurgeCachePayload>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(idResult);

var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var zoneClient = new ZoneClient(restClient.Object, CloudflareAPIEndpoint.V4Endpoint);
var result = await zoneClient.PurgeAllFiles(zoneIdentifier, purgeAll);

// assert
Expand All @@ -309,7 +308,7 @@ public async Task ZoneClient_PurgeAllFilesDoesntSwallowExceptions()
new CloudflareAPIError("1049", "<domain> is not a registered domain")
}));

var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var zoneClient = new ZoneClient(restClient.Object, CloudflareAPIEndpoint.V4Endpoint);
_ = await zoneClient.PurgeAllFiles(zoneIdentifier, true);
}

Expand All @@ -320,7 +319,7 @@ public async Task ZoneClient_PurgeAllFilesDoesntSwallowExceptions()
public async Task ZoneClient_PurgeAllFilesThrowsArgumentExceptionForInvalidIdentifierInputs(string identifier)
{
var restClient = new Mock<IRestClient>();
var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var zoneClient = new ZoneClient(restClient.Object, CloudflareAPIEndpoint.V4Endpoint);
_ = await zoneClient.PurgeAllFiles(identifier, true);
}

Expand All @@ -335,7 +334,7 @@ public async Task ZoneClient_PurgeFilesByTagsOrHostsCallsRestClient(string[] tag
It.IsAny<Uri>(), It.IsAny<PurgeFilesByTagsOrHostsPayload>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(idResult);

var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var zoneClient = new ZoneClient(restClient.Object, CloudflareAPIEndpoint.V4Endpoint);
var result = await zoneClient.PurgeFilesByTagsOrHosts(zoneIdentifier, tags, hosts);

// assert
Expand Down Expand Up @@ -368,7 +367,7 @@ public async Task ZoneClient_PurgeFilesByTagsOrHostsDoesntSwallowExceptions()
new CloudflareAPIError("1049", "<domain> is not a registered domain")
}));

var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var zoneClient = new ZoneClient(restClient.Object, CloudflareAPIEndpoint.V4Endpoint);
_ = await zoneClient.PurgeFilesByTagsOrHosts(zoneIdentifier, new string[] { "some-tag" }, new string[] { "example.invalid" });
}

Expand All @@ -377,7 +376,7 @@ public async Task ZoneClient_PurgeFilesByTagsOrHostsDoesntSwallowExceptions()
public async Task ZoneClient_PurgeFilesByTagsOrHostsThrowsArgumentExceptionForInvalidIdentifierInputs()
{
var restClient = new Mock<IRestClient>();
var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var zoneClient = new ZoneClient(restClient.Object, CloudflareAPIEndpoint.V4Endpoint);
_ = await zoneClient.PurgeFilesByTagsOrHosts(string.Empty, new string[] { "tags" }, new string[] { "hosts" });
}

Expand All @@ -387,7 +386,7 @@ public async Task ZoneClient_PurgeFilesByTagsOrHostsThrowsArgumentExceptionForIn
public async Task ZoneClient_PurgeFilesByTagsOrHostsThrowsArgumentExceptionForInvalidInputs(string[] tags, string[] hosts)
{
var restClient = new Mock<IRestClient>();
var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var zoneClient = new ZoneClient(restClient.Object, CloudflareAPIEndpoint.V4Endpoint);
_ = await zoneClient.PurgeFilesByTagsOrHosts(zoneIdentifier, tags, hosts);
}

Expand Down
Loading

0 comments on commit 8e60e6b

Please sign in to comment.