1
0
Fork 0
mirror of https://github.com/Radarr/Radarr synced 2024-12-28 02:40:01 +00:00
Radarr/NzbDrone.Core.Test/ProviderTests/NewznabProviderTest.cs

220 lines
6.5 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using FizzWare.NBuilder;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Providers;
using NzbDrone.Core.Repository;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.ProviderTests
{
[TestFixture]
// ReSharper disable InconsistentNaming
2013-02-02 20:54:03 +00:00
public class NewznabProviderTest : SqlCeTest<NewznabProvider>
{
2013-01-19 18:19:29 +00:00
[SetUp]
public void SetUp()
{
WithRealDb();
InitiateSubject();
}
[Test]
public void Save_should_clean_url_before_inserting()
{
2013-01-19 18:19:29 +00:00
var newznab = new NewznabDefinition { Name = "Newznab Provider", Enable = true, Url = "http://www.nzbdrone.com/gibberish/test.aspx?hello=world" };
2013-01-19 18:19:29 +00:00
const string expectedUrl = "http://www.nzbdrone.com";
2013-01-19 18:19:29 +00:00
var result = Subject.Save(newznab);
2013-01-19 18:19:29 +00:00
Db.Single<NewznabDefinition>(result).Url.Should().Be(expectedUrl);
}
[Test]
public void Save_should_clean_url_before_updating()
{
2013-01-19 18:19:29 +00:00
var newznab = new NewznabDefinition { Name = "Newznab Provider", Enable = true, Url = "http://www.nzbdrone.com/gibberish/test.aspx?hello=world" };
2011-12-15 04:15:53 +00:00
2013-01-19 18:19:29 +00:00
var result = Subject.Save(newznab);
2011-11-15 23:11:17 +00:00
2013-01-19 18:19:29 +00:00
Subject.All().Single(c => c.Id == result).Url.Should().Be("http://www.nzbdrone.com");
2011-11-15 23:11:17 +00:00
}
[Test]
public void Save_should_clean_url_before_inserting_when_url_is_not_empty()
{
var newznab = new NewznabDefinition { Name = "Newznab Provider", Enable = true, Url = "" };
2013-01-19 18:19:29 +00:00
var result = Subject.Save(newznab);
2011-11-15 23:11:17 +00:00
2013-01-19 18:19:29 +00:00
Subject.All().Single(c => c.Id == result).Url.Should().Be("");
2011-11-15 23:11:17 +00:00
}
[Test]
public void Save_should_clean_url_before_updating_when_url_is_not_empty()
{
2013-01-19 18:19:29 +00:00
var newznab = new NewznabDefinition { Name = "Newznab Provider", Enable = true, Url = "" };
var result = Subject.Save(newznab);
2011-12-15 04:15:53 +00:00
2013-01-19 18:19:29 +00:00
Db.Single<NewznabDefinition>(result).Url.Should().Be("");
}
[Test]
[Ignore("No longer clean newznab URLs")]
public void SaveAll_should_clean_urls_before_updating()
{
2013-01-19 18:19:29 +00:00
var definitions = Builder<NewznabDefinition>.CreateListOfSize(5)
.All()
.With(d => d.Url = "http://www.nzbdrone.com")
.Build();
var expectedUrl = "http://www.nzbdrone.com";
var newUrl = "http://www.nzbdrone.com/gibberish/test.aspx?hello=world";
2013-01-19 18:19:29 +00:00
Db.InsertMany(definitions);
definitions.ToList().ForEach(d => d.Url = newUrl);
2013-01-19 18:19:29 +00:00
Subject.SaveAll(definitions);
Db.Fetch<NewznabDefinition>().Where(d => d.Url == expectedUrl).Should().HaveCount(5);
}
[Test]
public void Enabled_should_return_all_enabled_newznab_providers()
{
2013-01-19 18:19:29 +00:00
var definitions = Builder<NewznabDefinition>.CreateListOfSize(5)
.TheFirst(2)
.With(d => d.Enable = false)
.TheLast(3)
.With(d => d.Enable = true)
.Build();
2013-01-19 18:19:29 +00:00
Db.InsertMany(definitions);
var result = Subject.Enabled();
result.Should().HaveCount(3);
result.All(d => d.Enable).Should().BeTrue();
}
[Test]
public void All_should_return_all_newznab_providers()
{
2013-01-19 18:19:29 +00:00
var userIndexers = Builder<NewznabDefinition>.CreateListOfSize(5)
.All().With(c => c.Url = "http://www.host.com/12")
.Build();
2013-01-19 18:19:29 +00:00
Db.InsertMany(userIndexers);
2013-01-19 18:19:29 +00:00
var result = Subject.All();
2013-01-19 18:19:29 +00:00
result.Should().HaveCount(8);
}
2013-01-19 18:19:29 +00:00
[Test]
2013-01-19 18:19:29 +00:00
public void All_should_return_empty_list_when_no_indexers_exist()
{
2013-01-19 18:19:29 +00:00
Db.Delete<NewznabDefinition>("");
2013-01-19 18:19:29 +00:00
Subject.All().Should().NotBeNull();
Subject.All().Should().BeEmpty();
}
[Test]
2013-01-19 18:19:29 +00:00
public void Delete_should_delete_newznab_provider()
{
2013-01-19 18:19:29 +00:00
var toBeDelete = Subject.All()[2];
2013-01-19 18:19:29 +00:00
Subject.Delete(toBeDelete.Id);
2013-01-19 18:19:29 +00:00
Subject.All().Should().NotBeEmpty();
Subject.All().Should().NotContain(c => c.Id == toBeDelete.Id);
}
2013-01-19 05:14:12 +00:00
2013-01-19 18:19:29 +00:00
[Test]
public void InitializeNewznabIndexers_should_initialize_build_in_indexers()
{
var indexers = Subject.All();
2013-01-19 18:19:29 +00:00
indexers.Should().NotBeEmpty();
indexers.Should().OnlyContain(i => i.BuiltIn);
}
[Test]
public void InitializeNewznabIndexers_should_initialize_new_indexers_only()
{
2013-01-19 18:19:29 +00:00
var definitions = Builder<NewznabDefinition>.CreateListOfSize(5)
.All()
.With(d => d.Id = 0)
.TheFirst(2)
.With(d => d.Url = "http://www.nzbdrone2.com")
.TheLast(3)
.With(d => d.Url = "http://www.nzbdrone.com")
.Build();
2013-01-19 18:19:29 +00:00
Db.Insert(definitions[0]);
Db.Insert(definitions[2]);
2013-01-19 05:14:12 +00:00
Mocker.SetConstant<IEnumerable<NewznabDefinition>>(definitions);
2013-01-19 18:19:29 +00:00
var result = Db.Fetch<NewznabDefinition>();
result.Where(d => d.Url == "http://www.nzbdrone.com").Should().HaveCount(1);
result.Where(d => d.Url == "http://www.nzbdrone2.com").Should().HaveCount(1);
}
[Test]
public void InitializeNewznabIndexers_should_not_blow_up_if_more_than_one_indexer_with_the_same_url_is_found()
{
var definition = Builder<NewznabDefinition>.CreateNew()
.With(d => d.Url = "http://www.nzbdrone2.com")
.With(d => d.BuiltIn = false)
.Build();
2013-01-19 18:19:29 +00:00
Db.Insert(definition);
Db.Insert(definition);
2013-01-19 05:14:12 +00:00
Mocker.SetConstant<IEnumerable<NewznabDefinition>>(new List<NewznabDefinition> { definition });
2013-01-19 18:19:29 +00:00
var result = Db.Fetch<NewznabDefinition>().Where(c => c.BuiltIn == false);
result.Should().HaveCount(2);
}
[Test]
public void CheckHostname_should_do_nothing_if_hostname_is_valid()
{
2013-01-19 18:19:29 +00:00
Subject.CheckHostname("http://www.google.com");
}
[Test]
public void CheckHostname_should_log_error_and_throw_exception_if_dnsHostname_is_invalid()
{
2013-01-19 18:19:29 +00:00
Assert.Throws<SocketException>(() => Subject.CheckHostname("http://BadName"));
ExceptionVerification.ExpectedErrors(1);
}
}
}