|
|
@ -9,6 +9,7 @@ using System.Collections.Generic; |
|
|
using System.Linq; |
|
|
using System.Linq; |
|
|
using System.Threading.Tasks; |
|
|
using System.Threading.Tasks; |
|
|
using FluentAssertions; |
|
|
using FluentAssertions; |
|
|
|
|
|
using Squidex.Domain.Apps.Core.Assets; |
|
|
using Squidex.Domain.Apps.Core.Schemas; |
|
|
using Squidex.Domain.Apps.Core.Schemas; |
|
|
using Squidex.Domain.Apps.Core.TestHelpers; |
|
|
using Squidex.Domain.Apps.Core.TestHelpers; |
|
|
using Squidex.Domain.Apps.Core.ValidateContent; |
|
|
using Squidex.Domain.Apps.Core.ValidateContent; |
|
|
@ -22,17 +23,24 @@ namespace Squidex.Domain.Apps.Core.Operations.ValidateContent.Validators |
|
|
public class AssetsValidatorTests : IClassFixture<TranslationsFixture> |
|
|
public class AssetsValidatorTests : IClassFixture<TranslationsFixture> |
|
|
{ |
|
|
{ |
|
|
private readonly List<string> errors = new List<string>(); |
|
|
private readonly List<string> errors = new List<string>(); |
|
|
private readonly IAssetInfo document = TestAssets.Document(DomainId.NewGuid()); |
|
|
private static readonly IAssetInfo Document = TestAssets.Document(DomainId.NewGuid()); |
|
|
private readonly IAssetInfo image1 = TestAssets.Image(DomainId.NewGuid()); |
|
|
private static readonly IAssetInfo Image1 = TestAssets.Image(DomainId.NewGuid()); |
|
|
private readonly IAssetInfo image2 = TestAssets.Image(DomainId.NewGuid()); |
|
|
private static readonly IAssetInfo Image2 = TestAssets.Image(DomainId.NewGuid()); |
|
|
private readonly IAssetInfo imageSvg = TestAssets.Svg(DomainId.NewGuid()); |
|
|
private static readonly IAssetInfo ImageSvg = TestAssets.Svg(DomainId.NewGuid()); |
|
|
|
|
|
private static readonly IAssetInfo Video = TestAssets.Video(DomainId.NewGuid()); |
|
|
|
|
|
|
|
|
|
|
|
public static IEnumerable<object[]> AssetsWithDimensions() |
|
|
|
|
|
{ |
|
|
|
|
|
yield return new object[] { Image1.AssetId }; |
|
|
|
|
|
yield return new object[] { Video.AssetId }; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
[Fact] |
|
|
[Fact] |
|
|
public async Task Should_not_add_error_if_assets_are_valid() |
|
|
public async Task Should_not_add_error_if_assets_are_valid() |
|
|
{ |
|
|
{ |
|
|
var sut = Validator(new AssetsFieldProperties()); |
|
|
var sut = Validator(new AssetsFieldProperties()); |
|
|
|
|
|
|
|
|
await sut.ValidateAsync(CreateValue(document.AssetId), errors); |
|
|
await sut.ValidateAsync(CreateValue(Document.AssetId), errors); |
|
|
|
|
|
|
|
|
Assert.Empty(errors); |
|
|
Assert.Empty(errors); |
|
|
} |
|
|
} |
|
|
@ -62,7 +70,7 @@ namespace Squidex.Domain.Apps.Core.Operations.ValidateContent.Validators |
|
|
{ |
|
|
{ |
|
|
var sut = Validator(new AssetsFieldProperties { AllowDuplicates = true }); |
|
|
var sut = Validator(new AssetsFieldProperties { AllowDuplicates = true }); |
|
|
|
|
|
|
|
|
await sut.ValidateAsync(CreateValue(image1.AssetId, image1.AssetId), errors); |
|
|
await sut.ValidateAsync(CreateValue(Image1.AssetId, Image1.AssetId), errors); |
|
|
|
|
|
|
|
|
Assert.Empty(errors); |
|
|
Assert.Empty(errors); |
|
|
} |
|
|
} |
|
|
@ -70,9 +78,9 @@ namespace Squidex.Domain.Apps.Core.Operations.ValidateContent.Validators |
|
|
[Fact] |
|
|
[Fact] |
|
|
public async Task Should_not_add_error_if_asset_is_an_image() |
|
|
public async Task Should_not_add_error_if_asset_is_an_image() |
|
|
{ |
|
|
{ |
|
|
var sut = Validator(new AssetsFieldProperties { MustBeImage = true }); |
|
|
var sut = Validator(new AssetsFieldProperties { ExpectedType = AssetType.Image }); |
|
|
|
|
|
|
|
|
await sut.ValidateAsync(CreateValue(imageSvg.AssetId, image1.AssetId), errors); |
|
|
await sut.ValidateAsync(CreateValue(ImageSvg.AssetId, Image1.AssetId), errors); |
|
|
|
|
|
|
|
|
Assert.Empty(errors); |
|
|
Assert.Empty(errors); |
|
|
} |
|
|
} |
|
|
@ -106,7 +114,7 @@ namespace Squidex.Domain.Apps.Core.Operations.ValidateContent.Validators |
|
|
{ |
|
|
{ |
|
|
var sut = Validator(new AssetsFieldProperties { MinSize = 5 * 1024 }); |
|
|
var sut = Validator(new AssetsFieldProperties { MinSize = 5 * 1024 }); |
|
|
|
|
|
|
|
|
await sut.ValidateAsync(CreateValue(document.AssetId, image1.AssetId), errors); |
|
|
await sut.ValidateAsync(CreateValue(Document.AssetId, Image1.AssetId), errors); |
|
|
|
|
|
|
|
|
errors.Should().BeEquivalentTo( |
|
|
errors.Should().BeEquivalentTo( |
|
|
new[] { "[1]: Size of 4 kB must be greater than 5 kB." }); |
|
|
new[] { "[1]: Size of 4 kB must be greater than 5 kB." }); |
|
|
@ -117,7 +125,7 @@ namespace Squidex.Domain.Apps.Core.Operations.ValidateContent.Validators |
|
|
{ |
|
|
{ |
|
|
var sut = Validator(new AssetsFieldProperties { MaxSize = 5 * 1024 }); |
|
|
var sut = Validator(new AssetsFieldProperties { MaxSize = 5 * 1024 }); |
|
|
|
|
|
|
|
|
await sut.ValidateAsync(CreateValue(document.AssetId, image1.AssetId), errors); |
|
|
await sut.ValidateAsync(CreateValue(Document.AssetId, Image1.AssetId), errors); |
|
|
|
|
|
|
|
|
errors.Should().BeEquivalentTo( |
|
|
errors.Should().BeEquivalentTo( |
|
|
new[] { "[2]: Size of 8 kB must be less than 5 kB." }); |
|
|
new[] { "[2]: Size of 8 kB must be less than 5 kB." }); |
|
|
@ -126,64 +134,69 @@ namespace Squidex.Domain.Apps.Core.Operations.ValidateContent.Validators |
|
|
[Fact] |
|
|
[Fact] |
|
|
public async Task Should_add_error_if_document_is_not_an_image() |
|
|
public async Task Should_add_error_if_document_is_not_an_image() |
|
|
{ |
|
|
{ |
|
|
var sut = Validator(new AssetsFieldProperties { MustBeImage = true }); |
|
|
var sut = Validator(new AssetsFieldProperties { ExpectedType = AssetType.Image }); |
|
|
|
|
|
|
|
|
await sut.ValidateAsync(CreateValue(document.AssetId, image1.AssetId), errors); |
|
|
await sut.ValidateAsync(CreateValue(Document.AssetId, Image1.AssetId), errors); |
|
|
|
|
|
|
|
|
errors.Should().BeEquivalentTo( |
|
|
errors.Should().BeEquivalentTo( |
|
|
new[] { "[1]: Not an image." }); |
|
|
new[] { "[1]: Not of expected type: Image." }); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
[Fact] |
|
|
[Theory] |
|
|
public async Task Should_add_error_if_image_width_is_too_small() |
|
|
[MemberData(nameof(AssetsWithDimensions))] |
|
|
|
|
|
public async Task Should_add_error_if_asset_width_is_too_small(DomainId videoOrImageId) |
|
|
{ |
|
|
{ |
|
|
var sut = Validator(new AssetsFieldProperties { MinWidth = 1000 }); |
|
|
var sut = Validator(new AssetsFieldProperties { MinWidth = 1000 }); |
|
|
|
|
|
|
|
|
await sut.ValidateAsync(CreateValue(document.AssetId, image1.AssetId), errors); |
|
|
await sut.ValidateAsync(CreateValue(Document.AssetId, videoOrImageId), errors); |
|
|
|
|
|
|
|
|
errors.Should().BeEquivalentTo( |
|
|
errors.Should().BeEquivalentTo( |
|
|
new[] { "[2]: Width 800px must be greater than 1000px." }); |
|
|
new[] { "[2]: Width 800px must be greater than 1000px." }); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
[Fact] |
|
|
[Theory] |
|
|
public async Task Should_add_error_if_image_width_is_too_big() |
|
|
[MemberData(nameof(AssetsWithDimensions))] |
|
|
|
|
|
public async Task Should_add_error_if_asset_width_is_too_big(DomainId videoOrImageId) |
|
|
{ |
|
|
{ |
|
|
var sut = Validator(new AssetsFieldProperties { MaxWidth = 700 }); |
|
|
var sut = Validator(new AssetsFieldProperties { MaxWidth = 700 }); |
|
|
|
|
|
|
|
|
await sut.ValidateAsync(CreateValue(document.AssetId, image1.AssetId), errors); |
|
|
await sut.ValidateAsync(CreateValue(Document.AssetId, videoOrImageId), errors); |
|
|
|
|
|
|
|
|
errors.Should().BeEquivalentTo( |
|
|
errors.Should().BeEquivalentTo( |
|
|
new[] { "[2]: Width 800px must be less than 700px." }); |
|
|
new[] { "[2]: Width 800px must be less than 700px." }); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
[Fact] |
|
|
[Theory] |
|
|
public async Task Should_add_error_if_image_height_is_too_small() |
|
|
[MemberData(nameof(AssetsWithDimensions))] |
|
|
|
|
|
public async Task Should_add_error_if_asset_height_is_too_small(DomainId videoOrImageId) |
|
|
{ |
|
|
{ |
|
|
var sut = Validator(new AssetsFieldProperties { MinHeight = 800 }); |
|
|
var sut = Validator(new AssetsFieldProperties { MinHeight = 800 }); |
|
|
|
|
|
|
|
|
await sut.ValidateAsync(CreateValue(document.AssetId, image1.AssetId), errors); |
|
|
await sut.ValidateAsync(CreateValue(Document.AssetId, videoOrImageId), errors); |
|
|
|
|
|
|
|
|
errors.Should().BeEquivalentTo( |
|
|
errors.Should().BeEquivalentTo( |
|
|
new[] { "[2]: Height 600px must be greater than 800px." }); |
|
|
new[] { "[2]: Height 600px must be greater than 800px." }); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
[Fact] |
|
|
[Theory] |
|
|
public async Task Should_add_error_if_image_height_is_too_big() |
|
|
[MemberData(nameof(AssetsWithDimensions))] |
|
|
|
|
|
public async Task Should_add_error_if_asset_height_is_too_big(DomainId videoOrImageId) |
|
|
{ |
|
|
{ |
|
|
var sut = Validator(new AssetsFieldProperties { MaxHeight = 500 }); |
|
|
var sut = Validator(new AssetsFieldProperties { MaxHeight = 500 }); |
|
|
|
|
|
|
|
|
await sut.ValidateAsync(CreateValue(document.AssetId, image1.AssetId), errors); |
|
|
await sut.ValidateAsync(CreateValue(Document.AssetId, videoOrImageId), errors); |
|
|
|
|
|
|
|
|
errors.Should().BeEquivalentTo( |
|
|
errors.Should().BeEquivalentTo( |
|
|
new[] { "[2]: Height 600px must be less than 500px." }); |
|
|
new[] { "[2]: Height 600px must be less than 500px." }); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
[Fact] |
|
|
[Theory] |
|
|
public async Task Should_add_error_if_image_has_invalid_aspect_ratio() |
|
|
[MemberData(nameof(AssetsWithDimensions))] |
|
|
|
|
|
public async Task Should_add_error_if_asset_has_invalid_aspect_ratio(DomainId videoOrImageId) |
|
|
{ |
|
|
{ |
|
|
var sut = Validator(new AssetsFieldProperties { AspectWidth = 1, AspectHeight = 1 }); |
|
|
var sut = Validator(new AssetsFieldProperties { AspectWidth = 1, AspectHeight = 1 }); |
|
|
|
|
|
|
|
|
await sut.ValidateAsync(CreateValue(document.AssetId, image1.AssetId), errors); |
|
|
await sut.ValidateAsync(CreateValue(Document.AssetId, videoOrImageId), errors); |
|
|
|
|
|
|
|
|
errors.Should().BeEquivalentTo( |
|
|
errors.Should().BeEquivalentTo( |
|
|
new[] { "[2]: Must have aspect ratio 1:1." }); |
|
|
new[] { "[2]: Must have aspect ratio 1:1." }); |
|
|
@ -194,7 +207,7 @@ namespace Squidex.Domain.Apps.Core.Operations.ValidateContent.Validators |
|
|
{ |
|
|
{ |
|
|
var sut = Validator(new AssetsFieldProperties { MinItems = 2 }); |
|
|
var sut = Validator(new AssetsFieldProperties { MinItems = 2 }); |
|
|
|
|
|
|
|
|
await sut.ValidateAsync(CreateValue(image1.AssetId), errors); |
|
|
await sut.ValidateAsync(CreateValue(Image1.AssetId), errors); |
|
|
|
|
|
|
|
|
errors.Should().BeEquivalentTo( |
|
|
errors.Should().BeEquivalentTo( |
|
|
new[] { "Must have at least 2 item(s)." }); |
|
|
new[] { "Must have at least 2 item(s)." }); |
|
|
@ -205,7 +218,7 @@ namespace Squidex.Domain.Apps.Core.Operations.ValidateContent.Validators |
|
|
{ |
|
|
{ |
|
|
var sut = Validator(new AssetsFieldProperties { MaxItems = 1 }); |
|
|
var sut = Validator(new AssetsFieldProperties { MaxItems = 1 }); |
|
|
|
|
|
|
|
|
await sut.ValidateAsync(CreateValue(image1.AssetId, image2.AssetId), errors); |
|
|
await sut.ValidateAsync(CreateValue(Image1.AssetId, Image2.AssetId), errors); |
|
|
|
|
|
|
|
|
errors.Should().BeEquivalentTo( |
|
|
errors.Should().BeEquivalentTo( |
|
|
new[] { "Must not have more than 1 item(s)." }); |
|
|
new[] { "Must not have more than 1 item(s)." }); |
|
|
@ -216,7 +229,7 @@ namespace Squidex.Domain.Apps.Core.Operations.ValidateContent.Validators |
|
|
{ |
|
|
{ |
|
|
var sut = Validator(new AssetsFieldProperties()); |
|
|
var sut = Validator(new AssetsFieldProperties()); |
|
|
|
|
|
|
|
|
await sut.ValidateAsync(CreateValue(image1.AssetId, image1.AssetId), errors); |
|
|
await sut.ValidateAsync(CreateValue(Image1.AssetId, Image1.AssetId), errors); |
|
|
|
|
|
|
|
|
errors.Should().BeEquivalentTo( |
|
|
errors.Should().BeEquivalentTo( |
|
|
new[] { "Must not contain duplicate values." }); |
|
|
new[] { "Must not contain duplicate values." }); |
|
|
@ -227,7 +240,7 @@ namespace Squidex.Domain.Apps.Core.Operations.ValidateContent.Validators |
|
|
{ |
|
|
{ |
|
|
var sut = Validator(new AssetsFieldProperties { AllowedExtensions = ImmutableList.Create("mp4") }); |
|
|
var sut = Validator(new AssetsFieldProperties { AllowedExtensions = ImmutableList.Create("mp4") }); |
|
|
|
|
|
|
|
|
await sut.ValidateAsync(CreateValue(document.AssetId, image1.AssetId), errors); |
|
|
await sut.ValidateAsync(CreateValue(Document.AssetId, Image1.AssetId), errors); |
|
|
|
|
|
|
|
|
errors.Should().BeEquivalentTo( |
|
|
errors.Should().BeEquivalentTo( |
|
|
new[] |
|
|
new[] |
|
|
@ -242,16 +255,16 @@ namespace Squidex.Domain.Apps.Core.Operations.ValidateContent.Validators |
|
|
return ids.ToList(); |
|
|
return ids.ToList(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private IValidator Validator(AssetsFieldProperties properties) |
|
|
private static IValidator Validator(AssetsFieldProperties properties) |
|
|
{ |
|
|
{ |
|
|
return new AssetsValidator(properties.IsRequired, properties, FoundAssets()); |
|
|
return new AssetsValidator(properties.IsRequired, properties, FoundAssets()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private CheckAssets FoundAssets() |
|
|
private static CheckAssets FoundAssets() |
|
|
{ |
|
|
{ |
|
|
return ids => |
|
|
return ids => |
|
|
{ |
|
|
{ |
|
|
var result = new List<IAssetInfo> { document, image1, image2, imageSvg }; |
|
|
var result = new List<IAssetInfo> { Document, Image1, Image2, ImageSvg, Video }; |
|
|
|
|
|
|
|
|
return Task.FromResult<IReadOnlyList<IAssetInfo>>(result); |
|
|
return Task.FromResult<IReadOnlyList<IAssetInfo>>(result); |
|
|
}; |
|
|
}; |
|
|
|