Skip to content

Commit

Permalink
Merge branch 'prerel-1.4.2' into 'main'
Browse files Browse the repository at this point in the history
Prerel 1.4.2

See merge request personal/wedding-share!20
  • Loading branch information
Chris Collins committed Dec 23, 2024
2 parents 8de5223 + 9f5baf2 commit 7861eee
Show file tree
Hide file tree
Showing 22 changed files with 414 additions and 177 deletions.
24 changes: 12 additions & 12 deletions WeddingShare.UnitTests/Tests/Controllers/GalleryControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class GalleryControllerTests
{
private readonly IWebHostEnvironment _env = Substitute.For<IWebHostEnvironment>();
private readonly IConfigHelper _config = Substitute.For<IConfigHelper>();
private readonly ISecretKeyHelper _secretKey = Substitute.For<ISecretKeyHelper>();
private readonly IGalleryHelper _gallery = Substitute.For<IGalleryHelper>();
private readonly IDatabaseHelper _database = Substitute.For<IDatabaseHelper>();
private readonly IFileHelper _file = Substitute.For<IFileHelper>();
private readonly IDeviceDetector _deviceDetector = Substitute.For<IDeviceDetector>();
Expand Down Expand Up @@ -64,8 +64,8 @@ public void Setup()
_database.GetAllGalleryItems(Arg.Any<int>(), GalleryItemState.Pending).Returns(Task.FromResult(MockData.MockGalleryItems(10, 1, GalleryItemState.Pending)));
_database.GetAllGalleryItems(Arg.Any<int>(), GalleryItemState.Approved).Returns(Task.FromResult(MockData.MockGalleryItems(10, 1, GalleryItemState.Approved)));

_secretKey.GetGallerySecretKey(Arg.Any<string>()).Returns("password");
_secretKey.GetGallerySecretKey("missing").Returns("123456");
_gallery.GetSecretKey(Arg.Any<string>()).Returns("password");
_gallery.GetSecretKey("missing").Returns("123456");

_config.GetOrDefault("Settings", "Allowed_File_Types", Arg.Any<string>()).Returns(".jpg,.jpeg,.png");
_config.GetOrDefault("Settings", "Default_Gallery_View", Arg.Any<int>()).Returns((int)ViewMode.Default);
Expand All @@ -85,7 +85,7 @@ public async Task GalleryController_Index(DeviceType deviceType, string id, stri
_deviceDetector.ParseDeviceType(Arg.Any<string>()).Returns(deviceType);
_config.GetOrDefault("Settings", "Single_Gallery_Mode", Arg.Any<bool>()).Returns(false);

var controller = new GalleryController(_env, _config, _database, _file, _secretKey, _deviceDetector, _image, _notification, _logger, _localizer);
var controller = new GalleryController(_env, _config, _database, _file, _gallery, _deviceDetector, _image, _notification, _logger, _localizer);
controller.ControllerContext.HttpContext = MockData.MockHttpContext();

ViewResult actual = (ViewResult)await controller.Index(id, key, mode, order);
Expand All @@ -110,7 +110,7 @@ public async Task GalleryController_Index_SingleGalleryMode(DeviceType deviceTyp
_deviceDetector.ParseDeviceType(Arg.Any<string>()).Returns(deviceType);
_config.GetOrDefault("Settings", "Single_Gallery_Mode", Arg.Any<bool>()).Returns(true);

var controller = new GalleryController(_env, _config, _database, _file, _secretKey, _deviceDetector, _image, _notification, _logger, _localizer);
var controller = new GalleryController(_env, _config, _database, _file, _gallery, _deviceDetector, _image, _notification, _logger, _localizer);
controller.ControllerContext.HttpContext = MockData.MockHttpContext();

ViewResult actual = (ViewResult)await controller.Index("default", "password", mode, order);
Expand Down Expand Up @@ -141,7 +141,7 @@ public async Task GalleryController_UploadImage(bool requiresReview, int fileCou
files.Add(new FormFile(null, 0, 0, "TestFile_001", $"{Guid.NewGuid()}.jpg"));
}

var controller = new GalleryController(_env, _config, _database, _file, _secretKey, _deviceDetector, _image, _notification, _logger, _localizer);
var controller = new GalleryController(_env, _config, _database, _file, _gallery, _deviceDetector, _image, _notification, _logger, _localizer);
controller.ControllerContext.HttpContext = MockData.MockHttpContext(
form: new Dictionary<string, StringValues>
{
Expand All @@ -164,7 +164,7 @@ public async Task GalleryController_UploadImage(bool requiresReview, int fileCou
[TestCase("")]
public async Task GalleryController_UploadImage_InvalidGallery(string? id)
{
var controller = new GalleryController(_env, _config, _database, _file, _secretKey, _deviceDetector, _image, _notification, _logger, _localizer);
var controller = new GalleryController(_env, _config, _database, _file, _gallery, _deviceDetector, _image, _notification, _logger, _localizer);
controller.ControllerContext.HttpContext = MockData.MockHttpContext(form: new Dictionary<string, StringValues>
{
{ "Id", id }
Expand All @@ -182,7 +182,7 @@ public async Task GalleryController_UploadImage_InvalidGallery(string? id)
[TestCase("")]
public async Task GalleryController_UploadImage_InvalidSecretKey(string? key)
{
var controller = new GalleryController(_env, _config, _database, _file, _secretKey, _deviceDetector, _image, _notification, _logger, _localizer);
var controller = new GalleryController(_env, _config, _database, _file, _gallery, _deviceDetector, _image, _notification, _logger, _localizer);
controller.ControllerContext.HttpContext = MockData.MockHttpContext(form: new Dictionary<string, StringValues>
{
{ "Id", "default" },
Expand All @@ -200,7 +200,7 @@ public async Task GalleryController_UploadImage_InvalidSecretKey(string? key)
[TestCase()]
public async Task GalleryController_UploadImage_MissingGallery()
{
var controller = new GalleryController(_env, _config, _database, _file, _secretKey, _deviceDetector, _image, _notification, _logger, _localizer);
var controller = new GalleryController(_env, _config, _database, _file, _gallery, _deviceDetector, _image, _notification, _logger, _localizer);
controller.ControllerContext.HttpContext = MockData.MockHttpContext(form: new Dictionary<string, StringValues>
{
{ "Id", Guid.NewGuid().ToString() }
Expand All @@ -217,7 +217,7 @@ public async Task GalleryController_UploadImage_MissingGallery()
[TestCase()]
public async Task GalleryController_UploadImage_NoFiles()
{
var controller = new GalleryController(_env, _config, _database, _file, _secretKey, _deviceDetector, _image, _notification, _logger, _localizer);
var controller = new GalleryController(_env, _config, _database, _file, _gallery, _deviceDetector, _image, _notification, _logger, _localizer);
controller.ControllerContext.HttpContext = MockData.MockHttpContext(form: new Dictionary<string, StringValues>
{
{ "Id", "default" },
Expand All @@ -235,7 +235,7 @@ public async Task GalleryController_UploadImage_NoFiles()
[TestCase()]
public async Task GalleryController_UploadImage_FileTooBig()
{
var controller = new GalleryController(_env, _config, _database, _file, _secretKey, _deviceDetector, _image, _notification, _logger, _localizer);
var controller = new GalleryController(_env, _config, _database, _file, _gallery, _deviceDetector, _image, _notification, _logger, _localizer);
controller.ControllerContext.HttpContext = MockData.MockHttpContext(
form: new Dictionary<string, StringValues>
{
Expand All @@ -257,7 +257,7 @@ public async Task GalleryController_UploadImage_FileTooBig()
[TestCase()]
public async Task GalleryController_UploadImage_InvalidFileType()
{
var controller = new GalleryController(_env, _config, _database, _file, _secretKey, _deviceDetector, _image, _notification, _logger, _localizer);
var controller = new GalleryController(_env, _config, _database, _file, _gallery, _deviceDetector, _image, _notification, _logger, _localizer);
controller.ControllerContext.HttpContext = MockData.MockHttpContext(
form: new Dictionary<string, StringValues>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace WeddingShare.UnitTests.Tests.Helpers
public class HomeControllerTests
{
private readonly IConfigHelper _config = Substitute.For<IConfigHelper>();
private readonly ISecretKeyHelper _secretKey = Substitute.For<ISecretKeyHelper>();
private readonly IGalleryHelper _gallery = Substitute.For<IGalleryHelper>();
private readonly IDeviceDetector _deviceDetector = Substitute.For<IDeviceDetector>();
private readonly ILogger<HomeController> _logger = Substitute.For<ILogger<HomeController>>();

Expand All @@ -36,9 +36,9 @@ public async Task HomeController_Index(DeviceType deviceType, bool singleGallery
{
_deviceDetector.ParseDeviceType(Arg.Any<string>()).Returns(deviceType);
_config.GetOrDefault("Settings", "Single_Gallery_Mode", Arg.Any<bool>()).Returns(singleGalleryMode);
_secretKey.GetGallerySecretKey(Arg.Any<string>()).Returns(secretKey);
_gallery.GetSecretKey(Arg.Any<string>()).Returns(secretKey);

var controller = new HomeController(_config, _secretKey, _deviceDetector, _logger);
var controller = new HomeController(_config, _gallery, _deviceDetector, _logger);
controller.ControllerContext.HttpContext = new DefaultHttpContext()
{
Session = new MockSession()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

namespace WeddingShare.UnitTests.Tests.Helpers
{
public class SecretKeyHelperTests
public class GalleryHelperTests
{
private readonly IDatabaseHelper _database = Substitute.For<IDatabaseHelper>();

public SecretKeyHelperTests()
public GalleryHelperTests()
{
_database.GetGallery("Gallery1").Returns(new GalleryModel() { SecretKey = "001" });
_database.GetGallery("Gallery2").Returns(new GalleryModel() { SecretKey = "002" });
Expand All @@ -22,7 +22,7 @@ public void Setup()
}

[TestCase()]
public async Task SecretKeyHelper_GetGallerySecretKey_DefaultEnvKey()
public async Task GalleryHelper_GetSecretKey_DefaultEnvKey()
{
var environment = Substitute.For<IEnvironmentWrapper>();
environment.GetEnvironmentVariable("SECRET_KEY").Returns("123");
Expand All @@ -34,12 +34,12 @@ public async Task SecretKeyHelper_GetGallerySecretKey_DefaultEnvKey()

var config = new ConfigHelper(environment, configuration, Substitute.For<ILogger<ConfigHelper>>());

var actual = await new SecretKeyHelper(config, _database).GetGallerySecretKey("Gallery3");
var actual = await new GalleryHelper(config, _database).GetSecretKey("Gallery3");
Assert.That(actual, Is.EqualTo("123"));
}

[TestCase()]
public async Task SecretKeyHelper_GetGallerySecretKey_GalleryEnvKey()
public async Task GalleryHelper_GetSecretKey_GalleryEnvKey()
{
var environment = Substitute.For<IEnvironmentWrapper>();
environment.GetEnvironmentVariable("SECRET_KEY").Returns("123");
Expand All @@ -52,14 +52,14 @@ public async Task SecretKeyHelper_GetGallerySecretKey_GalleryEnvKey()

var config = new ConfigHelper(environment, configuration, Substitute.For<ILogger<ConfigHelper>>());

var actual = await new SecretKeyHelper(config, _database).GetGallerySecretKey("Gallery1");
var actual = await new GalleryHelper(config, _database).GetSecretKey("Gallery1");
Assert.That(actual, Is.EqualTo("001"));
}

[TestCase("Gallery1", "001")]
[TestCase("Gallery2", "002")]
[TestCase("Gallery3", null)]
public async Task SecretKeyHelper_GetGallerySecretKey_Database(string galleryId, string key)
public async Task GalleryHelper_GetSecretKey_Database(string galleryId, string key)
{
var environment = Substitute.For<IEnvironmentWrapper>();
environment.GetEnvironmentVariable(Arg.Any<string>()).Returns(string.Empty);
Expand All @@ -68,8 +68,43 @@ public async Task SecretKeyHelper_GetGallerySecretKey_Database(string galleryId,

var config = new ConfigHelper(environment, configuration, Substitute.For<ILogger<ConfigHelper>>());

var actual = await new SecretKeyHelper(config, _database).GetGallerySecretKey(galleryId);
var actual = await new GalleryHelper(config, _database).GetSecretKey(galleryId);
Assert.That(actual, Is.EqualTo(key));
}

[TestCase()]
public async Task GalleryHelper_GetConfig_DefaultEnvKey()
{
var environment = Substitute.For<IEnvironmentWrapper>();
environment.GetEnvironmentVariable("SECRET_KEY").Returns("123");

var configuration = ConfigurationHelper.MockConfiguration(new Dictionary<string, string?>()
{
{ "Secret_Key_Gallery2", "002" }
});

var config = new ConfigHelper(environment, configuration, Substitute.For<ILogger<ConfigHelper>>());

var actual = new GalleryHelper(config, _database).GetConfig("Gallery3", "Secret_Key");
Assert.That(actual, Is.EqualTo("123"));
}

[TestCase()]
public async Task GalleryHelper_GetConfig_GalleryEnvKey()
{
var environment = Substitute.For<IEnvironmentWrapper>();
environment.GetEnvironmentVariable("SECRET_KEY").Returns("123");
environment.GetEnvironmentVariable("SECRET_KEY_GALLERY1").Returns("001");

var configuration = ConfigurationHelper.MockConfiguration(new Dictionary<string, string?>()
{
{ "Secret_Key_Gallery2", "002" }
});

var config = new ConfigHelper(environment, configuration, Substitute.For<ILogger<ConfigHelper>>());

var actual = new GalleryHelper(config, _database).GetConfig("Gallery1", "Secret_Key");
Assert.That(actual, Is.EqualTo("001"));
}
}
}
6 changes: 3 additions & 3 deletions WeddingShare/Attributes/RequiresSecretKeyAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var request = filterContext.HttpContext.Request;

var secretKeyHelper = filterContext.HttpContext.RequestServices.GetService<ISecretKeyHelper>();
if (secretKeyHelper != null)
var galleryHelper = filterContext.HttpContext.RequestServices.GetService<IGalleryHelper>();
if (galleryHelper != null)
{
var galleryId = (request.Query.ContainsKey("id") && !string.IsNullOrWhiteSpace(request.Query["id"])) ? request.Query["id"].ToString().ToLower() : "default";
var secretKey = secretKeyHelper.GetGallerySecretKey(galleryId).Result;
var secretKey = galleryHelper.GetSecretKey(galleryId).Result;

var key = request.Query.ContainsKey("key") ? request.Query["key"].ToString() : string.Empty;
if (!string.IsNullOrWhiteSpace(secretKey) && !string.Equals(secretKey, key))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static void AddDependencyInjectionConfiguration(this IServiceCollection s
{
services.AddSingleton<IConfigHelper, ConfigHelper>();
services.AddSingleton<IEnvironmentWrapper, EnvironmentWrapper>();
services.AddSingleton<ISecretKeyHelper, SecretKeyHelper>();
services.AddSingleton<IGalleryHelper, GalleryHelper>();
services.AddSingleton<IImageHelper, ImageHelper>();
services.AddSingleton<IFileHelper, FileHelper>();
services.AddSingleton<IDeviceDetector, DeviceDetector>();
Expand Down
59 changes: 59 additions & 0 deletions WeddingShare/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,65 @@ public async Task<IActionResult> ReviewPhoto(int id, ReviewAction action)
return Json(new { success = false });
}

[HttpPost]
public async Task<IActionResult> BulkReview(ReviewAction action)
{
if (User?.Identity != null && User.Identity.IsAuthenticated)
{
try
{
var items = await _database.GetPendingGalleryItems();
if (items != null && items.Any())
{
foreach (var review in items)
{
var galleryDir = Path.Combine(UploadsDirectory, review.GalleryName);
var reviewFile = Path.Combine(galleryDir, "Pending", review.Title);
if (action == ReviewAction.APPROVED)
{
_fileHelper.CreateDirectoryIfNotExists(ThumbnailsDirectory);

await _imageHelper.GenerateThumbnail(reviewFile, Path.Combine(ThumbnailsDirectory, $"{Path.GetFileNameWithoutExtension(reviewFile)}.webp"), _config.GetOrDefault("Settings", "Thumbnail_Size", 720));

_fileHelper.MoveFileIfExists(reviewFile, Path.Combine(galleryDir, review.Title));

review.State = GalleryItemState.Approved;
await _database.EditGalleryItem(review);
}
else if (action == ReviewAction.REJECTED)
{
var retain = _config.GetOrDefault("Settings", "Retain_Rejected_Items", false);
if (retain)
{
var rejectedDir = Path.Combine(galleryDir, "Rejected");
_fileHelper.CreateDirectoryIfNotExists(rejectedDir);
_fileHelper.MoveFileIfExists(reviewFile, Path.Combine(rejectedDir, review.Title));
}
else
{
_fileHelper.DeleteFileIfExists(reviewFile);
}

await _database.DeleteGalleryItem(review);
}
else if (action == ReviewAction.UNKNOWN)
{
throw new Exception(_localizer["Unknown_Review_Action"].Value);
}
}
}

return Json(new { success = true, action });
}
catch (Exception ex)
{
_logger.LogError(ex, $"{_localizer["Failed_Reviewing_Photo"].Value} - {ex?.Message}");
}
}

return Json(new { success = false });
}

[HttpPost]
public async Task<IActionResult> AddGallery(GalleryModel model)
{
Expand Down
Loading

0 comments on commit 7861eee

Please sign in to comment.