Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Feb 21, 2024
1 parent a60e5c6 commit 6dd617d
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 4 deletions.
2 changes: 1 addition & 1 deletion starsky-tools/mock/api/env/features.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"systemTrashEnabled":false,"useLocalDesktop":false}
{"systemTrashEnabled":false,"useLocalDesktop":false, "openEditorEnabled": false}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ namespace starsky.feature.desktop.Interfaces;

public interface IOpenEditorDesktopService
{
/// <summary>
/// Check if the file is less then the amount of files that are allowed to open
/// If there are more files to open it will return false and the front-end will ask for confirmation
/// </summary>
/// <param name="f">dot comma list of paths</param>
/// <returns>true is no confirmation and false ask are you sure</returns>
bool OpenAmountConfirmationChecker(string f);

/// <summary>
/// Is supported and enabled in the feature toggle
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,43 @@ public OpenEditorDesktopService(AppSettings appSettings,
_openEditorPreflight = openEditorPreflight;
}

/// <summary>
/// Get value from App Settings without getting a negative value
/// </summary>
/// <returns>setting</returns>
private int GetDesktopEditorAmountBeforeConfirmation()
{
var desktopEditorAmountBeforeConfirmation =
_appSettings.DesktopEditorAmountBeforeConfirmation ??
DesktopEditorAmountBeforeConfirmationDefault;
if ( _appSettings.DesktopEditorAmountBeforeConfirmation <= 1 )
{
desktopEditorAmountBeforeConfirmation = DesktopEditorAmountBeforeConfirmationDefault;
}

return desktopEditorAmountBeforeConfirmation;
}

/// <summary>
/// Default Setting for Desktop Editor Amount Before Confirmation
/// </summary>
private const int DesktopEditorAmountBeforeConfirmationDefault = 5;

/// <summary>
/// Check for Desktop Editor Amount Before Confirmation
/// </summary>
/// <param name="f">dot comma seperated values</param>
/// <returns>true</returns>
public bool OpenAmountConfirmationChecker(string f)
{
var inputFilePaths = PathHelper.SplitInputFilePaths(f);
return GetDesktopEditorAmountBeforeConfirmation() >= inputFilePaths.Length;
}

/// <summary>
/// Is feature toggle enabled and supported
/// </summary>
/// <returns>true is feature toggle enabled and supported</returns>
public bool IsEnabled()
{
return _appSettings.UseLocalDesktop == true &&
Expand Down
10 changes: 9 additions & 1 deletion starsky/starsky.foundation.platform/Models/AppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,9 @@ public Dictionary<string, string>? AccountRolesByEmailRegisterOverwrite
"/lost+found", "/.stfolder", "/.git"
};

/// <summary>
/// Auto Sync on Startup
/// </summary>
public bool? SyncOnStartup { get; set; } = true;

/// <summary>
Expand All @@ -747,6 +750,7 @@ public Dictionary<string, string>? AccountRolesByEmailRegisterOverwrite
/// But it seems a lot of cameras don't do this
/// We assume that the standard is followed, and for Camera brands that don't follow the specs use this setting.
/// </summary>
[PackageTelemetry]
public List<CameraMakeModel>? VideoUseLocalTime { get; set; } = new List<CameraMakeModel>
{
new CameraMakeModel("Sony", "A58")
Expand All @@ -757,7 +761,6 @@ public Dictionary<string, string>? AccountRolesByEmailRegisterOverwrite
/// </summary>
private bool? EnablePackageTelemetryPrivate { get; set; }


/// <summary>
/// Disable logout buttons in UI
/// And hides server specific features that are strange on a local desktop
Expand All @@ -779,6 +782,11 @@ public Dictionary<string, string>? AccountRolesByEmailRegisterOverwrite
public CollectionsOpenType.RawJpegMode DesktopCollectionsOpen { get; set; } =
CollectionsOpenType.RawJpegMode.Default;

/// <summary>
/// Number of files to open before confirmation
/// </summary>
public int? DesktopEditorAmountBeforeConfirmation { get; set; }

/// <summary>
/// Helps us improve the software
/// Please keep this enabled
Expand Down
18 changes: 18 additions & 0 deletions starsky/starsky/Controllers/DesktopEditorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,22 @@ public async Task<IActionResult> OpenAsync(

return Json(list);
}


/// <summary>
/// Check the amount of files to open before
/// </summary>
/// <param name="f">single or multiple subPaths</param>
/// <returns></returns>
/// <response code="200">bool, true is no confirmation, false is ask confirmation</response>
/// <response code="401">User unauthorized</response>
[HttpGet("/api/desktop-editor/amount-confirmation")]
[Produces("application/json")]
[ProducesResponseType(typeof(bool), 200)]
[ProducesResponseType(401)]
public IActionResult OpenAmountConfirmationChecker(string f)
{
var result = _openEditorDesktopService.OpenAmountConfirmationChecker(f);
return Json(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void FeaturesViewTest_Disabled()
// Arrange
var fakeIMoveToTrashService = new FakeIMoveToTrashService(new List<FileIndexItem>(), false);
var appSettingsFeaturesController = new AppSettingsFeaturesController(
fakeIMoveToTrashService, new FakeIOpenEditorDesktopService(),
fakeIMoveToTrashService, new FakeIOpenEditorDesktopService(false),
new AppSettings { UseLocalDesktop = false });

// Act
Expand All @@ -46,6 +46,7 @@ public void FeaturesViewTest_Disabled()
// Assert
Assert.IsFalse(json.UseLocalDesktop);
Assert.IsFalse(json.SystemTrashEnabled);
Assert.IsFalse(json.OpenEditorEnabled);
}

[TestMethod]
Expand All @@ -65,5 +66,6 @@ public void FeaturesViewTest_Enabled()
// Assert
Assert.IsTrue(json.UseLocalDesktop);
Assert.IsTrue(json.SystemTrashEnabled);
Assert.IsTrue(json.OpenEditorEnabled);
}
}
22 changes: 22 additions & 0 deletions starsky/starskytest/Controllers/DesktopEditorControllerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@ namespace starskytest.Controllers;
[TestClass]
public class DesktopEditorControllerTest
{
[TestMethod]
public void OpenAmountConfirmationChecker_FeatureToggleEnabled()
{
var controller = new DesktopEditorController(
new OpenEditorDesktopService(new AppSettings(),
new FakeIOpenApplicationNativeService(new List<string>(), "test"),
new FakeIOpenEditorPreflight(new List<PathImageFormatExistsAppPathModel>())));

controller.ControllerContext = new ControllerContext
{
HttpContext = new DefaultHttpContext()
};

var result = controller.OpenAmountConfirmationChecker("/test.jpg;/test2.jpg");

var castedResult = ( JsonResult )result;
var boolValue = ( bool? )castedResult.Value;
// mock is always true

Assert.IsTrue(boolValue);
}

[TestMethod]
public async Task OpenAsync_FeatureToggleDisabled()
{
Expand Down
12 changes: 11 additions & 1 deletion starsky/starskytest/FakeMocks/FakeIOpenEditorDesktopService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,21 @@ public class FakeIOpenEditorDesktopService : IOpenEditorDesktopService
{
private readonly bool _isEnabled;

public FakeIOpenEditorDesktopService(bool isEnabled = true)
public FakeIOpenEditorDesktopService()
{
_isEnabled = true;
}

public FakeIOpenEditorDesktopService(bool isEnabled)
{
_isEnabled = isEnabled;
}

public bool OpenAmountConfirmationChecker(string f)
{
return true;
}

public bool IsEnabled()
{
return _isEnabled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,66 @@ public async Task OpenAsync_ListInput_UnSupportedPlatform()
Assert.AreEqual("OpenEditor is not supported on this configuration", status);
Assert.AreEqual(0, list.Count);
}

[TestMethod]
public void OpenAmountConfirmationChecker_6Files()
{
var appSettings = new AppSettings { DesktopEditorAmountBeforeConfirmation = 5 };

var service = new OpenEditorDesktopService(appSettings,
new FakeIOpenApplicationNativeService(new List<string>(), "test"),
new FakeIOpenEditorPreflight(new List<PathImageFormatExistsAppPathModel>()));

var result =
service.OpenAmountConfirmationChecker(
"/test.jpg;/test2.jpg;/test3.jpg;/test4.jpg;/test5.jpg;/test6.jpg");
Assert.IsFalse(result);
}

[TestMethod]
public void OpenAmountConfirmationChecker_6Files_Null()
{
var appSettings = new AppSettings { DesktopEditorAmountBeforeConfirmation = null };

var service = new OpenEditorDesktopService(appSettings,
new FakeIOpenApplicationNativeService(new List<string>(), "test"),
new FakeIOpenEditorPreflight(new List<PathImageFormatExistsAppPathModel>()));

var result =
service.OpenAmountConfirmationChecker(
"/test.jpg;/test2.jpg;/test3.jpg;/test4.jpg;/test5.jpg;/test6.jpg");

// Assumes that the default value is 5
Assert.IsFalse(result);
}

[TestMethod]
public void OpenAmountConfirmationChecker_4Files()
{
var appSettings = new AppSettings { DesktopEditorAmountBeforeConfirmation = 4 };

var service = new OpenEditorDesktopService(appSettings,
new FakeIOpenApplicationNativeService(new List<string>(), "test"),
new FakeIOpenEditorPreflight(new List<PathImageFormatExistsAppPathModel>()));

var result =
service.OpenAmountConfirmationChecker("/test.jpg;/test2.jpg;/test3.jpg;/test4.jpg");
Assert.IsTrue(result);
}

[TestMethod]
public void OpenAmountConfirmationChecker_1File()
{
var appSettings = new AppSettings
{
DesktopEditorAmountBeforeConfirmation = -90 // invalid value
};

var service = new OpenEditorDesktopService(appSettings,
new FakeIOpenApplicationNativeService(new List<string>(), "test"),
new FakeIOpenEditorPreflight(new List<PathImageFormatExistsAppPathModel>()));

var result = service.OpenAmountConfirmationChecker("/test.jpg");
Assert.IsTrue(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public void GetMimeTypeByExtensionTest_NoExtension()
Assert.AreEqual("application/octet-stream",
MimeHelper.GetMimeTypeByFileName(string.Empty));
}

[TestMethod]
public void GetMimeTypeByExtensionTest_Null()
{
Assert.AreEqual("application/octet-stream",
MimeHelper.GetMimeTypeByFileName(null));
}

[TestMethod]
public void GetMimeType_NoExtension()
Expand Down

0 comments on commit 6dd617d

Please sign in to comment.