diff --git a/src/Ombi.Notifications/Agents/DiscordNotification.cs b/src/Ombi.Notifications/Agents/DiscordNotification.cs index ae344116fa..a1619de695 100644 --- a/src/Ombi.Notifications/Agents/DiscordNotification.cs +++ b/src/Ombi.Notifications/Agents/DiscordNotification.cs @@ -107,7 +107,7 @@ protected override async Task Send(NotificationMessage model, DiscordNotificatio var discordBody = new DiscordWebhookBody { content = model.Message, - username = settings.Username, + username = settings.Username ?? "Ombi", }; var fields = new List(); diff --git a/src/Ombi.Tests/Middlewear/ApiKeyMiddlewearTests.cs b/src/Ombi.Tests/Middlewear/ApiKeyMiddlewearTests.cs new file mode 100644 index 0000000000..5768994766 --- /dev/null +++ b/src/Ombi.Tests/Middlewear/ApiKeyMiddlewearTests.cs @@ -0,0 +1,101 @@ +using Microsoft.AspNetCore.Http; +using Moq; +using Moq.AutoMock; +using NUnit.Framework; +using NUnit.Framework.Constraints; +using Ombi.Core.Authentication; +using Ombi.Test.Common; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Ombi.Tests.Middlewear +{ + [TestFixture] + public class ApiKeyMiddlewearTests + { + private AutoMocker _mocker; + private ApiKeyMiddlewear _subject; + private Mock _serviceProviderMock; + + [SetUp] + public void Setup() + { + _mocker = new AutoMocker(); + _serviceProviderMock = new Mock(); + _mocker.Use(_serviceProviderMock); + _subject = _mocker.CreateInstance(); + } + + [Test] + public async Task NonApiAccess() + { + var context = GetContext(); + context.Request.Path = "/notanapi"; + await _subject.Invoke(context); + + _mocker.Verify(x => x.GetService(It.IsAny()), Times.Never); + } + + [Test] + public async Task ValidateUserAccessToken() + { + var context = GetContext(); + context.Request.Path = "/api"; + context.Request.Headers.Add("UserAccessToken", new Microsoft.Extensions.Primitives.StringValues("test")); + var user = new Store.Entities.OmbiUser + { + UserAccessToken = "test", + UserName = "unit test" + }; + var umMock = MockHelper.MockUserManager(new List + { + user + }); + umMock.Setup(x => x.GetRolesAsync(user)).ReturnsAsync(new List { "Admin" }); + _mocker.Setup(x => x.GetService(typeof(OmbiUserManager))) + .Returns(umMock.Object); + + + await _subject.Invoke(context); + + _mocker.Verify(x => x.GetService(It.IsAny()), 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 + { + user + }); + umMock.Setup(x => x.GetRolesAsync(user)).ReturnsAsync(new List { "Admin" }); + _mocker.Setup(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() + { + var context = new DefaultHttpContext(); + context.RequestServices = _serviceProviderMock.Object; + return context; + } + } +} diff --git a/src/Ombi.Tests/Ombi.Tests.csproj b/src/Ombi.Tests/Ombi.Tests.csproj index ce3b455330..77e1769f9f 100644 --- a/src/Ombi.Tests/Ombi.Tests.csproj +++ b/src/Ombi.Tests/Ombi.Tests.csproj @@ -9,6 +9,7 @@ + @@ -18,6 +19,7 @@ + diff --git a/src/Ombi/Middleware/ApiKeyMiddlewear.cs b/src/Ombi/Middleware/ApiKeyMiddlewear.cs index 0efe5b8603..d5e0115093 100644 --- a/src/Ombi/Middleware/ApiKeyMiddlewear.cs +++ b/src/Ombi/Middleware/ApiKeyMiddlewear.cs @@ -57,7 +57,7 @@ public async Task Invoke(HttpContext context) } } - private async Task ValidateUserAccessToken(HttpContext context, RequestDelegate next, string key) + private static async Task ValidateUserAccessToken(HttpContext context, RequestDelegate next, string key) { if (string.IsNullOrEmpty(key)) { @@ -74,11 +74,13 @@ private async Task ValidateUserAccessToken(HttpContext context, RequestDelegate } else { - var identity = new GenericIdentity(user.UserName); var roles = await um.GetRolesAsync(user); var principal = new GenericPrincipal(identity, roles.ToArray()); context.User = principal; + user.LastLoggedIn = DateTime.UtcNow; + await um.UpdateAsync(user); + await next.Invoke(context); } }