Skip to content

Commit

Permalink
Add test for getting ShopifyDomainUtility from DI
Browse files Browse the repository at this point in the history
  • Loading branch information
nozzlegear committed Dec 17, 2023
1 parent 9cc270f commit 513fecf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
2 changes: 2 additions & 0 deletions ShopifySharp.Tests/ShopifySharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<PackageReference Include="fsharp.core" Version="4.7.0" />
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
Expand Down
6 changes: 6 additions & 0 deletions ShopifySharp.Tests/TestClasses/TestException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#nullable enable
using System;

namespace ShopifySharp.Tests.TestClasses;

public class TestException : Exception;
48 changes: 28 additions & 20 deletions ShopifySharp.Tests/Utilities/ShopifyOauthUtilityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
using System.Linq;
using FluentAssertions;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using ShopifySharp.Enums;
using ShopifySharp.Tests.TestClasses;
using ShopifySharp.Utilities;
using Xunit;

Expand All @@ -18,14 +21,7 @@ public class ShopifyOauthUtilityTests
private const string RedirectUrl = "https://example.com/app";
private const string ClientId = "some-client-id";

private IShopifyDomainUtility _shopifyDomainUtility;
private IShopifyOauthUtility _utility;

public ShopifyOauthUtilityTests()
{
_shopifyDomainUtility = Substitute.For<IShopifyDomainUtility>();
_utility = new ShopifyOauthUtility(_shopifyDomainUtility);
}
private ShopifyOauthUtility? _utility;

[Theory]
[InlineData(null, null)]
Expand All @@ -35,12 +31,9 @@ public ShopifyOauthUtilityTests()
public void BuildAuthorizationUrl_WhenGivenStringScopes_ReturnsTheUrl(string? state, string[]? grants)
{
// Setup
_utility = new ShopifyOauthUtility();
var scopes = new[] { "some-permission-1", "some-permission-2" };

// Mock
_shopifyDomainUtility.BuildShopDomainUri(ShopDomain)
.Returns(new Uri(Uri.UriSchemeHttps + Uri.SchemeDelimiter + ShopDomain, UriKind.Absolute));

// Act
var result = _utility.BuildAuthorizationUrl(scopes, ShopDomain, ClientId, RedirectUrl, state, grants);

Expand Down Expand Up @@ -73,13 +66,10 @@ public void BuildAuthorizationUrl_WhenGivenStringScopes_ReturnsTheUrl(string? st
public void Builds_Authorization_Urls_With_Enums(string? state, string[]? grants)
{
// Setup
_utility = new ShopifyOauthUtility();
string[] expectedEnumStrings = ["read_customers", "write_customers"];
AuthorizationScope[] scopes = [ AuthorizationScope.ReadCustomers, AuthorizationScope.WriteCustomers ];

// Mock
_shopifyDomainUtility.BuildShopDomainUri(ShopDomain)
.Returns(new Uri(Uri.UriSchemeHttps + Uri.SchemeDelimiter + ShopDomain, UriKind.Absolute));

// Act
var result = _utility.BuildAuthorizationUrl(scopes, ShopDomain, ClientId, RedirectUrl, state, grants);

Expand Down Expand Up @@ -113,12 +103,9 @@ public void Builds_Authorization_Urls_With_Enums(string? state, string[]? grants
public void BuildAuthorizationUrl_WhenGivenAuthorizationUrlOptions_ReturnsTheUrl(string? state, string[]? grants)
{
// Setup
_utility = new ShopifyOauthUtility();
var scopes = new[] { "some-permission-1", "some-permission-2" };

// Mock
_shopifyDomainUtility.BuildShopDomainUri(ShopDomain)
.Returns(new Uri(Uri.UriSchemeHttps + Uri.SchemeDelimiter + ShopDomain, UriKind.Absolute));

// Act
var result = _utility.BuildAuthorizationUrl(new AuthorizationUrlOptions
{
Expand Down Expand Up @@ -151,4 +138,25 @@ public void BuildAuthorizationUrl_WhenGivenAuthorizationUrlOptions_ReturnsTheUrl
result.Query.Should().NotContain("grant_options[]=");
}
#endif

[Fact]
public void ShopifyOauthUtility_ShouldUseDomainUtilityFromDependencyInjection()
{
// Setup
IServiceCollection container = new ServiceCollection();
var domainUtility = Substitute.For<IShopifyDomainUtility>();
container.AddSingleton(domainUtility);
container.AddSingleton<ShopifyOauthUtility>();

// Mock
domainUtility.BuildShopDomainUri(ShopDomain)
.Throws<TestException>();

// Act
_utility = container.BuildServiceProvider().GetService<ShopifyOauthUtility>()!;
var act = () => _utility.BuildAuthorizationUrl([ "some-scope" ], ShopDomain, ClientId, RedirectUrl, "some-state", [ "some-grant" ]);

// Assert
act.Should().Throw<TestException>();
}
}

0 comments on commit 513fecf

Please sign in to comment.