1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-29 13:25:17 +01:00
bitwarden-server/test/Core.Test/Tokens/DataProtectorTokenFactoryTests.cs
Matt Gibson e2c6fc81f4
Feature/token service (#1785)
* Implement draft token service

* Add tokenizer and factory

* Handle expiring tokens through base class

* Allow direct token validity checks

* Add safe unprotect to tokenizer

* Add interface to tokenizer factory

* Use tokenizer

* Fix rebase

* Handle cleartext prefix in tokenizer base

* Use epoch milliseconds for expiration in tokens

* Use tokenizers

* Test tokens

* Linter fixes

* Add TokenizerFactory to DI services

* Test epoch milliseconds deserialization

* Use separate injectables for each token type

* Fix directory

* Add functional unprotect to token

* Fix namespace and correct object names

* Remove Tokenable interface

* Test remaining Tokens classes

* Dotnet format

* Fix sut provider errors with update

* Remove useless property

Co-authored-by: Hinton <oscar@oscarhinton.com>
2022-01-10 09:58:16 -06:00

55 lines
1.7 KiB
C#

using AutoFixture;
using Bit.Core.Tokens;
using Bit.Test.Common.AutoFixture;
using Bit.Test.Common.AutoFixture.Attributes;
using Bit.Test.Common.Helpers;
using Microsoft.AspNetCore.DataProtection;
using Xunit;
namespace Bit.Core.Test.Tokens
{
[SutProviderCustomize]
public class DataProtectorTokenFactoryTests
{
public static SutProvider<DataProtectorTokenFactory<TestTokenable>> GetSutProvider()
{
var fixture = new Fixture();
return new SutProvider<DataProtectorTokenFactory<TestTokenable>>(fixture)
.SetDependency<IDataProtectionProvider>(fixture.Create<EphemeralDataProtectionProvider>())
.Create();
}
[Theory, BitAutoData]
public void CanRoundTripTokenables(TestTokenable tokenable)
{
var sutProvider = GetSutProvider();
var token = sutProvider.Sut.Protect(tokenable);
var recoveredTokenable = sutProvider.Sut.Unprotect(token);
AssertHelper.AssertPropertyEqual(tokenable, recoveredTokenable);
}
[Theory, BitAutoData]
public void PrependsClearText(TestTokenable tokenable)
{
var sutProvider = GetSutProvider();
var token = sutProvider.Sut.Protect(tokenable);
Assert.StartsWith(sutProvider.GetDependency<string>("clearTextPrefix"), token);
}
[Theory, BitAutoData]
public void EncryptsToken(TestTokenable tokenable)
{
var sutProvider = GetSutProvider();
var prefix = sutProvider.GetDependency<string>("clearTextPrefix");
var token = sutProvider.Sut.Protect(tokenable);
Assert.NotEqual(new Token(token).RemovePrefix(prefix), tokenable.ToToken());
}
}
}