Autofac Moq NUnit - VianneyDoleans/Teia-Tests-Documentation GitHub Wiki

Autofac+Moq+NUnit

[TestFixture]
public class OAuth2ConfigServiceTests : DatabaseEnabledTestsBase
{
    private IOAuth2ConfigService _oAuth2ConfigService;

    public override void Init()
    {
        base.Init();
        _oAuth2ConfigService = _container.Resolve<IOAuth2ConfigService>();
    }

    private static OAuth2ConfigRecord GetTestPasswordRecord()
    {
        return new OAuth2ConfigRecord()
        {
            Name = "password",
            ClientId = "",
            ClientSecret = "",
            Username = "",
            Password = "",
            AccessTokenUrl = "",
            GrantType = "password",
            Ssl = true,
            ValidUntil = new DateTime(1753, 1, 1)
        };
    }

    private static OAuth2ConfigRecord GetTestClientCredentialsRecord()
    {
        return new OAuth2ConfigRecord()
        {
            Name = "clientCredentials",
            ClientId = "",
            ClientSecret = "",
            AccessTokenUrl = "",
            GrantType = "client_credentials",
            Ssl = true,
            ValidUntil = new DateTime(1753, 1, 1)
        };
    }

    public override void Register(ContainerBuilder builder)
    {
        builder.RegisterType<OAuth2ConfigService>().As<IOAuth2ConfigService>();
        builder.RegisterType<SiteService>().As<ISiteService>();
        builder.RegisterType<DefaultContentManager>().As<IContentManager>();
        builder.RegisterType<ContentDefinitionManager>().As<IContentDefinitionManager>();
        builder.RegisterType<DefaultContentManagerSession>().As<IContentManagerSession>();
        builder.RegisterType<DefaultContentDisplay>().As<IContentDisplay>();
        builder.RegisterType<Signals>().As<ISignals>();
        builder.RegisterType<SettingsFormatter>().As<ISettingsFormatter>();

        builder.RegisterInstance(new Mock<ICacheManager>().Object);

        var encryptionMock = new Mock<IEncryptionService>();
        encryptionMock.Setup(x => x.Encode(It.IsAny<byte[]>())).Returns<byte[]>(x =>
        {
            var array = x.ToList();
            array.Add(17);
            return array.ToArray();
        });
        encryptionMock.Setup(x => x.Decode(It.IsAny<byte[]>())).Returns<byte[]>(x =>
        {
            var array = x.ToList();
            if (x.Length > 0)
                array.RemoveAt(x.Length - 1);
            return array.ToArray();
        });
        builder.RegisterInstance(encryptionMock.Object);
    }

    protected override IEnumerable<Type> DatabaseTypes
    {
        get
        {
            return new[]
            {
                    typeof(OAuth2ConfigRecord)
            };
        }
    }

    [Test]
    public void AddEncryptPassword()
    {
        var record = GetTestPasswordRecord();
        var password = record.Password;

        _oAuth2ConfigService.Add(ref record);

        Assert.True(record.Password != password);
    }

    [Test]
    public void GetDecryptPassword()
    {
        var record = GetTestPasswordRecord();
        var password = record.Password;

        _oAuth2ConfigService.Add(ref record);

        Assert.True(_oAuth2ConfigService.Get(record.Id).Password == password);
    }

    [Test]
    public void AddNullPassword()
    {
        var record = GetTestPasswordRecord();
        record.Password = null;

        _oAuth2ConfigService.Add(ref record);

        Assert.True(record.Password == null);
    }

    [Test]
    public void GetNullPassword()
    {
        var record = GetTestPasswordRecord();
        record.Password = null;

        _oAuth2ConfigService.Add(ref record);

        Assert.True(_oAuth2ConfigService.Get(record.Id).Password == null);
    }

    [Test]
    public void AddisUpdatingRecord()
    {
        const string testString = "modification";

        var record = GetTestClientCredentialsRecord();
        var clientId = record.ClientId;

        _oAuth2ConfigService.Add(ref record);
        clientIdBefore = _oAuth2ConfigService.Get(record.Id).ClientId;
        record.ClientId = testString;
        _oAuth2ConfigService.Add(ref record);
        clientIdAfter = _oAuth2ConfigService.Get(record.Id).ClientId;

        Assert.True(clientIdBefore == clientId);
        Assert.True(clientIdAfter == testString);
    }
}
⚠️ **GitHub.com Fallback** ⚠️