forked from OrchardCMS/OrchardCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add LoginInfoConverter (OrchardCMS#15422)
--------- Co-authored-by: Mike Alhayek <mike@crestapps.com> Co-authored-by: hyzx86 <yanzhong.han@jizhousoft.com>
- Loading branch information
Showing
3 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/OrchardCore/OrchardCore.Users.Core/Json/LoginInfoJsonConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using System.Text.Json.Serialization; | ||
using Microsoft.AspNetCore.Identity; | ||
|
||
namespace OrchardCore.Users.Core.Json; | ||
|
||
public class LoginInfoJsonConverter : JsonConverter<UserLoginInfo> | ||
{ | ||
public static readonly LoginInfoJsonConverter Instance = new(); | ||
|
||
public override UserLoginInfo Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
var loginInfo = new UserLoginInfo(string.Empty, string.Empty, string.Empty); | ||
|
||
while (reader.Read()) | ||
{ | ||
if (reader.TokenType == JsonTokenType.EndObject) | ||
{ | ||
break; | ||
} | ||
|
||
if (reader.TokenType == JsonTokenType.PropertyName) | ||
{ | ||
var propertyName = reader.GetString(); | ||
reader.Read(); | ||
|
||
switch (propertyName) | ||
{ | ||
case nameof(UserLoginInfo.LoginProvider): | ||
loginInfo.LoginProvider = reader.GetString(); | ||
break; | ||
case nameof(UserLoginInfo.ProviderKey): | ||
loginInfo.ProviderKey = reader.GetString(); | ||
break; | ||
case nameof(UserLoginInfo.ProviderDisplayName): | ||
loginInfo.ProviderDisplayName = reader.GetString(); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return loginInfo; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, UserLoginInfo objectToWrite, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStartObject(); | ||
writer.WriteString(nameof(UserLoginInfo.LoginProvider), objectToWrite.LoginProvider); | ||
writer.WriteString(nameof(UserLoginInfo.ProviderKey), objectToWrite.ProviderKey); | ||
writer.WriteString(nameof(UserLoginInfo.ProviderDisplayName), objectToWrite.ProviderDisplayName); | ||
writer.WriteEndObject(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System.Text.Json; | ||
using OrchardCore.Tests.Apis.Context; | ||
using OrchardCore.Users.Core.Json; | ||
using OrchardCore.Users.Indexes; | ||
using OrchardCore.Users.Models; | ||
|
||
namespace OrchardCore.Tests.Serializers; | ||
|
||
public class JsonSerializerTests | ||
{ | ||
private const string _userLoginInfo = "{\"LoginProvider\":\"OpenIdConnect\",\"ProviderKey\":\"abc\",\"ProviderDisplayName\":\"default\"}"; | ||
|
||
private readonly JsonSerializerOptions _options; | ||
|
||
public JsonSerializerTests() | ||
{ | ||
_options = new JsonSerializerOptions(); | ||
_options.Converters.Add(new LoginInfoJsonConverter()); | ||
} | ||
|
||
[Fact] | ||
public void Deserialize_WhenCalled_ReturnValidUserLoginInfo() | ||
{ | ||
var obj = JsonSerializer.Deserialize<UserLoginInfo>(_userLoginInfo, _options); | ||
|
||
Assert.Equal("OpenIdConnect", obj.LoginProvider); | ||
Assert.Equal("abc", obj.ProviderKey); | ||
Assert.Equal("default", obj.ProviderDisplayName); | ||
} | ||
|
||
[Fact] | ||
public void Serialize_WhenCalled_ReturnValidJson() | ||
{ | ||
var loginInfo = new UserLoginInfo("OpenIdConnect", "abc", "default"); | ||
var json = JsonSerializer.Serialize(loginInfo, _options); | ||
|
||
Assert.Equal(_userLoginInfo, json); | ||
} | ||
|
||
[Fact] | ||
public async Task DefaultContentSerializer_SerializeAndDeserialize_UserWithUserLoginInfo() | ||
{ | ||
using var context = new SiteContext(); | ||
await context.InitializeAsync(); | ||
await context.UsingTenantScopeAsync(async scope => | ||
{ | ||
var loginInfo = new UserLoginInfo("OpenIdConnect", "abc", "default"); | ||
|
||
var newUser = new User() | ||
{ | ||
UserId = "abc", | ||
UserName = "mike", | ||
Email = "test@test.com", | ||
LoginInfos = | ||
[ | ||
loginInfo | ||
] | ||
}; | ||
|
||
var session = scope.ServiceProvider.GetRequiredService<YesSql.ISession>(); | ||
|
||
await session.SaveAsync(newUser); | ||
await session.SaveChangesAsync(); | ||
|
||
var dbUser = await session.Query<User, UserIndex>(x => x.UserId == "abc").FirstOrDefaultAsync(); | ||
|
||
Assert.NotNull(dbUser); | ||
|
||
var userLoginInfo = dbUser.LoginInfos.FirstOrDefault(); | ||
|
||
Assert.NotNull(userLoginInfo); | ||
Assert.Equal(loginInfo.LoginProvider, userLoginInfo.LoginProvider); | ||
Assert.Equal(loginInfo.ProviderKey, userLoginInfo.ProviderKey); | ||
Assert.Equal(loginInfo.ProviderDisplayName, userLoginInfo.ProviderDisplayName); | ||
}); | ||
} | ||
} |