Skip to content

Commit

Permalink
New: Better Application Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Qstick committed Jun 16, 2021
1 parent bbea256 commit 61bfa9e
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 24 deletions.
21 changes: 20 additions & 1 deletion src/NzbDrone.Core/Applications/Lidarr/Lidarr.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using FluentValidation.Results;
using Newtonsoft.Json.Linq;
using NLog;
Expand Down Expand Up @@ -31,7 +32,25 @@ public override ValidationResult Test()
{
var failures = new List<ValidationFailure>();

failures.AddIfNotNull(_lidarrV1Proxy.Test(Settings));
var testIndexer = new IndexerDefinition
{
Id = 0,
Name = "Test",
Protocol = DownloadProtocol.Usenet,
Capabilities = new IndexerCapabilities()
};

testIndexer.Capabilities.Categories.AddCategoryMapping(1, NewznabStandardCategory.Audio);

try
{
failures.AddIfNotNull(_lidarrV1Proxy.TestConnection(BuildLidarrIndexer(testIndexer, DownloadProtocol.Usenet), Settings));
}
catch (WebException ex)
{
_logger.Error(ex, "Unable to send test message");
failures.AddIfNotNull(new ValidationFailure("BaseUrl", "Unable to complete application test, cannot connect to Lidarr"));
}

return new ValidationResult(failures);
}
Expand Down
2 changes: 1 addition & 1 deletion src/NzbDrone.Core/Applications/Lidarr/LidarrSettings.cs
Expand Up @@ -28,7 +28,7 @@ public LidarrSettings()

public IEnumerable<int> SyncCategories { get; set; }

[FieldDefinition(0, Label = "Prowlarr Server", HelpText = "Prowlarr server URL as Lidarr sees it, including http(s):// and port if needed")]
[FieldDefinition(0, Label = "Prowlarr Server", HelpText = "Prowlarr server URL as Lidarr sees it, including http(s)://, port, and urlbase if needed")]
public string ProwlarrUrl { get; set; }

[FieldDefinition(1, Label = "Lidarr Server", HelpText = "Lidarr server URL, including http(s):// and port if needed")]
Expand Down
18 changes: 14 additions & 4 deletions src/NzbDrone.Core/Applications/Lidarr/LidarrV1Proxy.cs
Expand Up @@ -17,7 +17,7 @@ public interface ILidarrV1Proxy
List<LidarrIndexer> GetIndexerSchema(LidarrSettings settings);
void RemoveIndexer(int indexerId, LidarrSettings settings);
LidarrIndexer UpdateIndexer(LidarrIndexer indexer, LidarrSettings settings);
ValidationFailure Test(LidarrSettings settings);
ValidationFailure TestConnection(LidarrIndexer indexer, LidarrSettings settings);
}

public class LidarrV1Proxy : ILidarrV1Proxy
Expand Down Expand Up @@ -91,11 +91,15 @@ public LidarrIndexer UpdateIndexer(LidarrIndexer indexer, LidarrSettings setting
return Execute<LidarrIndexer>(request);
}

public ValidationFailure Test(LidarrSettings settings)
public ValidationFailure TestConnection(LidarrIndexer indexer, LidarrSettings settings)
{
var request = BuildRequest(settings, $"/api/v1/indexer/test", HttpMethod.POST);

request.SetContent(indexer.ToJson());

try
{
GetStatus(settings);
Execute<LidarrIndexer>(request);
}
catch (HttpException ex)
{
Expand All @@ -105,8 +109,14 @@ public ValidationFailure Test(LidarrSettings settings)
return new ValidationFailure("ApiKey", "API Key is invalid");
}

if (ex.Response.StatusCode == HttpStatusCode.BadRequest)
{
_logger.Error(ex, "Prowlarr URL is invalid");
return new ValidationFailure("ProwlarrUrl", "Prowlarr url is invalid, Lidarr cannot connect to Prowlarr");
}

_logger.Error(ex, "Unable to send test message");
return new ValidationFailure("ApiKey", "Unable to send test message");
return new ValidationFailure("BaseUrl", "Unable to complete application test");
}
catch (Exception ex)
{
Expand Down
21 changes: 20 additions & 1 deletion src/NzbDrone.Core/Applications/Radarr/Radarr.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using FluentValidation.Results;
using Newtonsoft.Json.Linq;
using NLog;
Expand Down Expand Up @@ -31,7 +32,25 @@ public override ValidationResult Test()
{
var failures = new List<ValidationFailure>();

failures.AddIfNotNull(_radarrV3Proxy.Test(Settings));
var testIndexer = new IndexerDefinition
{
Id = 0,
Name = "Test",
Protocol = DownloadProtocol.Usenet,
Capabilities = new IndexerCapabilities()
};

testIndexer.Capabilities.Categories.AddCategoryMapping(1, NewznabStandardCategory.Movies);

try
{
failures.AddIfNotNull(_radarrV3Proxy.TestConnection(BuildRadarrIndexer(testIndexer, DownloadProtocol.Usenet), Settings));
}
catch (WebException ex)
{
_logger.Error(ex, "Unable to send test message");
failures.AddIfNotNull(new ValidationFailure("BaseUrl", "Unable to complete application test, cannot connect to Radarr"));
}

return new ValidationResult(failures);
}
Expand Down
2 changes: 1 addition & 1 deletion src/NzbDrone.Core/Applications/Radarr/RadarrSettings.cs
Expand Up @@ -28,7 +28,7 @@ public RadarrSettings()

public IEnumerable<int> SyncCategories { get; set; }

[FieldDefinition(0, Label = "Prowlarr Server", HelpText = "Prowlarr server URL as Radarr sees it, including http(s):// and port if needed")]
[FieldDefinition(0, Label = "Prowlarr Server", HelpText = "Prowlarr server URL as Radarr sees it, including http(s)://, port, and urlbase if needed")]
public string ProwlarrUrl { get; set; }

[FieldDefinition(1, Label = "Radarr Server", HelpText = "Radarr server URL, including http(s):// and port if needed")]
Expand Down
18 changes: 14 additions & 4 deletions src/NzbDrone.Core/Applications/Radarr/RadarrV3Proxy.cs
Expand Up @@ -17,7 +17,7 @@ public interface IRadarrV3Proxy
List<RadarrIndexer> GetIndexerSchema(RadarrSettings settings);
void RemoveIndexer(int indexerId, RadarrSettings settings);
RadarrIndexer UpdateIndexer(RadarrIndexer indexer, RadarrSettings settings);
ValidationFailure Test(RadarrSettings settings);
ValidationFailure TestConnection(RadarrIndexer indexer, RadarrSettings settings);
}

public class RadarrV3Proxy : IRadarrV3Proxy
Expand Down Expand Up @@ -91,11 +91,15 @@ public RadarrIndexer UpdateIndexer(RadarrIndexer indexer, RadarrSettings setting
return Execute<RadarrIndexer>(request);
}

public ValidationFailure Test(RadarrSettings settings)
public ValidationFailure TestConnection(RadarrIndexer indexer, RadarrSettings settings)
{
var request = BuildRequest(settings, $"/api/v3/indexer/test", HttpMethod.POST);

request.SetContent(indexer.ToJson());

try
{
GetStatus(settings);
Execute<RadarrIndexer>(request);
}
catch (HttpException ex)
{
Expand All @@ -105,8 +109,14 @@ public ValidationFailure Test(RadarrSettings settings)
return new ValidationFailure("ApiKey", "API Key is invalid");
}

if (ex.Response.StatusCode == HttpStatusCode.BadRequest)
{
_logger.Error(ex, "Prowlarr URL is invalid");
return new ValidationFailure("ProwlarrUrl", "Prowlarr url is invalid, Radarr cannot connect to Prowlarr");
}

_logger.Error(ex, "Unable to send test message");
return new ValidationFailure("ApiKey", "Unable to send test message");
return new ValidationFailure("BaseUrl", "Unable to complete application test");
}
catch (Exception ex)
{
Expand Down
21 changes: 20 additions & 1 deletion src/NzbDrone.Core/Applications/Readarr/Readarr.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using FluentValidation.Results;
using Newtonsoft.Json.Linq;
using NLog;
Expand Down Expand Up @@ -31,7 +32,25 @@ public override ValidationResult Test()
{
var failures = new List<ValidationFailure>();

failures.AddIfNotNull(_readarrV1Proxy.Test(Settings));
var testIndexer = new IndexerDefinition
{
Id = 0,
Name = "Test",
Protocol = DownloadProtocol.Usenet,
Capabilities = new IndexerCapabilities()
};

testIndexer.Capabilities.Categories.AddCategoryMapping(1, NewznabStandardCategory.Books);

try
{
failures.AddIfNotNull(_readarrV1Proxy.TestConnection(BuildReadarrIndexer(testIndexer, DownloadProtocol.Usenet), Settings));
}
catch (WebException ex)
{
_logger.Error(ex, "Unable to send test message");
failures.AddIfNotNull(new ValidationFailure("BaseUrl", "Unable to complete application test, cannot connect to Readarr"));
}

return new ValidationResult(failures);
}
Expand Down
2 changes: 1 addition & 1 deletion src/NzbDrone.Core/Applications/Readarr/ReadarrSettings.cs
Expand Up @@ -28,7 +28,7 @@ public ReadarrSettings()

public IEnumerable<int> SyncCategories { get; set; }

[FieldDefinition(0, Label = "Prowlarr Server", HelpText = "Prowlarr server URL as Readarr sees it, including http(s):// and port if needed")]
[FieldDefinition(0, Label = "Prowlarr Server", HelpText = "Prowlarr server URL as Readarr sees it, including http(s)://, port, and urlbase if needed")]
public string ProwlarrUrl { get; set; }

[FieldDefinition(1, Label = "Readarr Server", HelpText = "Readarr server URL, including http(s):// and port if needed")]
Expand Down
18 changes: 14 additions & 4 deletions src/NzbDrone.Core/Applications/Readarr/ReadarrV1Proxy.cs
Expand Up @@ -17,7 +17,7 @@ public interface IReadarrV1Proxy
List<ReadarrIndexer> GetIndexerSchema(ReadarrSettings settings);
void RemoveIndexer(int indexerId, ReadarrSettings settings);
ReadarrIndexer UpdateIndexer(ReadarrIndexer indexer, ReadarrSettings settings);
ValidationFailure Test(ReadarrSettings settings);
ValidationFailure TestConnection(ReadarrIndexer indexer, ReadarrSettings settings);
}

public class ReadarrV1Proxy : IReadarrV1Proxy
Expand Down Expand Up @@ -91,11 +91,15 @@ public ReadarrIndexer UpdateIndexer(ReadarrIndexer indexer, ReadarrSettings sett
return Execute<ReadarrIndexer>(request);
}

public ValidationFailure Test(ReadarrSettings settings)
public ValidationFailure TestConnection(ReadarrIndexer indexer, ReadarrSettings settings)
{
var request = BuildRequest(settings, $"/api/v1/indexer/test", HttpMethod.POST);

request.SetContent(indexer.ToJson());

try
{
GetStatus(settings);
Execute<ReadarrIndexer>(request);
}
catch (HttpException ex)
{
Expand All @@ -105,8 +109,14 @@ public ValidationFailure Test(ReadarrSettings settings)
return new ValidationFailure("ApiKey", "API Key is invalid");
}

if (ex.Response.StatusCode == HttpStatusCode.BadRequest)
{
_logger.Error(ex, "Prowlarr URL is invalid");
return new ValidationFailure("ProwlarrUrl", "Prowlarr url is invalid, Readarr cannot connect to Prowlarr");
}

_logger.Error(ex, "Unable to send test message");
return new ValidationFailure("ApiKey", "Unable to send test message");
return new ValidationFailure("BaseUrl", "Unable to complete application test");
}
catch (Exception ex)
{
Expand Down
21 changes: 20 additions & 1 deletion src/NzbDrone.Core/Applications/Sonarr/Sonarr.cs
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using FluentValidation.Results;
using Newtonsoft.Json.Linq;
using NLog;
Expand Down Expand Up @@ -31,7 +32,25 @@ public override ValidationResult Test()
{
var failures = new List<ValidationFailure>();

failures.AddIfNotNull(_sonarrV3Proxy.Test(Settings));
var testIndexer = new IndexerDefinition
{
Id = 0,
Name = "Test",
Protocol = DownloadProtocol.Usenet,
Capabilities = new IndexerCapabilities()
};

testIndexer.Capabilities.Categories.AddCategoryMapping(1, NewznabStandardCategory.TV);

try
{
failures.AddIfNotNull(_sonarrV3Proxy.TestConnection(BuildSonarrIndexer(testIndexer, DownloadProtocol.Usenet), Settings));
}
catch (WebException ex)
{
_logger.Error(ex, "Unable to send test message");
failures.AddIfNotNull(new ValidationFailure("BaseUrl", "Unable to complete application test, cannot connect to Sonarr"));
}

return new ValidationResult(failures);
}
Expand Down
2 changes: 1 addition & 1 deletion src/NzbDrone.Core/Applications/Sonarr/SonarrSettings.cs
Expand Up @@ -28,7 +28,7 @@ public SonarrSettings()

public IEnumerable<int> SyncCategories { get; set; }

[FieldDefinition(0, Label = "Prowlarr Server", HelpText = "Prowlarr server URL as Sonarr sees it, including http(s):// and port if needed")]
[FieldDefinition(0, Label = "Prowlarr Server", HelpText = "Prowlarr server URL as Sonarr sees it, including http(s)://, port, and urlbase if needed")]
public string ProwlarrUrl { get; set; }

[FieldDefinition(1, Label = "Sonarr Server", HelpText = "Sonarr server URL, including http(s):// and port if needed")]
Expand Down
18 changes: 14 additions & 4 deletions src/NzbDrone.Core/Applications/Sonarr/SonarrV3Proxy.cs
Expand Up @@ -17,7 +17,7 @@ public interface ISonarrV3Proxy
List<SonarrIndexer> GetIndexerSchema(SonarrSettings settings);
void RemoveIndexer(int indexerId, SonarrSettings settings);
SonarrIndexer UpdateIndexer(SonarrIndexer indexer, SonarrSettings settings);
ValidationFailure Test(SonarrSettings settings);
ValidationFailure TestConnection(SonarrIndexer indexer, SonarrSettings settings);
}

public class SonarrV3Proxy : ISonarrV3Proxy
Expand Down Expand Up @@ -91,11 +91,15 @@ public SonarrIndexer UpdateIndexer(SonarrIndexer indexer, SonarrSettings setting
return Execute<SonarrIndexer>(request);
}

public ValidationFailure Test(SonarrSettings settings)
public ValidationFailure TestConnection(SonarrIndexer indexer, SonarrSettings settings)
{
var request = BuildRequest(settings, $"/api/v3/indexer/test", HttpMethod.POST);

request.SetContent(indexer.ToJson());

try
{
GetStatus(settings);
Execute<SonarrIndexer>(request);
}
catch (HttpException ex)
{
Expand All @@ -105,8 +109,14 @@ public ValidationFailure Test(SonarrSettings settings)
return new ValidationFailure("ApiKey", "API Key is invalid");
}

if (ex.Response.StatusCode == HttpStatusCode.BadRequest)
{
_logger.Error(ex, "Prowlarr URL is invalid");
return new ValidationFailure("ProwlarrUrl", "Prowlarr url is invalid, Sonarr cannot connect to Prowlarr");
}

_logger.Error(ex, "Unable to send test message");
return new ValidationFailure("ApiKey", "Unable to send test message");
return new ValidationFailure("BaseUrl", "Unable to complete application test");
}
catch (Exception ex)
{
Expand Down
33 changes: 33 additions & 0 deletions src/Prowlarr.Api.V1/Indexers/NewznabController.cs
Expand Up @@ -62,6 +62,39 @@ public async Task<IActionResult> GetNewznabResponse(int id, [FromQuery] NewznabR
}
}

if (id == 0)
{
switch (requestType)
{
case "caps":
var caps = new IndexerCapabilities();
foreach (var cat in NewznabStandardCategory.AllCats)
{
caps.Categories.AddCategoryMapping(1, cat);
}

return Content(caps.ToXml(), "application/rss+xml");
case "search":
case "tvsearch":
case "music":
case "book":
case "movie":
var results = new NewznabResults();
results.Releases = new List<ReleaseInfo>
{
new ReleaseInfo
{
Title = "Test Release",
Guid = "https://prowlarr.com",
DownloadUrl = "https://prowlarr.com",
PublishDate = DateTime.Now
}
};

return Content(results.ToXml(DownloadProtocol.Usenet), "application/rss+xml");
}
}

var indexer = _indexerFactory.Get(id);

if (indexer == null)
Expand Down

0 comments on commit 61bfa9e

Please sign in to comment.