Skip to content

Commit 5f0ae66

Browse files
committed
Tests added
1 parent 32ac48a commit 5f0ae66

20 files changed

+2419
-14
lines changed

src/AspNetCore.Authentication.Basic.sln

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleWebApi_2_0", "..\samp
2222
EndProject
2323
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleWebApi_3_1", "..\samples\SampleWebApi_3_1\SampleWebApi_3_1.csproj", "{2705DB4C-3BCE-4CFC-9A30-B4BFD1F28C56}"
2424
EndProject
25-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleWebApi_5_0", "..\samples\SampleWebApi_5_0\SampleWebApi_5_0.csproj", "{B82830A0-FDFC-469D-B2A8-D657CD216451}"
25+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleWebApi_5_0", "..\samples\SampleWebApi_5_0\SampleWebApi_5_0.csproj", "{B82830A0-FDFC-469D-B2A8-D657CD216451}"
26+
EndProject
27+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{F7494366-ED1D-4342-AE5D-DD6BE67C63DF}"
28+
EndProject
29+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetCore.Authentication.Basic.Tests", "..\test\AspNetCore.Authentication.Basic.Tests\AspNetCore.Authentication.Basic.Tests.csproj", "{335B0D1F-A428-4D2E-AF87-269C75F5F138}"
2630
EndProject
2731
Global
2832
GlobalSection(SharedMSBuildProjectFiles) = preSolution
@@ -57,6 +61,10 @@ Global
5761
{B82830A0-FDFC-469D-B2A8-D657CD216451}.Debug|Any CPU.Build.0 = Debug|Any CPU
5862
{B82830A0-FDFC-469D-B2A8-D657CD216451}.Release|Any CPU.ActiveCfg = Release|Any CPU
5963
{B82830A0-FDFC-469D-B2A8-D657CD216451}.Release|Any CPU.Build.0 = Release|Any CPU
64+
{335B0D1F-A428-4D2E-AF87-269C75F5F138}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
65+
{335B0D1F-A428-4D2E-AF87-269C75F5F138}.Debug|Any CPU.Build.0 = Debug|Any CPU
66+
{335B0D1F-A428-4D2E-AF87-269C75F5F138}.Release|Any CPU.ActiveCfg = Release|Any CPU
67+
{335B0D1F-A428-4D2E-AF87-269C75F5F138}.Release|Any CPU.Build.0 = Release|Any CPU
6068
EndGlobalSection
6169
GlobalSection(SolutionProperties) = preSolution
6270
HideSolutionNode = FALSE
@@ -67,6 +75,7 @@ Global
6775
{897E5C9C-8C0A-4FB6-960C-4D11AAFD4491} = {CF13271D-BF3F-4167-BEBA-DD02D33992F2}
6876
{2705DB4C-3BCE-4CFC-9A30-B4BFD1F28C56} = {CF13271D-BF3F-4167-BEBA-DD02D33992F2}
6977
{B82830A0-FDFC-469D-B2A8-D657CD216451} = {CF13271D-BF3F-4167-BEBA-DD02D33992F2}
78+
{335B0D1F-A428-4D2E-AF87-269C75F5F138} = {F7494366-ED1D-4342-AE5D-DD6BE67C63DF}
7079
EndGlobalSection
7180
GlobalSection(ExtensibilityGlobals) = postSolution
7281
SolutionGuid = {70815049-1680-480A-BF5A-00536D6C9C20}

src/AspNetCore.Authentication.Basic/AspNetCore.Authentication.Basic.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@
4343
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
4444
</ItemGroup>
4545

46+
<ItemGroup>
47+
<!-- Unit Testing - To make internal visible to tests -->
48+
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
49+
<_Parameter1>
50+
$(MSBuildProjectName).Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100196de3cf1767e4651bd29928a1dfdc149897770adb2439fd0f42a480f0f0c955606d6e6cae87cbdcdfbd1686ec7775789b82fa869387c31dcc740991f3d3d2db407b0a28d772fe343cdf8f1b12ee53bb625aee854f56e0be93f11c307c0dbc54aad277111fc04db0eed2513a20d5663c50412afd34e8c8fb98459d8f4d90e5b6</_Parameter1>
51+
</AssemblyAttribute>
52+
</ItemGroup>
53+
4654
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
4755
<DocumentationFile>bin\Release\$(TargetFramework)\AspNetCore.Authentication.Basic.xml</DocumentationFile>
4856
</PropertyGroup>

src/AspNetCore.Authentication.Basic/BasicHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,9 @@ private BasicCredentials DecodeBasicCredentials(string credentials)
253253
username = usernameAndPasswordSplit[0];
254254
password = usernameAndPasswordSplit[1];
255255
}
256-
catch (Exception)
256+
catch (Exception e)
257257
{
258-
throw new Exception($"Problem decoding '{BasicDefaults.AuthenticationScheme}' scheme credentials.");
258+
throw new Exception($"Problem decoding '{BasicDefaults.AuthenticationScheme}' scheme credentials.", e);
259259
}
260260

261261
if (string.IsNullOrWhiteSpace(username))

src/AspNetCore.Authentication.Basic/BasicPostConfigureOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public void PostConfigure(string name, BasicOptions options)
1515
{
1616
if (!options.SuppressWWWAuthenticateHeader && string.IsNullOrWhiteSpace(options.Realm))
1717
{
18-
throw new InvalidOperationException("Realm must be set in basic options");
18+
throw new InvalidOperationException($"{nameof(BasicOptions.Realm)} must be set in {typeof(BasicOptions).Name} when setting up the authentication.");
1919
}
2020

2121
if (options.Events?.OnValidateCredentials == null && options.EventsType == null && options.BasicUserValidationServiceType == null)

src/AspNetCore.Authentication.Basic/BasicUtils.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
namespace AspNetCore.Authentication.Basic
1010
{
11-
/// <summary>
12-
/// Utility class.
13-
/// </summary>
14-
internal static class BasicUtils
11+
/// <summary>
12+
/// Utility class.
13+
/// </summary>
14+
internal static class BasicUtils
1515
{
1616
/// <summary>
1717
/// Builds Claims Principal from the provided information.
@@ -30,14 +30,17 @@ internal static ClaimsPrincipal BuildClaimsPrincipal(string username, string sch
3030
var claimsList = new List<Claim>();
3131
if (claims != null) claimsList.AddRange(claims);
3232

33-
if (!claimsList.Any(c => c.Type == ClaimTypes.NameIdentifier))
33+
if (!string.IsNullOrWhiteSpace(username))
3434
{
35-
claimsList.Add(new Claim(ClaimTypes.NameIdentifier, username, ClaimValueTypes.String, claimsIssuer));
36-
}
35+
if (!claimsList.Any(c => c.Type == ClaimTypes.NameIdentifier))
36+
{
37+
claimsList.Add(new Claim(ClaimTypes.NameIdentifier, username, ClaimValueTypes.String, claimsIssuer));
38+
}
3739

38-
if (!claimsList.Any(c => c.Type == ClaimTypes.Name))
39-
{
40-
claimsList.Add(new Claim(ClaimTypes.Name, username, ClaimValueTypes.String, claimsIssuer));
40+
if (!claimsList.Any(c => c.Type == ClaimTypes.Name))
41+
{
42+
claimsList.Add(new Claim(ClaimTypes.Name, username, ClaimValueTypes.String, claimsIssuer));
43+
}
4144
}
4245

4346
return new ClaimsPrincipal(new ClaimsIdentity(claimsList, schemeName));
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net5.0;netcoreapp3.1;netcoreapp3.0;netcoreapp2.1;net461</TargetFrameworks>
5+
<IsPackable>false</IsPackable>
6+
<LangVersion>latest</LangVersion>
7+
</PropertyGroup>
8+
9+
<PropertyGroup>
10+
<!-- Strong Name Key -->
11+
<SignAssembly>true</SignAssembly>
12+
<AssemblyOriginatorKeyFile>$(SolutionDir)key.snk</AssemblyOriginatorKeyFile>
13+
<DelaySign>false</DelaySign>
14+
</PropertyGroup>
15+
16+
<ItemGroup>
17+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
18+
<PackageReference Include="xunit" Version="2.4.1" />
19+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
<PrivateAssets>all</PrivateAssets>
22+
</PackageReference>
23+
<PackageReference Include="coverlet.collector" Version="3.0.3">
24+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
25+
<PrivateAssets>all</PrivateAssets>
26+
</PackageReference>
27+
</ItemGroup>
28+
29+
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
30+
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="5.0.3" />
31+
</ItemGroup>
32+
33+
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
34+
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.1.11" />
35+
</ItemGroup>
36+
37+
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
38+
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.0.3" />
39+
</ItemGroup>
40+
41+
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.1'">
42+
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="5.0.3" />
43+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
44+
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.2.0" />
45+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
46+
</ItemGroup>
47+
48+
<ItemGroup Condition="'$(TargetFramework)' == 'net461'">
49+
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="5.0.3" />
50+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
51+
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.2.0" />
52+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
53+
</ItemGroup>
54+
55+
<ItemGroup>
56+
<ProjectReference Include="..\..\src\AspNetCore.Authentication.Basic\AspNetCore.Authentication.Basic.csproj" />
57+
</ItemGroup>
58+
59+
</Project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Mihir Dilip. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using Xunit;
5+
6+
namespace AspNetCore.Authentication.Basic.Tests
7+
{
8+
public class BasicDefaultsTests
9+
{
10+
[Fact]
11+
public void AuthenticationSchemeValueTest()
12+
{
13+
Assert.Equal("Basic", BasicDefaults.AuthenticationScheme);
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)