mirror of https://github.com/Squidex/squidex.git
8 changed files with 212 additions and 215 deletions
@ -1,209 +0,0 @@ |
|||||
// ==========================================================================
|
|
||||
// Guard.cs
|
|
||||
// Squidex Headless CMS
|
|
||||
// ==========================================================================
|
|
||||
// Copyright (c) Squidex Group
|
|
||||
// All rights reserved.
|
|
||||
// ==========================================================================
|
|
||||
|
|
||||
using System; |
|
||||
using System.Collections.Generic; |
|
||||
using System.Diagnostics; |
|
||||
using System.IO; |
|
||||
using System.Linq; |
|
||||
using System.Runtime.CompilerServices; |
|
||||
|
|
||||
// ReSharper disable InvertIf
|
|
||||
|
|
||||
namespace Squidex.Infrastructure |
|
||||
{ |
|
||||
public static class UpdateProperties |
|
||||
{ |
|
||||
[DebuggerStepThrough] |
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
public static void ValidNumber(float target, string parameterName) |
|
||||
{ |
|
||||
if (float.IsNaN(target) || float.IsPositiveInfinity(target) || float.IsNegativeInfinity(target)) |
|
||||
{ |
|
||||
throw new ArgumentException("Value must be a valid number.", parameterName); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[DebuggerStepThrough] |
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
public static void ValidNumber(double target, string parameterName) |
|
||||
{ |
|
||||
if (double.IsNaN(target) || double.IsPositiveInfinity(target) || double.IsNegativeInfinity(target)) |
|
||||
{ |
|
||||
throw new ArgumentException("Value must be a valid number.", parameterName); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[DebuggerStepThrough] |
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
public static void ValidSlug(string target, string parameterName) |
|
||||
{ |
|
||||
NotNullOrEmpty(target, parameterName); |
|
||||
|
|
||||
if (!target.IsSlug()) |
|
||||
{ |
|
||||
throw new ArgumentException("Target is not a valid slug.", parameterName); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[DebuggerStepThrough] |
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
public static void HasType<T>(object target, string parameterName) |
|
||||
{ |
|
||||
if (target != null && target.GetType() != typeof(T)) |
|
||||
{ |
|
||||
throw new ArgumentException($"The parameter must be of type {typeof(T)}", parameterName); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[DebuggerStepThrough] |
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
public static void HasType(object target, Type expectedType, string parameterName) |
|
||||
{ |
|
||||
if (target != null && expectedType != null && target.GetType() != expectedType) |
|
||||
{ |
|
||||
throw new ArgumentException($"The parameter must be of type {expectedType}", parameterName); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[DebuggerStepThrough] |
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
public static void Between<TValue>(TValue target, TValue lower, TValue upper, string parameterName) where TValue : IComparable |
|
||||
{ |
|
||||
if (!target.IsBetween(lower, upper)) |
|
||||
{ |
|
||||
throw new ArgumentException($"Value must be between {lower} and {upper}", parameterName); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[DebuggerStepThrough] |
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
public static void Enum<TEnum>(TEnum target, string parameterName) where TEnum : struct |
|
||||
{ |
|
||||
if (!target.IsEnumValue()) |
|
||||
{ |
|
||||
throw new ArgumentException($"Value must be a valid enum type {typeof(TEnum)}", parameterName); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[DebuggerStepThrough] |
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
public static void GreaterThan<TValue>(TValue target, TValue lower, string parameterName) where TValue : IComparable |
|
||||
{ |
|
||||
if (target.CompareTo(lower) <= 0) |
|
||||
{ |
|
||||
throw new ArgumentException($"Value must be greater than {lower}", parameterName); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[DebuggerStepThrough] |
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
public static void GreaterEquals<TValue>(TValue target, TValue lower, string parameterName) where TValue : IComparable |
|
||||
{ |
|
||||
if (target.CompareTo(lower) < 0) |
|
||||
{ |
|
||||
throw new ArgumentException($"Value must be greater or equals than {lower}", parameterName); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[DebuggerStepThrough] |
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
public static void LessThan<TValue>(TValue target, TValue upper, string parameterName) where TValue : IComparable |
|
||||
{ |
|
||||
if (target.CompareTo(upper) >= 0) |
|
||||
{ |
|
||||
throw new ArgumentException($"Value must be less than {upper}", parameterName); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[DebuggerStepThrough] |
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
public static void LessEquals<TValue>(TValue target, TValue upper, string parameterName) where TValue : IComparable |
|
||||
{ |
|
||||
if (target.CompareTo(upper) > 0) |
|
||||
{ |
|
||||
throw new ArgumentException($"Value must be less or equals than {upper}", parameterName); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[DebuggerStepThrough] |
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
public static void NotEmpty<TType>(ICollection<TType> enumerable, string parameterName) |
|
||||
{ |
|
||||
NotNull(enumerable, parameterName); |
|
||||
|
|
||||
if (enumerable.Count == 0) |
|
||||
{ |
|
||||
throw new ArgumentException("Collection does not contain an item", parameterName); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[DebuggerStepThrough] |
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
public static void NotEmpty(Guid target, string parameterName) |
|
||||
{ |
|
||||
if (target == Guid.Empty) |
|
||||
{ |
|
||||
throw new ArgumentException("Value cannot be empty.", parameterName); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[DebuggerStepThrough] |
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
public static void NotNull(object target, string parameterName) |
|
||||
{ |
|
||||
if (target == null) |
|
||||
{ |
|
||||
throw new ArgumentNullException(parameterName); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[DebuggerStepThrough] |
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
public static void NotDefault<T>(T target, string parameterName) |
|
||||
{ |
|
||||
if (Equals(target, default(T))) |
|
||||
{ |
|
||||
throw new ArgumentException("Value cannot be an the default value", parameterName); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[DebuggerStepThrough] |
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
public static void NotNullOrEmpty(string target, string parameterName) |
|
||||
{ |
|
||||
NotNull(target, parameterName); |
|
||||
|
|
||||
if (string.IsNullOrWhiteSpace(target)) |
|
||||
{ |
|
||||
throw new ArgumentException("String parameter cannot be null or empty and cannot contain only blanks.", parameterName); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[DebuggerStepThrough] |
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
public static void ValidFileName(string target, string parameterName) |
|
||||
{ |
|
||||
NotNullOrEmpty(target, parameterName); |
|
||||
|
|
||||
if (target.Intersect(Path.GetInvalidFileNameChars()).Any()) |
|
||||
{ |
|
||||
throw new ArgumentException("Value contains an invalid character.", parameterName); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[DebuggerStepThrough] |
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|
||||
public static void Valid(IValidatable target, string parameterName, Func<string> message) |
|
||||
{ |
|
||||
NotNull(target, parameterName); |
|
||||
|
|
||||
target.Validate(message); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,137 @@ |
|||||
|
// ==========================================================================
|
||||
|
// DomainObjectTest.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using Squidex.Infrastructure.CQRS.Events; |
||||
|
using Xunit; |
||||
|
using System.Linq; |
||||
|
|
||||
|
// ReSharper disable ConvertToConstant.Local
|
||||
|
|
||||
|
namespace Squidex.Infrastructure.CQRS |
||||
|
{ |
||||
|
public class DomainObjectTest |
||||
|
{ |
||||
|
private sealed class Event : IEvent |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
private sealed class UserDomainObject : DomainObject |
||||
|
{ |
||||
|
public UserDomainObject(Guid id, int version) |
||||
|
: base(id, version) |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
public void RaiseTestEvent(IEvent @event) |
||||
|
{ |
||||
|
RaiseEvent(@event); |
||||
|
} |
||||
|
|
||||
|
protected override void DispatchEvent(Envelope<IEvent> @event) |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_instantiate() |
||||
|
{ |
||||
|
var id = Guid.NewGuid(); |
||||
|
var ver = 123; |
||||
|
var sut = new UserDomainObject(id, ver); |
||||
|
|
||||
|
Assert.Equal(id, sut.Id); |
||||
|
Assert.Equal(ver, sut.Version); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_add_event_to_uncommitted_events_and_increase_version_when_raised() |
||||
|
{ |
||||
|
var event1 = new Event(); |
||||
|
var event2 = new Event(); |
||||
|
|
||||
|
var sut = new UserDomainObject(Guid.NewGuid(), 10); |
||||
|
|
||||
|
IAggregate aggregate = sut; |
||||
|
|
||||
|
sut.RaiseTestEvent(event1); |
||||
|
sut.RaiseTestEvent(event2); |
||||
|
|
||||
|
Assert.Equal(12, sut.Version); |
||||
|
|
||||
|
Assert.Equal(new IEvent[] { event1, event2 }, aggregate.GetUncomittedEvents().Select(x => x.Payload).ToArray()); |
||||
|
|
||||
|
aggregate.ClearUncommittedEvents(); |
||||
|
|
||||
|
Assert.Equal(0, sut.GetUncomittedEvents().Count); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_not_add_event_to_uncommitted_events_and_increase_version_when_raised() |
||||
|
{ |
||||
|
var event1 = new Event(); |
||||
|
var event2 = new Event(); |
||||
|
|
||||
|
var sut = new UserDomainObject(Guid.NewGuid(), 10); |
||||
|
|
||||
|
IAggregate aggregate = sut; |
||||
|
|
||||
|
aggregate.ApplyEvent(new Envelope<IEvent>(event1)); |
||||
|
aggregate.ApplyEvent(new Envelope<IEvent>(event2)); |
||||
|
|
||||
|
Assert.Equal(12, sut.Version); |
||||
|
Assert.Equal(0, sut.GetUncomittedEvents().Count); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_make_correct_equal_comparisons() |
||||
|
{ |
||||
|
var id1 = Guid.NewGuid(); |
||||
|
var id2 = Guid.NewGuid(); |
||||
|
|
||||
|
var user1a = new UserDomainObject(id1, 1); |
||||
|
var user1b = new UserDomainObject(id1, 2); |
||||
|
var user2 = new UserDomainObject(id2, 2); |
||||
|
|
||||
|
Assert.True(user1a.Equals(user1b)); |
||||
|
|
||||
|
Assert.False(user1a.Equals(user2)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_make_correct_object_equal_comparisons() |
||||
|
{ |
||||
|
var id1 = Guid.NewGuid(); |
||||
|
var id2 = Guid.NewGuid(); |
||||
|
|
||||
|
var user1a = new UserDomainObject(id1, 1); |
||||
|
|
||||
|
object user1b = new UserDomainObject(id1, 2); |
||||
|
object user2 = new UserDomainObject(id2, 2); |
||||
|
|
||||
|
Assert.True(user1a.Equals(user1b)); |
||||
|
|
||||
|
Assert.False(user1a.Equals(user2)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_provide_correct_hash_codes() |
||||
|
{ |
||||
|
var id1 = Guid.NewGuid(); |
||||
|
var id2 = Guid.NewGuid(); |
||||
|
|
||||
|
var user1a = new UserDomainObject(id1, 1); |
||||
|
var user1b = new UserDomainObject(id1, 2); |
||||
|
var user2 = new UserDomainObject(id2, 2); |
||||
|
|
||||
|
Assert.Equal(user1a.GetHashCode(), user1b.GetHashCode()); |
||||
|
|
||||
|
Assert.NotEqual(user1a.GetHashCode(), user2.GetHashCode()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,69 @@ |
|||||
|
// ==========================================================================
|
||||
|
// ExtensionsTests.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using System.Security.Claims; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Infrastructure.Security |
||||
|
{ |
||||
|
public class ExtensionsTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_retrieve_subject() |
||||
|
{ |
||||
|
TestClaimExtension(OpenIdClaims.Subject, x => x.OpenIdSubject()); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_retrieve_client_id() |
||||
|
{ |
||||
|
TestClaimExtension(OpenIdClaims.ClientId, x => x.OpenIdClientId()); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_retrieve_preferred_user_name() |
||||
|
{ |
||||
|
TestClaimExtension(OpenIdClaims.PreferredUserName, x => x.OpenIdPreferredUserName()); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_retrieve_name() |
||||
|
{ |
||||
|
TestClaimExtension(OpenIdClaims.Name, x => x.OpenIdName()); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_retrieve_nickname() |
||||
|
{ |
||||
|
TestClaimExtension(OpenIdClaims.NickName, x => x.OpenIdNickName()); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_retrieve_email() |
||||
|
{ |
||||
|
TestClaimExtension(OpenIdClaims.Email, x => x.OpenIdEmail()); |
||||
|
} |
||||
|
|
||||
|
private static void TestClaimExtension(string claimType, Func<ClaimsPrincipal, string> getter) |
||||
|
{ |
||||
|
var claimValue = Guid.NewGuid().ToString(); |
||||
|
|
||||
|
var claimsIdentity = new ClaimsIdentity(); |
||||
|
var claimsPrincipal = new ClaimsPrincipal(); |
||||
|
|
||||
|
claimsIdentity.AddClaim(new Claim(claimType, claimValue)); |
||||
|
|
||||
|
Assert.Null(getter(claimsPrincipal)); |
||||
|
|
||||
|
claimsPrincipal.AddIdentity(claimsIdentity); |
||||
|
|
||||
|
Assert.Equal(claimValue, getter(claimsPrincipal)); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue