mirror of https://github.com/Squidex/squidex.git
12 changed files with 252 additions and 38 deletions
@ -0,0 +1,31 @@ |
|||||
|
// ==========================================================================
|
||||
|
// UserTokenConverter.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using Newtonsoft.Json; |
||||
|
|
||||
|
namespace Squidex.Infrastructure.Json |
||||
|
{ |
||||
|
public sealed class UserTokenConverter : JsonConverter |
||||
|
{ |
||||
|
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) |
||||
|
{ |
||||
|
writer.WriteValue(value.ToString()); |
||||
|
} |
||||
|
|
||||
|
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) |
||||
|
{ |
||||
|
return reader.TokenType == JsonToken.Null ? null : UserToken.Parse((string)reader.Value); |
||||
|
} |
||||
|
|
||||
|
public override bool CanConvert(Type objectType) |
||||
|
{ |
||||
|
return objectType == typeof(UserToken); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,11 +1,64 @@ |
|||||
using System; |
// ==========================================================================
|
||||
using System.Collections.Generic; |
// UserToken.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
using System.Linq; |
using System.Linq; |
||||
using System.Threading.Tasks; |
|
||||
|
|
||||
namespace Squidex.Infrastructure |
namespace Squidex.Infrastructure |
||||
{ |
{ |
||||
public sealed class UserToken |
public sealed class UserToken : IEquatable<UserToken> |
||||
{ |
{ |
||||
|
public string Type { get; } |
||||
|
|
||||
|
public string Identifier { get; } |
||||
|
|
||||
|
public UserToken(string type, string identifier) |
||||
|
{ |
||||
|
Guard.NotNullOrEmpty(type, nameof(type)); |
||||
|
Guard.NotNullOrEmpty(identifier, nameof(identifier)); |
||||
|
|
||||
|
Type = type.ToLowerInvariant(); |
||||
|
|
||||
|
Identifier = identifier; |
||||
|
} |
||||
|
|
||||
|
public static UserToken Parse(string input) |
||||
|
{ |
||||
|
Guard.NotNullOrEmpty(input, nameof(input)); |
||||
|
|
||||
|
var parts = input.Split(new [] { ':' }, StringSplitOptions.RemoveEmptyEntries); |
||||
|
|
||||
|
if (parts.Length < 2) |
||||
|
{ |
||||
|
throw new ArgumentException("Input must have more than 2 parts divided by colon", nameof(input)); |
||||
|
} |
||||
|
|
||||
|
return new UserToken(parts[0], string.Join(":", parts.Skip(1))); |
||||
|
} |
||||
|
|
||||
|
public override string ToString() |
||||
|
{ |
||||
|
return $"{Type}:{Identifier}"; |
||||
|
} |
||||
|
|
||||
|
public override bool Equals(object obj) |
||||
|
{ |
||||
|
return Equals(obj as UserToken); |
||||
|
} |
||||
|
|
||||
|
public bool Equals(UserToken other) |
||||
|
{ |
||||
|
return other != null && (ReferenceEquals(this, other) || (Type.Equals(other.Type) && Identifier.Equals(other.Identifier))); |
||||
|
} |
||||
|
|
||||
|
public override int GetHashCode() |
||||
|
{ |
||||
|
return (Type.GetHashCode() * 397) ^ (Identifier.GetHashCode()); |
||||
|
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -1,17 +1,138 @@ |
|||||
using System; |
// ==========================================================================
|
||||
using System.Collections.Generic; |
// UserTokenTests.cs
|
||||
using System.Linq; |
// Squidex Headless CMS
|
||||
using System.Threading.Tasks; |
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
using Newtonsoft.Json; |
||||
|
using Squidex.Infrastructure.Json; |
||||
using Xunit; |
using Xunit; |
||||
|
// ReSharper disable RedundantCast
|
||||
|
|
||||
namespace Squidex.Infrastructure |
namespace Squidex.Infrastructure |
||||
{ |
{ |
||||
public class UserTokenTests |
public class UserTokenTests |
||||
{ |
{ |
||||
|
private static readonly JsonSerializerSettings serializerSettings = new JsonSerializerSettings(); |
||||
|
|
||||
|
static UserTokenTests() |
||||
|
{ |
||||
|
serializerSettings.Converters.Add(new UserTokenConverter()); |
||||
|
serializerSettings.NullValueHandling = NullValueHandling.Include; |
||||
|
} |
||||
|
|
||||
|
[Theory] |
||||
|
[InlineData("")] |
||||
|
[InlineData(" ")] |
||||
|
[InlineData(":")] |
||||
|
[InlineData("user")] |
||||
|
public void Should_throw_if_parsing_invalid_input(string input) |
||||
|
{ |
||||
|
Assert.Throws<ArgumentException>(() => UserToken.Parse(input)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_instantiate_token() |
||||
|
{ |
||||
|
var token = new UserToken("client", "client1"); |
||||
|
|
||||
|
Assert.Equal("client", token.Type); |
||||
|
Assert.Equal("client1", token.Identifier); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_instantiate_token_and_lower_type() |
||||
|
{ |
||||
|
var token = new UserToken("Client", "client1"); |
||||
|
|
||||
|
Assert.Equal("client", token.Type); |
||||
|
Assert.Equal("client1", token.Identifier); |
||||
|
} |
||||
|
|
||||
[Fact] |
[Fact] |
||||
public void Should_parse_user_token_from_string() |
public void Should_parse_user_token_from_string() |
||||
{ |
{ |
||||
var token = UserToken.Parse("") |
var token = UserToken.Parse("client:client1"); |
||||
|
|
||||
|
Assert.Equal("client", token.Type); |
||||
|
Assert.Equal("client1", token.Identifier); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_parse_user_token_with_colon_in_identifier() |
||||
|
{ |
||||
|
var token = UserToken.Parse("client:client1:app"); |
||||
|
|
||||
|
Assert.Equal("client", token.Type); |
||||
|
Assert.Equal("client1:app", token.Identifier); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_convert_user_token_to_string() |
||||
|
{ |
||||
|
var token = UserToken.Parse("client:client1"); |
||||
|
|
||||
|
Assert.Equal("client:client1", token.ToString()); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_make_correct_equal_comparisons() |
||||
|
{ |
||||
|
var token1a = UserToken.Parse("client:client1"); |
||||
|
var token1b = UserToken.Parse("client:client1"); |
||||
|
var token2 = UserToken.Parse("client:client2"); |
||||
|
|
||||
|
Assert.True(token1a.Equals(token1b)); |
||||
|
|
||||
|
Assert.False(token1a.Equals(token2)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_make_correct_object_equal_comparisons() |
||||
|
{ |
||||
|
var token1a = UserToken.Parse("client:client1"); |
||||
|
|
||||
|
object token1b = UserToken.Parse("client:client1"); |
||||
|
object token2 = UserToken.Parse("client:client2"); |
||||
|
|
||||
|
Assert.True(token1a.Equals(token1b)); |
||||
|
|
||||
|
Assert.False(token1a.Equals(token2)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_provide_correct_hash_codes() |
||||
|
{ |
||||
|
var token1a = UserToken.Parse("client:client1"); |
||||
|
var token1b = UserToken.Parse("client:client1"); |
||||
|
var token2 = UserToken.Parse("client:client2"); |
||||
|
|
||||
|
Assert.Equal(token1a.GetHashCode(), token1b.GetHashCode()); |
||||
|
|
||||
|
Assert.NotEqual(token1a.GetHashCode(), token2.GetHashCode()); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_serialize_and_deserialize_null_token() |
||||
|
{ |
||||
|
var input = Tuple.Create<UserToken>(null); |
||||
|
var json = JsonConvert.SerializeObject(input, serializerSettings); |
||||
|
var output = JsonConvert.DeserializeObject<Tuple<UserToken>>(json, serializerSettings); |
||||
|
|
||||
|
Assert.Equal(output.Item1, input.Item1); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_serialize_and_deserialize_valid_token() |
||||
|
{ |
||||
|
var input = Tuple.Create(UserToken.Parse("client:client1")); |
||||
|
var json = JsonConvert.SerializeObject(input, serializerSettings); |
||||
|
var output = JsonConvert.DeserializeObject<Tuple<UserToken>>(json, serializerSettings); |
||||
|
|
||||
|
Assert.Equal(output.Item1, input.Item1); |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue