Skip to content

Commit

Permalink
fix(ODataClientFactory): adjustment to validation of existence of the…
Browse files Browse the repository at this point in the history
… authentication token in the context service
  • Loading branch information
lucianareginalino committed Jul 12, 2024
1 parent 8f9c4b6 commit f7c7473
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/Liquid.Repository.OData/Liquid.Repository.OData.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<Copyright>Avanade 2019</Copyright>
<PackageProjectUrl>https://github.com/Avanade/Liquid-Application-Framework</PackageProjectUrl>
<PackageIcon>logo.png</PackageIcon>
<Version>8.0.0-beta-03</Version>
<Version>8.0.0-beta-04</Version>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IsPackable>true</IsPackable>
<DebugType>Full</DebugType>
Expand All @@ -31,8 +31,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Liquid.Core" Version="8.0.0-beta-06" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
<PackageReference Include="Liquid.Core" Version="8.0.0-beta-07" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageReference Include="Simple.OData.Client" Version="6.0.1" />
</ItemGroup>

Expand Down
9 changes: 5 additions & 4 deletions src/Liquid.Repository.OData/ODataClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ public ODataClientFactory(IOptions<ODataOptions> options, ILiquidContext context
///<inheritdoc/>
public IODataClient CreateODataClientAsync(string entityName)
{
var token = _context.Get("OdataToken").ToString();
var hasToken = _context.current.ContainsKey("OdataToken");
var token = _context?.Get("OdataToken")?.ToString();

Check warning on line 28 in src/Liquid.Repository.OData/ODataClientFactory.cs

View workflow job for this annotation

GitHub Actions / call-reusable-build-workflow / build

Remove this unnecessary check for null. (https://rules.sonarsource.com/csharp/RSPEC-2589)

if (string.IsNullOrEmpty(token))
if (!hasToken || string.IsNullOrEmpty(token))
{
throw new InvalidOperationException("Token is required to perform this operation. The 'OdataToken' variable" +
" must be declared in the LiquidContext service.");
throw new KeyNotFoundException("Token is required to perform this operation. The 'OdataToken' " +
"key was not found in the context.");
}

var settings = _options?.Value?.Settings.FirstOrDefault(x => x.EntityName == entityName);

Check warning on line 36 in src/Liquid.Repository.OData/ODataClientFactory.cs

View workflow job for this annotation

GitHub Actions / call-reusable-build-workflow / build

"Find" method should be used instead of the "FirstOrDefault" extension method. (https://rules.sonarsource.com/csharp/RSPEC-6602)
Expand Down
20 changes: 15 additions & 5 deletions test/Liquid.Repository.OData.Tests/ODataClientFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public ODataClientFactoryTests()

_context = Substitute.For<ILiquidContext>();
_context.Get("OdataToken").Returns("token");
_context.current.ContainsKey("OdataToken").Returns(true);

_sut = new ODataClientFactory(_options, _context);
}
Expand Down Expand Up @@ -80,18 +81,27 @@ public void OdataClientFactory_WhenValidateCertIsTrue_ReturnClient()
[Fact]
public void OdataClientFactory_WhenTokenIsNotSet_ThrowException()
{
_context.Get("OdataToken").Returns("");
var context = Substitute.For<ILiquidContext>();

Assert.Throws<InvalidOperationException>(() => _sut.CreateODataClientAsync("TestEntities"));
context.Get("OdataToken").Returns("");
context.current.ContainsKey("OdataToken").Returns(true);

var sut = new ODataClientFactory(_options, context);

Assert.Throws<KeyNotFoundException>(() => sut.CreateODataClientAsync("TestEntities"));
}

[Fact]
public void OdataClientFactory_WhenTokenIsNull_ThrowException()
public void OdataClientFactory_WhenTokenIsNotSetInContext_ThrowException()
{
var context = Substitute.For<ILiquidContext>();
var context = Substitute.For<ILiquidContext>();

context.Get("OdataToken").Returns(null);
context.current.ContainsKey("OdataToken").Returns(false);

var sut = new ODataClientFactory(_options, context);

Assert.Throws<NullReferenceException>(() => sut.CreateODataClientAsync("TestEntities"));
Assert.Throws<KeyNotFoundException>(() => sut.CreateODataClientAsync("TestEntities"));
}

[Fact]
Expand Down

0 comments on commit f7c7473

Please sign in to comment.