Skip to content

Commit

Permalink
Merge pull request #5 from Kencdk/kenchr/wip
Browse files Browse the repository at this point in the history
Updated IZoneDNSClient with Update and PATCH calls
Added RestClientFactory
Upgraded nuget packages.

Minor changes here and there.
  • Loading branch information
Kencdk authored May 10, 2020
2 parents 1348a5d + 47f3515 commit 211c66c
Show file tree
Hide file tree
Showing 20 changed files with 324 additions and 119 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Kenc.Cloudflare.Core.IntegrationTests.cs
namespace Kenc.Cloudflare.Core.IntegrationTests
{
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -55,9 +55,9 @@ private Clients.CloudflareClient CreateClient()

private string TestContextSetting(string name)
{
if (TestContext.Properties.TryGetValue(name, out object value))
if (TestContext.Properties.Contains(name))
{
return (string)value;
return (string)TestContext.Properties[name];
}

return System.Environment.GetEnvironmentVariable(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.3.2" />
<PackageReference Include="MSTest.TestFramework" Version="1.3.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.1" />
</ItemGroup>

<ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions src/Libraries/Kenc.Cloudflare.Core.Tests/HttpClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Kenc.Cloudflare.Core.Tests
{
using System.Net.Http;

class MyHttpClientFactory : IHttpClientFactory
{
public HttpClient CreateClient(string name)
{
return HttpClientFactory.Create();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="Moq" Version="4.10.1" />
<PackageReference Include="MSTest.TestAdapter" Version="1.3.2" />
<PackageReference Include="MSTest.TestFramework" Version="1.3.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="Moq" Version="4.14.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.1" />
</ItemGroup>

<ItemGroup>
Expand Down
35 changes: 18 additions & 17 deletions src/Libraries/Kenc.Cloudflare.Core.Tests/ZoneClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task ZoneClient_GetDoesntSwallowExceptions()
}));

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

[DataTestMethod]
Expand All @@ -58,7 +58,7 @@ public async Task ZoneClient_GetThrowsArgumentExceptionForInvalidIdentifierInput
{
var restClient = new Mock<IRestClient>();
var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var result = await zoneClient.GetAsync(identifier);
_ = await zoneClient.GetAsync(identifier);
}

[TestMethod]
Expand Down Expand Up @@ -113,7 +113,7 @@ public async Task ZoneClient_ListDoesntSwallowExceptions()
}));

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

[TestMethod]
Expand Down Expand Up @@ -155,7 +155,7 @@ public async Task ZoneClient_CreateDoesntSwallowExceptions()
Id = "01a7362d577a6c3019a474fd6f485823",
Name = "Demo Account"
};
var result = await zoneClient.CreateAsync("example.invalid", account);
_ = await zoneClient.CreateAsync("example.invalid", account);
}

[DataTestMethod]
Expand All @@ -176,7 +176,7 @@ public async Task ZoneClient_CreateThrowsArgumentExceptionForInvalidIdentifierIn
Id = accountId,
Name = accountName
};
var result = await zoneClient.CreateAsync(name, account);
_ = await zoneClient.CreateAsync(name, account);
}

[TestMethod]
Expand Down Expand Up @@ -210,7 +210,7 @@ public async Task ZoneClient_DeleteDoesntSwallowExceptions()
var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);

var identifier = "1235678";
var result = await zoneClient.DeleteAsync(identifier);
_ = await zoneClient.DeleteAsync(identifier);
}

[DataTestMethod]
Expand All @@ -221,15 +221,15 @@ public async Task ZoneClient_DeleteThrowsArgumentExceptionForInvalidIdentifierIn
{
var restClient = new Mock<IRestClient>();
var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var result = await zoneClient.DeleteAsync(identifier);
_ = await zoneClient.DeleteAsync(identifier);
}

[TestMethod]
public async Task ZoneClient_InitiateZoneActivationCheckCallsRestClient()
{
var idResult = new IdResult();
var restClient = new Mock<IRestClient>();
restClient.Setup(x => x.PutAsync<IdResult>(It.IsAny<Uri>(), It.IsAny<CancellationToken>()))
restClient.Setup(x => x.PutAsync<object, IdResult>(It.IsAny<Uri>(), null, It.IsAny<CancellationToken>()))
.ReturnsAsync(idResult);

var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
Expand All @@ -238,8 +238,9 @@ public async Task ZoneClient_InitiateZoneActivationCheckCallsRestClient()
// assert
Assert.AreSame(idResult, result, "The returned zone object should have been passed through");
restClient.Verify(x =>
x.PutAsync<IdResult>(
x.PutAsync<object, 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 @@ -250,13 +251,13 @@ public async Task ZoneClient_InitiateZoneActivationCheckCallsRestClient()
public async Task ZoneClient_InitiateZoneActivationCheckDoesntSwallowExceptions()
{
var restClient = new Mock<IRestClient>();
restClient.Setup(x => x.PutAsync<IdResult>(It.IsAny<Uri>(), It.IsAny<CancellationToken>()))
restClient.Setup(x => x.PutAsync<object, IdResult>(It.IsAny<Uri>(), null, 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 result = await zoneClient.InitiateZoneActivationCheckAsync(zoneIdentifier);
_ = await zoneClient.InitiateZoneActivationCheckAsync(zoneIdentifier);
}

[DataTestMethod]
Expand All @@ -267,7 +268,7 @@ public async Task ZoneClient_InitiateZoneActivationCheckThrowsArgumentExceptionF
{
var restClient = new Mock<IRestClient>();
var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var result = await zoneClient.InitiateZoneActivationCheckAsync(identifier);
_ = await zoneClient.InitiateZoneActivationCheckAsync(identifier);
}

[DataTestMethod]
Expand Down Expand Up @@ -309,7 +310,7 @@ public async Task ZoneClient_PurgeAllFilesDoesntSwallowExceptions()
}));

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

[DataTestMethod]
Expand All @@ -320,7 +321,7 @@ public async Task ZoneClient_PurgeAllFilesThrowsArgumentExceptionForInvalidIdent
{
var restClient = new Mock<IRestClient>();
var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var result = await zoneClient.PurgeAllFiles(identifier, true);
_ = await zoneClient.PurgeAllFiles(identifier, true);
}

[DataTestMethod]
Expand Down Expand Up @@ -368,7 +369,7 @@ public async Task ZoneClient_PurgeFilesByTagsOrHostsDoesntSwallowExceptions()
}));

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

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

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

public static IEnumerable<object[]> ZoneClient_PurgeFilesByTagsOrHostsThrowsArgumentExceptionForInvalidInputs_Data()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task ZoneDNSSettingsClient_GetDoesntSwallowExceptions()
}));

var client = new ZoneDNSSettingsClient(restClient.Object, CloudflareClient.V4Endpoint);
var result = await client.GetAsync(zoneIdentifier, "domain.invalid");
_ = await client.GetAsync(zoneIdentifier, "domain.invalid");
}

[DataTestMethod]
Expand All @@ -60,7 +60,7 @@ public async Task ZoneDNSSettingsClient_GetThrowsArgumentExceptionForInvalidInpu
{
var restClient = new Mock<IRestClient>();
var client = new ZoneDNSSettingsClient(restClient.Object, CloudflareClient.V4Endpoint);
var result = await client.GetAsync(identifier, name);
_ = await client.GetAsync(identifier, name);
}

[TestMethod]
Expand Down Expand Up @@ -125,7 +125,7 @@ public async Task ZoneDNSSettingsClient_ListDoesntSwallowExceptions()
}));

var zoneClient = new ZoneClient(restClient.Object, CloudflareClient.V4Endpoint);
var result = await zoneClient.ListAsync();
_ = await zoneClient.ListAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ public CloudflareClient(ICloudflareRestClientFactory restClientFactory, string u

Zones = new ZoneClient(restClient, endpoint);
UserClient = new UserClient(restClient, endpoint);
ZoneDNSSettingsClient = new ZoneDNSSettingsClient(restClient, endpoint);
}

public IZoneClient Zones { get; private set; }

public IUserClient UserClient { get; private set; }

public IZoneDNSSettingsClient ZoneDNSSettingsClient { get; private set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace Kenc.Cloudflare.Core.Clients
{
using System;

public class CloudflareClientFactory : ICloudflareClientFactory
{
private string username;
private string apiKey;
private ICloudflareRestClientFactory restClientFactory;
private Uri endpoint;

public CloudflareClientFactory()
{
}

public ICloudflareClient Create()
{
return new CloudflareClient(restClientFactory, username, apiKey, endpoint);
}

public ICloudflareClientFactory WithUsername(string username)
{
this.username = username;
return this;
}

public ICloudflareClientFactory WithAPIKey(string apiKey)
{
this.apiKey = apiKey;
return this;
}

public ICloudflareClientFactory WithEndpoint(Uri endpoint)
{
this.endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint));
return this;
}

public ICloudflareClientFactory WithRestClientFactory(ICloudflareRestClientFactory cloudflareRestClientFactory)
{
this.restClientFactory = cloudflareRestClientFactory;
return this;
}
}
}
Loading

0 comments on commit 211c66c

Please sign in to comment.