Skip to content

Commit

Permalink
feat(repo): add an extension method for DI easy injectionwhich takes …
Browse files Browse the repository at this point in the history
…an IOptions (UrlboxConfig)

chore(repo): split error into its own response file

refactor(repo): remove redundant webhookerror for generic UrlboxError

refactor(repo): place urlbox in main namespace, and make quality validated double

chore(repo): move di test into dir
  • Loading branch information
AJCJ1 authored and cjroebuck committed Jan 21, 2025
1 parent 13fbb50 commit 9ff0180
Show file tree
Hide file tree
Showing 20 changed files with 502 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UrlboxSDK;
using UrlboxSDK.Resource;

namespace UrlboxSDK.MsTest.DI;

[TestClass]
public class DependencyInjectionTests
Expand Down
169 changes: 169 additions & 0 deletions UrlboxSDK.MsTest/DI/Extension/UrlboxExtensionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UrlboxSDK.DI.Extension;
using UrlboxSDK.DI.Resource;

namespace UrlboxSDK.MsTest.DI.Extension
{
[TestClass]
public class UrlboxExtensionTest
{
/// <summary>
/// Tests registering the UrlboxConfig obj in Service Container as an IOptions
/// </summary>
[TestMethod]
public void AddUrlbox_RegistersUrlboxConfig()
{
ServiceCollection services = new();
string apiKey = "test-key";
string apiSecret = "test-secret";

services.AddUrlbox(options =>
{
options.Key = apiKey;
options.Secret = apiSecret;
options.WebhookSecret = "test-webhook";
options.BaseUrl = "https://test-urlbox.com";
});
ServiceProvider serviceProvider = services.BuildServiceProvider();

UrlboxConfig options = serviceProvider.GetRequiredService<IOptions<UrlboxConfig>>().Value;
Assert.AreEqual(apiKey, options.Key);
Assert.AreEqual(apiSecret, options.Secret);
Assert.AreEqual("test-webhook", options.WebhookSecret);
Assert.AreEqual("https://test-urlbox.com", options.BaseUrl);
}

/// <summary>
/// Tests that AddUrlbox adds an instance of Urlbox with the default lifetime
/// </summary>
[TestMethod]
public void AddUrlbox_RegistersUrlboxService()
{
ServiceCollection services = new();
string apiKey = "test-key";
string apiSecret = "test-secret";

services.AddUrlbox(options =>
{
options.Key = apiKey;
options.Secret = apiSecret;
options.WebhookSecret = "test-webhook";
options.BaseUrl = "https://test-urlbox.com";
});
ServiceProvider serviceProvider = services.BuildServiceProvider();

ServiceDescriptor descriptor = services.FirstOrDefault(serviceDescriptor => serviceDescriptor.ServiceType == typeof(IUrlbox));
Assert.IsNotNull(descriptor, "IUrlbox service is not registered.");
Assert.AreEqual(ServiceLifetime.Singleton, descriptor.Lifetime, "The registered lifetime is not the default Singleton.");

IUrlbox urlboxService = serviceProvider.GetRequiredService<IUrlbox>();
Assert.IsNotNull(urlboxService);
Assert.IsInstanceOfType(urlboxService, typeof(Urlbox));
}

[TestMethod]
public void AddUrlbox_CreatesUrlboxInstanceWithCorrectConfig()
{
ServiceCollection services = new();
string apiKey = "test-key";
string apiSecret = "test-secret";

services.AddUrlbox(options =>
{
options.Key = apiKey;
options.Secret = apiSecret;
options.WebhookSecret = "test-webhook";
options.BaseUrl = "https://test-urlbox.com";
});
ServiceProvider serviceProvider = services.BuildServiceProvider();

IUrlbox urlbox = (Urlbox)serviceProvider.GetRequiredService<IUrlbox>();

string jpgUrl = urlbox.GenerateJPEGUrl(Urlbox.Options(url: "https://urlbox.com").Build());

Assert.IsTrue(jpgUrl.Contains("test-key"));
}

[TestMethod]
public void AddUrlbox_RegistersAsSingleton()
{
ServiceCollection services = new();
string apiKey = "test-key";
string apiSecret = "test-secret";

services.AddUrlbox(options =>
{
options.Key = apiKey;
options.Secret = apiSecret;
}, ServiceLifetime.Singleton);
ServiceProvider serviceProvider = services.BuildServiceProvider();

ServiceDescriptor descriptor = services.FirstOrDefault(serviceDescriptor => serviceDescriptor.ServiceType == typeof(IUrlbox));
Assert.IsNotNull(descriptor, "IUrlbox service is not registered.");
Assert.AreEqual(ServiceLifetime.Singleton, descriptor.Lifetime, "The registered lifetime is not Singleton.");

IUrlbox instance1 = serviceProvider.GetRequiredService<IUrlbox>();
IUrlbox instance2 = serviceProvider.GetRequiredService<IUrlbox>();
Assert.AreSame(instance1, instance2); // Singleton instances should be the same
}

[TestMethod]
public void AddUrlbox_RegistersAsScoped()
{
ServiceCollection services = new();
string apiKey = "test-key";
string apiSecret = "test-secret";

services.AddUrlbox(options =>
{
options.Key = apiKey;
options.Secret = apiSecret;
}, ServiceLifetime.Scoped);
ServiceProvider serviceProvider = services.BuildServiceProvider();

ServiceDescriptor descriptor = services.FirstOrDefault(serviceDescriptor => serviceDescriptor.ServiceType == typeof(IUrlbox));
Assert.IsNotNull(descriptor, "IUrlbox service is not registered.");
Assert.AreEqual(ServiceLifetime.Scoped, descriptor.Lifetime, "The registered lifetime is not scoped.");

using (IServiceScope scope1 = serviceProvider.CreateScope())
{
IUrlbox instance1 = scope1.ServiceProvider.GetRequiredService<IUrlbox>();
IUrlbox instance2 = scope1.ServiceProvider.GetRequiredService<IUrlbox>();
Assert.AreSame(instance1, instance2); // Scoped instances within the same scope should be the same
}

using (IServiceScope scope2 = serviceProvider.CreateScope())
{
IUrlbox instance3 = scope2.ServiceProvider.GetRequiredService<IUrlbox>();
Assert.AreNotSame(instance3, serviceProvider.GetRequiredService<IUrlbox>()); // Scoped instances across scopes should differ
}
}

[TestMethod]
public void AddUrlbox_RegistersAsTransient()
{
ServiceCollection services = new();
string apiKey = "test-key";
string apiSecret = "test-secret";

services.AddUrlbox(options =>
{
options.Key = apiKey;
options.Secret = apiSecret;
}, ServiceLifetime.Transient);
ServiceProvider serviceProvider = services.BuildServiceProvider();

ServiceDescriptor descriptor = services.FirstOrDefault(serviceDescriptor => serviceDescriptor.ServiceType == typeof(IUrlbox));
Assert.IsNotNull(descriptor, "IUrlbox service is not registered.");
Assert.AreEqual(ServiceLifetime.Transient, descriptor.Lifetime, "The registered lifetime is not transient.");


var instance1 = serviceProvider.GetRequiredService<IUrlbox>();
var instance2 = serviceProvider.GetRequiredService<IUrlbox>();
Assert.AreNotSame(instance1, instance2); // Transient instances should always differ
}
}
}
80 changes: 80 additions & 0 deletions UrlboxSDK.MsTest/DI/Resource/UrlboxConfigTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UrlboxSDK.DI.Resource;

namespace UrlboxSDK.MsTest.DI.Resource
{
[TestClass]
public class UrlboxConfigTests
{
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void UrlboxConfig_ThrowsArgumentException_WhenKeyIsMissing()
{
UrlboxConfig config = new()
{
Secret = "valid-secret"
};

config.Validate();
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void UrlboxConfig_ThrowsArgumentException_WhenSecretIsMissing()
{
UrlboxConfig config = new()
{
Key = "valid-key"
};

config.Validate();
}

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

config.Validate();
}

[TestMethod]
public void UrlboxConfig_CreatesInstance_WhenWebhookSecretIsNotProvided()
{
UrlboxConfig config = new()
{
Key = "valid-key",
Secret = "valid-secret"
};

config.Validate();

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

[TestMethod]
public void UrlboxConfig_CreatesInstance_WhenWebhookSecretIsProvided()
{
UrlboxConfig config = new()
{
Key = "valid-key",
Secret = "valid-secret",
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using UrlboxSDK;
using UrlboxSDK.Options.Builder;
using UrlboxSDK.Options.Resource;
using UrlboxSDK.Resource;
using UrlboxSDK;

[TestClass]
public class UrlboxOptionsBuilderTests
Expand Down
49 changes: 46 additions & 3 deletions UrlboxSDK.MsTest/Options/Resource/UrlboxOptionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using UrlboxSDK;
using UrlboxSDK.Options.Resource;
using UrlboxSDK.Policy;
using UrlboxSDK.Resource;

[TestClass]
public class UrlboxOptionsTest
Expand Down Expand Up @@ -102,8 +101,8 @@ public void UrlboxOptions_CreatedWithPlatforms(
if (expectation == null)
{
Assert.ThrowsException<ArgumentException>(() => Urlbox.Options(url: "https://urlbox.com")
.Platform(platform)
.Build());
.Platform(platform)
.Build());
}
else
{
Expand All @@ -122,4 +121,48 @@ public void UrlboxOptions_CreatedWithPlatforms(
Assert.IsTrue(serialized.Contains(expectation));
}
}

[TestMethod]
public void Quality_ShouldThrowException_WhenOutOfRange()
{
var options = new UrlboxOptions(url: "https://urlbox.com");

Assert.ThrowsException<ArgumentOutOfRangeException>(() => options.Quality = -1, "Quality must be between 0 and 100.");
Assert.ThrowsException<ArgumentOutOfRangeException>(() => options.Quality = 101, "Quality must be between 0 and 100.");
}

[TestMethod]
public void Quality_ShouldAcceptValidValues()
{
var options = new UrlboxOptions(url: "https://urlbox.com");

options.Quality = 0;
Assert.AreEqual(0, options.Quality);
options.Quality = 50;
Assert.AreEqual(50, options.Quality);
options.Quality = 100;
Assert.AreEqual(100, options.Quality);
}

[TestMethod]
public void PdfScale_ShouldThrowException_WhenOutOfRange()
{
var options = new UrlboxOptions(url: "https://urlbox.com");

Assert.ThrowsException<ArgumentOutOfRangeException>(() => options.PdfScale = 0.09, "PdfScale must be between 0 and 100.");
Assert.ThrowsException<ArgumentOutOfRangeException>(() => options.PdfScale = 2.01, "PdfScale must be between 0 and 100.");
}

[TestMethod]
public void PdfScale_ShouldAcceptValidValues()
{
var options = new UrlboxOptions(url: "https://urlbox.com");

options.PdfScale = 0.1;
Assert.AreEqual(0.1, options.PdfScale);
options.PdfScale = 1.2;
Assert.AreEqual(1.2, options.PdfScale);
options.PdfScale = 2.0;
Assert.AreEqual(2.0, options.PdfScale);
}
}
2 changes: 1 addition & 1 deletion UrlboxSDK.MsTest/Resource/UrlboxBaseUrlTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UrlboxSDK;
using UrlboxSDK.Resource;
using UrlboxSDK;

[TestClass]
public class UrlboxRegionTest
Expand Down
4 changes: 1 addition & 3 deletions UrlboxSDK.MsTest/Resource/UrlboxTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using UrlboxSDK.Exception;
using UrlboxSDK.Resource;
using UrlboxSDK;
using UrlboxSDK.Options.Resource;
using UrlboxSDK.Response.Resource;
using UrlboxSDK.Factory;
Expand Down
10 changes: 1 addition & 9 deletions UrlboxSDK.MsTest/Webhook/Resource/UrlboxWebhookResponseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@
[TestClass]
public class UrlboxWebhookResponseTests
{
[TestMethod]
public void WebhookError_creates()
{
WebhookError error = new(message: "message");
Assert.IsInstanceOfType(error, typeof(WebhookError));
Assert.AreEqual("message", error.Message);
}

[TestMethod]
public void WebhookMeta_creates()
{
Expand Down Expand Up @@ -49,7 +41,7 @@ public void UrlboxWebhookResponse_CreatesMinGetters()
[TestMethod]
public void UrlboxWebhookResponse_CreatesMinGettersWithError()
{
WebhookError error = new(message: "message");
ErrorUrlboxResponse.UrlboxError error = new(message: "message", code: null, errors: null);
Meta meta = new(startTime: "START", endTime: "END");

UrlboxWebhookResponse webhookResponse = new(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UrlboxSDK;
using UrlboxSDK.Resource;
using UrlboxSDK.Webhook.Resource;

[TestClass]
Expand Down
Loading

0 comments on commit 9ff0180

Please sign in to comment.