-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adjust OpenId server settings deploy source to match recipe step, #6364…
… (#10089)
- Loading branch information
Showing
3 changed files
with
138 additions
and
5 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
89 changes: 89 additions & 0 deletions
89
test/OrchardCore.Tests/Modules/OrchardCore.OpenId/OpenIdServerDeploymentSourceTests.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,89 @@ | ||
using System; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Moq; | ||
using Newtonsoft.Json.Linq; | ||
using OrchardCore.Deployment; | ||
using OrchardCore.OpenId.Deployment; | ||
using OrchardCore.OpenId.Recipes; | ||
using OrchardCore.OpenId.Services; | ||
using OrchardCore.OpenId.Settings; | ||
using OrchardCore.Recipes.Models; | ||
using OrchardCore.Tests.Stubs; | ||
using Xunit; | ||
using static OrchardCore.OpenId.Settings.OpenIdServerSettings; | ||
|
||
namespace OrchardCore.Tests.Modules.OrchardCore.OpenId | ||
{ | ||
public class OpenIdServerDeploymentSourceTests | ||
{ | ||
private static OpenIdServerSettings CreateSettings(string authority, TokenFormat tokenFormat) | ||
{ | ||
return new OpenIdServerSettings | ||
{ | ||
Authority = new Uri(authority), | ||
AccessTokenFormat = tokenFormat | ||
}; | ||
} | ||
|
||
private static Mock<IOpenIdServerService> CreateServerServiceWithSettingsMock(OpenIdServerSettings settings) | ||
{ | ||
var serverService = new Mock<IOpenIdServerService>(); | ||
|
||
serverService | ||
.Setup(m => m.GetSettingsAsync()) | ||
.ReturnsAsync(settings); | ||
|
||
serverService | ||
.Setup(m => m.LoadSettingsAsync()) | ||
.ReturnsAsync(settings); | ||
|
||
return serverService; | ||
} | ||
|
||
[Fact] | ||
public async Task ServerDeploymentSourceIsReadableByRecipe() | ||
{ | ||
// Arrange | ||
var recipeFile = "Recipe.json"; | ||
|
||
var expectedSettings = CreateSettings("https://deploy.localhost", TokenFormat.JsonWebToken); | ||
var deployServerServiceMock = CreateServerServiceWithSettingsMock(expectedSettings); | ||
|
||
var actualSettings = CreateSettings("https://recipe.localhost", TokenFormat.DataProtection); | ||
var recipeServerServiceMock = CreateServerServiceWithSettingsMock(actualSettings); | ||
|
||
Assert.NotEqual(expectedSettings.Authority, actualSettings.Authority); | ||
Assert.NotEqual(expectedSettings.AccessTokenFormat, actualSettings.AccessTokenFormat); | ||
|
||
var fileBuilder = new MemoryFileBuilder(); | ||
var descriptor = new RecipeDescriptor(); | ||
var result = new DeploymentPlanResult(fileBuilder, descriptor); | ||
|
||
var deploymentSource = new OpenIdServerDeploymentSource(deployServerServiceMock.Object); | ||
|
||
// Act | ||
await deploymentSource.ProcessDeploymentStepAsync(new OpenIdServerDeploymentStep(), result); | ||
await result.FinalizeAsync(); | ||
|
||
var deploy = JObject.Parse( | ||
fileBuilder.GetFileContents( | ||
recipeFile, | ||
Encoding.UTF8)); | ||
|
||
var recipeContext = new RecipeExecutionContext | ||
{ | ||
RecipeDescriptor = descriptor, | ||
Name = deploy.Property("steps").Value.First.Value<string>("name"), | ||
Step = (JObject)deploy.Property("steps").Value.First, | ||
}; | ||
|
||
var recipeStep = new OpenIdServerSettingsStep(recipeServerServiceMock.Object); | ||
await recipeStep.ExecuteAsync(recipeContext); | ||
|
||
// Assert | ||
Assert.Equal(expectedSettings.Authority, actualSettings.Authority); | ||
Assert.Equal(expectedSettings.AccessTokenFormat, actualSettings.AccessTokenFormat); | ||
} | ||
} | ||
} |
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,36 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using OrchardCore.Deployment; | ||
|
||
namespace OrchardCore.Tests.Stubs | ||
{ | ||
/// <summary> | ||
/// In memory file builder that uses a dictionary as virtual file system. | ||
/// Intended for unit testing. | ||
/// </summary> | ||
public class MemoryFileBuilder | ||
: IFileBuilder | ||
{ | ||
public Dictionary<string, byte[]> VirtualFiles { get; private set; } = new Dictionary<string, byte[]>(); | ||
|
||
public async Task SetFileAsync(string subpath, Stream stream) | ||
{ | ||
using var ms = new MemoryStream(); | ||
await stream.CopyToAsync(ms); | ||
VirtualFiles[subpath] = ms.ToArray(); | ||
} | ||
|
||
/// <summary> | ||
/// Read the contents of a file built with this file builder, using the specified encoding. | ||
/// </summary> | ||
/// <param name="subpath">Path and/or file name</param> | ||
/// <param name="encoding">Encoding used to convert the byte array to string</param> | ||
/// <returns></returns> | ||
public string GetFileContents(string subpath, Encoding encoding) | ||
{ | ||
return encoding.GetString(VirtualFiles[subpath]); | ||
} | ||
} | ||
} |