2021-08-06 09:51:08 +02:00
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using Moq;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using Umbraco.Cms.Core.Configuration.Models;
|
2021-08-06 10:41:21 +02:00
|
|
|
using Umbraco.Cms.Core.Services.Implement;
|
2022-04-29 11:52:58 +02:00
|
|
|
using Umbraco.Cms.Web.Common.Mvc;
|
2021-08-06 09:51:08 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Services;
|
|
|
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class BasicAuthServiceTests
|
2021-08-06 09:51:08 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
[TestCase(true, ExpectedResult = true)]
|
|
|
|
|
[TestCase(false, ExpectedResult = false)]
|
|
|
|
|
public bool IsBasicAuthEnabled(bool enabled)
|
2021-08-06 09:51:08 +02:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
var sut = new BasicAuthService(
|
|
|
|
|
Mock.Of<IOptionsMonitor<BasicAuthSettings>>(
|
|
|
|
|
_ => _.CurrentValue == new BasicAuthSettings { Enabled = enabled }),
|
|
|
|
|
new IpAddressUtilities());
|
2021-08-06 09:51:08 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
return sut.IsBasicAuthEnabled();
|
|
|
|
|
}
|
2021-08-06 09:51:08 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[TestCase("::1", "1.1.1.1", ExpectedResult = false)]
|
|
|
|
|
[TestCase("::1", "1.1.1.1, ::1", ExpectedResult = true)]
|
|
|
|
|
[TestCase("127.0.0.1", "127.0.0.1, ::1", ExpectedResult = true)]
|
|
|
|
|
[TestCase("127.0.0.1", "", ExpectedResult = false)]
|
|
|
|
|
[TestCase("125.125.125.1", "125.125.125.0/24", ExpectedResult = true)]
|
|
|
|
|
[TestCase("125.125.124.1", "125.125.125.0/24", ExpectedResult = false)]
|
|
|
|
|
public bool IsIpAllowListed(string clientIpAddress, string commaSeperatedAllowlist)
|
|
|
|
|
{
|
|
|
|
|
var allowedIPs = commaSeperatedAllowlist.Split(",").Select(x => x.Trim()).ToArray();
|
|
|
|
|
var sut = new BasicAuthService(
|
|
|
|
|
Mock.Of<IOptionsMonitor<BasicAuthSettings>>(_ =>
|
|
|
|
|
_.CurrentValue == new BasicAuthSettings { AllowedIPs = allowedIPs }),
|
|
|
|
|
new IpAddressUtilities());
|
2021-08-06 09:51:08 +02:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
return sut.IsIpAllowListed(IPAddress.Parse(clientIpAddress));
|
2021-08-06 09:51:08 +02:00
|
|
|
}
|
|
|
|
|
}
|