Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Adding AuthorizePage & AuthorizeFolder without requiring a policy #5942

Merged
merged 1 commit into from
Mar 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,16 @@ public static RazorPagesOptions AuthorizePage(this RazorPagesOptions options, st
}

/// <summary>
/// Adds a <see cref="AuthorizeFilter"/> with the specified policy to all page under the specified path.
/// Adds a <see cref="AuthorizeFilter"/> to the page with the specified path.
/// </summary>
/// <param name="options">The <see cref="RazorPagesOptions"/> to configure.</param>
/// <param name="path">The path of the Razor Page.</param>
/// <returns>The <see cref="RazorPagesOptions"/>.</returns>
public static RazorPagesOptions AuthorizePage(this RazorPagesOptions options, string path) =>
AuthorizePage(options, path, policy: string.Empty);

/// <summary>
/// Adds a <see cref="AuthorizeFilter"/> with the specified policy to all pages under the specified path.
/// </summary>
/// <param name="options">The <see cref="RazorPagesOptions"/> to configure.</param>
/// <param name="folderPath">The folder path.</param>
Expand All @@ -84,6 +93,15 @@ public static RazorPagesOptions AuthorizeFolder(this RazorPagesOptions options,
return options;
}

/// <summary>
/// Adds a <see cref="AuthorizeFilter"/> to all pages under the specified path.
/// </summary>
/// <param name="options">The <see cref="RazorPagesOptions"/> to configure.</param>
/// <param name="folderPath">The folder path.</param>
/// <returns>The <see cref="RazorPagesOptions"/>.</returns>
public static RazorPagesOptions AuthorizeFolder(this RazorPagesOptions options, string folderPath) =>
AuthorizeFolder(options, folderPath, policy: string.Empty);

private class PageConvention : IPageApplicationModelConvention
{
private readonly string _path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void AddFilter_AddsFiltersToAllPages()
}

[Fact]
public void AuthorizePage_AddsAuthorizeFilterToSpecificPage()
public void AuthorizePage_AddsAuthorizeFilterWithPolicyToSpecificPage()
{
// Arrange
var options = new RazorPagesOptions();
Expand Down Expand Up @@ -66,10 +66,39 @@ public void AuthorizePage_AddsAuthorizeFilterToSpecificPage()
model => Assert.Empty(model.Filters));
}

[Fact]
public void AuthorizePage_AddsAuthorizeFilterWithoutPolicyToSpecificPage()
{
// Arrange
var options = new RazorPagesOptions();
var models = new[]
{
new PageApplicationModel("/Pages/Index.cshtml", "/Index.cshtml"),
new PageApplicationModel("/Pages/Users/Account.cshtml", "/Users/Account.cshtml"),
new PageApplicationModel("/Pages/Users/Contact.cshtml", "/Users/Contact.cshtml"),
};

// Act
options.AuthorizePage("/Users/Account.cshtml");
ApplyConventions(options, models);

// Assert
Assert.Collection(models,
model => Assert.Empty(model.Filters),
model =>
{
Assert.Equal("/Users/Account.cshtml", model.ViewEnginePath);
var authorizeFilter = Assert.IsType<AuthorizeFilter>(Assert.Single(model.Filters));
var authorizeData = Assert.IsType<AuthorizeAttribute>(Assert.Single(authorizeFilter.AuthorizeData));
Assert.Equal(string.Empty, authorizeData.Policy);
},
model => Assert.Empty(model.Filters));
}

[Theory]
[InlineData("/Users")]
[InlineData("/Users/")]
public void AuthorizePage_AddsAuthorizeFilterToPagesUnderFolder(string folderName)
public void AuthorizePage_AddsAuthorizeFilterWithPolicyToPagesUnderFolder(string folderName)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add optional param for policy in this way we can merge both tests in one, let me know if I can go with it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the tests are fine the way they are now.

{
// Arrange
var options = new RazorPagesOptions();
Expand Down Expand Up @@ -103,6 +132,43 @@ public void AuthorizePage_AddsAuthorizeFilterToPagesUnderFolder(string folderNam
});
}

[Theory]
[InlineData("/Users")]
[InlineData("/Users/")]
public void AuthorizePage_AddsAuthorizeFilterWithoutPolicyToPagesUnderFolder(string folderName)
{
// Arrange
var options = new RazorPagesOptions();
var models = new[]
{
new PageApplicationModel("/Pages/Index.cshtml", "/Index.cshtml"),
new PageApplicationModel("/Pages/Users/Account.cshtml", "/Users/Account.cshtml"),
new PageApplicationModel("/Pages/Users/Contact.cshtml", "/Users/Contact.cshtml"),
};

// Act
options.AuthorizeFolder(folderName);
ApplyConventions(options, models);

// Assert
Assert.Collection(models,
model => Assert.Empty(model.Filters),
model =>
{
Assert.Equal("/Users/Account.cshtml", model.ViewEnginePath);
var authorizeFilter = Assert.IsType<AuthorizeFilter>(Assert.Single(model.Filters));
var authorizeData = Assert.IsType<AuthorizeAttribute>(Assert.Single(authorizeFilter.AuthorizeData));
Assert.Equal(string.Empty, authorizeData.Policy);
},
model =>
{
Assert.Equal("/Users/Contact.cshtml", model.ViewEnginePath);
var authorizeFilter = Assert.IsType<AuthorizeFilter>(Assert.Single(model.Filters));
var authorizeData = Assert.IsType<AuthorizeAttribute>(Assert.Single(authorizeFilter.AuthorizeData));
Assert.Equal(string.Empty, authorizeData.Policy);
});
}

private static void ApplyConventions(RazorPagesOptions options, PageApplicationModel[] models)
{
foreach (var convention in options.Conventions)
Expand Down
2 changes: 1 addition & 1 deletion test/WebSites/RazorPagesWebSite/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void ConfigureServices(IServiceCollection services)
.AddCookieTempDataProvider()
.AddRazorPagesOptions(options =>
{
options.AuthorizePage("/HelloWorldWithAuth", string.Empty);
options.AuthorizePage("/HelloWorldWithAuth");
});
}

Expand Down
4 changes: 2 additions & 2 deletions test/WebSites/RazorPagesWebSite/StartupWithBasePath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public void ConfigureServices(IServiceCollection services)
.AddRazorPagesOptions(options =>
{
options.RootDirectory = "/Pages";
options.AuthorizePage("/Conventions/Auth", string.Empty);
options.AuthorizeFolder("/Conventions/AuthFolder", string.Empty);
options.AuthorizePage("/Conventions/Auth");
options.AuthorizeFolder("/Conventions/AuthFolder");
});
}

Expand Down