Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Core/Services/BasicAuthServiceTests.cs

41 lines
1.7 KiB
C#
Raw Normal View History

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;
2021-08-06 09:51:08 +02:00
using Umbraco.Cms.Core.Configuration.Models;
2021-08-06 10:41:21 +02:00
using Umbraco.Cms.Core.Services.Implement;
using Umbraco.Cms.Web.Common.Mvc;
using IpAddressUtilities = Umbraco.Cms.Web.Common.Mvc.IpAddressUtilities;
2021-08-06 09:51:08 +02:00
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Services
{
[TestFixture]
public class BasicAuthServiceTests
{
[TestCase(true, ExpectedResult = true)]
[TestCase(false, ExpectedResult = false)]
public bool IsBasicAuthEnabled(bool enabled)
{
var sut = new BasicAuthService(Mock.Of<IOptionsMonitor<BasicAuthSettings>>(_ => _.CurrentValue == new BasicAuthSettings() {Enabled = enabled}), new IpAddressUtilities());
2021-08-06 09:51:08 +02:00
return sut.IsBasicAuthEnabled();
}
[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)]
2021-08-06 10:41:21 +02:00
[TestCase("125.125.125.1", "125.125.125.0/24", ExpectedResult = true)]
[TestCase("125.125.124.1", "125.125.125.0/24", ExpectedResult = false)]
2021-08-06 10:44:35 +02:00
public bool IsIpAllowListed(string clientIpAddress, string commaSeperatedAllowlist)
2021-08-06 09:51:08 +02:00
{
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
return sut.IsIpAllowListed(IPAddress.Parse(clientIpAddress));
}
}
}