-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(repo): add an extension method for DI easy injectionwhich takes …
…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
Showing
20 changed files
with
502 additions
and
91 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
UrlboxSDK.MsTest/DependencyInjectionTest.cs → ...xSDK.MsTest/DI/DependencyInjectionTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
UrlboxSDK.MsTest/Webhook/Validator/UrlboxWebhookValidatorTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.