mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-08-22 06:13:22 -07:00
Tests working
This commit is contained in:
parent
ecd68ef2f4
commit
7c516d6b1b
2 changed files with 44 additions and 6 deletions
|
@ -2,7 +2,9 @@
|
||||||
using Moq;
|
using Moq;
|
||||||
using Moq.AutoMock;
|
using Moq.AutoMock;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
|
using NUnit.Framework.Constraints;
|
||||||
using Ombi.Core.Authentication;
|
using Ombi.Core.Authentication;
|
||||||
|
using Ombi.Test.Common;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
@ -43,15 +45,50 @@ namespace Ombi.Tests.Middlewear
|
||||||
var context = GetContext();
|
var context = GetContext();
|
||||||
context.Request.Path = "/api";
|
context.Request.Path = "/api";
|
||||||
context.Request.Headers.Add("UserAccessToken", new Microsoft.Extensions.Primitives.StringValues("test"));
|
context.Request.Headers.Add("UserAccessToken", new Microsoft.Extensions.Primitives.StringValues("test"));
|
||||||
|
var user = new Store.Entities.OmbiUser
|
||||||
var umMock = new Mock<OmbiUserManager>();
|
{
|
||||||
_mocker.Setup<IServiceProvider, OmbiUserManager>(x => (OmbiUserManager)x.GetService(typeof(OmbiUserManager)))
|
UserAccessToken = "test",
|
||||||
|
UserName = "unit test"
|
||||||
|
};
|
||||||
|
var umMock = MockHelper.MockUserManager(new List<Store.Entities.OmbiUser>
|
||||||
|
{
|
||||||
|
user
|
||||||
|
});
|
||||||
|
umMock.Setup(x => x.GetRolesAsync(user)).ReturnsAsync(new List<string> { "Admin" });
|
||||||
|
_mocker.Setup<IServiceProvider, object?>(x => x.GetService(typeof(OmbiUserManager)))
|
||||||
.Returns(umMock.Object);
|
.Returns(umMock.Object);
|
||||||
|
|
||||||
|
|
||||||
await _subject.Invoke(context);
|
await _subject.Invoke(context);
|
||||||
|
|
||||||
_mocker.Verify<IServiceProvider>(x => x.GetService(It.IsAny<Type>()), Times.Never);
|
_mocker.Verify<IServiceProvider>(x => x.GetService(It.IsAny<Type>()), Times.Once);
|
||||||
|
umMock.Verify(x => x.UpdateAsync(user), Times.Once);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task ValidateUserAccessToken_Token_Invalid()
|
||||||
|
{
|
||||||
|
var context = GetContext();
|
||||||
|
context.Request.Path = "/api";
|
||||||
|
context.Request.Headers.Add("UserAccessToken", new Microsoft.Extensions.Primitives.StringValues("invalid"));
|
||||||
|
var user = new Store.Entities.OmbiUser
|
||||||
|
{
|
||||||
|
UserAccessToken = "test",
|
||||||
|
UserName = "unit test"
|
||||||
|
};
|
||||||
|
var umMock = MockHelper.MockUserManager(new List<Store.Entities.OmbiUser>
|
||||||
|
{
|
||||||
|
user
|
||||||
|
});
|
||||||
|
umMock.Setup(x => x.GetRolesAsync(user)).ReturnsAsync(new List<string> { "Admin" });
|
||||||
|
_mocker.Setup<IServiceProvider, object?>(x => x.GetService(typeof(OmbiUserManager)))
|
||||||
|
.Returns(umMock.Object);
|
||||||
|
|
||||||
|
|
||||||
|
await _subject.Invoke(context);
|
||||||
|
|
||||||
|
Assert.That(context.Response.StatusCode, Is.EqualTo(401));
|
||||||
|
umMock.Verify(x => x.UpdateAsync(user), Times.Never);
|
||||||
}
|
}
|
||||||
|
|
||||||
private HttpContext GetContext()
|
private HttpContext GetContext()
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Ombi.Test.Common\Ombi.Test.Common.csproj" />
|
||||||
<ProjectReference Include="..\Ombi\Ombi.csproj" />
|
<ProjectReference Include="..\Ombi\Ombi.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue