Kavita/API.Tests/Helpers/CacheHelperTests.cs
Joseph Milazzo efb527035d
Account Email Support (#1000)
* Moved the Server Settings out into a button on nav header

* Refactored Mange Users page to the new design (skeleton). Implemented skeleton code for Invite User.

* Hashed out more of the code, but need to move all the email code to a Kavita controlled API server due to password credentials.

* Cleaned up some warnings

* When no user exists for an api key in Plugin controller, throw 401.

* Hooked in the ability to check if the Kavita instance can be accessed externally so we can determine if the user can invite or not.

* Hooked up some logic if the user's server isn't accessible, then default to old flow

* Basic flow is working for confirm email. Needs validation, error handling, etc.

* Refactored Password validation to account service

* Cleaned up the code in confirm-email to work much better.

* Refactored the login page to have a container functionality, so we can reuse the styles on multiple pages (registration pages). Hooked up the code for confirm email.

* Messy code, but making progress. Refactored Register to be used only for first time user registration. Added a new register component to handle first time flow only.

* Invite works much better, still needs a bit of work for non-accessible server setup. Started work on underlying manage users page to meet new design.

* Changed (you) to a star to indicate who you're logged in as.

* Inviting a user is now working and tested fully.

* Removed the register member component as we now have invite and confirm components.

* Editing a user is now working. Username change and Role/Library access from within one screen. Email changing is on hold.

* Cleaned up code for edit user and disabled email field for now.

* Cleaned up the code to indicate changing a user's email is not possible.

* Implemented a migration for existing accounts so they can validate their emails and still login.

* Change url for email server

* Implemented the ability to resend an email confirmation code (or regenerate for non accessible servers). Fixed an overflow on the confirm dialog.

* Took care of some code cleanup

* Removed 3 db calls from cover refresh and some misc cleanup

* Fixed a broken test
2022-01-30 14:45:57 -08:00

292 lines
9.9 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions.TestingHelpers;
using API.Entities;
using API.Helpers;
using API.Services;
using Xunit;
namespace API.Tests.Helpers;
public class CacheHelperTests
{
private const string TestCoverImageDirectory = @"c:\";
private const string TestCoverImageFile = "thumbnail.jpg";
private readonly string _testCoverPath = Path.Join(TestCoverImageDirectory, TestCoverImageFile);
private const string TestCoverArchive = @"file in folder.zip";
private readonly ICacheHelper _cacheHelper;
public CacheHelperTests()
{
var file = new MockFileData("")
{
LastWriteTime = DateTimeOffset.Now.Subtract(TimeSpan.FromMinutes(1))
};
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ Path.Join(TestCoverImageDirectory, TestCoverArchive), file },
{ Path.Join(TestCoverImageDirectory, TestCoverImageFile), file }
});
var fileService = new FileService(fileSystem);
_cacheHelper = new CacheHelper(fileService);
}
[Theory]
[InlineData("", false)]
[InlineData("C:/", false)]
[InlineData(null, false)]
public void CoverImageExists_DoesFileExist(string coverImage, bool exists)
{
Assert.Equal(exists, _cacheHelper.CoverImageExists(coverImage));
}
[Fact]
public void CoverImageExists_FileExists()
{
Assert.True(_cacheHelper.CoverImageExists(TestCoverArchive));
}
[Fact]
public void ShouldUpdateCoverImage_OnFirstRun()
{
var file = new MangaFile()
{
FilePath = TestCoverArchive,
LastModified = DateTime.Now
};
Assert.True(_cacheHelper.ShouldUpdateCoverImage(null, file, DateTime.Now.Subtract(TimeSpan.FromMinutes(1)),
false, false));
}
[Fact]
public void ShouldUpdateCoverImage_ShouldNotUpdateOnSecondRunWithCoverImageSetNotLocked()
{
// Represents first run
var file = new MangaFile()
{
FilePath = TestCoverArchive,
LastModified = DateTime.Now
};
Assert.False(_cacheHelper.ShouldUpdateCoverImage(_testCoverPath, file, DateTime.Now.Subtract(TimeSpan.FromMinutes(1)),
false, false));
}
[Fact]
public void ShouldUpdateCoverImage_ShouldNotUpdateOnSecondRunWithCoverImageSetNotLocked_2()
{
// Represents first run
var file = new MangaFile()
{
FilePath = TestCoverArchive,
LastModified = DateTime.Now
};
Assert.False(_cacheHelper.ShouldUpdateCoverImage(_testCoverPath, file, DateTime.Now,
false, false));
}
[Fact]
public void ShouldUpdateCoverImage_ShouldNotUpdateOnSecondRunWithCoverImageSetLocked()
{
// Represents first run
var file = new MangaFile()
{
FilePath = TestCoverArchive,
LastModified = DateTime.Now
};
Assert.False(_cacheHelper.ShouldUpdateCoverImage(_testCoverPath, file, DateTime.Now.Subtract(TimeSpan.FromMinutes(1)),
false, true));
}
[Fact]
public void ShouldUpdateCoverImage_ShouldNotUpdateOnSecondRunWithCoverImageSetLocked_Modified()
{
// Represents first run
var file = new MangaFile()
{
FilePath = TestCoverArchive,
LastModified = DateTime.Now
};
Assert.False(_cacheHelper.ShouldUpdateCoverImage(_testCoverPath, file, DateTime.Now.Subtract(TimeSpan.FromMinutes(1)),
false, true));
}
[Fact]
public void ShouldUpdateCoverImage_CoverImageSetAndReplaced_Modified()
{
var filesystemFile = new MockFileData("")
{
LastWriteTime = DateTimeOffset.Now
};
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ Path.Join(TestCoverImageDirectory, TestCoverArchive), filesystemFile },
{ Path.Join(TestCoverImageDirectory, TestCoverImageFile), filesystemFile }
});
var fileService = new FileService(fileSystem);
var cacheHelper = new CacheHelper(fileService);
var created = DateTime.Now.Subtract(TimeSpan.FromHours(1));
var file = new MangaFile()
{
FilePath = TestCoverArchive,
LastModified = DateTime.Now.Subtract(TimeSpan.FromMinutes(1))
};
Assert.True(cacheHelper.ShouldUpdateCoverImage(_testCoverPath, file, created,
false, false));
}
[Fact]
public void HasFileNotChangedSinceCreationOrLastScan_NotChangedSinceCreated()
{
var filesystemFile = new MockFileData("")
{
LastWriteTime = DateTimeOffset.Now
};
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ Path.Join(TestCoverImageDirectory, TestCoverArchive), filesystemFile },
{ Path.Join(TestCoverImageDirectory, TestCoverImageFile), filesystemFile }
});
var fileService = new FileService(fileSystem);
var cacheHelper = new CacheHelper(fileService);
var chapter = new Chapter()
{
Created = filesystemFile.LastWriteTime.DateTime,
LastModified = filesystemFile.LastWriteTime.DateTime
};
var file = new MangaFile()
{
FilePath = TestCoverArchive,
LastModified = filesystemFile.LastWriteTime.DateTime
};
Assert.True(cacheHelper.HasFileNotChangedSinceCreationOrLastScan(chapter, false, file));
}
[Fact]
public void HasFileNotChangedSinceCreationOrLastScan_NotChangedSinceLastModified()
{
var filesystemFile = new MockFileData("")
{
LastWriteTime = DateTimeOffset.Now
};
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ Path.Join(TestCoverImageDirectory, TestCoverArchive), filesystemFile },
{ Path.Join(TestCoverImageDirectory, TestCoverImageFile), filesystemFile }
});
var fileService = new FileService(fileSystem);
var cacheHelper = new CacheHelper(fileService);
var chapter = new Chapter()
{
Created = filesystemFile.LastWriteTime.DateTime,
LastModified = filesystemFile.LastWriteTime.DateTime
};
var file = new MangaFile()
{
FilePath = TestCoverArchive,
LastModified = filesystemFile.LastWriteTime.DateTime
};
Assert.True(cacheHelper.HasFileNotChangedSinceCreationOrLastScan(chapter, false, file));
}
[Fact]
public void HasFileNotChangedSinceCreationOrLastScan_NotChangedSinceLastModified_ForceUpdate()
{
var filesystemFile = new MockFileData("")
{
LastWriteTime = DateTimeOffset.Now
};
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ Path.Join(TestCoverImageDirectory, TestCoverArchive), filesystemFile },
{ Path.Join(TestCoverImageDirectory, TestCoverImageFile), filesystemFile }
});
var fileService = new FileService(fileSystem);
var cacheHelper = new CacheHelper(fileService);
var chapter = new Chapter()
{
Created = filesystemFile.LastWriteTime.DateTime,
LastModified = filesystemFile.LastWriteTime.DateTime
};
var file = new MangaFile()
{
FilePath = TestCoverArchive,
LastModified = filesystemFile.LastWriteTime.DateTime
};
Assert.False(cacheHelper.HasFileNotChangedSinceCreationOrLastScan(chapter, true, file));
}
[Fact]
public void HasFileNotChangedSinceCreationOrLastScan_ModifiedSinceLastScan()
{
var filesystemFile = new MockFileData("")
{
LastWriteTime = DateTimeOffset.Now
};
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ Path.Join(TestCoverImageDirectory, TestCoverArchive), filesystemFile },
{ Path.Join(TestCoverImageDirectory, TestCoverImageFile), filesystemFile }
});
var fileService = new FileService(fileSystem);
var cacheHelper = new CacheHelper(fileService);
var chapter = new Chapter()
{
Created = filesystemFile.LastWriteTime.DateTime.Subtract(TimeSpan.FromMinutes(10)),
LastModified = filesystemFile.LastWriteTime.DateTime.Subtract(TimeSpan.FromMinutes(10))
};
var file = new MangaFile()
{
FilePath = Path.Join(TestCoverImageDirectory, TestCoverArchive),
LastModified = filesystemFile.LastWriteTime.DateTime
};
Assert.False(cacheHelper.HasFileNotChangedSinceCreationOrLastScan(chapter, false, file));
}
[Fact]
public void HasFileNotChangedSinceCreationOrLastScan_ModifiedSinceLastScan_ButLastModifiedSame()
{
var filesystemFile = new MockFileData("")
{
LastWriteTime = DateTimeOffset.Now
};
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ Path.Join(TestCoverImageDirectory, TestCoverArchive), filesystemFile },
{ Path.Join(TestCoverImageDirectory, TestCoverImageFile), filesystemFile }
});
var fileService = new FileService(fileSystem);
var cacheHelper = new CacheHelper(fileService);
var chapter = new Chapter()
{
Created = filesystemFile.LastWriteTime.DateTime.Subtract(TimeSpan.FromMinutes(10)),
LastModified = filesystemFile.LastWriteTime.DateTime
};
var file = new MangaFile()
{
FilePath = Path.Join(TestCoverImageDirectory, TestCoverArchive),
LastModified = filesystemFile.LastWriteTime.DateTime
};
Assert.False(cacheHelper.HasFileNotChangedSinceCreationOrLastScan(chapter, false, file));
}
}