(cherry picked from commit c3d54b312ef18b837d54605ea78f1a263fd6900b)pull/2438/head
parent
10e230cc06
commit
b97d63cb5b
@ -0,0 +1,166 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using FizzWare.NBuilder;
|
||||
using FluentAssertions;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using NzbDrone.Core.Books;
|
||||
using NzbDrone.Core.DecisionEngine;
|
||||
using NzbDrone.Core.Indexers;
|
||||
using NzbDrone.Core.IndexerSearch;
|
||||
using NzbDrone.Core.IndexerSearch.Definitions;
|
||||
using NzbDrone.Core.Test.Framework;
|
||||
|
||||
namespace NzbDrone.Core.Test.IndexerSearchTests
|
||||
{
|
||||
public class ReleaseSearchServiceFixture : CoreTest<ReleaseSearchService>
|
||||
{
|
||||
private Mock<IIndexer> _mockIndexer;
|
||||
private Author _author;
|
||||
private Book _firstBook;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_mockIndexer = Mocker.GetMock<IIndexer>();
|
||||
_mockIndexer.SetupGet(s => s.Definition).Returns(new IndexerDefinition { Id = 1 });
|
||||
_mockIndexer.SetupGet(s => s.SupportsSearch).Returns(true);
|
||||
|
||||
Mocker.GetMock<IIndexerFactory>()
|
||||
.Setup(s => s.AutomaticSearchEnabled(true))
|
||||
.Returns(new List<IIndexer> { _mockIndexer.Object });
|
||||
|
||||
Mocker.GetMock<IMakeDownloadDecision>()
|
||||
.Setup(s => s.GetSearchDecision(It.IsAny<List<Parser.Model.ReleaseInfo>>(), It.IsAny<SearchCriteriaBase>()))
|
||||
.Returns(new List<DownloadDecision>());
|
||||
|
||||
_author = Builder<Author>.CreateNew()
|
||||
.With(v => v.Monitored = true)
|
||||
.Build();
|
||||
|
||||
_firstBook = Builder<Book>.CreateNew()
|
||||
.With(e => e.Author = _author)
|
||||
.Build();
|
||||
|
||||
var edition = Builder<Edition>.CreateNew()
|
||||
.With(e => e.Book = _firstBook)
|
||||
.With(e => e.Monitored = true)
|
||||
.Build();
|
||||
|
||||
_firstBook.Editions = new List<Edition> { edition };
|
||||
|
||||
Mocker.GetMock<IAuthorService>()
|
||||
.Setup(v => v.GetAuthor(_author.Id))
|
||||
.Returns(_author);
|
||||
}
|
||||
|
||||
private List<SearchCriteriaBase> WatchForSearchCriteria()
|
||||
{
|
||||
var result = new List<SearchCriteriaBase>();
|
||||
|
||||
_mockIndexer.Setup(v => v.Fetch(It.IsAny<BookSearchCriteria>()))
|
||||
.Callback<BookSearchCriteria>(s => result.Add(s))
|
||||
.Returns(new List<Parser.Model.ReleaseInfo>());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Tags_IndexerTags_AuthorNoTags_IndexerNotIncluded()
|
||||
{
|
||||
_mockIndexer.SetupGet(s => s.Definition).Returns(new IndexerDefinition
|
||||
{
|
||||
Id = 1,
|
||||
Tags = new HashSet<int> { 3 }
|
||||
});
|
||||
|
||||
var allCriteria = WatchForSearchCriteria();
|
||||
|
||||
Subject.BookSearch(_firstBook, false, true, false);
|
||||
|
||||
var criteria = allCriteria.OfType<BookSearchCriteria>().ToList();
|
||||
|
||||
criteria.Count.Should().Be(0);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Tags_IndexerNoTags_AuthorTags_IndexerIncluded()
|
||||
{
|
||||
_mockIndexer.SetupGet(s => s.Definition).Returns(new IndexerDefinition
|
||||
{
|
||||
Id = 1
|
||||
});
|
||||
|
||||
_author = Builder<Author>.CreateNew()
|
||||
.With(v => v.Monitored = true)
|
||||
.With(v => v.Tags = new HashSet<int> { 3 })
|
||||
.Build();
|
||||
|
||||
Mocker.GetMock<IAuthorService>()
|
||||
.Setup(v => v.GetAuthor(_author.Id))
|
||||
.Returns(_author);
|
||||
|
||||
var allCriteria = WatchForSearchCriteria();
|
||||
|
||||
Subject.BookSearch(_firstBook, false, true, false);
|
||||
|
||||
var criteria = allCriteria.OfType<BookSearchCriteria>().ToList();
|
||||
|
||||
criteria.Count.Should().Be(1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Tags_IndexerAndAuthorTagsMatch_IndexerIncluded()
|
||||
{
|
||||
_mockIndexer.SetupGet(s => s.Definition).Returns(new IndexerDefinition
|
||||
{
|
||||
Id = 1,
|
||||
Tags = new HashSet<int> { 1, 2, 3 }
|
||||
});
|
||||
|
||||
_author = Builder<Author>.CreateNew()
|
||||
.With(v => v.Monitored = true)
|
||||
.With(v => v.Tags = new HashSet<int> { 3, 4, 5 })
|
||||
.Build();
|
||||
|
||||
Mocker.GetMock<IAuthorService>()
|
||||
.Setup(v => v.GetAuthor(_author.Id))
|
||||
.Returns(_author);
|
||||
|
||||
var allCriteria = WatchForSearchCriteria();
|
||||
|
||||
Subject.BookSearch(_firstBook, false, true, false);
|
||||
|
||||
var criteria = allCriteria.OfType<BookSearchCriteria>().ToList();
|
||||
|
||||
criteria.Count.Should().Be(1);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Tags_IndexerAndAuthorTagsMismatch_IndexerNotIncluded()
|
||||
{
|
||||
_mockIndexer.SetupGet(s => s.Definition).Returns(new IndexerDefinition
|
||||
{
|
||||
Id = 1,
|
||||
Tags = new HashSet<int> { 1, 2, 3 }
|
||||
});
|
||||
|
||||
_author = Builder<Author>.CreateNew()
|
||||
.With(v => v.Monitored = true)
|
||||
.With(v => v.Tags = new HashSet<int> { 4, 5, 6 })
|
||||
.Build();
|
||||
|
||||
Mocker.GetMock<IAuthorService>()
|
||||
.Setup(v => v.GetAuthor(_author.Id))
|
||||
.Returns(_author);
|
||||
|
||||
var allCriteria = WatchForSearchCriteria();
|
||||
|
||||
Subject.BookSearch(_firstBook, false, true, false);
|
||||
|
||||
var criteria = allCriteria.OfType<BookSearchCriteria>().ToList();
|
||||
|
||||
criteria.Count.Should().Be(0);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
using FluentMigrator;
|
||||
using NzbDrone.Core.Datastore.Migration.Framework;
|
||||
|
||||
namespace NzbDrone.Core.Datastore.Migration
|
||||
{
|
||||
[Migration(028)]
|
||||
public class add_indexer_tags : NzbDroneMigrationBase
|
||||
{
|
||||
protected override void MainDbUpgrade()
|
||||
{
|
||||
Alter.Table("Indexers").AddColumn("Tags").AsString().Nullable();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
using System.Linq;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Core.Indexers;
|
||||
using NzbDrone.Core.IndexerSearch.Definitions;
|
||||
using NzbDrone.Core.Parser.Model;
|
||||
|
||||
namespace NzbDrone.Core.DecisionEngine.Specifications.RssSync
|
||||
{
|
||||
public class IndexerTagSpecification : IDecisionEngineSpecification
|
||||
{
|
||||
private readonly Logger _logger;
|
||||
private readonly IIndexerRepository _indexerRepository;
|
||||
|
||||
public IndexerTagSpecification(Logger logger, IIndexerRepository indexerRepository)
|
||||
{
|
||||
_logger = logger;
|
||||
_indexerRepository = indexerRepository;
|
||||
}
|
||||
|
||||
public SpecificationPriority Priority => SpecificationPriority.Default;
|
||||
public RejectionType Type => RejectionType.Permanent;
|
||||
|
||||
public virtual Decision IsSatisfiedBy(RemoteBook subject, SearchCriteriaBase searchCriteria)
|
||||
{
|
||||
// If indexer has tags, check that at least one of them is present on the author
|
||||
var indexerTags = _indexerRepository.Get(subject.Release.IndexerId).Tags;
|
||||
|
||||
if (indexerTags.Any() && indexerTags.Intersect(subject.Author.Tags).Empty())
|
||||
{
|
||||
_logger.Debug("Indexer {0} has tags. None of these are present on author {1}. Rejecting", subject.Release.Indexer, subject.Author);
|
||||
|
||||
return Decision.Reject("Author tags do not match any of the indexer tags");
|
||||
}
|
||||
|
||||
return Decision.Accept();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue