From f7c7473e7fd337cf6257b09cb6c1c9ad75527a10 Mon Sep 17 00:00:00 2001 From: Luciana Regina Lino Date: Fri, 12 Jul 2024 13:09:59 -0300 Subject: [PATCH 1/2] fix(ODataClientFactory): adjustment to validation of existence of the authentication token in the context service --- .../Liquid.Repository.OData.csproj | 6 +++--- .../ODataClientFactory.cs | 9 +++++---- .../ODataClientFactoryTests.cs | 20 ++++++++++++++----- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/Liquid.Repository.OData/Liquid.Repository.OData.csproj b/src/Liquid.Repository.OData/Liquid.Repository.OData.csproj index 8a64ddc..233be33 100644 --- a/src/Liquid.Repository.OData/Liquid.Repository.OData.csproj +++ b/src/Liquid.Repository.OData/Liquid.Repository.OData.csproj @@ -13,7 +13,7 @@ Avanade 2019 https://github.com/Avanade/Liquid-Application-Framework logo.png - 8.0.0-beta-03 + 8.0.0-beta-04 true true Full @@ -31,8 +31,8 @@ - - + + diff --git a/src/Liquid.Repository.OData/ODataClientFactory.cs b/src/Liquid.Repository.OData/ODataClientFactory.cs index 8a82817..42c3954 100644 --- a/src/Liquid.Repository.OData/ODataClientFactory.cs +++ b/src/Liquid.Repository.OData/ODataClientFactory.cs @@ -24,12 +24,13 @@ public ODataClientFactory(IOptions options, ILiquidContext context /// public IODataClient CreateODataClientAsync(string entityName) { - var token = _context.Get("OdataToken").ToString(); + var hasToken = _context.current.ContainsKey("OdataToken"); + var token = _context?.Get("OdataToken")?.ToString(); - 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); diff --git a/test/Liquid.Repository.OData.Tests/ODataClientFactoryTests.cs b/test/Liquid.Repository.OData.Tests/ODataClientFactoryTests.cs index d2e3406..4c26ad2 100644 --- a/test/Liquid.Repository.OData.Tests/ODataClientFactoryTests.cs +++ b/test/Liquid.Repository.OData.Tests/ODataClientFactoryTests.cs @@ -30,6 +30,7 @@ public ODataClientFactoryTests() _context = Substitute.For(); _context.Get("OdataToken").Returns("token"); + _context.current.ContainsKey("OdataToken").Returns(true); _sut = new ODataClientFactory(_options, _context); } @@ -80,18 +81,27 @@ public void OdataClientFactory_WhenValidateCertIsTrue_ReturnClient() [Fact] public void OdataClientFactory_WhenTokenIsNotSet_ThrowException() { - _context.Get("OdataToken").Returns(""); + var context = Substitute.For(); - Assert.Throws(() => _sut.CreateODataClientAsync("TestEntities")); + context.Get("OdataToken").Returns(""); + context.current.ContainsKey("OdataToken").Returns(true); + + var sut = new ODataClientFactory(_options, context); + + Assert.Throws(() => sut.CreateODataClientAsync("TestEntities")); } + [Fact] - public void OdataClientFactory_WhenTokenIsNull_ThrowException() + public void OdataClientFactory_WhenTokenIsNotSetInContext_ThrowException() { - var context = Substitute.For(); + var context = Substitute.For(); + + context.Get("OdataToken").Returns(null); + context.current.ContainsKey("OdataToken").Returns(false); var sut = new ODataClientFactory(_options, context); - Assert.Throws(() => sut.CreateODataClientAsync("TestEntities")); + Assert.Throws(() => sut.CreateODataClientAsync("TestEntities")); } [Fact] From a5eb87c862d1e505070d2c9a305bbe76602d97ea Mon Sep 17 00:00:00 2001 From: Luciana Regina Lino Date: Fri, 12 Jul 2024 14:46:49 -0300 Subject: [PATCH 2/2] fix(ODataClientFactory) : Sonar issue. --- src/Liquid.Repository.OData/ODataClientFactory.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Liquid.Repository.OData/ODataClientFactory.cs b/src/Liquid.Repository.OData/ODataClientFactory.cs index 42c3954..d539928 100644 --- a/src/Liquid.Repository.OData/ODataClientFactory.cs +++ b/src/Liquid.Repository.OData/ODataClientFactory.cs @@ -25,7 +25,7 @@ public ODataClientFactory(IOptions options, ILiquidContext context public IODataClient CreateODataClientAsync(string entityName) { var hasToken = _context.current.ContainsKey("OdataToken"); - var token = _context?.Get("OdataToken")?.ToString(); + var token = _context.Get("OdataToken")?.ToString(); if (!hasToken || string.IsNullOrEmpty(token)) {