2022-06-30 01:46:41 +02:00
|
|
|
|
using System.Text.Json;
|
2022-01-10 16:58:16 +01:00
|
|
|
|
using AutoFixture.Xunit2;
|
|
|
|
|
using Bit.Core.Utilities;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Test.Tokens;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2022-01-10 16:58:16 +01:00
|
|
|
|
public class ExpiringTokenTests
|
|
|
|
|
{
|
|
|
|
|
[Theory, AutoData]
|
|
|
|
|
public void ExpirationSerializesToEpochMilliseconds(DateTime expirationDate)
|
|
|
|
|
{
|
|
|
|
|
var sut = new TestExpiringTokenable
|
|
|
|
|
{
|
|
|
|
|
ExpirationDate = expirationDate
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = JsonSerializer.Serialize(sut);
|
|
|
|
|
var expectedDate = CoreHelpers.ToEpocMilliseconds(expirationDate);
|
|
|
|
|
|
|
|
|
|
Assert.Contains($"\"ExpirationDate\":{expectedDate}", result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Theory, AutoData]
|
|
|
|
|
public void ExpirationSerializationRoundTrip(DateTime expirationDate)
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-01-10 16:58:16 +01:00
|
|
|
|
var sut = new TestExpiringTokenable
|
|
|
|
|
{
|
|
|
|
|
ExpirationDate = expirationDate
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var intermediate = JsonSerializer.Serialize(sut);
|
|
|
|
|
var result = JsonSerializer.Deserialize<TestExpiringTokenable>(intermediate);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(sut.ExpirationDate, result.ExpirationDate, TimeSpan.FromMilliseconds(100));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void InvalidIfPastExpiryDate()
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-01-10 16:58:16 +01:00
|
|
|
|
var sut = new TestExpiringTokenable
|
|
|
|
|
{
|
|
|
|
|
ExpirationDate = DateTime.UtcNow.AddHours(-1)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Assert.False(sut.Valid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ValidIfWithinExpirationAndTokenReportsValid()
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-01-10 16:58:16 +01:00
|
|
|
|
var sut = new TestExpiringTokenable
|
|
|
|
|
{
|
|
|
|
|
ExpirationDate = DateTime.UtcNow.AddHours(1)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Assert.True(sut.Valid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void HonorsTokenIsValidAbstractMember()
|
2022-08-29 22:06:55 +02:00
|
|
|
|
{
|
2022-01-10 16:58:16 +01:00
|
|
|
|
var sut = new TestExpiringTokenable(forceInvalid: true)
|
|
|
|
|
{
|
|
|
|
|
ExpirationDate = DateTime.UtcNow.AddHours(1)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Assert.False(sut.Valid);
|
|
|
|
|
}
|
|
|
|
|
}
|