Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Core/Extensions/ClaimsPrincipalExtensionsTests.cs
Nikolaj Geisle d5809da665 V14: Move towards get guid (#15889)
* Implement using keymap for member

* Remove current usages of GetUserById

* User userId resolver to resolve user key

* Refactor user repository to use GUID not int

* Add happy path test

* Remove user in cache when user gets updated

* Use await in async method

* Fix up according to review

* Update IMetricsConsentService.cs to have async method

* Fix according to review

* Fix more according to comments

* Revert "Fix up according to review"

This reverts commit a75acaaa

* Get current backoffice user from method

* Update user repository delete functionality

* Fix up more test

* Try to get user by id if key fails

* Add user key as required claim

* Fix tests

* Don't set claim in BackofficeController

* Create constant for the Sub claim

---------

Co-authored-by: kjac <kja@umbraco.dk>
2024-04-11 13:53:34 +02:00

89 lines
2.8 KiB
C#

// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.Linq;
using System.Security.Claims;
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Extensions;
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Extensions;
[TestFixture]
public class ClaimsPrincipalExtensionsTests
{
[Test]
public void Get_Remaining_Ticket_Seconds()
{
var backOfficeIdentity = new ClaimsIdentity();
backOfficeIdentity.AddRequiredClaims(
Constants.Security.SuperUserIdAsString,
Constants.Security.SuperUserKey,
"test",
"test",
Enumerable.Empty<int>(),
Enumerable.Empty<int>(),
"en-US",
Guid.NewGuid().ToString(),
Enumerable.Empty<string>(),
Enumerable.Empty<string>());
var principal = new ClaimsPrincipal(backOfficeIdentity);
var expireSeconds = 99;
var elapsedSeconds = 3;
var remainingSeconds = expireSeconds - elapsedSeconds;
var now = DateTimeOffset.Now;
var then = now.AddSeconds(elapsedSeconds);
var expires = now.AddSeconds(expireSeconds).ToString("o");
backOfficeIdentity.AddClaim(new Claim(
Constants.Security.TicketExpiresClaimType,
expires,
ClaimValueTypes.DateTime,
Constants.Security.BackOfficeAuthenticationType,
Constants.Security.BackOfficeAuthenticationType,
backOfficeIdentity));
var ticketRemainingSeconds = principal.GetRemainingAuthSeconds(then);
Assert.AreEqual(remainingSeconds, ticketRemainingSeconds);
}
[Test]
public void AddOrUpdateClaim__Should_ensure_a_claim_is_not_added_twice()
{
var backOfficeIdentity = new ClaimsIdentity();
backOfficeIdentity.AddRequiredClaims(
Constants.Security.SuperUserIdAsString,
Constants.Security.SuperUserKey,
"test",
"test",
Enumerable.Empty<int>(),
Enumerable.Empty<int>(),
"en-US",
Guid.NewGuid().ToString(),
Enumerable.Empty<string>(),
Enumerable.Empty<string>());
var expireSeconds = 99;
var now = DateTimeOffset.Now;
var expires = now.AddSeconds(expireSeconds).ToString("o");
var claim = new Claim(
Constants.Security.TicketExpiresClaimType,
expires,
ClaimValueTypes.DateTime,
Constants.Security.BackOfficeAuthenticationType,
Constants.Security.BackOfficeAuthenticationType,
backOfficeIdentity);
backOfficeIdentity.AddOrUpdateClaim(claim);
backOfficeIdentity.AddOrUpdateClaim(claim);
Assert.AreEqual(1, backOfficeIdentity.Claims.Count(x => x.Type == Constants.Security.TicketExpiresClaimType));
}
}