1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-01 13:43:23 +01:00
bitwarden-server/test/Core.Test/Tokens/ExpiringTokenTests.cs

71 lines
1.7 KiB
C#
Raw Normal View History

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