Added validation in RoodDirProvider to stop adding of invalid root folders.

This commit is contained in:
kay.one 2011-06-12 20:56:10 -07:00
parent 8686eb5d32
commit 87079dcb52
2 changed files with 50 additions and 18 deletions

View File

@ -40,15 +40,14 @@ namespace NzbDrone.Core.Test
Assert.AreEqual(result.Count, 2); Assert.AreEqual(result.Count, 2);
} }
[Test] [TestCase("D:\\TV Shows\\")]
public void AddRootDir() [TestCase("//server//folder")]
public void AddRootDir(string path)
{ {
//Setup //Setup
var mocker = new AutoMoqer(); var mocker = new AutoMoqer();
mocker.SetConstant(MockLib.GetEmptyRepository()); mocker.SetConstant(MockLib.GetEmptyRepository());
const string path = @"C:\TV\";
//Act //Act
var rootDirProvider = mocker.Resolve<RootDirProvider>(); var rootDirProvider = mocker.Resolve<RootDirProvider>();
rootDirProvider.Add(new RootDir { Path = path }); rootDirProvider.Add(new RootDir { Path = path });
@ -62,25 +61,26 @@ namespace NzbDrone.Core.Test
Assert.AreEqual(path, rootDirs.First().Path); Assert.AreEqual(path, rootDirs.First().Path);
} }
[Test]
public void UpdateRootDir() [TestCase("D:\\TV Shows\\")]
[TestCase("//server//folder")]
public void UpdateRootDir(string newPath)
{ {
//Setup //Setup
var mocker = new AutoMoqer(); var mocker = new AutoMoqer();
mocker.SetConstant(MockLib.GetEmptyRepository()); mocker.SetConstant(MockLib.GetEmptyRepository());
const string path = @"C:\TV2";
//Act //Act
var rootDirProvider = mocker.Resolve<RootDirProvider>(); var rootDirProvider = mocker.Resolve<RootDirProvider>();
rootDirProvider.Add(new RootDir { Path = @"C:\TV" }); rootDirProvider.Add(new RootDir { Path = @"C:\TV" });
rootDirProvider.Update(new RootDir { Id = 1, Path = path }); rootDirProvider.Update(new RootDir { Id = 1, Path = newPath });
//Assert //Assert
var rootDirs = rootDirProvider.GetAll(); var rootDirs = rootDirProvider.GetAll();
Assert.IsNotEmpty(rootDirs); Assert.IsNotEmpty(rootDirs);
rootDirs.Should().HaveCount(1); rootDirs.Should().HaveCount(1);
Assert.AreEqual(path, rootDirs.First().Path); Assert.AreEqual(newPath, rootDirs.First().Path);
} }
[Test] [Test]
@ -144,5 +144,25 @@ namespace NzbDrone.Core.Test
var mocker = new AutoMoqer(); var mocker = new AutoMoqer();
mocker.Resolve<RootDirProvider>().GetUnmappedFolders(""); mocker.Resolve<RootDirProvider>().GetUnmappedFolders("");
} }
[TestCase("")]
[TestCase(null)]
[TestCase("BAD PATH")]
[ExpectedException(typeof(ArgumentException))]
public void invalid_folder_path_throws_on_add(string path)
{
var mocker = new AutoMoqer();
mocker.Resolve<RootDirProvider>().Add(new RootDir { Id = 0, Path = path });
}
[TestCase("")]
[TestCase(null)]
[TestCase("BAD PATH")]
[ExpectedException(typeof(ArgumentException))]
public void invalid_folder_path_throws_on_update(string path)
{
var mocker = new AutoMoqer();
mocker.Resolve<RootDirProvider>().Update(new RootDir { Id = 2, Path = path });
}
} }
} }

View File

@ -11,15 +11,15 @@ namespace NzbDrone.Core.Providers
{ {
public class RootDirProvider public class RootDirProvider
{ {
private readonly IRepository _sonioRepo; private readonly IRepository _repository;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly DiskProvider _diskProvider; private readonly DiskProvider _diskProvider;
private readonly SeriesProvider _seriesProvider; private readonly SeriesProvider _seriesProvider;
public RootDirProvider(IRepository sonicRepo, SeriesProvider seriesProvider, DiskProvider diskProvider) public RootDirProvider(IRepository repository, SeriesProvider seriesProvider, DiskProvider diskProvider)
{ {
_sonioRepo = sonicRepo; _repository = repository;
_diskProvider = diskProvider; _diskProvider = diskProvider;
_seriesProvider = seriesProvider; _seriesProvider = seriesProvider;
} }
@ -28,27 +28,39 @@ namespace NzbDrone.Core.Providers
public virtual List<RootDir> GetAll() public virtual List<RootDir> GetAll()
{ {
return _sonioRepo.All<RootDir>().ToList(); return _repository.All<RootDir>().ToList();
} }
public virtual int Add(RootDir rootDir) public virtual void Add(RootDir rootDir)
{ {
return Convert.ToInt32(_sonioRepo.Add(rootDir)); ValidatePath(rootDir);
_repository.Add(rootDir);
} }
public virtual void Remove(int rootDirId) public virtual void Remove(int rootDirId)
{ {
_sonioRepo.Delete<RootDir>(rootDirId); _repository.Delete<RootDir>(rootDirId);
} }
public virtual void Update(RootDir rootDir) public virtual void Update(RootDir rootDir)
{ {
_sonioRepo.Update(rootDir); ValidatePath(rootDir);
_repository.Update(rootDir);
}
private static void ValidatePath(RootDir rootDir)
{
if (String.IsNullOrWhiteSpace(rootDir.Path) || !Path.IsPathRooted(rootDir.Path))
{
throw new ArgumentException("Invalid path");
}
} }
public virtual RootDir GetRootDir(int rootDirId) public virtual RootDir GetRootDir(int rootDirId)
{ {
return _sonioRepo.Single<RootDir>(rootDirId); return _repository.Single<RootDir>(rootDirId);
} }
public List<String> GetUnmappedFolders(string path) public List<String> GetUnmappedFolders(string path)