Fixed a bit of a stupid bug in the resetter and added unit tests around it to make sure this never happens again.

pull/385/head
tidusjar 8 years ago
parent 3563f166c9
commit 2d0b87b558

@ -52,6 +52,10 @@
<HintPath>..\packages\NUnit.3.2.0\lib\net45\nunit.framework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Ploeh.AutoFixture, Version=3.40.0.0, Culture=neutral, PublicKeyToken=b24654c590009d4f, processorArchitecture=MSIL">
<HintPath>..\packages\AutoFixture.3.40.0\lib\net40\Ploeh.AutoFixture.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Quartz, Version=2.3.3.0, Culture=neutral, PublicKeyToken=f6b8c98a402cc8a4, processorArchitecture=MSIL">
<HintPath>..\packages\Quartz.2.3.3\lib\net40\Quartz.dll</HintPath>
<Private>True</Private>
@ -67,6 +71,7 @@
<Otherwise />
</Choose>
<ItemGroup>
<Compile Include="UserRequestLimitResetterTests.cs" />
<Compile Include="NotificationServiceTests.cs" />
<Compile Include="PlexAvailabilityCheckerTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

@ -0,0 +1,151 @@
#region Copyright
/************************************************************************
Copyright (c) 2016 Jamie Rees
File: NotificationServiceTests.cs
Created By: Jamie Rees
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
using PlexRequests.Core;
using PlexRequests.Core.SettingModels;
using PlexRequests.Services.Interfaces;
using PlexRequests.Services.Jobs;
using PlexRequests.Services.Notification;
using PlexRequests.Store;
using PlexRequests.Store.Models;
using PlexRequests.Store.Repository;
using Ploeh.AutoFixture;
using Quartz;
namespace PlexRequests.Services.Tests
{
[TestFixture]
public class UserRequestLimitResetterTests
{
public UserRequestLimitResetter Resetter { get; set; }
private Mock<IJobRecord> JobMock { get; set; }
private Mock<IRepository<RequestLimit>> RepoMock { get; set; }
private Mock<ISettingsService<PlexRequestSettings>> SettingsMock { get; set; }
private Mock<IJobExecutionContext> ContextMock { get; set; }
private Fixture F { get; set; }
[SetUp]
public void Setup()
{
F = new Fixture();
JobMock = new Mock<IJobRecord>();
RepoMock = new Mock<IRepository<RequestLimit>>();
SettingsMock = new Mock<ISettingsService<PlexRequestSettings>>();
ContextMock = new Mock<IJobExecutionContext>();
SettingsMock.Setup(x => x.GetSettings()).Returns(new PlexRequestSettings());
Resetter = new UserRequestLimitResetter(JobMock.Object, RepoMock.Object, SettingsMock.Object);
}
[TearDown]
public void Teardown()
{
SettingsMock.Verify(x => x.GetSettings(), Times.Once);
JobMock.Verify(x => x.Record(It.IsAny<string>()), Times.Once());
}
[Test]
public void ResetTurnedOff()
{
SetupSettings(0, 0, 0);
Resetter = new UserRequestLimitResetter(JobMock.Object, RepoMock.Object, SettingsMock.Object);
Resetter.Execute(ContextMock.Object);
RepoMock.Verify(x => x.Delete(It.IsAny<RequestLimit>()), Times.Never);
}
[TestCaseSource(nameof(ResetData))]
public void Reset(int movie, int tv, int album, RequestType type)
{
SetupSettings(movie, tv, album);
RepoMock.Setup(x => x.GetAll()).Returns(F.Build<RequestLimit>()
.With(x => x.FirstRequestDate, DateTime.Now.AddDays(-8))
.With(x => x.RequestType, type).CreateMany());
Resetter = new UserRequestLimitResetter(JobMock.Object, RepoMock.Object, SettingsMock.Object);
Resetter.Execute(ContextMock.Object);
RepoMock.Verify(x => x.Delete(It.IsAny<RequestLimit>()), Times.Exactly(3));
}
[TestCaseSource(nameof(DoNotResetData))]
public void DoNotReset(int days, RequestType type)
{
SetupSettings(1, 1, 1);
RepoMock.Setup(x => x.GetAll()).Returns(F.Build<RequestLimit>()
.With(x => x.FirstRequestDate, DateTime.Now.AddDays(days))
.With(x => x.RequestType, type).CreateMany());
Resetter = new UserRequestLimitResetter(JobMock.Object, RepoMock.Object, SettingsMock.Object);
Resetter.Execute(ContextMock.Object);
RepoMock.Verify(x => x.Delete(It.IsAny<RequestLimit>()), Times.Never);
}
static readonly IEnumerable<TestCaseData> ResetData = new List<TestCaseData>
{
new TestCaseData(1,0,0,RequestType.Movie).SetName("Reset Movies"),
new TestCaseData(0,1,0,RequestType.TvShow).SetName("Reset TV Shows"),
new TestCaseData(0,0,1,RequestType.Album).SetName("Reset Albums"),
new TestCaseData(1,1,1,RequestType.Album).SetName("Reset Albums with all enabled"),
};
private static readonly IEnumerable<TestCaseData> DoNotResetData = new List<TestCaseData>
{
new TestCaseData(1, RequestType.Movie).SetName("1 Day(s)"),
new TestCaseData(-6, RequestType.TvShow).SetName("-6 Day(s)"),
new TestCaseData(-1, RequestType.TvShow).SetName("-1 Day(s)"),
new TestCaseData(-2, RequestType.Album).SetName("-2 Day(s)"),
new TestCaseData(-3, RequestType.TvShow).SetName("-3 Day(s)"),
new TestCaseData(-4, RequestType.Movie).SetName("-4 Day(s)"),
new TestCaseData(-5, RequestType.TvShow).SetName("-5 Day(s)"),
new TestCaseData(-6, RequestType.Movie).SetName("-6 Day(s)"),
new TestCaseData(0, RequestType.TvShow).SetName("0 Day(s)"),
};
private void SetupSettings(int movie, int tv, int album)
{
SettingsMock.Setup(x => x.GetSettings())
.Returns(new PlexRequestSettings { MovieWeeklyRequestLimit = movie, TvWeeklyRequestLimit = tv, AlbumWeeklyRequestLimit = album });
}
}
}

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AutoFixture" version="3.40.0" targetFramework="net452" />
<package id="Common.Logging" version="3.0.0" targetFramework="net452" />
<package id="Common.Logging.Core" version="3.0.0" targetFramework="net452" />
<package id="Moq" version="4.2.1510.2205" targetFramework="net46" />

@ -109,7 +109,8 @@ namespace PlexRequests.Services.Jobs
var users = allUsers.Where(x => x.RequestType == type);
foreach (var u in users)
{
if (u.FirstRequestDate > DateTime.UtcNow.AddDays(-7))
var daysDiff = (u.FirstRequestDate - DateTime.UtcNow.AddDays(-7)).Days;
if (daysDiff <= 0)
{
Repo.Delete(u);
}

@ -17,7 +17,7 @@
{
<label class="control-label"><a href="@Model.UpdateUri" target="_blank"><i class="fa fa-check"></i></a></label>
<br />
<button id="autoUpdate" class="btn btn-success-outline">Automatic Update <i class="fa fa-download"></i></button>
@*<button id="autoUpdate" class="btn btn-success-outline">Automatic Update <i class="fa fa-download"></i></button>*@
}
else
{

Loading…
Cancel
Save