Skip to content

Commit

Permalink
refactor(repo): repo uses config obj to extract configuration logic f…
Browse files Browse the repository at this point in the history
…rom main class
  • Loading branch information
AJCJ1 committed Dec 27, 2024
1 parent 1ec7023 commit 8ed5634
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 132 deletions.
2 changes: 1 addition & 1 deletion UrlboxSDK.MsTest/DI/Extension/UrlboxExtensionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UrlboxSDK.Config.Resource;
using UrlboxSDK.DI.Extension;
using UrlboxSDK.DI.Resource;

namespace UrlboxSDK.MsTest.DI.Extension
{
Expand Down
36 changes: 22 additions & 14 deletions UrlboxSDK.MsTest/DI/Resource/UrlboxConfigTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UrlboxSDK.DI.Resource;
using UrlboxSDK.Config.Resource;

namespace UrlboxSDK.MsTest.DI.Resource
{
Expand Down Expand Up @@ -31,15 +31,6 @@ public void UrlboxConfig_ThrowsArgumentException_WhenSecretIsMissing()
config.Validate();
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void UrlboxConfig_ThrowsArgumentException_WhenBothKeyAndSecretAreMissing()
{
UrlboxConfig config = new();

config.Validate();
}

[TestMethod]
public void UrlboxConfig_CreatesInstance_WhenWebhookSecretIsNotProvided()
{
Expand All @@ -49,8 +40,6 @@ public void UrlboxConfig_CreatesInstance_WhenWebhookSecretIsNotProvided()
Secret = "valid-secret"
};

config.Validate();

Assert.IsNotNull(config);
Assert.AreEqual("valid-key", config.Key);
Assert.AreEqual("valid-secret", config.Secret);
Expand All @@ -68,13 +57,32 @@ public void UrlboxConfig_CreatesInstance_WhenWebhookSecretIsProvided()
WebhookSecret = "webhook-secret"
};

config.Validate();

Assert.IsNotNull(config);
Assert.AreEqual("valid-key", config.Key);
Assert.AreEqual("valid-secret", config.Secret);
Assert.AreEqual("webhook-secret", config.WebhookSecret);
Assert.AreEqual(Urlbox.BASE_URL, config.BaseUrl);
Assert.IsTrue(config.ThrowOnUrlboxError);
}

[TestMethod]
public void UrlboxConfig_CreatesInstance_BaseUrl_andThrowsSet()
{
UrlboxConfig config = new()
{
Key = "valid-key",
Secret = "valid-secret",
WebhookSecret = "webhook-secret",
BaseUrl = "https://example.com",
ThrowOnUrlboxError = false
};

Assert.IsNotNull(config);
Assert.AreEqual("valid-key", config.Key);
Assert.AreEqual("valid-secret", config.Secret);
Assert.AreEqual("webhook-secret", config.WebhookSecret);
Assert.AreEqual("https://example.com", config.BaseUrl);
Assert.IsFalse(config.ThrowOnUrlboxError);
}
}
}
29 changes: 20 additions & 9 deletions UrlboxSDK.MsTest/Factory/UrlboxFactoryTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UrlboxSDK.Config.Resource;
using UrlboxSDK.Factory;

namespace UrlboxSDK.MSTest.Factory
Expand All @@ -21,7 +22,12 @@ public void Create_ShouldReturnInstanceOfIUrlbox()
string key = "test-key";
string secret = "test-secret";

var result = factory.Create(key, secret);
UrlboxConfig config = new()
{
Key = key,
Secret = secret
};
var result = factory.Create(config);

Assert.IsNotNull(result);
Assert.IsInstanceOfType(result, typeof(IUrlbox));
Expand All @@ -33,25 +39,30 @@ public void Create_WithWebhookSecret_ShouldReturnValidInstance()
string key = "test-key";
string secret = "test-secret";
string webhookSecret = "test-webhook-secret";

var result = factory.Create(key, secret, webhookSecret);
UrlboxConfig config = new()
{
Key = key,
Secret = secret,
WebhookSecret = webhookSecret
};
var result = factory.Create(config);

Assert.IsNotNull(result);
Assert.IsInstanceOfType(result, typeof(IUrlbox));
}


[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Create_ShouldThrowException_WhenKeyIsNull()
public void FromCredentials_Success()
{
factory.Create(null!, "test-secret");
Urlbox urlbox = UrlboxFactory.FromCredentials("test_key", "test_secret", "test_webhook");
Assert.IsInstanceOfType(urlbox, typeof(Urlbox));
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void Create_ShouldThrowException_WhenSecretIsNull()
public void FromCredentials_Exception()
{
factory.Create("test-key", null!);
Assert.ThrowsException<ArgumentException>(() => UrlboxFactory.FromCredentials("", "", ""));
}
}
}
21 changes: 15 additions & 6 deletions UrlboxSDK.MsTest/Resource/UrlboxBaseUrlTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UrlboxSDK.Config.Resource;
using UrlboxSDK.Factory;

namespace UrlboxSDK.MsTest.Resource;

Expand All @@ -8,18 +10,25 @@ public class UrlboxRegionTest
[TestMethod]
public void Baseurl_NotSet()
{
Urlbox fromCredentials = Urlbox.FromCredentials("MY_API_KEY", "secret", "webhook_secret");
Urlbox fromCredentials = UrlboxFactory.FromCredentials("MY_API_KEY", "secret", "webhook_secret");
Assert.IsInstanceOfType(fromCredentials, typeof(Urlbox));
Urlbox fromNew = Urlbox.FromCredentials("MY_API_KEY", "secret", "webhook_secret");
Urlbox fromNew = UrlboxFactory.FromCredentials("MY_API_KEY", "secret", "webhook_secret");
Assert.IsInstanceOfType(fromNew, typeof(Urlbox));
}

[TestMethod]
public void Baseurl_included()
{
Urlbox fromCredentials = Urlbox.FromCredentials("MY_API_KEY", "secret", "webhook_secret", "someBaseUrl");
Assert.IsInstanceOfType(fromCredentials, typeof(Urlbox));
Urlbox fromNew = Urlbox.FromCredentials("MY_API_KEY", "secret", "webhook_secret", "someBaseUrl");
Assert.IsInstanceOfType(fromNew, typeof(Urlbox));
UrlboxFactory factory = new();
UrlboxConfig config = new()
{
Key = "test-key",
Secret = "test-secret",
WebhookSecret = "test-webhook",
BaseUrl = "https://test-urlbox.com",
ThrowOnUrlboxError = false
};
IUrlbox urlbox = factory.Create(config);
Assert.IsInstanceOfType(urlbox, typeof(Urlbox));
}
}
38 changes: 11 additions & 27 deletions UrlboxSDK.MsTest/UrlboxTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Collections.Generic;
using UrlboxSDK.Exception;
using UrlboxSDK.MsTest.Utils;
using UrlboxSDK.Config.Resource;

namespace UrlboxSDK.MsTest;

Expand Down Expand Up @@ -134,28 +135,15 @@ public void TestInitialize()
{
client = new MockHttpClientFixture();
renderLinkFactory = new RenderLinkFactory("MY_API_KEY", "secret");
urlbox = new(key: "MY_API_KEY", secret: "secret", webhookSecret: "webhook_secret", renderLinkFactory: renderLinkFactory, httpClient: client.HttpClient);
}

[TestMethod]
public void FromCredentials_Success()
{
Urlbox urlbox = Urlbox.FromCredentials("test_key", "test_secret", "test_webhook");
Assert.IsInstanceOfType(urlbox, typeof(Urlbox));
}

[TestMethod]
public void FromCredentials_Exception()
{
Assert.ThrowsException<ArgumentException>(() => Urlbox.FromCredentials("", "", ""));
UrlboxConfig config = new()
{
Key = "MY_API_KEY",
Secret = "secret",
WebhookSecret = "webhook_secret",
};
urlbox = new(config, renderLinkFactory: renderLinkFactory, httpClient: client.HttpClient);
}

[TestMethod]
public void WithBaseUrl_Exception()
{
Urlbox urlbox = Urlbox.FromCredentials("test_key", "test_secret", "test_webhook", baseUrl: "TEST");
Assert.IsInstanceOfType(urlbox, typeof(Urlbox));
}

[TestMethod]
public void GenerateRenderLink_WithAllOptions()
Expand Down Expand Up @@ -305,7 +293,7 @@ public void GenerateRenderLink_IgnoreEmptyValuesAndFormat()
[TestMethod]
public void GenerateSignedRenderLink_Succeeds()
{
UrlboxOptions options = new(url: "https://bbc.co.uk");
UrlboxOptions options = Urlbox.Options(url: "https://bbc.co.uk").Build();
options.Format = Format.Jpeg;
string output = urlbox.GenerateSignedRenderLink(options);
Assert.AreEqual("https://api.urlbox.com/v1/MY_API_KEY/8e00ad9a8d7c4abcd462a9b8ec041c3661f13995/jpeg?url=https%3A%2F%2Fbbc.co.uk", output, "Not OK!");
Expand All @@ -314,19 +302,15 @@ public void GenerateSignedRenderLink_Succeeds()
[TestMethod]
public void GenerateRenderLink_FormatWorks()
{
UrlboxOptions options = new(url: "https://bbc.co.uk");
options.Format = Format.Avif;
UrlboxOptions options = Urlbox.Options(url: "https://bbc.co.uk").Format(Format.Avif).Build();
string output = urlbox.GenerateRenderLink(options);
Assert.AreEqual("https://api.urlbox.com/v1/MY_API_KEY/8e00ad9a8d7c4abcd462a9b8ec041c3661f13995/avif?url=https%3A%2F%2Fbbc.co.uk", output, "Not OK!");
}

[TestMethod]
public void GenerateRenderLink_WithHtml()
{
UrlboxOptions options = new(html: "<h1>test</h1>")
{
FullPage = true
};
UrlboxOptions options = Urlbox.Options(html: "<h1>test</h1>").FullPage().Build();
string output = urlbox.GenerateRenderLink(options);

Assert.AreEqual("https://api.urlbox.com/v1/MY_API_KEY/931010e45a7936be4a6bc208e4ef0675fd216832/png?full_page=true&html=%3Ch1%3Etest%3C%2Fh1%3E", output);
Expand Down
17 changes: 14 additions & 3 deletions UrlboxSDK.MsTest/Webhook/Validator/UrlboxWebhookValidatorTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UrlboxSDK.Config.Resource;
using UrlboxSDK.Webhook.Resource;

namespace UrlboxSDK.MsTest.Webhook.Validator;
Expand All @@ -12,7 +13,13 @@ public class UrlboxWebhookValidatorTests
[TestInitialize]
public void TestInitialize()
{
urlbox = new Urlbox("key", "secret", "webhook_secret");
UrlboxConfig config = new()
{
Key = "key",
Secret = "secret",
WebhookSecret = "webhook_secret"
};
urlbox = new Urlbox(config);
}

[TestMethod]
Expand Down Expand Up @@ -53,7 +60,6 @@ public void VerifyWebhookSignature_FailsNoSha()
[TestMethod]
public void Urlbox_createsWithWebhookValidator()
{
Urlbox urlbox = new("key", "secret", "webhook");
// Shar of 'content' should not match 321, but method should run if 'webhook' passed.
System.Exception result = Assert.ThrowsException<System.Exception>(() => urlbox.VerifyWebhookSignature("t=123,sha256=321", "content"));

Expand All @@ -66,7 +72,12 @@ public void Urlbox_createsWithWebhookValidator()
[TestMethod]
public void Urlbox_throwsWhenWithoutWebhookValidator()
{
Urlbox urlbox = new("key", "secret");
UrlboxConfig config = new()
{
Key = "key",
Secret = "secret"
};
urlbox = new Urlbox(config);
// Should throw bc no webhook set so no validator instance
ArgumentException result = Assert.ThrowsException<ArgumentException>(() => urlbox.VerifyWebhookSignature("t=123,sha256=321", "content"));
Assert.AreEqual(result.Message, "Please set your webhook secret in the Urlbox instance before calling this method.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using UrlboxSDK;

namespace UrlboxSDK.DI.Resource;
namespace UrlboxSDK.Config.Resource;

/// <summary>
/// Represents the config settings for Urlbox, specifically for DI.
Expand All @@ -13,21 +11,21 @@ public class UrlboxConfig
public string? Secret { get; set; }
public string? WebhookSecret { get; set; }
public string BaseUrl { get; set; } = Urlbox.BASE_URL;
public bool ThrowOnUrlboxError { get; set; } = true;

/// <summary>
/// Allows for parameterless construction of UrlboxConfig while still validating presence of key/secret
/// </summary>
/// <exception cref="ArgumentException"></exception>
public void Validate()
{
if (string.IsNullOrWhiteSpace(Key))
if (string.IsNullOrEmpty(Key))
{
throw new ArgumentException("UrlboxConfig.Key is required and cannot be null or empty.");
throw new ArgumentException("Please provide your Urlbox.com API Key");
}

if (string.IsNullOrWhiteSpace(Secret))
if (string.IsNullOrEmpty(Secret))
{
throw new ArgumentException("UrlboxConfig.Secret is required and cannot be null or empty.");
throw new ArgumentException("Please provide your Urlbox.com API Secret");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using UrlboxSDK.DI.Resource;
using UrlboxSDK.Config.Resource;

namespace UrlboxSDK.DI.Extension;
/// <summary>
Expand All @@ -26,11 +26,9 @@ public static IServiceCollection AddUrlbox(
// Register Urlbox service with lifetime
services.Add(new ServiceDescriptor(typeof(IUrlbox), serviceProvider =>
{
UrlboxConfig options = serviceProvider.GetRequiredService<IOptions<UrlboxConfig>>().Value;

options.Validate();

return new Urlbox(options.Key!, options.Secret!, options.WebhookSecret, options.BaseUrl);
UrlboxConfig config = serviceProvider.GetRequiredService<IOptions<UrlboxConfig>>().Value;
config.Validate();
return new Urlbox(config);
}, lifetime));
return services;
}
Expand Down
12 changes: 3 additions & 9 deletions UrlboxSDK/Factory/IUrlboxFactory.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
using UrlboxSDK.Config.Resource;

namespace UrlboxSDK.Factory;

public interface IUrlboxFactory
{
IUrlbox Create(string key, string secret, string? webhookSecret = null);
}

public class UrlboxFactory : IUrlboxFactory
{
public IUrlbox Create(string key, string secret, string? webhookSecret = null)
{
return new Urlbox(key, secret, webhookSecret);
}
IUrlbox Create(UrlboxConfig config);
}
Loading

0 comments on commit 8ed5634

Please sign in to comment.